Skip to content

Commit

Permalink
Optimizing crypto/ed25519.go (#21)
Browse files Browse the repository at this point in the history
* Optimizing `crypto/ed25519.go`

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`

* Cleaning up after comments

- Moved constant definition top
- Using `PublicKeyLen` instead of `32`

* Reverting back the changes

- Simplifying the function isn't possible, I thought it was but checking it on a text editor, I realized is not possible.

* Added new constant, and fixing naming convention

* Updating `PrivateKeySeedSize` to `...Len`

- Updating naming convention
- Fixing wrong variable naming (`PrivateKeyLen` (64) to `PublicKeyLen` (32))

* Using `Len` instead of `Size`

Consistency

* Adding `PrivateKeySeedLen`
  • Loading branch information
rafael-abuawad authored Feb 23, 2023
1 parent 3a16ce7 commit bf0880a
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions crypto/ed25519.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ type (
)

const (
PublicKeyLen = ed25519.PublicKeySize
PrivateKeyLen = ed25519.PrivateKeySize
SignatureLen = ed25519.SignatureSize
PublicKeyCompressedLen = 33
PublicKeyLen = ed25519.PublicKeySize
PrivateKeyLen = ed25519.PrivateKeySize
PrivateKeySeedLen = ed25519.SeedSize
SignatureLen = ed25519.SignatureSize
)

var (
Expand Down Expand Up @@ -51,11 +53,11 @@ func ParseAddress(hrp, saddr string) (PublicKey, error) {
if phrp != hrp {
return EmptyPublicKey, ErrIncorrectHrp
}
if len(paddr) != 33 /* compressed public key size */ {
if len(paddr) != PublicKeyCompressedLen {
return EmptyPublicKey, ErrInvalidPublicKey
}
var p PublicKey
copy(p[:], paddr[:32])
copy(p[:], paddr[:PublicKeyLen])
return p, nil
}

Expand All @@ -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
rpk := p[PrivateKeySeedLen:] // privateKey == private|public
var pk PublicKey
copy(pk[:], rpk)
return pk
Expand Down

0 comments on commit bf0880a

Please sign in to comment.