github.com/consensys/gnark-crypto@v0.14.0/internal/generator/edwards/eddsa/template/eddsa.test.go.tmpl (about) 1 import ( 2 "crypto/sha256" 3 "math/big" 4 "math/rand" 5 "testing" 6 7 crand "crypto/rand" 8 9 "fmt" 10 11 "github.com/consensys/gnark-crypto/hash" 12 "github.com/consensys/gnark-crypto/ecc/{{.Name}}/twistededwards" 13 "github.com/consensys/gnark-crypto/ecc/{{.Name}}/fr" 14 ) 15 16 17 func Example() { 18 // instantiate hash function 19 hFunc := hash.MIMC_{{ .EnumID }}.New() 20 21 // create a eddsa key pair 22 privateKey, _ := GenerateKey(crand.Reader) 23 publicKey := privateKey.PublicKey 24 25 // generate a message (the size must be a multiple of the size of Fr) 26 var _msg fr.Element 27 _msg.SetRandom() 28 msg := _msg.Marshal() 29 30 // sign the message 31 signature, _ := privateKey.Sign(msg, hFunc) 32 33 // verifies signature 34 isValid, _ := publicKey.Verify(signature, msg, hFunc) 35 if !isValid { 36 fmt.Println("1. invalid signature") 37 } else { 38 fmt.Println("1. valid signature") 39 } 40 41 // Output: 1. valid signature 42 } 43 44 func TestNonMalleability(t *testing.T) { 45 46 // buffer too big 47 t.Run("buffer_overflow", func(t *testing.T) { 48 bsig := make([]byte, 2*sizeFr+1) 49 var sig Signature 50 _, err := sig.SetBytes(bsig) 51 if err != errWrongSize { 52 t.Fatal("should raise wrong size error") 53 } 54 }) 55 56 // R overflows p_mod 57 t.Run("R_overflow", func(t *testing.T) { 58 bsig := make([]byte, 2*sizeFr) 59 frMod := fr.Modulus() 60 r := big.NewInt(1) 61 r.Add(frMod, r) 62 buf := r.Bytes() 63 for i := 0; i < sizeFr; i++ { 64 bsig[sizeFr-1-i] = buf[i] 65 } 66 67 var sig Signature 68 _, err := sig.SetBytes(bsig) 69 if err != errRBiggerThanPMod { 70 t.Fatal("should raise error r >= p_mod") 71 } 72 }) 73 74 // S overflows r_mod 75 t.Run("S_overflow", func(t *testing.T) { 76 bsig := make([]byte, 2*sizeFr) 77 o := big.NewInt(1) 78 cp := twistededwards.GetEdwardsCurve() 79 o.Add(&cp.Order, o) 80 buf := o.Bytes() 81 copy(bsig[sizeFr:], buf[:]) 82 big.NewInt(1).FillBytes(bsig[:sizeFr]) 83 84 var sig Signature 85 _, err := sig.SetBytes(bsig) 86 if err != errSBiggerThanRMod { 87 t.Fatal("should raise error s >= r_mod") 88 } 89 }) 90 91 } 92 93 func TestNoZeros(t *testing.T) { 94 t.Run("R.Y=0", func(t *testing.T) { 95 // R points are 0 96 var sig Signature 97 sig.R.X.SetInt64(1) 98 sig.R.Y.SetInt64(0) 99 s := big.NewInt(1) 100 s.FillBytes(sig.S[:]) 101 bts := sig.Bytes() 102 var newSig Signature 103 _, err := newSig.SetBytes(bts) 104 if err != errZero { 105 t.Fatal("expected error for zero R.Y") 106 } 107 }) 108 t.Run("S=0", func(t *testing.T) { 109 // S is 0 110 var R twistededwards.PointAffine 111 cp := twistededwards.GetEdwardsCurve() 112 R.ScalarMultiplication(&cp.Base, big.NewInt(1)) 113 var sig Signature 114 sig.R.Set(&R) 115 bts := sig.Bytes() 116 var newSig Signature 117 _, err := newSig.SetBytes(bts) 118 if err != errZero { 119 t.Fatal("expected error for zero S") 120 } 121 }) 122 } 123 124 func TestSerialization(t *testing.T) { 125 126 src := rand.NewSource(0) 127 r := rand.New(src) //#nosec G404 weak rng is fine here 128 129 privKey1, err := GenerateKey(r) 130 if err != nil { 131 t.Fatal(err) 132 } 133 pubKey1 := privKey1.PublicKey 134 135 privKey2, err := GenerateKey(r) 136 if err != nil { 137 t.Fatal(err) 138 } 139 pubKey2 := privKey2.PublicKey 140 141 pubKeyBin1 := pubKey1.Bytes() 142 pubKey2.SetBytes(pubKeyBin1) 143 pubKeyBin2 := pubKey2.Bytes() 144 if len(pubKeyBin1) != len(pubKeyBin2) { 145 t.Fatal("Inconsistent size") 146 } 147 for i := 0; i < len(pubKeyBin1); i++ { 148 if pubKeyBin1[i] != pubKeyBin2[i] { 149 t.Fatal("Error serialize(deserialize(.))") 150 } 151 } 152 153 privKeyBin1 := privKey1.Bytes() 154 privKey2.SetBytes(privKeyBin1) 155 privKeyBin2 := privKey2.Bytes() 156 if len(privKeyBin1) != len(privKeyBin2) { 157 t.Fatal("Inconsistent size") 158 } 159 for i := 0; i < len(privKeyBin1); i++ { 160 if privKeyBin1[i] != privKeyBin2[i] { 161 t.Fatal("Error serialize(deserialize(.))") 162 } 163 } 164 } 165 166 func TestEddsaMIMC(t *testing.T) { 167 168 src := rand.NewSource(0) 169 r := rand.New(src) //#nosec G404 weak rng is fine here 170 171 // create eddsa obj and sign a message 172 privKey, err := GenerateKey(r) 173 if err != nil { 174 t.Fatal(nil) 175 } 176 pubKey := privKey.PublicKey 177 hFunc := hash.MIMC_{{ .EnumID }}.New() 178 179 var frMsg fr.Element 180 frMsg.SetString("44717650746155748460101257525078853138837311576962212923649547644148297035978") 181 msgBin := frMsg.Bytes() 182 signature, err := privKey.Sign(msgBin[:], hFunc) 183 if err != nil { 184 t.Fatal(err) 185 } 186 187 // verifies correct msg 188 res, err := pubKey.Verify(signature, msgBin[:], hFunc) 189 if err != nil { 190 t.Fatal(err) 191 } 192 if !res { 193 t.Fatal("Verify correct signature should return true") 194 } 195 196 // verifies wrong msg 197 frMsg.SetString("44717650746155748460101257525078853138837311576962212923649547644148297035979") 198 msgBin = frMsg.Bytes() 199 res, err = pubKey.Verify(signature, msgBin[:], hFunc) 200 if err != nil { 201 t.Fatal(err) 202 } 203 if res { 204 t.Fatal("Verify wrong signature should be false") 205 } 206 207 } 208 209 func TestEddsaSHA256(t *testing.T) { 210 211 src := rand.NewSource(0) 212 r := rand.New(src) //#nosec G404 weak rng is fine here 213 214 hFunc := sha256.New() 215 216 // create eddsa obj and sign a message 217 // create eddsa obj and sign a message 218 219 privKey, err := GenerateKey(r) 220 pubKey := privKey.PublicKey 221 if err != nil { 222 t.Fatal(err) 223 } 224 225 signature, err := privKey.Sign([]byte("message"), hFunc) 226 if err != nil { 227 t.Fatal(err) 228 } 229 230 // verifies correct msg 231 res, err := pubKey.Verify(signature, []byte("message"), hFunc) 232 if err != nil { 233 t.Fatal(err) 234 } 235 if !res { 236 t.Fatal("Verify correct signature should return true") 237 } 238 239 // verifies wrong msg 240 res, err = pubKey.Verify(signature, []byte("wrong_message"), hFunc) 241 if err != nil { 242 t.Fatal(err) 243 } 244 if res { 245 t.Fatal("Verify wrong signature should be false") 246 } 247 248 } 249 250 // benchmarks 251 252 func BenchmarkVerify(b *testing.B) { 253 254 src := rand.NewSource(0) 255 r := rand.New(src) //#nosec G404 weak rng is fine here 256 257 hFunc := hash.MIMC_{{ .EnumID }}.New() 258 259 // create eddsa obj and sign a message 260 privKey, err := GenerateKey(r) 261 pubKey := privKey.PublicKey 262 if err != nil { 263 b.Fatal(err) 264 } 265 var frMsg fr.Element 266 frMsg.SetString("44717650746155748460101257525078853138837311576962212923649547644148297035978") 267 msgBin := frMsg.Bytes() 268 signature, _ := privKey.Sign(msgBin[:], hFunc) 269 270 b.ResetTimer() 271 for i := 0; i < b.N; i++ { 272 pubKey.Verify(signature, msgBin[:], hFunc) 273 } 274 }