-
Notifications
You must be signed in to change notification settings - Fork 212
/
signatures.go
90 lines (74 loc) · 1.99 KB
/
signatures.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
package types
import (
"encoding/hex"
"github.com/spacemeshos/go-scale"
)
const (
EdSignatureSize = 64
VrfSignatureSize = 80
)
type EdSignature [EdSignatureSize]byte
// EmptyEdSignature is a canonical empty EdSignature.
var EmptyEdSignature EdSignature
// EncodeScale implements scale codec interface.
func (s *EdSignature) EncodeScale(encoder *scale.Encoder) (int, error) {
return scale.EncodeByteArray(encoder, s[:])
}
// DecodeScale implements scale codec interface.
func (s *EdSignature) DecodeScale(decoder *scale.Decoder) (int, error) {
return scale.DecodeByteArray(decoder, s[:])
}
// String returns a string representation of the Signature, for logging purposes.
// It implements the Stringer interface.
func (s EdSignature) String() string {
return hex.EncodeToString(s.Bytes())
}
// Bytes returns the byte representation of the Signature.
func (s *EdSignature) Bytes() []byte {
if s == nil {
return nil
}
return s[:]
}
type VrfSignature [VrfSignatureSize]byte
// EmptyVrfSignature is a canonical empty VrfSignature.
var EmptyVrfSignature VrfSignature
// String returns a string representation of the Signature, for logging purposes.
// It implements the Stringer interface.
func (s VrfSignature) String() string {
return hex.EncodeToString(s.Bytes())
}
// Bytes returns the byte representation of the Signature.
func (s *VrfSignature) Bytes() []byte {
if s == nil {
return nil
}
return s[:]
}
// Cmp compares s and x and returns:
//
// -1 if s < x
// 0 if s == x
// +1 if s > x
//
// The comparison is done in little endian order.
// Additionally, if x is nil, -1 is returned.
func (s *VrfSignature) Cmp(x *VrfSignature) int {
if x == nil {
return -1
}
// VRF signatures are little endian, so we need to compare in reverse order.
for i := len(s) - 1; i >= 0; i-- {
if s[i] < x[i] {
return -1
}
if s[i] > x[i] {
return 1
}
}
return 0
}
// LSB returns the least significant bit of the signature, so either 0 or 1.
func (s *VrfSignature) LSB() byte {
return s[0] & 1
}