Skip to content

Commit 4bf944c

Browse files
committed
crypto: rename constants to more appropriate names
1 parent 371d9f5 commit 4bf944c

File tree

14 files changed

+46
-48
lines changed

14 files changed

+46
-48
lines changed

crypto/ed25519/key.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import (
66
)
77

88
const (
9-
// PublicKeySize is the size, in bytes, of public keys as used in this package.
10-
PublicKeySize = ed25519.PublicKeySize
11-
// PrivateKeySize is the size, in bytes, of private keys as used in this package.
12-
PrivateKeySize = ed25519.PrivateKeySize
13-
// SignatureSize is the size, in bytes, of signatures generated and verified by this package.
14-
SignatureSize = ed25519.SignatureSize
9+
// PublicKeyBytesSize is the size, in bytes, of public keys in raw bytes.
10+
PublicKeyBytesSize = ed25519.PublicKeySize
11+
// PrivateKeyBytesSize is the size, in bytes, of private keys in raw bytes.
12+
PrivateKeyBytesSize = ed25519.PrivateKeySize
13+
// SignatureBytesSize is the size, in bytes, of signatures in raw bytes.
14+
SignatureBytesSize = ed25519.SignatureSize
1515

1616
MultibaseCode = uint64(0xed)
1717
)

crypto/ed25519/key_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ var harness = helpers.TestHarness[PublicKey, PrivateKey]{
1717
PrivateKeyFromPKCS8DER: PrivateKeyFromPKCS8DER,
1818
PrivateKeyFromPKCS8PEM: PrivateKeyFromPKCS8PEM,
1919
MultibaseCode: MultibaseCode,
20-
PublicKeySize: PublicKeySize,
21-
PrivateKeySize: PrivateKeySize,
22-
SignatureSize: SignatureSize,
20+
PublicKeyBytesSize: PublicKeyBytesSize,
21+
PrivateKeyBytesSize: PrivateKeyBytesSize,
22+
SignatureBytesSize: SignatureBytesSize,
2323
}
2424

