Klefki
Klefki (Japanese: クレッフィ Cleffy) is a dual-type Steel/Fairy Pokémon introduced in Generation VI. It is not known to evolve into or from any other Pokémon.
TL; DR
Klefki is a playground for researching elliptic curve group based algorithms & applications, such as MPC, HE, ZKP, and Bitcoin/Ethereum. All data types & structures are based on mathematical defination of abstract algebra.
Check the Document
Try it!
For Installation (require python>=3.6):
pip3 install klefki
klefki shell
Have Fun!!!!
Elliptic Curve Group Example
- Test pairing
from klefki.curves.barreto_naehrig import bn128
G1 = bn128.ECGBN128.G1
G2 = bn128.ECGBN128.G2
G = G1
e = bn128.ECGBN128.e
one = bn128.BN128FP12.one()
p1 = e(G2, G1)
p2 = e(G2, G1 @ 2)
assert p1 * p1 == p2
- Create Custom Groups
import klefki.const as const
from klefki.algebra.fields import FiniteField
from klefki.algebra.groups import EllipticCurveGroup
from klefki.algebra.groups import EllipicCyclicSubgroup
from klefki.curves.arith import short_weierstrass_form_curve_addition2
class FiniteFieldSecp256k1(FiniteField):
P = const.SECP256K1_P
class FiniteFieldCyclicSecp256k1(FiniteField):
P = const.SECP256K1_N
class EllipticCurveGroupSecp256k1(EllipticCurveGroup):
"""
y^2 = x^3 + A * x + B
"""
N = const.SECP256K1_N
A = const.SECP256K1_A
B = const.SECP256K1_B
def op(self, g):
field = self.id[0].__class__
x, y = short_weierstrass_form_curve_addition2(
self.x, self.y,
g.x, g.y,
field.zero(),
field.zero(),
field.zero(),
field(self.A),
field(self.B),
field
)
if x == y == field(0):
return self.__class__(0)
return self.__class__((x, y))
ZKP Examples
- Play with r1cs
from klefki.zkp.r1cs import R1CS
from functools import partial
@R1CS.r1cs
def t(x):
y = x**3
return y + x + 5
s = t.witness(3)
assert R1CS.verify(s, *t.r1cs)
assert s[2] == t(3)
MPC Examples (SSSS/VSS)
from klefki.crypto.ssss import SSSS
from klefki.const import SECP256K1_P as P
from klefki.algebra.utils import randfield
from klefki.algebra.meta import field
import random
def test_ssss():
F = field(P)
s = SSSS(F)
k = random.randint(1, 100)
n = k * 3
secret = randfield(F)
s.setup(secret, k, n)
assert s.decrypt([s.join() for _ in range(k-1)]) != secret
assert s.decrypt([s.join() for _ in range(k+1)]) == secret
assert s.decrypt([s.join() for _ in range(k+2)]) == secret
PubKey/PrivKey Examples
With AAT(Abstract Algebra Type) you can easily implement the bitcoin priv/pub key and sign/verify algorithms like this:
import random
from klefki.utils import to_sha256int
from klefki.algebra.concrete import (
JacobianGroupSecp256k1 as JG,
EllipticCurveCyclicSubgroupSecp256k1 as CG,
EllipticCurveGroupSecp256k1 as ECG,
FiniteFieldCyclicSecp256k1 as CF
)
N = CG.N
G = CG.G
def random_privkey() -> CF:
return CF(random.randint(1, N))
def pubkey(priv: CF) -> ECG:
return ECG(JG(G @ priv))
def sign(priv: CF, m: str) -> tuple:
k = CF(random.randint(1, N))
z = CF(to_sha256int(m))
r = CF((G @ k).value[0]) # From Secp256k1Field to CyclicSecp256k1Field
s = z / k + priv * r / k
return r, s
def verify(pub: ECG, sig: tuple, mhash: int):
r, s = sig
z = CF(mhash)
u1 = z / s
u2 = r / s
rp = G @ u1 + pub @ u2
return r == rp.value[0]
Even proof the Sign/Verify algorithm mathematically.
def proof():
priv = random_privkey()
m = 'test'
k = CF(random_privkey())
z = CF(to_sha256int(m))
r = CF((G @ k).value[0])
s = z / k + priv * r / k
assert k == z / s + priv * r / s
assert G @ k == G @ (z / s + priv * r / s)
assert G @ k == G @ (z / s) + G @ priv @ (r / s)
pub = G @ priv
assert pub == pubkey(priv)
assert G @ k == G @ (z / s) + pub @ (r / s)
u1 = z / s
u2 = r / s
assert G @ k == G @ u1 + pub @ u2
Or transform your Bitcoin Private Key to EOS Private/Pub key (or back)
from klefki.bitcoin.private import decode_privkey
from klefki.eos.public import gen_pub_key
from klefki.eos.private import encode_privkey
def test_to_eos(priv):
key = decode_privkey(priv)
eos_priv = encode_privkey(key)
eos_pub = gen_pub_key(key)
print(eos_priv, eos_pub)
Metadata
Release files for klefki 1.7.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| klefki-1.7.1-py3-none-any.whl | Python 3 | none | any | Details |
Release files / klefki-1.7.1-py3-none-any.whl
| Download URL | klefki-1.7.1-py3-none-any.whl |
|---|---|
| Size | 85.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8db34867eb9c7826b55d108005a971b7786b01fdff5eefd5118da6d1a9e26118
|
|
BLAKE2b-256 checksum How to use checksums |
0234ebfa2843e627f07915fa4071e226f86fda58c969cb32a565d4840e42fc16
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.2 CPython/3.8.2
|
Security for klefki 1.7.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.