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

Optimizing crypto/ed25519.go #21

Merged
merged 7 commits into from
Feb 23, 2023
Merged

Optimizing crypto/ed25519.go #21

merged 7 commits into from
Feb 23, 2023

Conversation

rafael-abuawad
Copy link
Contributor

  1. Avoid unnecessary copying of byte arrays, it is not necessary to make a copy of the byte array in these cases since the byte arrays are the same size and have the same underlying structure.

  2. Use of constants instead of literals To make it more declarativeconst CompressedPublicKeySize = 33 instead if len(paddr) != 33

1. Avoid unnecessary copying of byte arrays.
     it is not necessary to make a copy of the byte array in these cases since the byte arrays are the same size and have the same underlying structure. 
2. Use of constants instead of literals `const CompressedPublicKeySize = 33` instead `if len(paddr) != 33`
crypto/ed25519.go Outdated Show resolved Hide resolved
var pk PublicKey
copy(pk[:], rpk)
return pk
return PublicKey(p[32:])
Copy link
Contributor

Choose a reason for hiding this comment

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

Very nice...not sure why I didn't do this in the first place.

You should use PublicKeyLen here instead of 32. I also think you should keep the commend that explains why we go 32 in.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done! Thank you!

@patrick-ogrady patrick-ogrady added the enhancement New feature or request label Feb 23, 2023
@patrick-ogrady patrick-ogrady added this to the v0.0.2 milestone Feb 23, 2023
- Moved constant definition top
- Using `PublicKeyLen` instead of `32`
crypto/ed25519.go Outdated Show resolved Hide resolved
@patrick-ogrady
Copy link
Contributor

patrick-ogrady commented Feb 23, 2023

Ahhh that's why I didn't use it. Is this feature new in go1.20 (tests don't run on that yet)?

Error: crypto/ed25519.go:78:20: cannot convert p[PublicKeyLen:] (value of type []byte) to type PublicKey

I agree much cleaner (nice find)...may just need to update the versions rq first.

Update: yeah looks like this added it golang/go#46505.

Update 2: we'll be able to remove a TON of memory allocations using this. creating an issue now to apply this change all over the codebase (#23).

- Simplifying the function isn't possible, I thought it was but checking it on a text editor, I realized is not possible.
@@ -75,7 +75,10 @@ func GeneratePrivateKey() (PrivateKey, error) {
// PublicKey returns a PublicKey associated with the Ed25519 PrivateKey p.
// The PublicKey is the last 32 bytes of p.
func (p PrivateKey) PublicKey() PublicKey {
return PublicKey(p[PublicKeyLen:])
rpk := p[PrivateKeyLen:] // privateKey == private|public
Copy link
Contributor

Choose a reason for hiding this comment

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

You can put a TODO here for #23 so we make sure to catch it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Or just leave it. LGTM now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added the PrivateKeyLen variable to be more explicit, this MR is just some minor clean-ups.

@patrick-ogrady
Copy link
Contributor

🤦 lol forgot about this...sigh.

image

So question is do we want a separate const for "private key component" of privatekey. Looking in the crypto codebase to see if they have a name for this component.

@patrick-ogrady
Copy link
Contributor

Ahh they call this the SeedSize: https://cs.opensource.google/go/go/+/refs/tags/go1.20.1:src/crypto/ed25519/ed25519.go;l=204

So let's add a constant called PrivateKeySeedSize = ed25519.SeedSize and use that.

@rafael-abuawad
Copy link
Contributor Author

Ahhh that's why I didn't use it. Is this feature new in go1.20 (tests don't run on that yet)?

Error: crypto/ed25519.go:78:20: cannot convert p[PublicKeyLen:] (value of type []byte) to type PublicKey

I agree much cleaner (nice find)...may just need to update the versions rq first.

Update: yeah looks like this added it golang/go#46505.

Update 2: we'll be able to remove a TON of memory allocations using this. creating an issue now to apply this change all over the codebase (#23).

Yeah sorry about that, I'm fairly new with Go, and I didn't check for backward compatibility.

@patrick-ogrady
Copy link
Contributor

Ahhh that's why I didn't use it. Is this feature new in go1.20 (tests don't run on that yet)?

Error: crypto/ed25519.go:78:20: cannot convert p[PublicKeyLen:] (value of type []byte) to type PublicKey

I agree much cleaner (nice find)...may just need to update the versions rq first.
Update: yeah looks like this added it golang/go#46505.
Update 2: we'll be able to remove a TON of memory allocations using this. creating an issue now to apply this change all over the codebase (#23).

Yeah sorry about that, I'm fairly new with Go, and I didn't check for backward compatibility.

You've actually been able to do this since go1.17 but the old style was super gross, so I stayed away. But 1.20 makes it MUCH better:
image

@rafael-abuawad
Copy link
Contributor Author

Should I define a PrivateKeySeedSize instead of CompressedPublicKeySize? Wouldn't that be 1 byte short?

@patrick-ogrady
Copy link
Contributor

patrick-ogrady commented Feb 23, 2023

Should I define a PrivateKeySeedSize instead of CompressedPublicKeySize? Wouldn't that be 1 byte short?

You should define PrivateKeySeedLen in addition to CompressedPublicKeySize (what you already did). 👍

@patrick-ogrady
Copy link
Contributor

Note the Len above just to be consistent.

- Updating naming convention
- Fixing wrong variable naming (`PrivateKeyLen` (64) to `PublicKeyLen` (32))
@rafael-abuawad
Copy link
Contributor Author

rafael-abuawad commented Feb 23, 2023

@patrick-ogrady I don't understand why are we defining PrivateKeySeedLen? 😕

@@ -74,7 +76,7 @@ func GeneratePrivateKey() (PrivateKey, error) {
// PublicKey returns a PublicKey associated with the Ed25519 PrivateKey p.
// The PublicKey is the last 32 bytes of p.
func (p PrivateKey) PublicKey() PublicKey {
rpk := p[32:] // privateKey == private|public
Copy link
Contributor

Choose a reason for hiding this comment

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

Put PrivateKeySeedLen here ^^

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you!

@patrick-ogrady
Copy link
Contributor

patrick-ogrady commented Feb 23, 2023

Found a few erroneous comments and am going to do a pass on this after I merge it as well 👍 .

@patrick-ogrady
Copy link
Contributor

@rafael-abuawad Going to merge this and make the lint fixes myself. Thanks for putting this up!

@patrick-ogrady patrick-ogrady merged commit bf0880a into ava-labs:main Feb 23, 2023
@patrick-ogrady
Copy link
Contributor

Continues here: #26

@patrick-ogrady
Copy link
Contributor

Congrats on being the first external contributor! https://twitter.com/_patrickogrady/status/1628870022360895488

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants