github.com/consensys/gnark@v0.11.0/internal/generator/backend/template/zkpschemes/plonk/tests/marshal.go.tmpl (about) 1 2 import ( 3 {{ template "import_curve" . }} 4 {{ template "import_fr" . }} 5 "testing" 6 "math/big" 7 "math/rand" 8 "github.com/consensys/gnark/io" 9 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func TestProofSerialization(t *testing.T) { 14 // create a proof 15 var proof Proof 16 proof.randomize() 17 18 assert.NoError(t, io.RoundTripCheck(&proof, func() interface{} { return new(Proof) })) 19 } 20 21 func TestProvingKeySerialization(t *testing.T) { 22 // random pk 23 var pk ProvingKey 24 pk.randomize() 25 26 assert.NoError(t, io.RoundTripCheck(&pk, func() interface{} { return new(ProvingKey) })) 27 } 28 29 func TestVerifyingKeySerialization(t *testing.T) { 30 // create a random vk 31 var vk VerifyingKey 32 vk.randomize() 33 34 assert.NoError(t, io.RoundTripCheck(&vk, func() interface{} { return new(VerifyingKey) })) 35 } 36 37 func (pk *ProvingKey) randomize() { 38 39 var vk VerifyingKey 40 vk.randomize() 41 pk.Vk = &vk 42 43 pk.Kzg.G1 = make([]curve.G1Affine, 32) 44 pk.KzgLagrange.G1 = make([]curve.G1Affine, 32) 45 for i := range pk.Kzg.G1 { 46 pk.Kzg.G1[i] = randomG1Point() 47 pk.KzgLagrange.G1[i] = randomG1Point() 48 } 49 50 } 51 52 func (vk *VerifyingKey) randomize() { 53 vk.Size = rand.Uint64() //#nosec G404 weak rng is fine here 54 vk.SizeInv.SetRandom() 55 vk.Generator.SetRandom() 56 vk.NbPublicVariables = rand.Uint64() //#nosec G404 weak rng is fine here 57 vk.CommitmentConstraintIndexes = []uint64{rand.Uint64()} //#nosec G404 weak rng is fine here 58 vk.CosetShift.SetRandom() 59 60 vk.S[0] = randomG1Point() 61 vk.S[1] = randomG1Point() 62 vk.S[2] = randomG1Point() 63 64 vk.Kzg.G1 = randomG1Point() 65 vk.Kzg.G2[0] = randomG2Point() 66 vk.Kzg.G2[1] = randomG2Point() 67 68 vk.Ql = randomG1Point() 69 vk.Qr = randomG1Point() 70 vk.Qm = randomG1Point() 71 vk.Qo = randomG1Point() 72 vk.Qk = randomG1Point() 73 vk.Qcp = randomG1Points(rand.Intn(4)) //#nosec G404 weak rng is fine here 74 } 75 76 func (proof *Proof) randomize() { 77 proof.LRO[0] = randomG1Point() 78 proof.LRO[1] = randomG1Point() 79 proof.LRO[2] = randomG1Point() 80 proof.Z = randomG1Point() 81 proof.H[0] = randomG1Point() 82 proof.H[1] = randomG1Point() 83 proof.H[2] = randomG1Point() 84 proof.BatchedProof.H = randomG1Point() 85 proof.BatchedProof.ClaimedValues = randomScalars(2) 86 proof.ZShiftedOpening.H = randomG1Point() 87 proof.ZShiftedOpening.ClaimedValue.SetRandom() 88 proof.Bsb22Commitments = randomG1Points(rand.Intn(4)) //#nosec G404 weak rng is fine here 89 } 90 91 func randomG2Point() curve.G2Affine { 92 _, _, _, r := curve.Generators() 93 r.ScalarMultiplication(&r, big.NewInt(int64(rand.Uint64()))) //#nosec G404 weak rng is fine here 94 return r 95 } 96 97 func randomG1Point() curve.G1Affine { 98 _, _, r, _ := curve.Generators() 99 r.ScalarMultiplication(&r, big.NewInt(int64(rand.Uint64()))) //#nosec G404 weak rng is fine here 100 return r 101 } 102 103 func randomG1Points(n int) []curve.G1Affine { 104 res := make([]curve.G1Affine, n) 105 for i := range res { 106 res[i] = randomG1Point() 107 } 108 return res 109 } 110 111 func randomScalars(n int) []fr.Element { 112 v := make([]fr.Element, n) 113 one := fr.One() 114 for i := 0; i < len(v); i++ { 115 if i == 0 { 116 v[i].SetRandom() 117 } else { 118 v[i].Add(&v[i-1], &one) 119 } 120 } 121 return v 122 }