feat: rename Discriminant to Algorithm
This commit is contained in:
committed by
Michael Muré
parent
68e0d91f64
commit
58ebd20b04
@@ -23,13 +23,13 @@ func ExampleDecode() {
|
||||
handleErr(err)
|
||||
|
||||
fmt.Printf("%T\n", vs)
|
||||
fmt.Printf("Discriminator: %d\n", vs.Discriminator())
|
||||
fmt.Printf("Algorithm: %d\n", vs.Algorithm())
|
||||
fmt.Printf("Hash: %d\n", vs.Hash())
|
||||
fmt.Printf("PayloadEncoding: %d\n", vs.PayloadEncoding())
|
||||
|
||||
// Output:
|
||||
// varsig.EdDSAVarsig
|
||||
// Discriminator: 237
|
||||
// Algorithm: 237
|
||||
// Hash: 19
|
||||
// PayloadEncoding: 3
|
||||
}
|
||||
|
||||
@@ -115,7 +115,7 @@ func TestRoundTrip(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, tc.varsig.Version(), rt.Version())
|
||||
require.Equal(t, tc.varsig.Discriminator(), rt.Discriminator())
|
||||
require.Equal(t, tc.varsig.Algorithm(), rt.Algorithm())
|
||||
require.Equal(t, tc.varsig.PayloadEncoding(), rt.PayloadEncoding())
|
||||
|
||||
switch vs := tc.varsig.(type) {
|
||||
|
||||
@@ -173,14 +173,14 @@ func EncodePayloadEncoding(enc PayloadEncoding) []byte {
|
||||
return res
|
||||
}
|
||||
|
||||
// Discriminator is (usually) the value representing the public key type of
|
||||
// Algorithm is (usually) the value representing the public key type of
|
||||
// the algorithm used to create the signature.
|
||||
//
|
||||
// There is no set list of constants here, nor is there a decode function
|
||||
// as the author of an implementation should include the constant with the
|
||||
// implementation, and the decoding is handled by the Handler, which uses
|
||||
// the Discriminator to choose the correct implementation. Also note that
|
||||
// some of the Discriminator values for a specific implementation have
|
||||
// the Algorithm to choose the correct implementation. Also note that
|
||||
// some of the Algorithm values for a specific implementation have
|
||||
// changed between varsig v0 and v1, so it's possible to have more than one
|
||||
// constant defined per implementation.
|
||||
type Discriminator uint64
|
||||
type Algorithm uint64
|
||||
|
||||
6
ecdsa.go
6
ecdsa.go
@@ -5,8 +5,8 @@ import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// DiscriminatorECDSA is the value specifying an ECDSA signature.
|
||||
const DiscriminatorECDSA = Discriminator(0xec)
|
||||
// AlgorithmECDSA is the value specifying an ECDSA signature.
|
||||
const AlgorithmECDSA = Algorithm(0xec)
|
||||
|
||||
// ECDSACurve are values that specify which ECDSA curve is used when
|
||||
// generating the signature.
|
||||
@@ -51,7 +51,7 @@ type ECDSAVarsig struct {
|
||||
func NewECDSAVarsig(curve ECDSACurve, hashAlgorithm Hash, payloadEncoding PayloadEncoding) ECDSAVarsig {
|
||||
return ECDSAVarsig{
|
||||
varsig: varsig{
|
||||
disc: DiscriminatorECDSA,
|
||||
algo: AlgorithmECDSA,
|
||||
payEnc: payloadEncoding,
|
||||
},
|
||||
curve: curve,
|
||||
|
||||
6
eddsa.go
6
eddsa.go
@@ -5,8 +5,8 @@ import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// DiscriminatorEdDSA is the value specifying an EdDSA signature.
|
||||
const DiscriminatorEdDSA = Discriminator(0xed)
|
||||
// AlgorithmEdDSA is the value specifying an EdDSA signature.
|
||||
const AlgorithmEdDSA = Algorithm(0xed)
|
||||
|
||||
// EdDSACurve are values that specify which Edwards curve is used when
|
||||
// generating the signature.
|
||||
@@ -49,7 +49,7 @@ type EdDSAVarsig struct {
|
||||
func NewEdDSAVarsig(curve EdDSACurve, hashAlgorithm Hash, payloadEncoding PayloadEncoding) EdDSAVarsig {
|
||||
return EdDSAVarsig{
|
||||
varsig: varsig{
|
||||
disc: DiscriminatorEdDSA,
|
||||
algo: AlgorithmEdDSA,
|
||||
payEnc: payloadEncoding,
|
||||
},
|
||||
curve: curve,
|
||||
|
||||
@@ -29,7 +29,7 @@ func TestUCANExampleV1(t *testing.T) {
|
||||
require.True(t, ok)
|
||||
|
||||
assert.Equal(t, varsig.Version1, ed25519V.Version())
|
||||
assert.Equal(t, varsig.DiscriminatorEdDSA, ed25519V.Discriminator())
|
||||
assert.Equal(t, varsig.AlgorithmEdDSA, ed25519V.Algorithm())
|
||||
assert.Equal(t, varsig.CurveEd25519, ed25519V.Curve())
|
||||
assert.Equal(t, varsig.HashSha2_512, ed25519V.Hash())
|
||||
assert.Equal(t, varsig.PayloadEncodingDAGCBOR, ed25519V.PayloadEncoding())
|
||||
|
||||
4
error.go
4
error.go
@@ -16,9 +16,9 @@ var ErrUnknownHash = errors.New("unknown hash algorithm")
|
||||
// for this field may vary based on the varsig version.
|
||||
var ErrUnsupportedPayloadEncoding = errors.New("unsupported payload encoding")
|
||||
|
||||
// ErrUnknownDiscriminator is returned when the Registry doesn't have a
|
||||
// ErrUnknownAlgorithm is returned when the Registry doesn't have a
|
||||
// parsing function for the decoded signing algorithm.
|
||||
var ErrUnknownDiscriminator = errors.New("unknown signing algorithm")
|
||||
var ErrUnknownAlgorithm = errors.New("unknown signing algorithm")
|
||||
|
||||
// ErrUnknownEdDSACurve is returned when the decoded uvarint isn't either
|
||||
// CurveEd25519 or CurveEd448.
|
||||
|
||||
26
registry.go
26
registry.go
@@ -22,15 +22,15 @@ type DecodeFunc func(BytesReader) (Varsig, error)
|
||||
|
||||
// Registry contains a mapping between known signing algorithms and
|
||||
// functions that can parse varsigs for that signing algorithm.
|
||||
type Registry map[Discriminator]DecodeFunc
|
||||
type Registry map[Algorithm]DecodeFunc
|
||||
|
||||
// DefaultRegistry provides a Registry containing the mappings for the
|
||||
// signing algorithms which have an implementation within this library.
|
||||
func DefaultRegistry() Registry {
|
||||
return map[Discriminator]DecodeFunc{
|
||||
DiscriminatorRSA: decodeRSA,
|
||||
DiscriminatorEdDSA: decodeEdDSA,
|
||||
DiscriminatorECDSA: decodeECDSA,
|
||||
return map[Algorithm]DecodeFunc{
|
||||
AlgorithmRSA: decodeRSA,
|
||||
AlgorithmEdDSA: decodeEdDSA,
|
||||
AlgorithmECDSA: decodeECDSA,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ func NewRegistry() Registry {
|
||||
|
||||
// Register allows new mappings between a signing algorithm and its parsing
|
||||
// function to the Registry.
|
||||
func (rs Registry) Register(alg Discriminator, decodeFunc DecodeFunc) {
|
||||
func (rs Registry) Register(alg Algorithm, decodeFunc DecodeFunc) {
|
||||
rs[alg] = decodeFunc
|
||||
}
|
||||
|
||||
@@ -63,7 +63,7 @@ func (rs Registry) DecodeStream(r BytesReader) (Varsig, error) {
|
||||
return nil, fmt.Errorf("%w: expected %d, got %d", ErrBadPrefix, Prefix, pre)
|
||||
}
|
||||
|
||||
vers, disc, err := rs.decodeVersAnddisc(r)
|
||||
vers, algo, err := rs.decodeVersAndAlgo(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -72,15 +72,15 @@ func (rs Registry) DecodeStream(r BytesReader) (Varsig, error) {
|
||||
return nil, fmt.Errorf("%w: %d", ErrUnsupportedVersion, vers)
|
||||
}
|
||||
|
||||
decodeFunc, ok := rs[Discriminator(disc)]
|
||||
decodeFunc, ok := rs[Algorithm(algo)]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("%w: %x", ErrUnknownDiscriminator, disc)
|
||||
return nil, fmt.Errorf("%w: %x", ErrUnknownAlgorithm, algo)
|
||||
}
|
||||
|
||||
return decodeFunc(r)
|
||||
}
|
||||
|
||||
func (rs Registry) decodeVersAnddisc(r BytesReader) (Version, Discriminator, error) {
|
||||
func (rs Registry) decodeVersAndAlgo(r BytesReader) (Version, Algorithm, error) {
|
||||
vers, err := binary.ReadUvarint(r)
|
||||
if err != nil {
|
||||
return Version(vers), 0, err
|
||||
@@ -91,10 +91,10 @@ func (rs Registry) decodeVersAnddisc(r BytesReader) (Version, Discriminator, err
|
||||
}
|
||||
|
||||
if vers >= 64 {
|
||||
return 0, Discriminator(vers), nil
|
||||
return 0, Algorithm(vers), nil
|
||||
}
|
||||
|
||||
disc, err := binary.ReadUvarint(r)
|
||||
algo, err := binary.ReadUvarint(r)
|
||||
|
||||
return Version(vers), Discriminator(disc), err
|
||||
return Version(vers), Algorithm(algo), err
|
||||
}
|
||||
|
||||
@@ -21,35 +21,35 @@ func TestRegistry_Decode(t *testing.T) {
|
||||
vs, err := reg.DecodeStream(bytes.NewReader(data))
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, varsig.Version1, vs.Version())
|
||||
assert.Equal(t, testDiscriminator1, vs.Discriminator())
|
||||
assert.Equal(t, testAlgorithm1, vs.Algorithm())
|
||||
})
|
||||
}
|
||||
|
||||
const (
|
||||
testDiscriminator0 varsig.Discriminator = 0x1000
|
||||
testDiscriminator1 varsig.Discriminator = 0x1001
|
||||
testAlgorithm0 varsig.Algorithm = 0x1000
|
||||
testAlgorithm1 varsig.Algorithm = 0x1001
|
||||
)
|
||||
|
||||
func testRegistry(t *testing.T) varsig.Registry {
|
||||
t.Helper()
|
||||
|
||||
reg := varsig.NewRegistry()
|
||||
reg.Register(testDiscriminator0, testDecodeFunc(testDiscriminator0))
|
||||
reg.Register(testDiscriminator1, testDecodeFunc(testDiscriminator1))
|
||||
reg.Register(testAlgorithm0, testDecodeFunc(testAlgorithm0))
|
||||
reg.Register(testAlgorithm1, testDecodeFunc(testAlgorithm1))
|
||||
|
||||
return reg
|
||||
}
|
||||
|
||||
func testDecodeFunc(disc varsig.Discriminator) varsig.DecodeFunc {
|
||||
func testDecodeFunc(algo varsig.Algorithm) varsig.DecodeFunc {
|
||||
return func(r varsig.BytesReader) (varsig.Varsig, error) {
|
||||
return &testVarsig{disc: disc}, nil
|
||||
return &testVarsig{algo: algo}, nil
|
||||
}
|
||||
}
|
||||
|
||||
var _ varsig.Varsig = testVarsig{}
|
||||
|
||||
type testVarsig struct {
|
||||
disc varsig.Discriminator
|
||||
algo varsig.Algorithm
|
||||
payEnc varsig.PayloadEncoding
|
||||
}
|
||||
|
||||
@@ -57,8 +57,8 @@ func (v testVarsig) Version() varsig.Version {
|
||||
return varsig.Version1
|
||||
}
|
||||
|
||||
func (v testVarsig) Discriminator() varsig.Discriminator {
|
||||
return v.disc
|
||||
func (v testVarsig) Algorithm() varsig.Algorithm {
|
||||
return v.algo
|
||||
}
|
||||
|
||||
func (v testVarsig) Hash() varsig.Hash {
|
||||
|
||||
6
rsa.go
6
rsa.go
@@ -4,8 +4,8 @@ import (
|
||||
"encoding/binary"
|
||||
)
|
||||
|
||||
// DiscriminatorRSA is the value specifying an RSA signature.
|
||||
const DiscriminatorRSA = Discriminator(0x1205)
|
||||
// AlgorithmRSA is the value specifying an RSA signature.
|
||||
const AlgorithmRSA = Algorithm(0x1205)
|
||||
|
||||
var _ Varsig = RSAVarsig{}
|
||||
|
||||
@@ -22,7 +22,7 @@ type RSAVarsig struct {
|
||||
func NewRSAVarsig(hashAlgorithm Hash, keyLen uint64, payloadEncoding PayloadEncoding) RSAVarsig {
|
||||
return RSAVarsig{
|
||||
varsig: varsig{
|
||||
disc: DiscriminatorRSA,
|
||||
algo: AlgorithmRSA,
|
||||
payEnc: payloadEncoding,
|
||||
},
|
||||
hashAlg: hashAlgorithm,
|
||||
|
||||
@@ -29,7 +29,7 @@ func TestRSAVarsig(t *testing.T) {
|
||||
require.True(t, ok)
|
||||
|
||||
require.Equal(t, varsig.Version1, rsaVs.Version())
|
||||
require.Equal(t, varsig.DiscriminatorRSA, rsaVs.Discriminator())
|
||||
require.Equal(t, varsig.AlgorithmRSA, rsaVs.Algorithm())
|
||||
require.Equal(t, varsig.HashSha2_256, rsaVs.Hash())
|
||||
require.Equal(t, varsig.PayloadEncodingDAGCBOR, rsaVs.PayloadEncoding())
|
||||
require.Equal(t, uint64(keyLen), rsaVs.KeyLength())
|
||||
|
||||
14
varsig.go
14
varsig.go
@@ -29,8 +29,8 @@ type Varsig interface {
|
||||
// Version returns the varsig's version field.
|
||||
Version() Version
|
||||
|
||||
// Discriminator returns the algorithm used to produce the corresponding signature.
|
||||
Discriminator() Discriminator
|
||||
// Algorithm returns the algorithm used to produce the corresponding signature.
|
||||
Algorithm() Algorithm
|
||||
|
||||
// Hash returns the hash used on the data before signature.
|
||||
Hash() Hash
|
||||
@@ -55,7 +55,7 @@ func DecodeStream(r BytesReader) (Varsig, error) {
|
||||
}
|
||||
|
||||
type varsig struct {
|
||||
disc Discriminator
|
||||
algo Algorithm
|
||||
payEnc PayloadEncoding
|
||||
}
|
||||
|
||||
@@ -64,10 +64,10 @@ func (v varsig) Version() Version {
|
||||
return Version1
|
||||
}
|
||||
|
||||
// Discriminator returns the algorithm used to produce the corresponding
|
||||
// Algorithm returns the algorithm used to produce the corresponding
|
||||
// signature.
|
||||
func (v varsig) Discriminator() Discriminator {
|
||||
return v.disc
|
||||
func (v varsig) Algorithm() Algorithm {
|
||||
return v.algo
|
||||
}
|
||||
|
||||
// PayloadEncoding returns the codec that was used to encode the signed
|
||||
@@ -84,7 +84,7 @@ func (v varsig) encode() []byte {
|
||||
|
||||
buf = binary.AppendUvarint(buf, Prefix)
|
||||
buf = binary.AppendUvarint(buf, uint64(Version1))
|
||||
buf = binary.AppendUvarint(buf, uint64(v.disc))
|
||||
buf = binary.AppendUvarint(buf, uint64(v.algo))
|
||||
|
||||
return buf
|
||||
}
|
||||
|
||||
@@ -21,13 +21,13 @@ func ExampleDecode() {
|
||||
handleErr(err)
|
||||
|
||||
fmt.Printf("%T\n", vs)
|
||||
fmt.Printf("Discriminator: %d\n", vs.Discriminator())
|
||||
fmt.Printf("Algorithm: %d\n", vs.Algorithm())
|
||||
fmt.Printf("Hash: %d\n", vs.Hash())
|
||||
fmt.Printf("PayloadEncoding: %d\n", vs.PayloadEncoding())
|
||||
|
||||
// Output:
|
||||
// varsig.EdDSAVarsig
|
||||
// Discriminator: 237
|
||||
// Algorithm: 237
|
||||
// Hash: 19
|
||||
// PayloadEncoding: 3
|
||||
}
|
||||
@@ -100,7 +100,7 @@ func TestDecode(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
vs, err := varsig.Decode(data)
|
||||
require.ErrorIs(t, err, varsig.ErrUnknownDiscriminator)
|
||||
require.ErrorIs(t, err, varsig.ErrUnknownAlgorithm)
|
||||
assert.Nil(t, vs)
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user