github.com/consensys/gnark@v0.11.0/backend/plonk/bls24-315/marshal_test.go (about)

     1  // Copyright 2020 ConsenSys Software Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Code generated by gnark DO NOT EDIT
    16  
    17  package plonk
    18  
    19  import (
    20  	curve "github.com/consensys/gnark-crypto/ecc/bls24-315"
    21  
    22  	"github.com/consensys/gnark-crypto/ecc/bls24-315/fr"
    23  	"github.com/consensys/gnark/io"
    24  	"math/big"
    25  	"math/rand"
    26  	"testing"
    27  
    28  	"github.com/stretchr/testify/assert"
    29  )
    30  
    31  func TestProofSerialization(t *testing.T) {
    32  	// create a  proof
    33  	var proof Proof
    34  	proof.randomize()
    35  
    36  	assert.NoError(t, io.RoundTripCheck(&proof, func() interface{} { return new(Proof) }))
    37  }
    38  
    39  func TestProvingKeySerialization(t *testing.T) {
    40  	// random pk
    41  	var pk ProvingKey
    42  	pk.randomize()
    43  
    44  	assert.NoError(t, io.RoundTripCheck(&pk, func() interface{} { return new(ProvingKey) }))
    45  }
    46  
    47  func TestVerifyingKeySerialization(t *testing.T) {
    48  	// create a random vk
    49  	var vk VerifyingKey
    50  	vk.randomize()
    51  
    52  	assert.NoError(t, io.RoundTripCheck(&vk, func() interface{} { return new(VerifyingKey) }))
    53  }
    54  
    55  func (pk *ProvingKey) randomize() {
    56  
    57  	var vk VerifyingKey
    58  	vk.randomize()
    59  	pk.Vk = &vk
    60  
    61  	pk.Kzg.G1 = make([]curve.G1Affine, 32)
    62  	pk.KzgLagrange.G1 = make([]curve.G1Affine, 32)
    63  	for i := range pk.Kzg.G1 {
    64  		pk.Kzg.G1[i] = randomG1Point()
    65  		pk.KzgLagrange.G1[i] = randomG1Point()
    66  	}
    67  
    68  }
    69  
    70  func (vk *VerifyingKey) randomize() {
    71  	vk.Size = rand.Uint64() //#nosec G404 weak rng is fine here
    72  	vk.SizeInv.SetRandom()
    73  	vk.Generator.SetRandom()
    74  	vk.NbPublicVariables = rand.Uint64()                     //#nosec G404 weak rng is fine here
    75  	vk.CommitmentConstraintIndexes = []uint64{rand.Uint64()} //#nosec G404 weak rng is fine here
    76  	vk.CosetShift.SetRandom()
    77  
    78  	vk.S[0] = randomG1Point()
    79  	vk.S[1] = randomG1Point()
    80  	vk.S[2] = randomG1Point()
    81  
    82  	vk.Kzg.G1 = randomG1Point()
    83  	vk.Kzg.G2[0] = randomG2Point()
    84  	vk.Kzg.G2[1] = randomG2Point()
    85  
    86  	vk.Ql = randomG1Point()
    87  	vk.Qr = randomG1Point()
    88  	vk.Qm = randomG1Point()
    89  	vk.Qo = randomG1Point()
    90  	vk.Qk = randomG1Point()
    91  	vk.Qcp = randomG1Points(rand.Intn(4)) //#nosec G404 weak rng is fine here
    92  }
    93  
    94  func (proof *Proof) randomize() {
    95  	proof.LRO[0] = randomG1Point()
    96  	proof.LRO[1] = randomG1Point()
    97  	proof.LRO[2] = randomG1Point()
    98  	proof.Z = randomG1Point()
    99  	proof.H[0] = randomG1Point()
   100  	proof.H[1] = randomG1Point()
   101  	proof.H[2] = randomG1Point()
   102  	proof.BatchedProof.H = randomG1Point()
   103  	proof.BatchedProof.ClaimedValues = randomScalars(2)
   104  	proof.ZShiftedOpening.H = randomG1Point()
   105  	proof.ZShiftedOpening.ClaimedValue.SetRandom()
   106  	proof.Bsb22Commitments = randomG1Points(rand.Intn(4)) //#nosec G404 weak rng is fine here
   107  }
   108  
   109  func randomG2Point() curve.G2Affine {
   110  	_, _, _, r := curve.Generators()
   111  	r.ScalarMultiplication(&r, big.NewInt(int64(rand.Uint64()))) //#nosec G404 weak rng is fine here
   112  	return r
   113  }
   114  
   115  func randomG1Point() curve.G1Affine {
   116  	_, _, r, _ := curve.Generators()
   117  	r.ScalarMultiplication(&r, big.NewInt(int64(rand.Uint64()))) //#nosec G404 weak rng is fine here
   118  	return r
   119  }
   120  
   121  func randomG1Points(n int) []curve.G1Affine {
   122  	res := make([]curve.G1Affine, n)
   123  	for i := range res {
   124  		res[i] = randomG1Point()
   125  	}
   126  	return res
   127  }
   128  
   129  func randomScalars(n int) []fr.Element {
   130  	v := make([]fr.Element, n)
   131  	one := fr.One()
   132  	for i := 0; i < len(v); i++ {
   133  		if i == 0 {
   134  			v[i].SetRandom()
   135  		} else {
   136  			v[i].Add(&v[i-1], &one)
   137  		}
   138  	}
   139  	return v
   140  }