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