github.com/cloudflare/circl@v1.5.0/ecc/goldilocks/curve_test.go (about)

     1  package goldilocks_test
     2  
     3  import (
     4  	"crypto/rand"
     5  	"testing"
     6  
     7  	"github.com/cloudflare/circl/ecc/goldilocks"
     8  	"github.com/cloudflare/circl/internal/test"
     9  )
    10  
    11  func TestScalarMult(t *testing.T) {
    12  	const testTimes = 1 << 8
    13  	var e goldilocks.Curve
    14  	k := &goldilocks.Scalar{}
    15  	zero := &goldilocks.Scalar{}
    16  
    17  	t.Run("rG=0", func(t *testing.T) {
    18  		order := e.Order()
    19  		for i := 0; i < testTimes; i++ {
    20  			got := e.ScalarBaseMult(&order)
    21  			got.ToAffine()
    22  			want := e.Identity()
    23  
    24  			if !e.IsOnCurve(got) || !e.IsOnCurve(want) || !got.IsEqual(want) {
    25  				want.ToAffine()
    26  				test.ReportError(t, got, want)
    27  			}
    28  		}
    29  	})
    30  	t.Run("rP=0", func(t *testing.T) {
    31  		order := e.Order()
    32  		for i := 0; i < testTimes; i++ {
    33  			P := randomPoint()
    34  
    35  			got := e.ScalarMult(&order, P)
    36  			got.ToAffine()
    37  			want := e.Identity()
    38  
    39  			if !e.IsOnCurve(got) || !e.IsOnCurve(want) || !got.IsEqual(want) {
    40  				want.ToAffine()
    41  				test.ReportError(t, got, want, P, order)
    42  			}
    43  		}
    44  	})
    45  	t.Run("kG", func(t *testing.T) {
    46  		I := e.Identity()
    47  		for i := 0; i < testTimes; i++ {
    48  			_, _ = rand.Read(k[:])
    49  
    50  			got := e.ScalarBaseMult(k)
    51  			want := e.CombinedMult(k, zero, I) // k*G + 0*I
    52  
    53  			if !e.IsOnCurve(got) || !e.IsOnCurve(want) || !got.IsEqual(want) {
    54  				test.ReportError(t, got, want, k)
    55  			}
    56  		}
    57  	})
    58  	t.Run("kP", func(t *testing.T) {
    59  		for i := 0; i < testTimes; i++ {
    60  			P := randomPoint()
    61  			_, _ = rand.Read(k[:])
    62  
    63  			got := e.ScalarMult(k, P)
    64  			want := e.CombinedMult(zero, k, P)
    65  
    66  			if !e.IsOnCurve(got) || !e.IsOnCurve(want) || !got.IsEqual(want) {
    67  				test.ReportError(t, got, want, P, k)
    68  			}
    69  		}
    70  	})
    71  	t.Run("kG+lP", func(t *testing.T) {
    72  		G := e.Generator()
    73  		l := &goldilocks.Scalar{}
    74  		for i := 0; i < testTimes; i++ {
    75  			P := randomPoint()
    76  			_, _ = rand.Read(k[:])
    77  			_, _ = rand.Read(l[:])
    78  
    79  			kG := e.ScalarMult(k, G)
    80  			lP := e.ScalarMult(l, P)
    81  			got := e.Add(kG, lP)
    82  			want := e.CombinedMult(k, l, P)
    83  
    84  			if !e.IsOnCurve(got) || !e.IsOnCurve(want) || !got.IsEqual(want) {
    85  				test.ReportError(t, got, want, P, k, l)
    86  			}
    87  		}
    88  	})
    89  }
    90  
    91  func BenchmarkCurve(b *testing.B) {
    92  	var e goldilocks.Curve
    93  	var k, l goldilocks.Scalar
    94  	_, _ = rand.Read(k[:])
    95  	_, _ = rand.Read(l[:])
    96  	P := randomPoint()
    97  
    98  	b.Run("ScalarMult", func(b *testing.B) {
    99  		for i := 0; i < b.N; i++ {
   100  			P = e.ScalarMult(&k, P)
   101  		}
   102  	})
   103  	b.Run("ScalarBaseMult", func(b *testing.B) {
   104  		for i := 0; i < b.N; i++ {
   105  			e.ScalarBaseMult(&k)
   106  		}
   107  	})
   108  	b.Run("CombinedMult", func(b *testing.B) {
   109  		for i := 0; i < b.N; i++ {
   110  			P = e.CombinedMult(&k, &l, P)
   111  		}
   112  	})
   113  }