zkp-playground
Description
zkp-playground is a playground for ZKP algorithms.
The project was created on the basis of the Klefki and Plonk_Py libraries.
Klefki Documentation
Requirements:
- python>=3.6
Installation
pip3 install zkp_playground
zkp_playground shell
ZKP Examples
- Groth16
from zkp_playground.curves.bns.bn128 import BN128ScalarFP as FP, ECGBN128 as ECG
from zkp_playground.zkp.groth16 import groth16
from zkp_playground.zkp.groth16.r1cs import R1CS
from zkp_playground.zkp.groth16.qap import QAP
@R1CS.r1cs(FP)
def f(x, k, c):
y = x + c + k
return y ** 3
g = ECG.G1
h = ECG.G2
qap = QAP(f.A, f.B, f.C)
U, V, W, T = qap.qap
a = f.witness(FP(89), FP(8), FP(8))
H = qap.H(a)
R = groth16.RationalGenerator(
FP, ECG, ECG, ECG, ECG.e, ECG.G1, ECG.G2, 4, U, V, W, T)
tau, sigma = groth16.setup(R, len(a))
pi = groth16.prov(R, H, tau, sigma, a)
assert groth16.vfy(R, tau, a, pi)
- Plonk
from zkp_playground.zkp.plonk.trusted_setup import setup_algo
from zkp_playground.zkp.plonk.prover import prover_algo
from zkp_playground.zkp.plonk.verifier import verifier_algo
def permute_idices(wires: list[str]) -> list[int]:
# This function takes an array "circuit" of arbitrary values and returns an
# array with shuffles the indices of "circuit" for repeating values
size = len(wires)
permutation = [i + 1 for i in range(size)]
for i in range(size):
for j in range(i + 1, size):
if wires[i] == wires[j]:
permutation[i], permutation[j] = permutation[j], permutation[i]
break
return permutation
# Wires
a = ["x", "var1", "var2", "1", "1", "var3", "empty1", "empty2"]
b = ["x", "x", "x", "5", "35", "5", "empty3", "empty4"]
c = ["var1", "var2", "var3", "5", "35", "35", "empty5", "empty6"]
wires = a + b + c
# Gates
add = [1, 1, 0, -1, 0]
mul = [0, 0, 1, -1, 0]
const5 = [0, 1, 0, 0, -5]
public_input = [0, 1, 0, 0, 0]
empty = [0, 0, 0, 0, 0]
gates_matrix = [mul, mul, add, const5, public_input, add, empty, empty]
permutation = permute_idices(wires)
# We can provide public input 35. For that we need to specify the position
# of the gate in L and the value of the public input in p_i
L = [4]
p_i = 35
public_inputs = (L, p_i)
n = len(gates_matrix)
# matrix transpose
gates_matrix = list(zip(*gates_matrix))
# To get the witness, the prover applies his private input x=3 to the
# circuit and writes down the value of every wire.
witness = [
3, 9, 27, 1, 1, 30, 0, 0,
3, 3, 3, 5, 35, 5, 0, 0,
9, 27, 30, 5, 35, 35, 0, 0,
]
# We start with a setup that computes the trusted setup and does some
# precomputation
CRS, Qs, p_i_poly, perm_prep, verifier_prep = setup_algo(
gates_matrix, permutation, *public_inputs
)
# The prover calculates the proof
proof_SNARK, u = prover_algo(witness, CRS, Qs, p_i_poly, perm_prep)
# Verifier checks if proof checks out
verifier_algo(proof_SNARK, n, p_i_poly, verifier_prep, perm_prep[2])
Elliptic Curve Group Example
- Test pairing
from zkp_playground.curves.bns import bn128
G1 = bn128.ECGBN128.G1
G2 = bn128.ECGBN128.G2
G = G1
e = bn128.ECGBN128.e
one = bn128.BN128FP12.one()
p1 = e(G1, G2)
p2 = e(G1 @ 2, G2)
assert p1 * p1 == p2
- Create Custom Groups
import zkp_playground.const as const
from zkp_playground.algebra.fields import FiniteField
from zkp_playground.algebra.groups import EllipticCurveGroup
from zkp_playground.curves.arith import short_weierstrass_form_curve_addition
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_addition(
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))
Metadata
Release files for zkp-playground 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| zkp-playground-1.0.0.tar.gz | 68.7 kB | Without provenance | Details for zkp-playground-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| zkp_playground-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for zkp_playground-1.0.0-py3-none-any.whl |
Total release size: 160.8 kB
Release files / zkp-playground-1.0.0.tar.gz
| Download URL | zkp-playground-1.0.0.tar.gz |
|---|---|
| Size | 68.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0e5e1d424394e30b54240f946247fff5c649c0e8576d19eb2ccd495796300d77
|
|
BLAKE2b-256 checksum How to use checksums |
10976a264cb887cf694e3c477728f0446eadc179bb65ebdf6ec09a7a3705ff73
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.12
|
Release files / zkp_playground-1.0.0-py3-none-any.whl
| Download URL | zkp_playground-1.0.0-py3-none-any.whl |
|---|---|
| Size | 92.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
700c04cb89abd94788df93031350a8581c757b7082a5980e72979cb26000119f
|
|
BLAKE2b-256 checksum How to use checksums |
d745fa11e696423c07bc283ca8fcb5c37f9c0bf990ac4ac6ba151ad2d2bc2118
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.12
|
Security for zkp-playground 1.0.0
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.