github.com/cloudflare/circl@v1.5.0/dh/sidh/internal/p434/arith_test.go (about)

     1  // Code generated by go generate; DO NOT EDIT.
     2  // This file was generated by robots.
     3  
     4  package p434
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/cloudflare/circl/dh/sidh/internal/common"
    10  )
    11  
    12  // Package-level storage for this field element is intended to deter
    13  // compiler optimizations.
    14  var (
    15  	benchmarkFp   common.Fp
    16  	benchmarkFpX2 common.FpX2
    17  	bench_x       = common.Fp{17026702066521327207, 5108203422050077993, 10225396685796065916, 11153620995215874678, 6531160855165088358, 15302925148404145445, 1248821577836769963, 9789766903037985294, 7493111552032041328, 10838999828319306046, 18103257655515297935, 27403304611634}
    18  	bench_y       = common.Fp{4227467157325093378, 10699492810770426363, 13500940151395637365, 12966403950118934952, 16517692605450415877, 13647111148905630666, 14223628886152717087, 7167843152346903316, 15855377759596736571, 4300673881383687338, 6635288001920617779, 30486099554235}
    19  	bench_z       = common.FpX2{1595347748594595712, 10854920567160033970, 16877102267020034574, 12435724995376660096, 3757940912203224231, 8251999420280413600, 3648859773438820227, 17622716832674727914, 11029567000887241528, 11216190007549447055, 17606662790980286987, 4720707159513626555, 12887743598335030915, 14954645239176589309, 14178817688915225254, 1191346797768989683, 12629157932334713723, 6348851952904485603, 16444232588597434895, 7809979927681678066, 14642637672942531613, 3092657597757640067, 10160361564485285723, 240071237}
    20  )
    21  
    22  func TestFpCswap(t *testing.T) {
    23  	one := common.Fp{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
    24  	two := common.Fp{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}
    25  
    26  	x := one
    27  	y := two
    28  
    29  	cswapP434(&x, &y, 0)
    30  	for i := 0; i < FpWords; i++ {
    31  		if (x[i] != one[i]) || (y[i] != two[i]) {
    32  			t.Error("Found", x, "expected", one)
    33  		}
    34  	}
    35  
    36  	cswapP434(&x, &y, 1)
    37  	for i := 0; i < FpWords; i++ {
    38  		if (x[i] != two[i]) || (y[i] != one[i]) {
    39  			t.Error("Found", x, "expected", two)
    40  		}
    41  	}
    42  }
    43  
    44  func TestFpCmov(t *testing.T) {
    45  	one := common.Fp{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
    46  	two := common.Fp{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}
    47  
    48  	x := one
    49  	y := two
    50  
    51  	cmovP434(&x, &y, 0)
    52  	for i := 0; i < FpWords; i++ {
    53  		if x[i] != one[i] {
    54  			t.Error("Found", x, "expected", one)
    55  		}
    56  		if y[i] != two[i] {
    57  			t.Error("Found", y, "expected", two)
    58  		}
    59  	}
    60  
    61  	cmovP434(&x, &y, 1)
    62  	for i := 0; i < FpWords; i++ {
    63  		if x[i] != two[i] {
    64  			t.Error("Found", x, "expected", two)
    65  		}
    66  		if y[i] != two[i] {
    67  			t.Error("Found", y, "expected", two)
    68  		}
    69  	}
    70  }
    71  
    72  // Benchmarking for field arithmetic
    73  func BenchmarkMul(b *testing.B) {
    74  	for n := 0; n < b.N; n++ {
    75  		mulP434(&benchmarkFpX2, &bench_x, &bench_y)
    76  	}
    77  }
    78  
    79  func BenchmarkRdc(b *testing.B) {
    80  	z := bench_z
    81  
    82  	// This benchmark actually computes garbage, because
    83  	// rdcP434 mangles its input, but since it's
    84  	// constant-time that shouldn't matter for the benchmarks.
    85  	for n := 0; n < b.N; n++ {
    86  		rdcP434(&benchmarkFp, &z)
    87  	}
    88  }
    89  
    90  func BenchmarkAdd(b *testing.B) {
    91  	for n := 0; n < b.N; n++ {
    92  		addP434(&benchmarkFp, &bench_x, &bench_y)
    93  	}
    94  }
    95  
    96  func BenchmarkSub(b *testing.B) {
    97  	for n := 0; n < b.N; n++ {
    98  		subP434(&benchmarkFp, &bench_x, &bench_y)
    99  	}
   100  }
   101  
   102  func BenchmarkCswap(b *testing.B) {
   103  	x, y := bench_x, bench_y
   104  	for n := 0; n < b.N; n++ {
   105  		cswapP434(&x, &y, 1)
   106  		cswapP434(&x, &y, 0)
   107  	}
   108  }
   109  
   110  func BenchmarkMod(b *testing.B) {
   111  	x := bench_x
   112  	for n := 0; n < b.N; n++ {
   113  		modP434(&x)
   114  	}
   115  }
   116  
   117  func BenchmarkX2AddLazy(b *testing.B) {
   118  	x, y, z := bench_z, bench_z, bench_z
   119  	for n := 0; n < b.N; n++ {
   120  		adlP434(&x, &y, &z)
   121  	}
   122  }
   123  
   124  func BenchmarkX2SubLazy(b *testing.B) {
   125  	x, y, z := bench_z, bench_z, bench_z
   126  	for n := 0; n < b.N; n++ {
   127  		sulP434(&x, &y, &z)
   128  	}
   129  }