All Projects → ZeroProphet → klefki

ZeroProphet / klefki

Licence: GPL-3.0 License
Klefki is a playground for researching elliptic curve group based algorithm, such as MPC, ZKP and HE. All data types & structures are based on mathematical defination of abstract algebra.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to klefki

bellman
Bellman zkSNARK library for community with Ethereum's BN256 support
Stars: ✭ 121 (+908.33%)
Mutual labels:  zero-knowledge, zksnark
groth16
A Rust implementation of the Groth16 zkSNARK
Stars: ✭ 66 (+450%)
Mutual labels:  zksnark
zkp
Experimental zero-knowledge proof compiler in Rust macros
Stars: ✭ 121 (+908.33%)
Mutual labels:  zero-knowledge
coinaly
🚀 Fast and easy to use mobile trade interface for cryptocurrencies. Track your trades to the moon and beyond. Currently only for Bittrex.
Stars: ✭ 32 (+166.67%)
Mutual labels:  cryptocoins
zkp-ecdsa
Proves knowledge of an ECDSA-P256 signature under one of many public keys that are stored in a list.
Stars: ✭ 118 (+883.33%)
Mutual labels:  zero-knowledge
examples
Examples of NuID's zero knowledge authentication and key management facilities in various languages and frameworks. Open an Issue or PR if you'd like to see your favorite tool here.
Stars: ✭ 42 (+250%)
Mutual labels:  zero-knowledge
AlgebraicRelations.jl
Relational Algebra, now with more algebra!
Stars: ✭ 31 (+158.33%)
Mutual labels:  algebra
Euler
The open-source computational framework for the Swift language
Stars: ✭ 37 (+208.33%)
Mutual labels:  algebra
radb
RA (radb): A relational algebra interpreter over relational databases
Stars: ✭ 48 (+300%)
Mutual labels:  algebra
FlashPaper
One-time encrypted password/secret sharing
Stars: ✭ 85 (+608.33%)
Mutual labels:  zero-knowledge
relational
Educational tool for relational algebra
Stars: ✭ 70 (+483.33%)
Mutual labels:  algebra
swm-core
Pure Math in Pure Swift.
Stars: ✭ 190 (+1483.33%)
Mutual labels:  algebra
schnorr-nizk
Schnorr Protocol for Non-interactive Zero-Knowledge Proofs
Stars: ✭ 67 (+458.33%)
Mutual labels:  zero-knowledge
oxygenjs
This a JavaScript Library for the Numerical Javascript and Machine Learning
Stars: ✭ 13 (+8.33%)
Mutual labels:  algebra
crypto-database
Database for crypto data, supporting several exchanges. Can be used for TA, bots, backtest, realtime trading, etc.
Stars: ✭ 72 (+500%)
Mutual labels:  cryptocoins
zkc
zero-knowledge chat suite
Stars: ✭ 96 (+700%)
Mutual labels:  zero-knowledge
coinfox
Crypto Coin Portfolio Manager - Track all of your Bitcoin and crypto coin investments
Stars: ✭ 56 (+366.67%)
Mutual labels:  cryptocoins
bitcoin-cryptocurrency-tutorial
PHP & Cryptocurrencies Collections. Powered By https://btcschools.net
Stars: ✭ 142 (+1083.33%)
Mutual labels:  cryptocoins
symbolicnim
A symbolic library written purely in Nim with the ability to compile expressions into efficient functions.
Stars: ✭ 25 (+108.33%)
Mutual labels:  algebra
pyecsca
Python Elliptic Curve Side-Channel Analysis toolkit.
Stars: ✭ 23 (+91.67%)
Mutual labels:  elliptic-curve-cryptography

Klefki

travis Maintenance PyPI version klefki PyPI license PyPI status Generic badge

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)
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].