Skip to content

Commit

Permalink
Compute RecoveryId() in ecdsa.Signature.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmpfs committed Nov 23, 2022
1 parent 1c20cbb commit d44e983
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
17 changes: 17 additions & 0 deletions pkg/ecdsa/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,20 @@ func (sig Signature) Verify(X curve.Point, hash []byte) bool {
R2 = sInv.Act(R2)
return R2.Equal(sig.R)
}

func (sig Signature) RecoveryId() byte {
r := sig.R.(*curve.Secp256k1Point)
s := sig.S.(*curve.Secp256k1Scalar)

var recid byte = 0

if !r.HasEvenY() {
recid = 1;
}

if s.Value().IsOverHalfOrder() {
recid ^= 1
}

return recid
}
4 changes: 4 additions & 0 deletions pkg/math/curve/secp256k1.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ func (*Secp256k1Scalar) Curve() Curve {
return Secp256k1{}
}

func (s *Secp256k1Scalar) Value() *secp256k1.ModNScalar {
return &s.value
}

func (s *Secp256k1Scalar) MarshalBinary() ([]byte, error) {
data := s.value.Bytes()
return data[:], nil
Expand Down

0 comments on commit d44e983

Please sign in to comment.