-
Notifications
You must be signed in to change notification settings - Fork 386
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
chore(deps): update btcec to the latest version #1329
Merged
gfanton
merged 10 commits into
gnolang:master
from
ajnavarro:chore/update-btcd-latest-version
Nov 23, 2023
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3595ec5
chore: Update btcec to the latest version.
ajnavarro ff0b049
fix mispelled words
ajnavarro d0b56c7
linter
ajnavarro 6f392b0
go mod tidy
ajnavarro 6b9ef98
Refactor gomod
ajnavarro 673bcdb
Update btcutil too
ajnavarro 1ae6215
Add golden test.
ajnavarro 22d96e2
gofumpt
ajnavarro dc676da
go mod tidy other go.mod files
ajnavarro 12492f2
Merge branch 'master' into chore/update-btcd-latest-version
ajnavarro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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 |
---|---|---|
|
@@ -3,29 +3,25 @@ | |
package secp256k1 | ||
|
||
import ( | ||
"math/big" | ||
|
||
secp256k1 "github.com/btcsuite/btcd/btcec" | ||
"github.com/btcsuite/btcd/btcec/v2" | ||
"github.com/btcsuite/btcd/btcec/v2/ecdsa" | ||
"github.com/decred/dcrd/dcrec/secp256k1/v4" | ||
|
||
"github.com/gnolang/gno/tm2/pkg/crypto" | ||
) | ||
|
||
// used to reject malleable signatures | ||
// see: | ||
// - https://github.com/ethereum/go-ethereum/blob/f9401ae011ddf7f8d2d95020b7446c17f8d98dc1/crypto/signature_nocgo.go#L90-L93 | ||
// - https://github.com/ethereum/go-ethereum/blob/f9401ae011ddf7f8d2d95020b7446c17f8d98dc1/crypto/crypto.go#L39 | ||
var secp256k1halfN = new(big.Int).Rsh(secp256k1.S256().N, 1) | ||
|
||
// Sign creates an ECDSA signature on curve Secp256k1, using SHA256 on the msg. | ||
// The returned signature will be of the form R || S (in lower-S form). | ||
func (privKey PrivKeySecp256k1) Sign(msg []byte) ([]byte, error) { | ||
priv, _ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKey[:]) | ||
sig, err := priv.Sign(crypto.Sha256(msg)) | ||
priv, _ := btcec.PrivKeyFromBytes(privKey[:]) | ||
|
||
sig, err := ecdsa.SignCompact(priv, crypto.Sha256(msg), false) // ref uncompressed pubkey | ||
if err != nil { | ||
return nil, err | ||
} | ||
sigBytes := serializeSig(sig) | ||
return sigBytes, nil | ||
|
||
// remove compact sig recovery code byte at the beginning | ||
return sig[1:], nil | ||
} | ||
|
||
// VerifyBytes verifies a signature of the form R || S. | ||
|
@@ -34,37 +30,36 @@ | |
if len(sigStr) != 64 { | ||
return false | ||
} | ||
pub, err := secp256k1.ParsePubKey(pubKey[:], secp256k1.S256()) | ||
|
||
pub, err := secp256k1.ParsePubKey(pubKey[:]) | ||
if err != nil { | ||
return false | ||
} | ||
// parse the signature: | ||
signature := signatureFromBytes(sigStr) | ||
// Reject malleable signatures. libsecp256k1 does this check but btcec doesn't. | ||
// see: https://github.com/ethereum/go-ethereum/blob/f9401ae011ddf7f8d2d95020b7446c17f8d98dc1/crypto/signature_nocgo.go#L90-L93 | ||
if signature.S.Cmp(secp256k1halfN) > 0 { | ||
|
||
psig, ok := signatureFromBytes(sigStr) | ||
if !ok { | ||
return false | ||
} | ||
return signature.Verify(crypto.Sha256(msg), pub) | ||
|
||
return psig.Verify(crypto.Sha256(msg), pub) | ||
} | ||
|
||
// Read Signature struct from R || S. Caller needs to ensure | ||
// that len(sigStr) == 64. | ||
func signatureFromBytes(sigStr []byte) *secp256k1.Signature { | ||
return &secp256k1.Signature{ | ||
R: new(big.Int).SetBytes(sigStr[:32]), | ||
S: new(big.Int).SetBytes(sigStr[32:64]), | ||
func signatureFromBytes(sigStr []byte) (*ecdsa.Signature, bool) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this safe to make the changes here? |
||
// parse the signature: | ||
var r, s secp256k1.ModNScalar | ||
if r.SetByteSlice(sigStr[:32]) { | ||
return nil, false // overflow | ||
} | ||
if s.SetByteSlice(sigStr[32:]) { | ||
gfanton marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return nil, false | ||
} | ||
|
||
// Reject malleable signatures. libsecp256k1 does this check but btcec doesn't. | ||
if s.IsOverHalfOrder() { | ||
return nil, false | ||
} | ||
} | ||
|
||
// Serialize signature to R || S. | ||
// R, S are padded to 32 bytes respectively. | ||
func serializeSig(sig *secp256k1.Signature) []byte { | ||
rBytes := sig.R.Bytes() | ||
sBytes := sig.S.Bytes() | ||
sigBytes := make([]byte, 64) | ||
// 0 pad the byte arrays from the left if they aren't big enough. | ||
copy(sigBytes[32-len(rBytes):32], rBytes) | ||
copy(sigBytes[64-len(sBytes):64], sBytes) | ||
return sigBytes | ||
return ecdsa.NewSignature(&r, &s), true | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but there's no BER conversion?
What does that even mean?