github.com/cloudflare/circl@v1.5.0/sign/internal/dilithium/poly_test.go (about) 1 package dilithium 2 3 import "testing" 4 5 func TestExceeds(t *testing.T) { 6 for i := 0; i < N; i++ { 7 var p Poly 8 for v := 0; v < 10; v++ { 9 p[i] = uint32(v) 10 if p.Exceeds(uint32(10)) { 11 t.Fatal() 12 } 13 p[i] = Q - uint32(v) 14 if p.Exceeds(uint32(10)) { 15 t.Fatal() 16 } 17 } 18 for v := 10; v < 20; v++ { 19 p[i] = uint32(v) 20 if !p.Exceeds(uint32(10)) { 21 t.Fatal() 22 } 23 p[i] = Q - uint32(v) 24 if !p.Exceeds(uint32(10)) { 25 t.Fatal() 26 } 27 } 28 } 29 } 30 31 func TestSubAgainstGeneric(t *testing.T) { 32 for k := 0; k < 1000; k++ { 33 var p1, p2, a, b Poly 34 a.RandLe2Q() 35 b.RandLe2Q() 36 p1.Sub(&a, &b) 37 p2.subGeneric(&a, &b) 38 if p1 != p2 { 39 t.Fatalf("Sub(%v, %v) =\n%v\n!= %v", a, b, p1, p2) 40 } 41 } 42 } 43 44 func TestAddAgainstGeneric(t *testing.T) { 45 for k := 0; k < 1000; k++ { 46 var p1, p2, a, b Poly 47 a.RandLe2Q() 48 b.RandLe2Q() 49 p1.Add(&a, &b) 50 p2.addGeneric(&a, &b) 51 if p1 != p2 { 52 t.Fatalf("Add(%v, %v) =\n%v\n!= %v", a, b, p1, p2) 53 } 54 } 55 } 56 57 func TestMulHatAgainstGeneric(t *testing.T) { 58 for k := 0; k < 1000; k++ { 59 var p1, p2, a, b Poly 60 a.RandLe2Q() 61 b.RandLe2Q() 62 p1.MulHat(&a, &b) 63 p2.mulHatGeneric(&a, &b) 64 if p1 != p2 { 65 t.Fatalf("MulHat(%v, %v) =\n%v\n!= %v", a, b, p1, p2) 66 } 67 } 68 } 69 70 func TestReduceLe2QAgainstGeneric(t *testing.T) { 71 for k := 0; k < 1000; k++ { 72 var a Poly 73 r := randSliceUint32(N) 74 copy(a[:], r) 75 p1 := a 76 p2 := a 77 p1.reduceLe2QGeneric() 78 p2.ReduceLe2Q() 79 if p1 != p2 { 80 t.Fatalf("%v !=\n%v", p1, p2) 81 } 82 } 83 } 84 85 func TestNormalizeAgainstGeneric(t *testing.T) { 86 for k := 0; k < 1000; k++ { 87 var a Poly 88 r := randSliceUint32(N) 89 copy(a[:], r) 90 p1 := a 91 p2 := a 92 p1.normalizeGeneric() 93 p2.Normalize() 94 if p1 != p2 { 95 t.Fatalf("%v !=\n%v", p1, p2) 96 } 97 } 98 } 99 100 func TestMulBy2ToDAgainstGeneric(t *testing.T) { 101 for k := 0; k < 1000; k++ { 102 var p1, p2, q Poly 103 q.RandLe2Q() 104 p1.mulBy2toDGeneric(&q) 105 p2.MulBy2toD(&q) 106 if p1 != p2 { 107 t.Fatalf("MulBy2ToD(%v) =\n%v\n!= %v", q, p1, p2) 108 } 109 } 110 } 111 112 func BenchmarkNormalizeGeneric(b *testing.B) { 113 var p Poly 114 for i := 0; i < b.N; i++ { 115 p.normalizeGeneric() 116 } 117 } 118 119 func BenchmarkMulHatGeneric(b *testing.B) { 120 var p Poly 121 for i := 0; i < b.N; i++ { 122 p.mulHatGeneric(&p, &p) 123 } 124 } 125 126 func BenchmarkAddGeneric(b *testing.B) { 127 var p Poly 128 for i := 0; i < b.N; i++ { 129 p.addGeneric(&p, &p) 130 } 131 } 132 133 func BenchmarkSubGeneric(b *testing.B) { 134 var p Poly 135 for i := 0; i < b.N; i++ { 136 p.subGeneric(&p, &p) 137 } 138 } 139 140 func BenchmarkReduceLe2QGeneric(b *testing.B) { 141 var p Poly 142 for i := 0; i < b.N; i++ { 143 p.reduceLe2QGeneric() 144 } 145 } 146 147 func BenchmarkNormalizeAssumingLe2QGeneric(b *testing.B) { 148 var p Poly 149 for i := 0; i < b.N; i++ { 150 p.normalizeAssumingLe2QGeneric() 151 } 152 } 153 154 func BenchmarkExceedsGeneric(b *testing.B) { 155 var p Poly 156 for i := 0; i < b.N; i++ { 157 p.exceedsGeneric(uint32(10)) 158 } 159 } 160 161 func BenchmarkMulBy2toDGeneric(b *testing.B) { 162 var p, q Poly 163 for i := 0; i < b.N; i++ { 164 p.mulBy2toDGeneric(&q) 165 } 166 } 167 168 func BenchmarkMulHat(b *testing.B) { 169 var p Poly 170 for i := 0; i < b.N; i++ { 171 p.MulHat(&p, &p) 172 } 173 } 174 175 func BenchmarkAdd(b *testing.B) { 176 var p Poly 177 for i := 0; i < b.N; i++ { 178 p.Add(&p, &p) 179 } 180 } 181 182 func BenchmarkSub(b *testing.B) { 183 var p Poly 184 for i := 0; i < b.N; i++ { 185 p.Sub(&p, &p) 186 } 187 } 188 189 func BenchmarkReduceLe2Q(b *testing.B) { 190 var p Poly 191 for i := 0; i < b.N; i++ { 192 p.ReduceLe2Q() 193 } 194 } 195 196 func BenchmarkNormalize(b *testing.B) { 197 var p Poly 198 for i := 0; i < b.N; i++ { 199 p.Normalize() 200 } 201 } 202 203 func BenchmarkNormalizeAssumingLe2Q(b *testing.B) { 204 var p Poly 205 for i := 0; i < b.N; i++ { 206 p.NormalizeAssumingLe2Q() 207 } 208 } 209 210 func BenchmarkExceeds(b *testing.B) { 211 var p Poly 212 for i := 0; i < b.N; i++ { 213 p.Exceeds(uint32(10)) 214 } 215 } 216 217 func BenchmarkMulBy2toD(b *testing.B) { 218 var p, q Poly 219 for i := 0; i < b.N; i++ { 220 p.MulBy2toD(&q) 221 } 222 }