github.com/cloudflare/circl@v1.5.0/sign/eddilithium3/eddilithium_test.go (about) 1 package eddilithium3_test 2 3 import ( 4 "encoding/binary" 5 "testing" 6 7 "github.com/cloudflare/circl/sign/eddilithium3" 8 ) 9 10 func BenchmarkVerify(b *testing.B) { 11 // Note that Dilithium precomputes quite a bit during Unpacking/Keygen 12 // instead of at the moment of verification (as compared to the reference 13 // implementation. A fair comparison thus should sum verification 14 // times with unpacking times.) 15 var seed [57]byte 16 var msg [8]byte 17 var sig [eddilithium3.SignatureSize]byte 18 pk, sk := eddilithium3.NewKeyFromSeed(&seed) 19 eddilithium3.SignTo(sk, msg[:], sig[:]) 20 b.ResetTimer() 21 for i := 0; i < b.N; i++ { 22 // We should generate a new signature for every verify attempt, 23 // as this influences the time a little bit. This difference, however, 24 // is small and generating a new signature in between creates a lot 25 // pressure on the allocator which makes an accurate measurement hard. 26 eddilithium3.Verify(pk, msg[:], sig[:]) 27 } 28 } 29 30 func BenchmarkSign(b *testing.B) { 31 // Note that Dilithium precomputes quite a bit during Unpacking/Keygen 32 // instead of at the moment of signing (as compared to the reference 33 // implementation. A fair comparison thus should sum sign times with 34 // unpacking times.) 35 var seed [57]byte 36 var msg [8]byte 37 var sig [eddilithium3.SignatureSize]byte 38 _, sk := eddilithium3.NewKeyFromSeed(&seed) 39 b.ResetTimer() 40 for i := 0; i < b.N; i++ { 41 binary.LittleEndian.PutUint64(msg[:], uint64(i)) 42 eddilithium3.SignTo(sk, msg[:], sig[:]) 43 } 44 } 45 46 func BenchmarkGenerateKey(b *testing.B) { 47 var seed [57]byte 48 for i := 0; i < b.N; i++ { 49 binary.LittleEndian.PutUint64(seed[:], uint64(i)) 50 eddilithium3.NewKeyFromSeed(&seed) 51 } 52 } 53 54 func BenchmarkPublicFromPrivate(b *testing.B) { 55 var seed [57]byte 56 for i := 0; i < b.N; i++ { 57 b.StopTimer() 58 binary.LittleEndian.PutUint64(seed[:], uint64(i)) 59 _, sk := eddilithium3.NewKeyFromSeed(&seed) 60 b.StartTimer() 61 sk.Public() 62 } 63 } 64 65 func TestSignThenVerifyAndPkSkPacking(t *testing.T) { 66 var seed [eddilithium3.SeedSize]byte 67 var sig [eddilithium3.SignatureSize]byte 68 var msg [8]byte 69 var pkb1, pkb2 [eddilithium3.PublicKeySize]byte 70 var skb1, skb2 [eddilithium3.PrivateKeySize]byte 71 var pk2 eddilithium3.PublicKey 72 var sk2 eddilithium3.PrivateKey 73 for i := uint64(0); i < 100; i++ { 74 binary.LittleEndian.PutUint64(seed[:], i) 75 pk, sk := eddilithium3.NewKeyFromSeed(&seed) 76 for j := uint64(0); j < 10; j++ { 77 binary.LittleEndian.PutUint64(msg[:], j) 78 eddilithium3.SignTo(sk, msg[:], sig[:]) 79 if !eddilithium3.Verify(pk, msg[:], sig[:]) { 80 t.Fatal() 81 } 82 } 83 pk.Pack(&pkb1) 84 pk2.Unpack(&pkb1) 85 pk2.Pack(&pkb2) 86 if pkb1 != pkb2 { 87 t.Fatal() 88 } 89 sk.Pack(&skb1) 90 sk2.Unpack(&skb1) 91 sk2.Pack(&skb2) 92 if skb1 != skb2 { 93 t.Fatal() 94 } 95 } 96 } 97 98 func TestPublicFromPrivate(t *testing.T) { 99 var seed [eddilithium3.SeedSize]byte 100 for i := uint64(0); i < 100; i++ { 101 binary.LittleEndian.PutUint64(seed[:], i) 102 pk, sk := eddilithium3.NewKeyFromSeed(&seed) 103 pk2 := sk.Public().(*eddilithium3.PublicKey) 104 var pkb1, pkb2 [eddilithium3.PublicKeySize]byte 105 pk.Pack(&pkb1) 106 pk2.Pack(&pkb2) 107 if pkb1 != pkb2 { 108 t.Fatal() 109 } 110 } 111 }