github.com/consensys/gnark-crypto@v0.14.0/ecc/utils_test.go (about)

     1  package ecc
     2  
     3  import (
     4  	"math/big"
     5  	"testing"
     6  )
     7  
     8  func TestNafDecomposition(t *testing.T) {
     9  	t.Parallel()
    10  	// TODO write a real test...
    11  	exp := big.NewInt(13)
    12  	var result [400]int8
    13  	lExp := NafDecomposition(exp, result[:])
    14  	dec := result[:lExp]
    15  
    16  	res := [5]int8{1, 0, -1, 0, 1}
    17  	for i, v := range dec {
    18  		if v != res[i] {
    19  			t.Error("Error in NafDecomposition")
    20  		}
    21  	}
    22  }
    23  
    24  func TestSplitting(t *testing.T) {
    25  	t.Parallel()
    26  
    27  	var lambda, r, s, _s, zero big.Int
    28  	var l Lattice
    29  
    30  	r.SetString("21888242871839275222246405745257275088548364400416034343698204186575808495617", 10)
    31  	lambda.SetString("4407920970296243842393367215006156084916469457145843978461", 10)
    32  
    33  	PrecomputeLattice(&r, &lambda, &l)
    34  
    35  	s.SetString("183927522224640574525727508854836440041603434369820418657580", 10)
    36  
    37  	v := SplitScalar(&s, &l)
    38  	_s.Mul(&v[1], &lambda).Add(&_s, &v[0]).Sub(&_s, &s)
    39  	_s.Mod(&_s, &r)
    40  	if _s.Cmp(&zero) != 0 {
    41  		t.Fatal("Error split scalar")
    42  	}
    43  
    44  }
    45  
    46  func BenchmarkSplitting256(b *testing.B) {
    47  
    48  	var lambda, r, s big.Int
    49  	var l Lattice
    50  
    51  	r.SetString("21888242871839275222246405745257275088548364400416034343698204186575808495617", 10)
    52  	lambda.SetString("4407920970296243842393367215006156084916469457145843978461", 10)
    53  	PrecomputeLattice(&r, &lambda, &l)
    54  	s.SetString("183927522224640574525727508854836440041603434369820418657580", 10)
    55  
    56  	b.ResetTimer()
    57  	for i := 0; i < b.N; i++ {
    58  		SplitScalar(&s, &l)
    59  	}
    60  
    61  }