|
| 1 | +package gblsminsig |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/gordian-engine/gordian/gcrypto" |
| 9 | + blst "github.com/supranational/blst/bindings/go" |
| 10 | +) |
| 11 | + |
| 12 | +const keyTypeName = "bls-minsig" |
| 13 | + |
| 14 | +// The domain separation tag is a requirement per RFC9380 (Hashing to Elliptic Curves). |
| 15 | +// See sections 2.2.5 (domain separation), |
| 16 | +// 3.1 (domain separation requirements), |
| 17 | +// and 8.10 (suite ID naming conventions). |
| 18 | +// |
| 19 | +// Furthermore, see also draft-irtf-cfrg-bls-signature-05, |
| 20 | +// section 4.1 (ciphersuite format), |
| 21 | +// as that is the actual format being followed here. |
| 22 | +// |
| 23 | +// The ciphersuite ID according to the BLS signature document is: |
| 24 | +// |
| 25 | +// "BLS_SIG_" || H2C_SUITE_ID || SC_TAG || "_" |
| 26 | +// |
| 27 | +// And the H2C_SUITE_ID, per RFC9380 section 8.8.1, is: |
| 28 | +// |
| 29 | +// BLS12381G1_XMD:SHA-256_SSWU_RO_ |
| 30 | +// |
| 31 | +// Which only leaves the SC_TAG value, which is "NUL" for the basic scheme. |
| 32 | +var DomainSeparationTag = []byte("BLS_SIG_BLS12381G1_XMD:SHA-256_SSWU_RO_NUL_") |
| 33 | + |
| 34 | +// Register registers the BLS minimzed-signature key type with the given Registry. |
| 35 | +func Register(reg *gcrypto.Registry) { |
| 36 | + reg.Register(keyTypeName, PubKey{}, NewPubKey) |
| 37 | +} |
| 38 | + |
| 39 | +// PubKey wraps a blst.P2Affine and defines methods for the [gcrypto.PubKey] interface. |
| 40 | +type PubKey blst.P2Affine |
| 41 | + |
| 42 | +// NewPubKey decodes a compressed p2 affine point |
| 43 | +// and returns the public key for it. |
| 44 | +func NewPubKey(b []byte) (gcrypto.PubKey, error) { |
| 45 | + // This is checked inside Uncompress too, |
| 46 | + // but checking it here is an opportunity to return a more meaningful error. |
| 47 | + if len(b) != blst.BLST_P2_COMPRESS_BYTES { |
| 48 | + return nil, fmt.Errorf("expected %d compressed bytes, got %d", blst.BLST_P2_COMPRESS_BYTES, len(b)) |
| 49 | + } |
| 50 | + |
| 51 | + p2a := new(blst.P2Affine) |
| 52 | + p2a = p2a.Uncompress(b) |
| 53 | + |
| 54 | + if p2a == nil { |
| 55 | + return nil, errors.New("failed to decompress input") |
| 56 | + } |
| 57 | + |
| 58 | + if !p2a.KeyValidate() { |
| 59 | + return nil, errors.New("input key failed validation") |
| 60 | + } |
| 61 | + |
| 62 | + pk := PubKey(*p2a) |
| 63 | + return pk, nil |
| 64 | +} |
| 65 | + |
| 66 | +// Equal reports whether other is the same public key as k. |
| 67 | +func (k PubKey) Equal(other gcrypto.PubKey) bool { |
| 68 | + o, ok := other.(PubKey) |
| 69 | + if !ok { |
| 70 | + return false |
| 71 | + } |
| 72 | + |
| 73 | + p2a := blst.P2Affine(k) |
| 74 | + |
| 75 | + p2o := blst.P2Affine(o) |
| 76 | + return p2a.Equals(&p2o) |
| 77 | +} |
| 78 | + |
| 79 | +// PubKeyBytes returns the compressed bytes underlying k's P2 affine point. |
| 80 | +func (k PubKey) PubKeyBytes() []byte { |
| 81 | + p2a := blst.P2Affine(k) |
| 82 | + return p2a.Compress() |
| 83 | +} |
| 84 | + |
| 85 | +// Verify reports whether sig matches k for msg. |
| 86 | +func (k PubKey) Verify(msg, sig []byte) bool { |
| 87 | + // Signature is P1, and we assume the signature is compressed. |
| 88 | + p1a := new(blst.P1Affine) |
| 89 | + p1a = p1a.Uncompress(sig) |
| 90 | + if p1a == nil { |
| 91 | + return false |
| 92 | + } |
| 93 | + |
| 94 | + // Unclear if false is the correct input here. |
| 95 | + if !p1a.SigValidate(false) { |
| 96 | + return false |
| 97 | + } |
| 98 | + |
| 99 | + // Cast the public key back to p2, |
| 100 | + // so we can verify it against the p1 signature. |
| 101 | + p2a := blst.P2Affine(k) |
| 102 | + |
| 103 | + return p1a.Verify(false, &p2a, false, blst.Message(msg), DomainSeparationTag) |
| 104 | +} |
| 105 | + |
| 106 | +// TypeName returns the type name for minimized-signature BLS signatures. |
| 107 | +func (k PubKey) TypeName() string { |
| 108 | + return keyTypeName |
| 109 | +} |
| 110 | + |
| 111 | +// Signer satisfies [gcrypto.Signer] for minimized-signature BLS. |
| 112 | +type Signer struct { |
| 113 | + // The secret is a scalar, |
| 114 | + // but the blst package aliases it as SecretKey |
| 115 | + // to add a few more methods. |
| 116 | + secret blst.SecretKey |
| 117 | + |
| 118 | + // The point is the effective public key. |
| 119 | + // The point on its own is insufficient to derive the secret. |
| 120 | + point blst.P2Affine |
| 121 | +} |
| 122 | + |
| 123 | +// NewSigner returns a new signer. |
| 124 | +// The initial key material must be at least 32 bytes, |
| 125 | +// and should be cryptographically random. |
| 126 | +func NewSigner(ikm []byte) (Signer, error) { |
| 127 | + if len(ikm) < blst.BLST_SCALAR_BYTES { |
| 128 | + return Signer{}, fmt.Errorf( |
| 129 | + "ikm data too short: got %d, need at least %d", |
| 130 | + len(ikm), blst.BLST_SCALAR_BYTES, |
| 131 | + ) |
| 132 | + } |
| 133 | + salt := []byte("TODO") // Need to decide how to get the salt configurable. |
| 134 | + secretKey := blst.KeyGenV5(ikm, salt) |
| 135 | + |
| 136 | + point := new(blst.P2Affine) |
| 137 | + point = point.From(secretKey) |
| 138 | + |
| 139 | + return Signer{ |
| 140 | + secret: *secretKey, |
| 141 | + point: *point, |
| 142 | + }, nil |
| 143 | +} |
| 144 | + |
| 145 | +// PubKey returns the [PubKey] for s |
| 146 | +// (which is actually the p2 point). |
| 147 | +func (s Signer) PubKey() gcrypto.PubKey { |
| 148 | + return PubKey(s.point) |
| 149 | +} |
| 150 | + |
| 151 | +// Sign produces the signed point for the given input. |
| 152 | +// |
| 153 | +// It uses the [DomainSeparationTag], |
| 154 | +// which must be provided to verification too. |
| 155 | +// The [PubKey] type in this package is hardcoded to use the same DST. |
| 156 | +func (s Signer) Sign(_ context.Context, input []byte) ([]byte, error) { |
| 157 | + sig := new(blst.P1Affine).Sign(&s.secret, input, DomainSeparationTag, true) |
| 158 | + |
| 159 | + // sig could be nil only if option parsing failed. |
| 160 | + if sig == nil { |
| 161 | + return nil, errors.New("failed to sign") |
| 162 | + } |
| 163 | + |
| 164 | + // The signature is a new point on the p1 affine curve. |
| 165 | + return sig.Compress(), nil |
| 166 | +} |
0 commit comments