github.com/cloudflare/circl@v1.5.0/sign/mldsa/mldsa44/internal/sample_test.go (about) 1 // Code generated from mode3/internal/sample_test.go by gen.go 2 3 package internal 4 5 import ( 6 "encoding/binary" 7 "testing" 8 9 common "github.com/cloudflare/circl/sign/internal/dilithium" 10 ) 11 12 func TestVectorDeriveUniform(t *testing.T) { 13 var p, p2 common.Poly 14 var seed [32]byte 15 p2 = common.Poly{ 16 2901364, 562527, 5258502, 3885002, 4190126, 4460268, 6884052, 17 3514511, 5383040, 213206, 2155865, 5179607, 3551954, 2312357, 18 6066350, 8126097, 1179080, 4787182, 6552182, 6713644, 19 1561067, 7626063, 7859743, 5052321, 7032876, 7815031, 157938, 20 1865184, 490802, 5717642, 3451902, 7000218, 3743250, 1677431, 21 1875427, 5596150, 671623, 3819041, 6247594, 1014875, 4933545, 22 7122446, 6682963, 3388398, 3335295, 943002, 1145083, 3113071, 23 105967, 1916675, 7474561, 1107006, 700548, 2147909, 1603855, 24 5049181, 437882, 6118899, 5656914, 6731065, 3066622, 865453, 25 5427634, 981549, 4650873, 861291, 4003872, 5104220, 6171453, 26 3723302, 7426315, 6137283, 4874820, 6052561, 53441, 5032874, 27 5614778, 2248550, 1756499, 8280764, 8263880, 7600081, 28 5118374, 795344, 7543392, 6869925, 1841187, 4181568, 584562, 29 7483939, 4938664, 6863397, 5126354, 5218129, 6236086, 30 4149293, 379169, 4368487, 7490569, 3409215, 1580463, 3081737, 31 1278732, 7109719, 7371700, 2097931, 399836, 1700274, 7188595, 32 6830029, 1548850, 6593138, 6849097, 1518037, 2859442, 33 7772265, 7325153, 3281191, 7856131, 4995056, 4684325, 34 1351194, 8223904, 6817307, 2484146, 131782, 397032, 7436778, 35 7973479, 3171829, 5624626, 3540123, 7150120, 8313283, 36 3604714, 1043574, 117692, 7797783, 7909392, 903315, 7335342, 37 7501562, 5826142, 2709813, 8245473, 2369045, 2782257, 38 5762833, 6474114, 6862031, 424522, 594248, 2626630, 7659983, 39 5642869, 4075194, 1592129, 245547, 5271031, 3205046, 982375, 40 267873, 1286496, 7230481, 3208972, 7485411, 676111, 4944500, 41 2959742, 5934456, 1414847, 6067948, 1709895, 4648315, 126008, 42 8258986, 2183134, 2302072, 4674924, 4306056, 7465311, 43 6500270, 4247428, 4016815, 4973426, 294287, 2456847, 3289700, 44 2732169, 1159447, 5569724, 140001, 3237977, 8007761, 5874533, 45 255652, 3119586, 2102434, 6248250, 8152822, 8006066, 7708625, 46 6997719, 6260212, 6186962, 6636650, 7836834, 7998017, 47 2061516, 1197591, 1706544, 733027, 2392907, 2700000, 8254598, 48 4488002, 160495, 2985325, 2036837, 2703633, 6406550, 3579947, 49 6195178, 5552390, 6804584, 6305468, 5731980, 6095195, 50 3323409, 1322661, 6690942, 3374630, 5615167, 479044, 3136054, 51 4380418, 2833144, 7829577, 1770522, 6056687, 240415, 14780, 52 3740517, 5224226, 3547288, 2083124, 4699398, 3654239, 53 5624978, 585593, 3655369, 2281739, 3338565, 1908093, 7784706, 54 4352830, 55 } 56 for i := 0; i < 32; i++ { 57 seed[i] = byte(i) 58 } 59 PolyDeriveUniform(&p, &seed, 30000) 60 if p != p2 { 61 t.Fatalf("%v != %v", p, p2) 62 } 63 } 64 65 func TestDeriveUniform(t *testing.T) { 66 var p common.Poly 67 var seed [32]byte 68 for i := 0; i < 100; i++ { 69 binary.LittleEndian.PutUint64(seed[:], uint64(i)) 70 PolyDeriveUniform(&p, &seed, uint16(i)) 71 if !PolyNormalized(&p) { 72 t.Fatal() 73 } 74 } 75 } 76 77 func TestDeriveUniformLeqEta(t *testing.T) { 78 var p common.Poly 79 var seed [64]byte 80 for i := 0; i < 100; i++ { 81 binary.LittleEndian.PutUint64(seed[:], uint64(i)) 82 PolyDeriveUniformLeqEta(&p, &seed, uint16(i)) 83 for j := 0; j < common.N; j++ { 84 if p[j] < common.Q-Eta || p[j] > common.Q+Eta { 85 t.Fatal() 86 } 87 } 88 } 89 } 90 91 func TestDeriveUniformLeGamma1(t *testing.T) { 92 var p common.Poly 93 var seed [64]byte 94 for i := 0; i < 100; i++ { 95 binary.LittleEndian.PutUint64(seed[:], uint64(i)) 96 PolyDeriveUniformLeGamma1(&p, &seed, uint16(i)) 97 for j := 0; j < common.N; j++ { 98 if (p[j] > Gamma1 && p[j] <= common.Q-Gamma1) || p[j] >= common.Q { 99 t.Fatal() 100 } 101 } 102 } 103 } 104 105 func TestDeriveUniformBall(t *testing.T) { 106 var p common.Poly 107 var seed [CTildeSize]byte 108 for i := 0; i < 100; i++ { 109 binary.LittleEndian.PutUint64(seed[:], uint64(i)) 110 PolyDeriveUniformBall(&p, seed[:]) 111 nonzero := 0 112 for j := 0; j < common.N; j++ { 113 if p[j] != 0 { 114 if p[j] != 1 && p[j] != common.Q-1 { 115 t.Fatal() 116 } 117 nonzero++ 118 } 119 } 120 if nonzero != Tau { 121 t.Fatal() 122 } 123 } 124 } 125 126 func TestDeriveUniformX4(t *testing.T) { 127 if !DeriveX4Available { 128 t.SkipNow() 129 } 130 var ps [4]common.Poly 131 var p common.Poly 132 var seed [32]byte 133 nonces := [4]uint16{12345, 54321, 13532, 37377} 134 135 for i := 0; i < len(seed); i++ { 136 seed[i] = byte(i) 137 } 138 139 PolyDeriveUniformX4([4]*common.Poly{&ps[0], &ps[1], &ps[2], &ps[3]}, &seed, 140 nonces) 141 for i := 0; i < 4; i++ { 142 PolyDeriveUniform(&p, &seed, nonces[i]) 143 if ps[i] != p { 144 t.Fatal() 145 } 146 } 147 } 148 149 func TestDeriveUniformBallX4(t *testing.T) { 150 if !DeriveX4Available { 151 t.SkipNow() 152 } 153 var ps [4]common.Poly 154 var p common.Poly 155 var seed [CTildeSize]byte 156 PolyDeriveUniformBallX4( 157 [4]*common.Poly{&ps[0], &ps[1], &ps[2], &ps[3]}, 158 seed[:], 159 ) 160 for j := 0; j < 4; j++ { 161 PolyDeriveUniformBall(&p, seed[:]) 162 if ps[j] != p { 163 t.Fatalf("%d\n%v\n%v", j, ps[j], p) 164 } 165 } 166 } 167 168 func BenchmarkPolyDeriveUniformBall(b *testing.B) { 169 var seed [32]byte 170 var p common.Poly 171 var w1 VecK 172 for i := 0; i < b.N; i++ { 173 w1[0][0] = uint32(i) 174 PolyDeriveUniformBall(&p, seed[:]) 175 } 176 } 177 178 func BenchmarkPolyDeriveUniformBallX4(b *testing.B) { 179 var seed [32]byte 180 var p common.Poly 181 var w1 VecK 182 for i := 0; i < b.N; i++ { 183 w1[0][0] = uint32(i) 184 PolyDeriveUniformBallX4( 185 [4]*common.Poly{&p, &p, &p, &p}, 186 seed[:], 187 ) 188 } 189 } 190 191 func BenchmarkPolyDeriveUniform(b *testing.B) { 192 var seed [32]byte 193 var p common.Poly 194 for i := 0; i < b.N; i++ { 195 PolyDeriveUniform(&p, &seed, uint16(i)) 196 } 197 } 198 199 func BenchmarkPolyDeriveUniformX4(b *testing.B) { 200 if !DeriveX4Available { 201 b.SkipNow() 202 } 203 var seed [32]byte 204 var p [4]common.Poly 205 for i := 0; i < b.N; i++ { 206 nonce := uint16(4 * i) 207 PolyDeriveUniformX4([4]*common.Poly{&p[0], &p[1], &p[2], &p[3]}, 208 &seed, [4]uint16{nonce, nonce + 1, nonce + 2, nonce + 3}) 209 } 210 } 211 212 func BenchmarkPolyDeriveUniformLeGamma1(b *testing.B) { 213 var seed [64]byte 214 var p common.Poly 215 for i := 0; i < b.N; i++ { 216 PolyDeriveUniformLeGamma1(&p, &seed, uint16(i)) 217 } 218 }