forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
proof_of_possession.go
111 lines (93 loc) · 2.64 KB
/
proof_of_possession.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package signer
import (
"encoding/json"
"errors"
"github.com/MetalBlockchain/metalgo/utils/crypto/bls"
"github.com/MetalBlockchain/metalgo/utils/formatting"
)
var (
_ Signer = (*ProofOfPossession)(nil)
errInvalidProofOfPossession = errors.New("invalid proof of possession")
)
type ProofOfPossession struct {
PublicKey [bls.PublicKeyLen]byte `serialize:"true" json:"publicKey"`
// BLS signature proving ownership of [PublicKey]. The signed message is the
// [PublicKey].
ProofOfPossession [bls.SignatureLen]byte `serialize:"true" json:"proofOfPossession"`
// publicKey is the parsed version of [PublicKey]. It is populated in
// [Verify].
publicKey *bls.PublicKey
}
func NewProofOfPossession(sk *bls.SecretKey) *ProofOfPossession {
pk := bls.PublicFromSecretKey(sk)
pkBytes := bls.PublicKeyToBytes(pk)
sig := bls.SignProofOfPossession(sk, pkBytes)
sigBytes := bls.SignatureToBytes(sig)
pop := &ProofOfPossession{
publicKey: pk,
}
copy(pop.PublicKey[:], pkBytes)
copy(pop.ProofOfPossession[:], sigBytes)
return pop
}
func (p *ProofOfPossession) Verify() error {
publicKey, err := bls.PublicKeyFromBytes(p.PublicKey[:])
if err != nil {
return err
}
signature, err := bls.SignatureFromBytes(p.ProofOfPossession[:])
if err != nil {
return err
}
if !bls.VerifyProofOfPossession(publicKey, signature, p.PublicKey[:]) {
return errInvalidProofOfPossession
}
p.publicKey = publicKey
return nil
}
func (p *ProofOfPossession) Key() *bls.PublicKey {
return p.publicKey
}
type jsonProofOfPossession struct {
PublicKey string `json:"publicKey"`
ProofOfPossession string `json:"proofOfPossession"`
}
func (p *ProofOfPossession) MarshalJSON() ([]byte, error) {
pk, err := formatting.Encode(formatting.HexNC, p.PublicKey[:])
if err != nil {
return nil, err
}
pop, err := formatting.Encode(formatting.HexNC, p.ProofOfPossession[:])
if err != nil {
return nil, err
}
return json.Marshal(jsonProofOfPossession{
PublicKey: pk,
ProofOfPossession: pop,
})
}
func (p *ProofOfPossession) UnmarshalJSON(b []byte) error {
jsonBLS := jsonProofOfPossession{}
err := json.Unmarshal(b, &jsonBLS)
if err != nil {
return err
}
pkBytes, err := formatting.Decode(formatting.HexNC, jsonBLS.PublicKey)
if err != nil {
return err
}
pk, err := bls.PublicKeyFromBytes(pkBytes)
if err != nil {
return err
}
popBytes, err := formatting.Decode(formatting.HexNC, jsonBLS.ProofOfPossession)
if err != nil {
return err
}
copy(p.PublicKey[:], pkBytes)
copy(p.ProofOfPossession[:], popBytes)
p.publicKey = pk
return nil
}