Package: klefki
KlefkiKlefki (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; DRKlefki 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 DocumentTry it!For Installation (require python>=3.6):pip3installklefki

klefkishellHave Fun!!!!Elliptic Curve Group ExampleTest pairingfromklefki.curves.barreto_naehrigimportbn128G1=bn128.ECGBN128.G1G2=bn128.ECGBN128.G2G=G1e=bn128.ECGBN128.eone=bn128.BN128FP12.one()p1=e(G2,G1)p2=e(G2,G1@2)assertp1*p1==p2Create Custom Groupsimportklefki.constasconstfromklefki.algebra.fieldsimportFiniteFieldfromklefki.algebra.groupsimportEllipticCurveGroupfromklefki.algebra.groupsimportEllipicCyclicSubgroupfromklefki.curves.arithimportshort_weierstrass_form_curve_addition2classFiniteFieldSecp256k1(FiniteField):P=const.SECP256K1_PclassFiniteFieldCyclicSecp256k1(FiniteField):P=const.SECP256K1_NclassEllipticCurveGroupSecp256k1(EllipticCurveGroup):"""y^2 = x^3 + A * x + B"""N=const.SECP256K1_NA=const.SECP256K1_AB=const.SECP256K1_Bdefop(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)ifx==y==field(0):returnself.__class__(0)returnself.__class__((x,y))ZKP ExamplesPlay with r1csfromklefki.zkp.r1csimportR1CSfromfunctoolsimportpartial@R1CS.r1csdeft(x):y=x**3returny+x+5s=t.witness(3)assertR1CS.verify(s,*t.r1cs)asserts[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)]) == secretPubKey/PrivKey ExamplesWithAAT(Abstract Algebra Type)you can easily implement the bitcoinpriv/pub keyandsign/verifyalgorithms like this:importrandomfromklefki.utilsimportto_sha256intfromklefki.algebra.concreteimport(JacobianGroupSecp256k1asJG,EllipticCurveCyclicSubgroupSecp256k1asCG,EllipticCurveGroupSecp256k1asECG,FiniteFieldCyclicSecp256k1asCF)N=CG.NG=CG.Gdefrandom_privkey()->CF:returnCF(random.randint(1,N))defpubkey(priv:CF)->ECG:returnECG(JG(G@priv))defsign(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 CyclicSecp256k1Fields=z/k+priv*r/kreturnr,sdefverify(pub:ECG,sig:tuple,mhash:int):r,s=sigz=CF(mhash)u1=z/su2=r/srp=G@u1+pub@u2returnr==rp.value[0]Even proof theSign/Verifyalgorithm mathematically.defproof():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/kassertk==z/s+priv*r/sassertG@k==G@(z/s+priv*r/s)assertG@k==G@(z/s)+G@priv@(r/s)pub=G@privassertpub==pubkey(priv)assertG@k==G@(z/s)+pub@(r/s)u1=z/su2=r/sassertG@k==G@u1+pub@u2Or transform your Bitcoin Private Key to EOS Private/Pub key (or back)fromklefki.bitcoin.privateimportdecode_privkeyfromklefki.eos.publicimportgen_pub_keyfromklefki.eos.privateimportencode_privkeydeftest_to_eos(priv):key=decode_privkey(priv)eos_priv=encode_privkey(key)eos_pub=gen_pub_key(key)print(eos_priv,eos_pub)