Skip to content

Commit

Permalink
Fix comments and refactor some checks
Browse files Browse the repository at this point in the history
  • Loading branch information
elichai committed Mar 18, 2021
1 parent 28a56f4 commit 5382a48
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
10 changes: 6 additions & 4 deletions ecdsa_privkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import (
"unsafe"
)

// ECDSAPrivateKey is a type representing a Secp256k1 private key.
// This private key can be used to create Schnorr/ECDSA signatures
// ECDSAPrivateKey is a type representing a Secp256k1 ECDSA private key.
type ECDSAPrivateKey struct {
privateKey [SerializedPrivateKeySize]byte
init bool
Expand Down Expand Up @@ -92,12 +91,15 @@ func GenerateECDSAPrivateKey() (key *ECDSAPrivateKey, err error) {
cPtr := (*C.uchar)(&key.privateKey[0])
for {
n, tmpErr := rand.Read(key.privateKey[:])
if tmpErr != nil || n != len(key.privateKey) {
if tmpErr != nil {
return nil, tmpErr
}
if n != len(key.privateKey) {
panic("The standard library promises that this should never happen")
}
ret := C.secp256k1_ec_seckey_verify(C.secp256k1_context_no_precomp, cPtr)
if ret == 1 {
return
return key, nil
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions ecdsa_pubkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type ECDSAPublicKey struct {
init bool
}

// SerializedECDSAPublicKey is a is a byte array representing the storage representation of a compressed or uncompressed ECDSAPublicKey
// SerializedECDSAPublicKey is a is a byte array representing the storage representation of a compressed ECDSAPublicKey
type SerializedECDSAPublicKey [SerializedECDSAPublicKeySize]byte

// IsEqual returns true if target is the same as key.
Expand Down Expand Up @@ -69,8 +69,8 @@ func (key *ECDSAPublicKey) ECDSAVerify(hash *Hash, signature *ECDSASignature) bo
}

// DeserializeECDSAPubKey deserializes a serialized ECDSA public key, verifying it's valid.
// it supports both compressed(33 bytes) and uncompressed(65 bytes) public keys.
// it does not support hybrid(65 bytes) keys.
// it supports only compressed(33 bytes) public keys.
// it does not support uncompressed(65 bytes) or hybrid(65 bytes) keys.
func DeserializeECDSAPubKey(serializedPubKey []byte) (*ECDSAPublicKey, error) {
if len(serializedPubKey) != SerializedECDSAPublicKeySize {
return nil, errors.New(fmt.Sprintf("serializedPubKey has to be %d bytes, instead got :%d", SerializedECDSAPublicKeySize, len(serializedPubKey)))
Expand Down
5 changes: 4 additions & 1 deletion schnorr_keypair.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,12 @@ func GenerateSchnorrKeyPair() (key *SchnorrKeyPair, err error) {
rawKey := SerializedPrivateKey{}
for {
n, err := rand.Read(rawKey[:])
if err != nil || n != len(rawKey) {
if err != nil {
return nil, err
}
if n != len(rawKey) {
panic("The standard library promises that this should never happen")
}
key, err = DeserializeSchnorrPrivateKey(&rawKey)
if err == nil {
return key, nil
Expand Down
4 changes: 1 addition & 3 deletions schnorr_pubkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type SchnorrPublicKey struct {
init bool
}

// SerializedSchnorrPublicKey is a is a byte array representing the storage representation of a compressed or uncompressed SchnorrPublicKey
// SerializedSchnorrPublicKey is a is a byte array representing the storage representation of a SchnorrPublicKey
type SerializedSchnorrPublicKey [SerializedSchnorrPublicKeySize]byte

// IsEqual returns true if target is the same as key.
Expand Down Expand Up @@ -71,8 +71,6 @@ func (key *SchnorrPublicKey) SchnorrVerify(hash *Hash, signature *SchnorrSignatu
}

// DeserializeSchnorrPubKey deserializes a serialized schnorr public key, verifying it's valid.
// it supports both compressed(33 bytes) and uncompressed(65 bytes) public keys.
// it does not support hybrid(65 bytes) keys.
func DeserializeSchnorrPubKey(serializedPubKey []byte) (*SchnorrPublicKey, error) {
if len(serializedPubKey) != SerializedSchnorrPublicKeySize {
return nil, errors.New(fmt.Sprintf("serializedPubKey has to be %d bytes, instead got :%d", SerializedSchnorrPublicKeySize, len(serializedPubKey)))
Expand Down

0 comments on commit 5382a48

Please sign in to comment.