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