-
Notifications
You must be signed in to change notification settings - Fork 366
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement NIST P-256 and P-384 curves (#697)
* refactor: use goldilocks modulus from gnark-crypto * refactor: use secp256k1 moduli from gnark-crypto * refactor: move emulation parameters to separate package * chore: godoc compatible emulation parms docs * docs: add package documentation for emulation params * refactor: use type embedding for common params * docs: expand FieldParams doc * feat: add P384 emulation params * feat: add P256 field emulation params * refactor: rename to emparams * feat: add P256 and P384 curves * chore: refer to moduli in curve parameter getter * fix: wrong precomputation * feat: merge contribution commit cbf25b5 Author: SherLzp <lzp.sher@gmail.com> Date: Thu Mar 30 13:57:51 2023 +0800 secp256r1: ecdsa * refactor: use type alias
- Loading branch information
Showing
4 changed files
with
250 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package ecdsa | ||
|
||
import ( | ||
cryptoecdsa "crypto/ecdsa" | ||
"crypto/elliptic" | ||
"crypto/rand" | ||
"crypto/sha256" | ||
"crypto/sha512" | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/consensys/gnark-crypto/ecc" | ||
"github.com/consensys/gnark/std/math/emulated" | ||
"github.com/consensys/gnark/test" | ||
"golang.org/x/crypto/cryptobyte" | ||
"golang.org/x/crypto/cryptobyte/asn1" | ||
) | ||
|
||
func TestEcdsaP256PreHashed(t *testing.T) { | ||
|
||
// generate parameters | ||
privKey, _ := cryptoecdsa.GenerateKey(elliptic.P256(), rand.Reader) | ||
publicKey := privKey.PublicKey | ||
|
||
// sign | ||
msg := []byte("testing ECDSA (pre-hashed)") | ||
msgHash := sha256.Sum256(msg) | ||
sigBin, _ := privKey.Sign(rand.Reader, msgHash[:], nil) | ||
|
||
// check that the signature is correct | ||
var ( | ||
r, s = &big.Int{}, &big.Int{} | ||
inner cryptobyte.String | ||
) | ||
input := cryptobyte.String(sigBin) | ||
if !input.ReadASN1(&inner, asn1.SEQUENCE) || | ||
!input.Empty() || | ||
!inner.ReadASN1Integer(r) || | ||
!inner.ReadASN1Integer(s) || | ||
!inner.Empty() { | ||
panic("invalid sig") | ||
} | ||
flag := cryptoecdsa.Verify(&publicKey, msgHash[:], r, s) | ||
if !flag { | ||
t.Errorf("can't verify signature") | ||
} | ||
|
||
circuit := EcdsaCircuit[emulated.P256Fp, emulated.P256Fr]{} | ||
witness := EcdsaCircuit[emulated.P256Fp, emulated.P256Fr]{ | ||
Sig: Signature[emulated.P256Fr]{ | ||
R: emulated.ValueOf[emulated.P256Fr](r), | ||
S: emulated.ValueOf[emulated.P256Fr](s), | ||
}, | ||
Msg: emulated.ValueOf[emulated.P256Fr](msgHash[:]), | ||
Pub: PublicKey[emulated.P256Fp, emulated.P256Fr]{ | ||
X: emulated.ValueOf[emulated.P256Fp](privKey.PublicKey.X), | ||
Y: emulated.ValueOf[emulated.P256Fp](privKey.PublicKey.Y), | ||
}, | ||
} | ||
assert := test.NewAssert(t) | ||
err := test.IsSolved(&circuit, &witness, ecc.BN254.ScalarField()) | ||
assert.NoError(err) | ||
|
||
} | ||
|
||
func TestEcdsaP384PreHashed(t *testing.T) { | ||
|
||
// generate parameters | ||
privKey, _ := cryptoecdsa.GenerateKey(elliptic.P384(), rand.Reader) | ||
publicKey := privKey.PublicKey | ||
|
||
// sign | ||
msg := []byte("testing ECDSA (pre-hashed)") | ||
msgHash := sha512.Sum384(msg) | ||
sigBin, _ := privKey.Sign(rand.Reader, msgHash[:], nil) | ||
|
||
// check that the signature is correct | ||
var ( | ||
r, s = &big.Int{}, &big.Int{} | ||
inner cryptobyte.String | ||
) | ||
input := cryptobyte.String(sigBin) | ||
if !input.ReadASN1(&inner, asn1.SEQUENCE) || | ||
!input.Empty() || | ||
!inner.ReadASN1Integer(r) || | ||
!inner.ReadASN1Integer(s) || | ||
!inner.Empty() { | ||
panic("invalid sig") | ||
} | ||
flag := cryptoecdsa.Verify(&publicKey, msgHash[:], r, s) | ||
if !flag { | ||
t.Errorf("can't verify signature") | ||
} | ||
|
||
circuit := EcdsaCircuit[emulated.P384Fp, emulated.P384Fr]{} | ||
witness := EcdsaCircuit[emulated.P384Fp, emulated.P384Fr]{ | ||
Sig: Signature[emulated.P384Fr]{ | ||
R: emulated.ValueOf[emulated.P384Fr](r), | ||
S: emulated.ValueOf[emulated.P384Fr](s), | ||
}, | ||
Msg: emulated.ValueOf[emulated.P384Fr](msgHash[:]), | ||
Pub: PublicKey[emulated.P384Fp, emulated.P384Fr]{ | ||
X: emulated.ValueOf[emulated.P384Fp](privKey.PublicKey.X), | ||
Y: emulated.ValueOf[emulated.P384Fp](privKey.PublicKey.Y), | ||
}, | ||
} | ||
assert := test.NewAssert(t) | ||
err := test.IsSolved(&circuit, &witness, ecc.BN254.ScalarField()) | ||
assert.NoError(err) | ||
|
||
} |