2525
func TestSuite(t *testing.T) {

crypto/ed25519/private.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type PrivateKey struct {
2121
// This compact serialization format is the raw key material, without metadata or structure.
2222
// It errors if the slice is not the right size.
2323
func PrivateKeyFromBytes(b []byte) (PrivateKey, error) {
24-
if len(b) != PrivateKeySize {
24+
if len(b) != PrivateKeyBytesSize {
2525
return PrivateKey{}, fmt.Errorf("invalid ed25519 private key size")
2626
}
2727
// make a copy
@@ -76,7 +76,7 @@ func (p PrivateKey) SignToASN1(message []byte) ([]byte, error) {
7676
func (p PrivateKey) ToBytes() []byte {
7777
// Copy the private key to a fixed size buffer that can get allocated on the
7878
// caller's stack after inlining.
79-
var buf [PrivateKeySize]byte
79+
var buf [PrivateKeyBytesSize]byte
8080
return append(buf[:0], p.k...)
8181
}
8282

crypto/ed25519/public.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type PublicKey struct {
2323
// This compact serialization format is the raw key material, without metadata or structure.
2424
// It errors if the slice is not the right size.
2525
func PublicKeyFromBytes(b []byte) (PublicKey, error) {
26-
if len(b) != PublicKeySize {
26+
if len(b) != PublicKeyBytesSize {
2727
return PublicKey{}, fmt.Errorf("invalid ed25519 public key size")
2828
}
2929
// make a copy
@@ -39,7 +39,7 @@ func PublicKeyFromPublicKeyMultibase(multibase string) (PublicKey, error) {
3939
if code != MultibaseCode {
4040
return PublicKey{}, fmt.Errorf("invalid code")
4141
}
42-
if len(bytes) != PublicKeySize {
42+
if len(bytes) != PublicKeyBytesSize {
4343
return PublicKey{}, fmt.Errorf("invalid ed25519 public key size")
4444
}
4545
return PublicKeyFromBytes(bytes)
@@ -69,7 +69,7 @@ func PublicKeyFromX509PEM(str string) (PublicKey, error) {
6969
func (p PublicKey) ToBytes() []byte {
7070
// Copy the private key to a fixed size buffer that can get allocated on the
7171
// caller's stack after inlining.
72-
var buf [PublicKeySize]byte
72+
var buf [PublicKeyBytesSize]byte
7373
return append(buf[:0], p.k...)
7474
}
7575

@@ -110,7 +110,7 @@ func (p PublicKey) VerifyASN1(message, signature []byte) bool {
110110
if !s.ReadASN1BitString(&bitString) {
111111
return false
112112
}
113-
if bitString.BitLength != SignatureSize*8 {
113+
if bitString.BitLength != SignatureBytesSize*8 {
114114
return false
115115
}
116116

crypto/internal/testsuite.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ type TestHarness[PubT crypto.PublicKey, PrivT crypto.PrivateKey] struct {
2929

3030
MultibaseCode uint64
3131

32-
PublicKeySize int
33-
PrivateKeySize int
34-
SignatureSize int
32+
PublicKeyBytesSize int
33+
PrivateKeyBytesSize int
34+
SignatureBytesSize int
3535
}
3636

3737
func TestSuite[PubT crypto.PublicKey, PrivT crypto.PrivateKey](t *testing.T, harness TestHarness[PubT, PrivT]) {
@@ -108,12 +108,14 @@ func TestSuite[PubT crypto.PublicKey, PrivT crypto.PrivateKey](t *testing.T, har
108108
rtPub, err := harness.PublicKeyFromBytes(bytes)
109109
require.NoError(t, err)
110110
require.True(t, pub.Equal(rtPub))
111+
require.Equal(t, harness.PublicKeyBytesSize, len(bytes))
111112

112113
bytes = priv.ToBytes()
113114
stats.bytesPrivSize = len(bytes)
114115
rtPriv, err := harness.PrivateKeyFromBytes(bytes)
115116
require.NoError(t, err)
116117
require.True(t, priv.Equal(rtPriv))
118+
require.Equal(t, harness.PrivateKeyBytesSize, len(bytes))
117119
})
118120

119121
t.Run("MultibaseRoundTrip", func(t *testing.T) {
@@ -193,7 +195,7 @@ func TestSuite[PubT crypto.PublicKey, PrivT crypto.PrivateKey](t *testing.T, har
193195
name: "Bytes signature",
194196
signer: spriv.SignToBytes,
195197
verifier: spub.VerifyBytes,
196-
expectedSize: harness.SignatureSize,
198+
expectedSize: harness.SignatureBytesSize,
197199
stats: &stats.sigRawSize,
198200
},
199201
{

crypto/p256/key.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ import (
77
)
88

99
const (
10-
// TODO
11-
PublicKeySize = 33
12-
PrivateKeySize = 32
13-
SignatureSize = 64
10+
// PublicKeyBytesSize is the size, in bytes, of public keys in raw bytes.
11+
PublicKeyBytesSize = 33
12+
// PrivateKeyBytesSize is the size, in bytes, of private keys in raw bytes.
13+
PrivateKeyBytesSize = 32
14+
// SignatureBytesSize is the size, in bytes, of signatures in raw bytes.
15+
SignatureBytesSize = 64
1416

1517
MultibaseCode = uint64(0x1200)
1618
)

crypto/p256/key_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ var harness = helpers.TestHarness[*PublicKey, *PrivateKey]{
1717
PrivateKeyFromPKCS8DER: PrivateKeyFromPKCS8DER,
1818
PrivateKeyFromPKCS8PEM: PrivateKeyFromPKCS8PEM,
1919
MultibaseCode: MultibaseCode,
20-
PublicKeySize: PublicKeySize,
21-
PrivateKeySize: PrivateKeySize,
22-
SignatureSize: SignatureSize,
20+
PublicKeyBytesSize: PublicKeyBytesSize,
21+
PrivateKeyBytesSize: PrivateKeyBytesSize,
22+
SignatureBytesSize: SignatureBytesSize,
2323
}
2424

2525
func TestSuite(t *testing.T) {

crypto/p256/private.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type PrivateKey ecdsa.PrivateKey
2222
// This compact serialization format is the raw key material, without metadata or structure.
2323
// It errors if the slice is not the right size.
2424
func PrivateKeyFromBytes(b []byte) (*PrivateKey, error) {
25-
if len(b) != PrivateKeySize {
25+
if len(b) != PrivateKeyBytesSize {
2626
return nil, fmt.Errorf("invalid P-256 private key size")
2727
}
2828

@@ -73,7 +73,7 @@ func (p *PrivateKey) Public() crypto.PublicKey {
7373

7474
func (p *PrivateKey) ToBytes() []byte {
7575
// fixed size buffer that can get allocated on the caller's stack after inlining.
76-
var buf [PrivateKeySize]byte
76+
var buf [PrivateKeyBytesSize]byte
7777
((*ecdsa.PrivateKey)(p)).D.FillBytes(buf[:])
7878
return buf[:]
7979
}

crypto/p256/public.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type PublicKey ecdsa.PublicKey
2121
// This compact serialization format is the raw key material, without metadata or structure.
2222
// It errors if the slice is not the right size.
2323
func PublicKeyFromBytes(b []byte) (*PublicKey, error) {
24-
if len(b) != PublicKeySize {
24+
if len(b) != PublicKeyBytesSize {
2525
return nil, fmt.Errorf("invalid P-256 public key size")
2626
}
2727
x, y := elliptic.UnmarshalCompressed(elliptic.P256(), b)
@@ -103,7 +103,7 @@ func (p *PublicKey) ToX509PEM() string {
103103
*/
104104

105105
func (p *PublicKey) VerifyBytes(message, signature []byte) bool {
106-
if len(signature) != SignatureSize {
106+
if len(signature) != SignatureBytesSize {
107107
return false
108108
}
109109

crypto/x25519/key.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@ import (
66
)
77

88
const (
9-
// PublicKeySize is the size, in bytes, of public keys as used in this package.
10-
PublicKeySize = 32
11-
// PrivateKeySize is the size, in bytes, of private keys as used in this package.
12-
PrivateKeySize = 32
13-
// SignatureSize is the size, in bytes, of signatures generated and verified by this package.
14-
SignatureSize = 32
9+
// PublicKeyBytesSize is the size, in bytes, of public keys in raw bytes.
10+
PublicKeyBytesSize = 32
11+
// PrivateKeyBytesSize is the size, in bytes, of private keys in raw bytes.
12+
PrivateKeyBytesSize = 32
1513

1614
MultibaseCode = uint64(0xec)
1715
)

0 commit comments

Comments
 (0)