-
Notifications
You must be signed in to change notification settings - Fork 296
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
multi: use secp256k1 types and fields directly. #1211
Conversation
hdkeychain/extendedkey.go
Outdated
pkx, pky := chainec.Secp256k1.ScalarBaseMult(k.key) | ||
pubKey := chainec.Secp256k1.NewPublicKey(pkx, pky) | ||
pkx, pky := secp256k1.S256().ScalarBaseMult(k.key) | ||
pubKey := secp256k1.NewPublicKey(pkx, pky) |
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.
Would prefer the following to match upstream and avoid an unnecessary GC:
pubKey := secp256k1.PublicKey{Curve: secp256k1.S256(), X: pkx, Y: pky}
hdkeychain/extendedkey.go
Outdated
ilNum := new(big.Int).SetBytes(il) | ||
if ilNum.Cmp(chainec.Secp256k1.GetN()) >= 0 || ilNum.Sign() == 0 { | ||
if ilNum.Cmp(curve.Params().N) >= 0 || ilNum.Sign() == 0 { |
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.
Why call .Params()
? N
is already available from the curve struct.
hdkeychain/extendedkey.go
Outdated
@@ -271,22 +273,22 @@ func (k *ExtendedKey) Child(i uint32) (*ExtendedKey, error) { | |||
// childKey = parse256(Il) + parenKey | |||
keyNum := new(big.Int).SetBytes(k.key) | |||
ilNum.Add(ilNum, keyNum) | |||
ilNum.Mod(ilNum, chainec.Secp256k1.GetN()) | |||
ilNum.Mod(ilNum, curve.Params().N) |
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.
Same here
hdkeychain/extendedkey.go
Outdated
@@ -295,9 +297,9 @@ func (k *ExtendedKey) Child(i uint32) (*ExtendedKey, error) { | |||
// derive the final child key. | |||
// | |||
// childKey = serP(point(parse256(Il)) + parentKey) | |||
childX, childY := chainec.Secp256k1.Add(ilx, ily, pubKey.GetX(), | |||
childX, childY := curve.Add(ilx, ily, pubKey.GetX(), |
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.
Why call .GetX
? X
is available on the struct directly. In fact, GetX
should ultimately be removed entirely from secp256k1.PublicKey
(it can't yet since it's still required for the chainec
interface) since it doesn't even follow effective Go guidelines about getters.
hdkeychain/extendedkey.go
Outdated
@@ -295,9 +297,9 @@ func (k *ExtendedKey) Child(i uint32) (*ExtendedKey, error) { | |||
// derive the final child key. | |||
// | |||
// childKey = serP(point(parse256(Il)) + parentKey) | |||
childX, childY := chainec.Secp256k1.Add(ilx, ily, pubKey.GetX(), | |||
childX, childY := curve.Add(ilx, ily, pubKey.GetX(), | |||
pubKey.GetY()) |
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.
Same deal with .Y
.
hdkeychain/extendedkey.go
Outdated
pubKey.GetY()) | ||
pk := chainec.Secp256k1.NewPublicKey(childX, childY) | ||
pk := secp256k1.NewPublicKey(childX, childY) |
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.
Once again, please use the struct directly to avoid the GC overhead in crypto and to more closely match upstream.
pk := secp256k1.PublicKey{Curve: secp256k1.S256(), X: childX, Y: childY}
hdkeychain/extendedkey.go
Outdated
@@ -472,7 +474,7 @@ func NewMaster(seed []byte, net *chaincfg.Params) (*ExtendedKey, error) { | |||
|
|||
// Ensure the key in usable. | |||
secretKeyNum := new(big.Int).SetBytes(secretKey) | |||
if secretKeyNum.Cmp(chainec.Secp256k1.GetN()) >= 0 || | |||
if secretKeyNum.Cmp(secp256k1.S256().Params().N) >= 0 || |
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.
Same deal here regarding .N
.
hdkeychain/extendedkey.go
Outdated
@@ -520,13 +522,13 @@ func NewKeyFromString(key string) (*ExtendedKey, error) { | |||
// of the order of the secp256k1 curve and not be 0. | |||
keyData = keyData[1:] | |||
keyNum := new(big.Int).SetBytes(keyData) | |||
if keyNum.Cmp(chainec.Secp256k1.GetN()) >= 0 || keyNum.Sign() == 0 { | |||
if keyNum.Cmp(secp256k1.S256().Params().N) >= 0 || keyNum.Sign() == 0 { |
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.
And here.
The current description would cause the issue to be closed. Please reword it so it won't. Also, please add more description to the commit message which describes why it's being changed. Github might go away one day, so the commit messages need to make it clear what is happening without relying on pointing to external issue numbers. Not to mention it frustrates forks with wrong numbers if you put them in commits. |
70521f3
to
adbd701
Compare
hdkeychain/extendedkey.go
Outdated
childX, childY := chainec.Secp256k1.Add(ilx, ily, pubKey.GetX(), | ||
pubKey.GetY()) | ||
pk := chainec.Secp256k1.NewPublicKey(childX, childY) | ||
childX, childY := curve.Add(ilx, ily, pubKey.X, |
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.
Should be able to fit on the same line without issue.
387fd0e
to
f48e68a
Compare
rpctest/memwallet.go
Outdated
func keyToAddr(key chainec.PrivateKey, net *chaincfg.Params) (dcrutil.Address, error) { | ||
pubKey := chainec.Secp256k1.NewPublicKey(key.Public()) | ||
func keyToAddr(key *secp256k1.PrivateKey, net *chaincfg.Params) (dcrutil.Address, error) { | ||
pubKey := secp256k1.NewPublicKey(key.Public()) |
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.
No need to pull out the X and Y of the public key just to recreate it and cause more allocations that have to be GC'd. Just cast it.
pubKey := (*secp256k1.PublicKey)(&key.PublicKey)
c304d5b
to
10cbe0a
Compare
The chainec package is scheduled to be removed. This PR replaces chainec function calls within the hdkeychain package with the underlining secp256k1 types and fields which breaks its dependence on a package soon to be removed. memwallet, a member of rpctest which calls hdkeychain functions is also updated.
10cbe0a
to
2e293f2
Compare
multi: update gcs indexing and serving code to match latest BIP 158 instance
The
chainec
package is scheduled to be removed. This PR replaceschainec
function calls within thehdkeychain
package with the underliningsecp256k1
types and fields which breaks its dependence on a package soon to be removed.memwallet
, a member ofrpctest
which callshdkeychain
functions is also updated.This is work towards #1191