github.com/cloudflare/circl@v1.5.0/sign/dilithium/mode3/internal/dilithium_test.go (about) 1 package internal 2 3 import ( 4 "encoding/binary" 5 "io" 6 "testing" 7 8 common "github.com/cloudflare/circl/sign/internal/dilithium" 9 ) 10 11 // Checks whether p is normalized. Only used in tests. 12 func PolyNormalized(p *common.Poly) bool { 13 p2 := *p 14 p2.Normalize() 15 return p2 == *p 16 } 17 18 func BenchmarkSkUnpack(b *testing.B) { 19 var buf [PrivateKeySize]byte 20 var sk PrivateKey 21 for i := 0; i < b.N; i++ { 22 sk.Unpack(&buf) 23 } 24 } 25 26 func BenchmarkPkUnpack(b *testing.B) { 27 var buf [PublicKeySize]byte 28 var pk PublicKey 29 for i := 0; i < b.N; i++ { 30 pk.Unpack(&buf) 31 } 32 } 33 34 func BenchmarkVerify(b *testing.B) { 35 // Note that the expansion of the matrix A is done at Unpacking/Keygen 36 // instead of at the moment of verification (as in the reference 37 // implementation.) 38 var ( 39 seed [32]byte 40 msg [8]byte 41 sig [SignatureSize]byte 42 rnd [32]byte 43 ) 44 pk, sk := NewKeyFromSeed(&seed) 45 SignTo(sk, func(w io.Writer) { _, _ = w.Write(msg[:]) }, rnd, sig[:]) 46 b.ResetTimer() 47 for i := 0; i < b.N; i++ { 48 // We should generate a new signature for every verify attempt, 49 // as this influences the time a little bit. This difference, however, 50 // is small and generating a new signature in between creates a lot 51 // pressure on the allocator which makes an accurate measurement hard. 52 Verify(pk, func(w io.Writer) { _, _ = w.Write(msg[:]) }, sig[:]) 53 } 54 } 55 56 func BenchmarkSign(b *testing.B) { 57 // Note that the expansion of the matrix A is done at Unpacking/Keygen 58 // instead of at the moment of signing (as in the reference implementation.) 59 var ( 60 seed [32]byte 61 msg [8]byte 62 sig [SignatureSize]byte 63 rnd [32]byte 64 ) 65 _, sk := NewKeyFromSeed(&seed) 66 b.ResetTimer() 67 for i := 0; i < b.N; i++ { 68 binary.LittleEndian.PutUint64(msg[:], uint64(i)) 69 SignTo(sk, func(w io.Writer) { _, _ = w.Write(msg[:]) }, rnd, sig[:]) 70 } 71 } 72 73 func BenchmarkGenerateKey(b *testing.B) { 74 var seed [32]byte 75 for i := 0; i < b.N; i++ { 76 binary.LittleEndian.PutUint64(seed[:], uint64(i)) 77 NewKeyFromSeed(&seed) 78 } 79 } 80 81 func BenchmarkPublicFromPrivate(b *testing.B) { 82 var seed [32]byte 83 for i := 0; i < b.N; i++ { 84 b.StopTimer() 85 binary.LittleEndian.PutUint64(seed[:], uint64(i)) 86 _, sk := NewKeyFromSeed(&seed) 87 b.StartTimer() 88 sk.Public() 89 } 90 } 91 92 func TestSignThenVerifyAndPkSkPacking(t *testing.T) { 93 var ( 94 seed [common.SeedSize]byte 95 sig [SignatureSize]byte 96 msg [8]byte 97 pkb [PublicKeySize]byte 98 skb [PrivateKeySize]byte 99 pk2 PublicKey 100 sk2 PrivateKey 101 rnd [32]byte 102 ) 103 for i := uint64(0); i < 100; i++ { 104 binary.LittleEndian.PutUint64(seed[:], i) 105 pk, sk := NewKeyFromSeed(&seed) 106 if !sk.Equal(sk) { 107 t.Fatal() 108 } 109 for j := uint64(0); j < 10; j++ { 110 binary.LittleEndian.PutUint64(msg[:], j) 111 SignTo(sk, func(w io.Writer) { _, _ = w.Write(msg[:]) }, rnd, sig[:]) 112 if !Verify(pk, func(w io.Writer) { _, _ = w.Write(msg[:]) }, sig[:]) { 113 t.Fatal() 114 } 115 } 116 pk.Pack(&pkb) 117 pk2.Unpack(&pkb) 118 if !pk.Equal(&pk2) { 119 t.Fatal() 120 } 121 sk.Pack(&skb) 122 sk2.Unpack(&skb) 123 if !sk.Equal(&sk2) { 124 t.Fatal() 125 } 126 } 127 } 128 129 func TestPublicFromPrivate(t *testing.T) { 130 var seed [common.SeedSize]byte 131 for i := uint64(0); i < 100; i++ { 132 binary.LittleEndian.PutUint64(seed[:], i) 133 pk, sk := NewKeyFromSeed(&seed) 134 pk2 := sk.Public() 135 if !pk.Equal(pk2) { 136 t.Fatal() 137 } 138 } 139 } 140 141 func TestGamma1Size(t *testing.T) { 142 var expected int 143 switch Gamma1Bits { 144 case 17: 145 expected = 576 146 case 19: 147 expected = 640 148 } 149 if expected != PolyLeGamma1Size { 150 t.Fatal() 151 } 152 }