github.com/cloudflare/circl@v1.5.0/sign/ed25519/point_test.go (about)

     1  package ed25519
     2  
     3  import (
     4  	"crypto/rand"
     5  	"flag"
     6  	"testing"
     7  
     8  	"github.com/cloudflare/circl/internal/test"
     9  )
    10  
    11  func randomPoint(P *pointR1) {
    12  	k := make([]byte, paramB)
    13  	_, _ = rand.Read(k[:])
    14  	P.fixedMult(k)
    15  }
    16  
    17  func TestPoint(t *testing.T) {
    18  	const testTimes = 1 << 10
    19  
    20  	t.Run("add", func(t *testing.T) {
    21  		var P pointR1
    22  		var Q pointR1
    23  		var R pointR2
    24  		for i := 0; i < testTimes; i++ {
    25  			randomPoint(&P)
    26  			_16P := P
    27  			R.fromR1(&P)
    28  			// 16P = 2^4P
    29  			for j := 0; j < 4; j++ {
    30  				_16P.double()
    31  			}
    32  			// 16P = P+P...+P
    33  			Q.SetIdentity()
    34  			for j := 0; j < 16; j++ {
    35  				Q.add(&R)
    36  			}
    37  
    38  			got := _16P.isEqual(&Q)
    39  			want := true
    40  			if got != want {
    41  				test.ReportError(t, got, want, P)
    42  			}
    43  		}
    44  	})
    45  
    46  	t.Run("fixed", func(t *testing.T) {
    47  		var P, Q, R pointR1
    48  		k := make([]byte, paramB)
    49  		l := make([]byte, paramB)
    50  		for i := 0; i < testTimes; i++ {
    51  			randomPoint(&P)
    52  			_, _ = rand.Read(k[:])
    53  
    54  			Q.fixedMult(k[:])
    55  			R.doubleMult(&P, k[:], l[:])
    56  
    57  			got := Q.isEqual(&R)
    58  			want := true
    59  			if got != want {
    60  				test.ReportError(t, got, want, P, k)
    61  			}
    62  		}
    63  	})
    64  }
    65  
    66  var runLongBench = flag.Bool("long", false, "runs longer benchmark")
    67  
    68  func BenchmarkPoint(b *testing.B) {
    69  	if !*runLongBench {
    70  		b.Log("Skipped one long bench, add -long flag to run longer bench")
    71  		b.SkipNow()
    72  	}
    73  
    74  	k := make([]byte, paramB)
    75  	l := make([]byte, paramB)
    76  	_, _ = rand.Read(k)
    77  	_, _ = rand.Read(l)
    78  
    79  	var P pointR1
    80  	var Q pointR2
    81  	var R pointR3
    82  	randomPoint(&P)
    83  	Q.fromR1(&P)
    84  	b.Run("toAffine", func(b *testing.B) {
    85  		for i := 0; i < b.N; i++ {
    86  			P.toAffine()
    87  		}
    88  	})
    89  	b.Run("double", func(b *testing.B) {
    90  		for i := 0; i < b.N; i++ {
    91  			P.double()
    92  		}
    93  	})
    94  	b.Run("mixadd", func(b *testing.B) {
    95  		for i := 0; i < b.N; i++ {
    96  			P.mixAdd(&R)
    97  		}
    98  	})
    99  	b.Run("add", func(b *testing.B) {
   100  		for i := 0; i < b.N; i++ {
   101  			P.add(&Q)
   102  		}
   103  	})
   104  	b.Run("fixedMult", func(b *testing.B) {
   105  		for i := 0; i < b.N; i++ {
   106  			P.fixedMult(k)
   107  		}
   108  	})
   109  	b.Run("doubleMult", func(b *testing.B) {
   110  		for i := 0; i < b.N; i++ {
   111  			P.doubleMult(&P, k, l)
   112  		}
   113  	})
   114  }