-
Notifications
You must be signed in to change notification settings - Fork 670
/
public.go
72 lines (60 loc) · 2.19 KB
/
public.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package bls
import (
"errors"
blst "github.com/supranational/blst/bindings/go"
)
const PublicKeyLen = blst.BLST_P1_COMPRESS_BYTES
var (
ErrNoPublicKeys = errors.New("no public keys")
ErrFailedPublicKeyDecompress = errors.New("couldn't decompress public key")
errInvalidPublicKey = errors.New("invalid public key")
errFailedPublicKeyAggregation = errors.New("couldn't aggregate public keys")
)
type (
PublicKey = blst.P1Affine
AggregatePublicKey = blst.P1Aggregate
)
// PublicKeyToBytes returns the compressed big-endian format of the public key.
func PublicKeyToBytes(pk *PublicKey) []byte {
return pk.Compress()
}
// PublicKeyFromBytes parses the compressed big-endian format of the public key
// into a public key.
func PublicKeyFromBytes(pkBytes []byte) (*PublicKey, error) {
pk := new(PublicKey).Uncompress(pkBytes)
if pk == nil {
return nil, ErrFailedPublicKeyDecompress
}
if !pk.KeyValidate() {
return nil, errInvalidPublicKey
}
return pk, nil
}
// AggregatePublicKeys aggregates a non-zero number of public keys into a single
// aggregated public key.
// Invariant: all [pks] have been validated.
func AggregatePublicKeys(pks []*PublicKey) (*PublicKey, error) {
if len(pks) == 0 {
return nil, ErrNoPublicKeys
}
var agg AggregatePublicKey
if !agg.Aggregate(pks, false) {
return nil, errFailedPublicKeyAggregation
}
return agg.ToAffine(), nil
}
// Verify the [sig] of [msg] against the [pk].
// The [sig] and [pk] may have been an aggregation of other signatures and keys.
// Invariant: [pk] and [sig] have both been validated.
func Verify(pk *PublicKey, sig *Signature, msg []byte) bool {
return sig.Verify(false, pk, false, msg, ciphersuiteSignature)
}
// Verify the possession of the secret pre-image of [sk] by verifying a [sig] of
// [msg] against the [pk].
// The [sig] and [pk] may have been an aggregation of other signatures and keys.
// Invariant: [pk] and [sig] have both been validated.
func VerifyProofOfPossession(pk *PublicKey, sig *Signature, msg []byte) bool {
return sig.Verify(false, pk, false, msg, ciphersuiteProofOfPossession)
}