github.com/cloudflare/circl@v1.5.0/ecc/bls12381/ff/uroot_test.go (about)

     1  package ff
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/cloudflare/circl/internal/test"
     7  )
     8  
     9  func randomURoot(t testing.TB) *URoot {
    10  	u := &URoot{}
    11  	HardExponentiation(u, randomCyclo6(t))
    12  	return u
    13  }
    14  
    15  func TestURoot(t *testing.T) {
    16  	const testTimes = 1 << 8
    17  	t.Run("no_alias", func(t *testing.T) {
    18  		var want, got URoot
    19  		x := randomURoot(t)
    20  		got = *x
    21  		got.Sqr(&got)
    22  		want = *x
    23  		want.Mul(&want, &want)
    24  		if got.IsEqual(&want) == 0 {
    25  			test.ReportError(t, got, want, x)
    26  		}
    27  	})
    28  	t.Run("order", func(t *testing.T) {
    29  		order := ScalarOrder()
    30  
    31  		var z URoot
    32  		for i := 0; i < 16; i++ {
    33  			x := randomURoot(t)
    34  			(*Cyclo6)(&z).exp((*Cyclo6)(x), order)
    35  
    36  			// x^order = 1
    37  			got := z.IsIdentity()
    38  			want := 1
    39  			if got != want {
    40  				test.ReportError(t, got, want, x, z)
    41  			}
    42  		}
    43  	})
    44  	t.Run("mul_inv", func(t *testing.T) {
    45  		var z URoot
    46  		for i := 0; i < testTimes; i++ {
    47  			x := randomURoot(t)
    48  			y := randomURoot(t)
    49  
    50  			// x*y*x^1 = y
    51  			z.Inv(x)
    52  			z.Mul(&z, y)
    53  			z.Mul(&z, x)
    54  			got := z
    55  			want := y
    56  			if got.IsEqual(want) == 0 {
    57  				test.ReportError(t, got, want, x, y)
    58  			}
    59  		}
    60  	})
    61  	t.Run("mul_sqr", func(t *testing.T) {
    62  		var want, got URoot
    63  		for i := 0; i < testTimes; i++ {
    64  			x := randomURoot(t)
    65  
    66  			// x*x = x^2
    67  			got.Mul(x, x)
    68  			want.Sqr(x)
    69  			if got.IsEqual(&want) == 0 {
    70  				test.ReportError(t, got, want, x)
    71  			}
    72  		}
    73  	})
    74  }
    75  
    76  func BenchmarkURoot(b *testing.B) {
    77  	x := randomURoot(b)
    78  	y := randomURoot(b)
    79  	z := randomURoot(b)
    80  	b.Run("Mul", func(b *testing.B) {
    81  		for i := 0; i < b.N; i++ {
    82  			z.Mul(x, y)
    83  		}
    84  	})
    85  	b.Run("Sqr", func(b *testing.B) {
    86  		for i := 0; i < b.N; i++ {
    87  			z.Sqr(x)
    88  		}
    89  	})
    90  	b.Run("Inv", func(b *testing.B) {
    91  		for i := 0; i < b.N; i++ {
    92  			z.Inv(x)
    93  		}
    94  	})
    95  }