Skip to content

Add NewSecretKeyFromEd25519Bytes#59

Merged
jimjbrettj merged 3 commits intoChainSafe:masterfrom
oldgreen:secret_key_from_ed25519_bytes
May 4, 2023
Merged

Add NewSecretKeyFromEd25519Bytes#59
jimjbrettj merged 3 commits intoChainSafe:masterfrom
oldgreen:secret_key_from_ed25519_bytes

Conversation

@oldgreen
Copy link
Contributor

@oldgreen oldgreen commented May 2, 2023

https://github.com/w3f/schnorrkel/blob/ab3e3d609cd8b9eefbe0333066f698c40fd09582/src/keys.rs#L495-L533

This is useful for working with the exported polkadot.js account JSON file.

@CLAassistant
Copy link

CLAassistant commented May 2, 2023

CLA assistant check
All committers have signed the CLA.

}
}

func NewSecretKeyFromEd25519Bytes(b [SecretKeySize + 32]byte) *SecretKey {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func NewSecretKeyFromEd25519Bytes(b [SecretKeySize + 32]byte) *SecretKey {
func NewSecretKeyFromEd25519Bytes(b [SecretKeySize + NonceSize]byte) *SecretKey {

add a const NonceSize = 32 perhaps?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you look at the file, you'll see the number 32 all over the place. I don't want to refactor the whole file in this pull request. In my view the refactoring (which I agree should happen) should probably bring the constants closer to the rust implementation.

keys.go Outdated
Comment on lines +120 to +123
copy(sk.key[:], b[:32])
t := divideScalarByCofactor(sk.key[:])

copy(sk.key[:], t)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
copy(sk.key[:], b[:32])
t := divideScalarByCofactor(sk.key[:])
copy(sk.key[:], t)
copy(sk.key[:], divideScalarByCofactor(b[:32]))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This suggested change is not correct, because divideScalarByCofactor actually changes the slice in argument. But I'll commit the following, that has one copy less. Thanks.

diff --git a/keys.go b/keys.go
index 4ebfe1d..da0cf5b 100644
--- a/keys.go
+++ b/keys.go
@@ -118,9 +118,8 @@ func NewSecretKeyFromEd25519Bytes(b [SecretKeySize + 32]byte) *SecretKey {
        }
 
        copy(sk.key[:], b[:32])
-       t := divideScalarByCofactor(sk.key[:])
+       divideScalarByCofactor(sk.key[:])
 
-       copy(sk.key[:], t)
        copy(sk.nonce[:], b[32:])
 
        return sk

@danforbes danforbes requested a review from timwu20 May 3, 2023 00:56
Copy link
Contributor

@jimjbrettj jimjbrettj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, nice work!

@jimjbrettj
Copy link
Contributor

@oldgreen Thanks for this! All looks good to me, I believe you just still need to sign the contributor agreement

@jimjbrettj jimjbrettj merged commit 97bcebe into ChainSafe:master May 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants