-
Notifications
You must be signed in to change notification settings - Fork 212
/
vrf.go
46 lines (36 loc) · 1.23 KB
/
vrf.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
package signing
import (
"github.com/oasisprotocol/curve25519-voi/primitives/ed25519"
"github.com/oasisprotocol/curve25519-voi/primitives/ed25519/extra/ecvrf"
"github.com/spacemeshos/go-spacemesh/common/types"
)
// VRFSigner is a signer for VRF purposes.
type VRFSigner struct {
privateKey ed25519.PrivateKey
nodeID types.NodeID
}
// Sign signs a message for VRF purposes.
func (s VRFSigner) Sign(msg []byte) types.VrfSignature {
return *(*[types.VrfSignatureSize]byte)(ecvrf.Prove(s.privateKey, msg))
}
// NodeID of the signer.
func (s VRFSigner) NodeID() types.NodeID {
return s.nodeID
}
// PublicKey of the signer.
func (s VRFSigner) PublicKey() *PublicKey {
return NewPublicKey(s.nodeID.Bytes())
}
type VRFVerifier func(types.NodeID, []byte, types.VrfSignature) bool
func NewVRFVerifier() VRFVerifier {
return VRFVerify
}
// Verify verifies that a signature matches public key and message.
func (v VRFVerifier) Verify(nodeID types.NodeID, msg []byte, sig types.VrfSignature) bool {
return v(nodeID, msg, sig)
}
// VRFVerify verifies that a signature matches public key and message.
func VRFVerify(nodeID types.NodeID, msg []byte, sig types.VrfSignature) bool {
valid, _ := ecvrf.Verify(nodeID.Bytes(), sig[:], msg)
return valid
}