github.com/consensys/gnark@v0.11.0/test/blueprint_solver_test.go (about)

     1  package test
     2  
     3  import (
     4  	"math/big"
     5  	"math/rand"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/consensys/gnark-crypto/ecc"
    10  )
    11  
    12  func TestBigIntToElement(t *testing.T) {
    13  	t.Parallel()
    14  	// sample a random big.Int, convert it to an element, and back
    15  	// to a big.Int, and check that it's the same
    16  	s := blueprintSolver{q: ecc.BN254.ScalarField()}
    17  	b := big.NewInt(0)
    18  	for i := 0; i < 50; i++ {
    19  		b.Rand(rand.New(rand.NewSource(time.Now().Unix())), s.q) //#nosec G404 -- This is a false positive
    20  		e := s.toElement(b)
    21  		b2 := s.ToBigInt(e)
    22  		if b.Cmp(b2) != 0 {
    23  			t.Fatal("b != b2")
    24  		}
    25  	}
    26  
    27  }
    28  
    29  func TestBigIntToUint32Slice(t *testing.T) {
    30  	t.Parallel()
    31  	// sample a random big.Int, write it to a uint32 slice, and back
    32  	// to a big.Int, and check that it's the same
    33  	s := blueprintSolver{q: ecc.BN254.ScalarField()}
    34  	b1 := big.NewInt(0)
    35  	b2 := big.NewInt(0)
    36  
    37  	for i := 0; i < 50; i++ {
    38  		b1.Rand(rand.New(rand.NewSource(time.Now().Unix())), s.q) //#nosec G404 -- This is a false positive
    39  		b2.Rand(rand.New(rand.NewSource(time.Now().Unix())), s.q) //#nosec G404 -- This is a false positive
    40  		wb1 := wrappedBigInt{b1}
    41  		wb2 := wrappedBigInt{b2}
    42  		var to []uint32
    43  		wb1.Compress(&to)
    44  		wb2.Compress(&to)
    45  
    46  		if len(to) != 24 {
    47  			t.Fatal("wrong length: expected 2*len of constraint.Element (uint32 words)")
    48  		}
    49  
    50  		e1, n := s.Read(to)
    51  		if n != 12 {
    52  			t.Fatal("wrong length: expected 1 len of constraint.Element (uint32 words)")
    53  		}
    54  		e2, n := s.Read(to[n:])
    55  		if n != 12 {
    56  			t.Fatal("wrong length: expected 1 len of constraint.Element (uint32 words)")
    57  		}
    58  		rb1, rb2 := s.ToBigInt(e1), s.ToBigInt(e2)
    59  		if rb1.Cmp(b1) != 0 || rb2.Cmp(b2) != 0 {
    60  			t.Fatal("rb1 != b1 || rb2 != b2")
    61  		}
    62  	}
    63  
    64  }