...

Source file src/github.com/cloudflare/circl/ecc/bls12381/ff/cyclo6_test.go

Documentation: github.com/cloudflare/circl/ecc/bls12381/ff

     1  package ff
     2  
     3  import (
     4  	"math/big"
     5  	"testing"
     6  
     7  	"github.com/cloudflare/circl/internal/test"
     8  )
     9  
    10  func randomCyclo6(t testing.TB) *Cyclo6 {
    11  	c := &Cyclo6{}
    12  	EasyExponentiation(c, randomFp12(t))
    13  	return c
    14  }
    15  
    16  // phi6primeSq evaluates the 6-th cyclotomic polynomial, \phi_6(x) = x^2-x+1, at p^2.
    17  func phi6primeSq() []byte {
    18  	one := big.NewInt(1)
    19  	p := new(big.Int).SetBytes(fpOrder[:]) // p
    20  	p2 := new(big.Int).Mul(p, p)           // p^2
    21  	p4 := new(big.Int).Sub(p2, one)        // p^2 - 1
    22  	p4.Mul(p4, p2)                         // p^4 - p^2
    23  	p4.Add(p4, one)                        // p^4 - p^2 + 1
    24  	return p4.Bytes()
    25  }
    26  
    27  func TestCyclo6(t *testing.T) {
    28  	const testTimes = 1 << 10
    29  	t.Run("no_alias", func(t *testing.T) {
    30  		var want, got Cyclo6
    31  		x := randomCyclo6(t)
    32  		got = *x
    33  		got.Sqr(&got)
    34  		want = *x
    35  		want.Mul(&want, &want)
    36  		if got.IsEqual(&want) == 0 {
    37  			test.ReportError(t, got, want, x)
    38  		}
    39  	})
    40  	t.Run("order", func(t *testing.T) {
    41  		cyclo6Order := phi6primeSq()
    42  		var z Cyclo6
    43  		for i := 0; i < 16; i++ {
    44  			x := randomCyclo6(t)
    45  			z.exp(x, cyclo6Order)
    46  
    47  			// x^phi6primeSq = 1
    48  			got := z.IsIdentity()
    49  			want := 1
    50  			if got != want {
    51  				test.ReportError(t, got, want, x, z)
    52  			}
    53  		}
    54  	})
    55  	t.Run("mul_inv", func(t *testing.T) {
    56  		var z Cyclo6
    57  		for i := 0; i < testTimes; i++ {
    58  			x := randomCyclo6(t)
    59  			y := randomCyclo6(t)
    60  
    61  			// x*y*x^1 = y
    62  			z.Inv(x)
    63  			z.Mul(&z, y)
    64  			z.Mul(&z, x)
    65  			got := z
    66  			want := y
    67  			if got.IsEqual(want) == 0 {
    68  				test.ReportError(t, got, want, x, y)
    69  			}
    70  		}
    71  	})
    72  	t.Run("mul_sqr", func(t *testing.T) {
    73  		var want, got Cyclo6
    74  		for i := 0; i < testTimes; i++ {
    75  			x := randomCyclo6(t)
    76  
    77  			// x*x = x^2
    78  			got.Mul(x, x)
    79  			want.Sqr(x)
    80  			if got.IsEqual(&want) == 0 {
    81  				test.ReportError(t, got, want, x)
    82  			}
    83  		}
    84  	})
    85  
    86  	t.Run("invFp12_vs_invCyclo6", func(t *testing.T) {
    87  		var want, got Fp12
    88  		var y Cyclo6
    89  		for i := 0; i < testTimes; i++ {
    90  			x := randomCyclo6(t)
    91  
    92  			y.Inv(x)
    93  			got = (Fp12)(y)
    94  			want.Inv((*Fp12)(x))
    95  
    96  			if got.IsEqual(&want) == 0 {
    97  				test.ReportError(t, got, want, x)
    98  			}
    99  		}
   100  	})
   101  }
   102  
   103  func BenchmarkCyclo6(b *testing.B) {
   104  	x := randomCyclo6(b)
   105  	y := randomCyclo6(b)
   106  	z := randomCyclo6(b)
   107  	b.Run("Mul", func(b *testing.B) {
   108  		for i := 0; i < b.N; i++ {
   109  			z.Mul(x, y)
   110  		}
   111  	})
   112  	b.Run("Sqr", func(b *testing.B) {
   113  		for i := 0; i < b.N; i++ {
   114  			z.Sqr(x)
   115  		}
   116  	})
   117  	b.Run("Inv", func(b *testing.B) {
   118  		for i := 0; i < b.N; i++ {
   119  			z.Inv(x)
   120  		}
   121  	})
   122  	b.Run("PowToX", func(b *testing.B) {
   123  		for i := 0; i < b.N; i++ {
   124  			z.PowToX(x)
   125  		}
   126  	})
   127  }
   128  

View as plain text