github.com/cloudflare/circl@v1.5.0/sign/internal/dilithium/ntt_test.go (about) 1 package dilithium 2 3 import "testing" 4 5 func (p *Poly) RandLe2Q() { 6 max := 2 * uint32(Q) 7 r := randSliceUint32WithMax(N, max) 8 copy(p[:], r) 9 } 10 11 func TestNTTAgainstGeneric(t *testing.T) { 12 for k := 0; k < 1000; k++ { 13 var p Poly 14 p.RandLe2Q() 15 q1 := p 16 q2 := p 17 q1.NTT() 18 q2.nttGeneric() 19 if q1 != q2 { 20 t.Fatalf("NTT(%v) = %v != %v", p, q1, q2) 21 } 22 } 23 } 24 25 func TestNTT(t *testing.T) { 26 for k := 0; k < 1000; k++ { 27 var p, q Poly 28 p.RandLe2Q() 29 q = p 30 q.Normalize() 31 p.NTT() 32 for i := uint(0); i < N; i++ { 33 if p[i] > 18*Q { 34 t.Fatalf("NTT(%v)[%d] = %d > 18*Q", q, i, p[i]) 35 } 36 } 37 p.ReduceLe2Q() 38 p.InvNTT() 39 for i := uint(0); i < N; i++ { 40 if p[i] > 2*Q { 41 t.Fatalf("InvNTT(%v)[%d] > 2*Q", q, i) 42 } 43 } 44 p.Normalize() 45 for i := uint(0); i < N; i++ { 46 if p[i] != uint32((uint64(q[i])*uint64(1<<32))%Q) { 47 t.Fatalf("%v != %v", p, q) 48 } 49 } 50 } 51 } 52 53 func BenchmarkNTTGeneric(b *testing.B) { 54 var p Poly 55 for i := 0; i < b.N; i++ { 56 p.nttGeneric() 57 } 58 } 59 60 func BenchmarkInvNTTGeneric(b *testing.B) { 61 var p Poly 62 for i := 0; i < b.N; i++ { 63 p.invNttGeneric() 64 } 65 } 66 67 func BenchmarkNTT(b *testing.B) { 68 var p Poly 69 for i := 0; i < b.N; i++ { 70 p.NTT() 71 } 72 } 73 74 func BenchmarkInvNTT(b *testing.B) { 75 var p Poly 76 for i := 0; i < b.N; i++ { 77 p.InvNTT() 78 } 79 }