github.com/cloudflare/circl@v1.5.0/pke/kyber/internal/common/sample_test.go (about) 1 package common 2 3 import ( 4 "testing" 5 ) 6 7 func BenchmarkDeriveNoise2(b *testing.B) { 8 var p Poly 9 var seed [32]byte 10 for i := 0; i < b.N; i++ { 11 p.DeriveNoise2(seed[:], 37) 12 } 13 } 14 15 func BenchmarkDeriveNoise3(b *testing.B) { 16 var p Poly 17 var seed [32]byte 18 for i := 0; i < b.N; i++ { 19 p.DeriveNoise3(seed[:], 37) 20 } 21 } 22 23 func TestPolyDeriveNoise3Ref(t *testing.T) { 24 var p Poly 25 26 want := Poly{ 27 0, 0, 1, -1, 0, 2, 0, -1, -1, 3, 0, 1, -2, -2, 0, 1, -2, 28 1, 0, -2, 3, 0, 0, 0, 1, 3, 1, 1, 2, 1, -1, -1, -1, 0, 1, 29 0, 1, 0, 2, 0, 1, -2, 0, -1, -1, -2, 1, -1, -1, 2, -1, 1, 30 1, 2, -3, -1, -1, 0, 0, 0, 0, 1, -1, -2, -2, 0, -2, 0, 0, 31 0, 1, 0, -1, -1, 1, -2, 2, 0, 0, 2, -2, 0, 1, 0, 1, 1, 1, 32 0, 1, -2, -1, -2, -1, 1, 0, 0, 0, 0, 0, 1, 0, -1, -1, 0, 33 -1, 1, 0, 1, 0, -1, -1, 0, -2, 2, 0, -2, 1, -1, 0, 1, -1, 34 -1, 2, 1, 0, 0, -2, -1, 2, 0, 0, 0, -1, -1, 3, 1, 0, 1, 0, 35 1, 0, 2, 1, 0, 0, 1, 0, 1, 0, 0, -1, -1, -1, 0, 1, 3, 1, 36 0, 1, 0, 1, -1, -1, -1, -1, 0, 0, -2, -1, -1, 2, 0, 1, 0, 37 1, 0, 2, -2, 0, 1, 1, -3, -1, -2, -1, 0, 1, 0, 1, -2, 2, 38 2, 1, 1, 0, -1, 0, -1, -1, 1, 0, -1, 2, 1, -1, 1, 2, -2, 39 1, 2, 0, 1, 2, 1, 0, 0, 2, 1, 2, 1, 0, 2, 1, 0, 0, -1, -1, 40 1, -1, 0, 1, -1, 2, 2, 0, 0, -1, 1, 1, 1, 1, 0, 0, -2, 0, 41 -1, 1, 2, 0, 0, 1, 1, -1, 1, 0, 1, 42 } 43 44 var seed [32]byte 45 46 for i := 0; i < 32; i++ { 47 seed[i] = byte(i) 48 } 49 50 p.DeriveNoise3(seed[:], 37) 51 52 if p != want { 53 t.Fatal() 54 } 55 } 56 57 func TestPolyDeriveNoise2Ref(t *testing.T) { 58 var p Poly 59 60 want := Poly{ 61 1, 0, 1, -1, -1, -2, -1, -1, 2, 0, -1, 0, 0, -1, 62 1, 1, -1, 1, 0, 2, -2, 0, 1, 2, 0, 0, -1, 1, 0, -1, 63 1, -1, 1, 2, 1, 1, 0, -1, 1, -1, -2, -1, 1, -1, -1, 64 -1, 2, -1, -1, 0, 0, 1, 1, -1, 1, 1, 1, 1, -1, -2, 65 0, 1, 0, 0, 2, 1, -1, 2, 0, 0, 1, 1, 0, -1, 0, 0, 66 -1, -1, 2, 0, 1, -1, 2, -1, -1, -1, -1, 0, -2, 0, 67 2, 1, 0, 0, 0, -1, 0, 0, 0, -1, -1, 0, -1, -1, 0, 68 -1, 0, 0, -2, 1, 1, 0, 1, 0, 1, 0, 1, 1, -1, 2, 0, 69 1, -1, 1, 2, 0, 0, 0, 0, -1, -1, -1, 0, 1, 0, -1, 70 2, 0, 0, 1, 1, 1, 0, 1, -1, 1, 2, 1, 0, 2, -1, 1, 71 -1, -2, -1, -2, -1, 1, 0, -2, -2, -1, 1, 0, 0, 0, 72 0, 1, 0, 0, 0, 2, 2, 0, 1, 0, -1, -1, 0, 2, 0, 0, 73 -2, 1, 0, 2, 1, -1, -2, 0, 0, -1, 1, 1, 0, 0, 2, 74 0, 1, 1, -2, 1, -2, 1, 1, 0, 2, 0, -1, 0, -1, 0, 75 1, 2, 0, 1, 0, -2, 1, -2, -2, 1, -1, 0, -1, 1, 1, 76 0, 0, 0, 1, 0, -1, 1, 1, 0, 0, 0, 0, 1, 0, 1, -1, 77 0, 1, -1, -1, 2, 0, 0, 1, -1, 0, 1, -1, 0, 78 } 79 80 var seed [32]byte 81 82 for i := 0; i < 32; i++ { 83 seed[i] = byte(i) 84 } 85 86 p.DeriveNoise2(seed[:], 37) 87 88 if p != want { 89 t.Fatal() 90 } 91 } 92 93 func TestPolyDeriveUniformRef(t *testing.T) { 94 var p Poly 95 96 // Generated by reference implementation. 97 want := Poly{ 98 797, 993, 161, 6, 2608, 2385, 2096, 2661, 1676, 247, 2440, 99 342, 634, 194, 1570, 2848, 986, 684, 3148, 3208, 2018, 351, 100 2288, 612, 1394, 170, 1521, 3119, 58, 596, 2093, 1549, 409, 101 2156, 1934, 1730, 1324, 388, 446, 418, 1719, 2202, 1812, 102 98, 1019, 2369, 214, 2699, 28, 1523, 2824, 273, 402, 2899, 103 246, 210, 1288, 863, 2708, 177, 3076, 349, 44, 949, 854, 104 1371, 957, 292, 2502, 1617, 1501, 254, 7, 1761, 2581, 2206, 105 2655, 1211, 629, 1274, 2358, 816, 2766, 2115, 2985, 1006, 106 2433, 856, 2596, 3192, 1, 1378, 2345, 707, 1891, 1669, 536, 107 1221, 710, 2511, 120, 1176, 322, 1897, 2309, 595, 2950, 108 1171, 801, 1848, 695, 2912, 1396, 1931, 1775, 2904, 893, 109 2507, 1810, 2873, 253, 1529, 1047, 2615, 1687, 831, 1414, 110 965, 3169, 1887, 753, 3246, 1937, 115, 2953, 586, 545, 1621, 111 1667, 3187, 1654, 1988, 1857, 512, 1239, 1219, 898, 3106, 112 391, 1331, 2228, 3169, 586, 2412, 845, 768, 156, 662, 478, 113 1693, 2632, 573, 2434, 1671, 173, 969, 364, 1663, 2701, 114 2169, 813, 1000, 1471, 720, 2431, 2530, 3161, 733, 1691, 115 527, 2634, 335, 26, 2377, 1707, 767, 3020, 950, 502, 426, 116 1138, 3208, 2607, 2389, 44, 1358, 1392, 2334, 875, 2097, 117 173, 1697, 2578, 942, 1817, 974, 1165, 2853, 1958, 2973, 118 3282, 271, 1236, 1677, 2230, 673, 1554, 96, 242, 1729, 2518, 119 1884, 2272, 71, 1382, 924, 1807, 1610, 456, 1148, 2479, 120 2152, 238, 2208, 2329, 713, 1175, 1196, 757, 1078, 3190, 121 3169, 708, 3117, 154, 1751, 3225, 1364, 154, 23, 2842, 1105, 122 1419, 79, 5, 2013, 123 } 124 125 var seed [32]byte 126 127 for i := 0; i < 32; i++ { 128 seed[i] = byte(i) 129 } 130 131 p.DeriveUniform(&seed, 1, 0) 132 p.Detangle() 133 p.Normalize() 134 135 if p != want { 136 t.Fatalf("%v\n%v", p, want) 137 } 138 } 139 140 func BenchmarkPolyDeriveUniform(b *testing.B) { 141 var p Poly 142 var seed [32]byte 143 for i := 0; i < b.N; i++ { 144 p.DeriveUniform(&seed, 0, 0) 145 } 146 } 147 148 func BenchmarkPolyDeriveUniformX4(b *testing.B) { 149 if !DeriveX4Available { 150 b.SkipNow() 151 } 152 153 var p [4]Poly 154 var seed [32]byte 155 for i := 0; i < b.N; i++ { 156 PolyDeriveUniformX4( 157 [4]*Poly{&p[0], &p[1], &p[2], &p[3]}, 158 &seed, 159 [4]uint8{0, 1, 2, 3}, 160 [4]uint8{4, 5, 6, 7}, 161 ) 162 } 163 } 164 165 func TestPolyDeriveUniformX4(t *testing.T) { 166 if !DeriveX4Available { 167 t.SkipNow() 168 } 169 170 var p2 Poly 171 var p [4]Poly 172 var seed [32]byte 173 174 for i := 0; i < 32; i++ { 175 seed[i] = byte(i) 176 } 177 178 PolyDeriveUniformX4( 179 [4]*Poly{&p[0], &p[1], &p[2], &p[3]}, 180 &seed, 181 [4]uint8{0, 1, 2, 3}, 182 [4]uint8{4, 5, 6, 7}, 183 ) 184 185 for i := 0; i < 4; i++ { 186 p2.DeriveUniform(&seed, uint8(i), uint8(i+4)) 187 if p2 != p[i] { 188 t.Fatalf("%d\n%v\n%v", i, p2, p[i]) 189 } 190 } 191 }