github.com/consensys/gnark-crypto@v0.14.0/ecc/bw6-633/ecdsa/ecdsa_test.go (about) 1 // Copyright 2020 Consensys Software Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Code generated by consensys/gnark-crypto DO NOT EDIT 16 17 package ecdsa 18 19 import ( 20 "crypto/rand" 21 "crypto/sha256" 22 "github.com/consensys/gnark-crypto/ecc/bw6-633/fr" 23 "math/big" 24 "testing" 25 26 "github.com/leanovate/gopter" 27 "github.com/leanovate/gopter/prop" 28 ) 29 30 func TestECDSA(t *testing.T) { 31 32 t.Parallel() 33 parameters := gopter.DefaultTestParameters() 34 properties := gopter.NewProperties(parameters) 35 36 properties.Property("[BW6-633] test the signing and verification", prop.ForAll( 37 func() bool { 38 39 privKey, _ := GenerateKey(rand.Reader) 40 publicKey := privKey.PublicKey 41 42 msg := []byte("testing ECDSA") 43 hFunc := sha256.New() 44 sig, _ := privKey.Sign(msg, hFunc) 45 flag, _ := publicKey.Verify(sig, msg, hFunc) 46 47 return flag 48 }, 49 )) 50 51 properties.Property("[BW6-633] test the signing and verification (pre-hashed)", prop.ForAll( 52 func() bool { 53 54 privKey, _ := GenerateKey(rand.Reader) 55 publicKey := privKey.PublicKey 56 57 msg := []byte("testing ECDSA") 58 sig, _ := privKey.Sign(msg, nil) 59 flag, _ := publicKey.Verify(sig, msg, nil) 60 61 return flag 62 }, 63 )) 64 65 properties.TestingRun(t, gopter.ConsoleReporter(false)) 66 } 67 68 func TestNonMalleability(t *testing.T) { 69 70 // buffer too big 71 t.Run("buffer_overflow", func(t *testing.T) { 72 bsig := make([]byte, 2*sizeFr+1) 73 var sig Signature 74 _, err := sig.SetBytes(bsig) 75 if err != errWrongSize { 76 t.Fatal("should raise wrong size error") 77 } 78 }) 79 80 // R overflows p_mod 81 t.Run("R_overflow", func(t *testing.T) { 82 bsig := make([]byte, 2*sizeFr) 83 r := big.NewInt(1) 84 frMod := fr.Modulus() 85 r.Add(r, frMod) 86 buf := r.Bytes() 87 copy(bsig, buf[:]) 88 89 var sig Signature 90 _, err := sig.SetBytes(bsig) 91 if err != errRBiggerThanRMod { 92 t.Fatal("should raise error r >= r_mod") 93 } 94 }) 95 96 // S overflows p_mod 97 t.Run("S_overflow", func(t *testing.T) { 98 bsig := make([]byte, 2*sizeFr) 99 r := big.NewInt(1) 100 frMod := fr.Modulus() 101 r.Add(r, frMod) 102 buf := r.Bytes() 103 copy(bsig[sizeFr:], buf[:]) 104 big.NewInt(1).FillBytes(bsig[:sizeFr]) 105 106 var sig Signature 107 _, err := sig.SetBytes(bsig) 108 if err != errSBiggerThanRMod { 109 t.Fatal("should raise error s >= r_mod") 110 } 111 }) 112 113 } 114 115 func TestNoZeros(t *testing.T) { 116 t.Run("R=0", func(t *testing.T) { 117 // R is 0 118 var sig Signature 119 big.NewInt(0).FillBytes(sig.R[:]) 120 big.NewInt(1).FillBytes(sig.S[:]) 121 bts := sig.Bytes() 122 var newSig Signature 123 _, err := newSig.SetBytes(bts) 124 if err != errZero { 125 t.Fatal("expected error for zero R") 126 } 127 }) 128 t.Run("S=0", func(t *testing.T) { 129 // S is 0 130 var sig Signature 131 big.NewInt(1).FillBytes(sig.R[:]) 132 big.NewInt(0).FillBytes(sig.S[:]) 133 bts := sig.Bytes() 134 var newSig Signature 135 _, err := newSig.SetBytes(bts) 136 if err != errZero { 137 t.Fatal("expected error for zero S") 138 } 139 }) 140 } 141 142 // ------------------------------------------------------------ 143 // benches 144 145 func BenchmarkSignECDSA(b *testing.B) { 146 147 privKey, _ := GenerateKey(rand.Reader) 148 149 msg := []byte("benchmarking ECDSA sign()") 150 b.ResetTimer() 151 for i := 0; i < b.N; i++ { 152 privKey.Sign(msg, nil) 153 } 154 } 155 156 func BenchmarkVerifyECDSA(b *testing.B) { 157 158 privKey, _ := GenerateKey(rand.Reader) 159 msg := []byte("benchmarking ECDSA sign()") 160 sig, _ := privKey.Sign(msg, nil) 161 162 b.ResetTimer() 163 for i := 0; i < b.N; i++ { 164 privKey.PublicKey.Verify(sig, msg, nil) 165 } 166 }