Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ECDSA error types #497

Merged
merged 4 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions ecc/bn254/ecdsa/ecdsa.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ecc/bn254/ecdsa/marshal.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions ecc/secp256k1/ecdsa/ecdsa.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ecc/secp256k1/ecdsa/marshal.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions ecc/stark-curve/ecdsa/ecdsa.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ecc/stark-curve/ecdsa/marshal.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 13 additions & 3 deletions internal/generator/ecdsa/template/ecdsa.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ const (
sizeSignature = 2 * sizeFr
)

{{- if or (eq .Name "secp256k1") (eq .Name "bn254") (eq .Name "stark-curve") }}
var (
// ErrNoSqrtR is returned when x^3+ax+b is not a square in the field. This
// is used for public key recovery and allows to detect if the signature is
// valid or not.
ErrNoSqrtR = errors.New("x^3+ax+b is not a square in the field")
)
{{- end }}

var order = fr.Modulus()

// PublicKey represents an ECDSA public key
Expand Down Expand Up @@ -103,10 +112,10 @@ func HashToInt(hash []byte) *big.Int {
}

{{- if or (eq .Name "secp256k1") (eq .Name "bn254") (eq .Name "stark-curve") }}
// RecoverP recovers the value P (prover commitment) when creating a signature.
// recoverP recovers the value P (prover commitment) when creating a signature.
// It uses the recovery information v and part of the decomposed signature r. It
// is used internally for recovering the public key.
func RecoverP(v uint, r *big.Int) (*{{ .CurvePackage }}.G1Affine, error) {
func recoverP(v uint, r *big.Int) (*{{ .CurvePackage }}.G1Affine, error) {
if r.Cmp(fr.Modulus()) >= 0 {
return nil, errors.New("r is larger than modulus")
}
Expand Down Expand Up @@ -135,7 +144,8 @@ func RecoverP(v uint, r *big.Int) (*{{ .CurvePackage }}.G1Affine, error) {
y.Mod(y, fp.Modulus())
// y = sqrt(y^2)
if y.ModSqrt(y, fp.Modulus()) == nil {
return nil, errors.New("no square root")
// there is no square root, return error constant
return nil, ErrNoSqrtR
}
// check that y has same oddity as defined by v
if y.Bit(0) != yChoice {
Expand Down
2 changes: 1 addition & 1 deletion internal/generator/ecdsa/template/marshal.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (pk *PublicKey) RecoverFrom(msg []byte, v uint, r, s *big.Int) error {
if s.Cmp(big.NewInt(0)) <= 0 {
return errors.New("s is negative")
}
P, err := RecoverP(v, r)
P, err := recoverP(v, r)
if err != nil {
return err
}
Expand Down
Loading