Package: zkp-playground
zkp-playgroundDescriptionzkp-playground is a playground for ZKP algorithms.The project was created on the basis of theKlefkiandPlonk_Pylibraries.KlefkiDocumentationRequirements:python>=3.6Installationpip3installzkp_playground

zkp_playgroundshellZKP ExamplesGroth16fromzkp_playground.curves.bns.bn128importBN128ScalarFPasFP,ECGBN128asECGfromzkp_playground.zkp.groth16importgroth16fromzkp_playground.zkp.groth16.r1csimportR1CSfromzkp_playground.zkp.groth16.qapimportQAP@R1CS.r1cs(FP)deff(x,k,c):y=x+c+kreturny**3g=ECG.G1h=ECG.G2qap=QAP(f.A,f.B,f.C)U,V,W,T=qap.qapa=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)assertgroth16.vfy(R,tau,a,pi)Plonkfromzkp_playground.zkp.plonk.trusted_setupimportsetup_algofromzkp_playground.zkp.plonk.proverimportprover_algofromzkp_playground.zkp.plonk.verifierimportverifier_algodefpermute_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 valuessize=len(wires)permutation=[i+1foriinrange(size)]foriinrange(size):forjinrange(i+1,size):ifwires[i]==wires[j]:permutation[i],permutation[j]=permutation[j],permutation[i]breakreturnpermutation# Wiresa=["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# Gatesadd=[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_iL=[4]p_i=35public_inputs=(L,p_i)n=len(gates_matrix)# matrix transposegates_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# precomputationCRS,Qs,p_i_poly,perm_prep,verifier_prep=setup_algo(gates_matrix,permutation,*public_inputs)# The prover calculates the proofproof_SNARK,u=prover_algo(witness,CRS,Qs,p_i_poly,perm_prep)# Verifier checks if proof checks outverifier_algo(proof_SNARK,n,p_i_poly,verifier_prep,perm_prep[2])Elliptic Curve Group ExampleTest pairingfromzkp_playground.curves.bnsimportbn128G1=bn128.ECGBN128.G1G2=bn128.ECGBN128.G2G=G1e=bn128.ECGBN128.eone=bn128.BN128FP12.one()p1=e(G1,G2)p2=e(G1@2,G2)assertp1*p1==p2Create Custom Groupsimportzkp_playground.constasconstfromzkp_playground.algebra.fieldsimportFiniteFieldfromzkp_playground.algebra.groupsimportEllipticCurveGroupfromzkp_playground.curves.arithimportshort_weierstrass_form_curve_additionclassFiniteFieldSecp256k1(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_addition(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))