-
Notifications
You must be signed in to change notification settings - Fork 12
/
schnorr_signature.go
64 lines (54 loc) · 2.25 KB
/
schnorr_signature.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
package secp256k1
import (
"encoding/hex"
"github.com/pkg/errors"
)
const (
// SerializedSchnorrSignatureSize defines the length in bytes of SerializedSchnorrSignature
SerializedSchnorrSignatureSize = 64
)
// SchnorrSignature is a type representing a Schnorr Signature.
// The struct itself is an opaque data type that should only be created via the supplied methods.
type SchnorrSignature struct {
signature [SerializedSchnorrSignatureSize]byte
}
// SerializedSchnorrSignature is a is a byte array representing the storage representation of a SchnorrSignature
type SerializedSchnorrSignature [SerializedSchnorrSignatureSize]byte
// IsEqual returns true if target is the same as signature.
func (signature *SchnorrSignature) IsEqual(target *SchnorrSignature) bool {
if signature == nil && target == nil {
return true
}
if signature == nil || target == nil {
return false
}
return *signature.Serialize() == *target.Serialize()
}
// String returns the SerializedSchnorrSignature as the hexadecimal string
func (serialized SerializedSchnorrSignature) String() string {
return hex.EncodeToString(serialized[:])
}
// String returns the SchnorrSignature as the hexadecimal string
func (signature SchnorrSignature) String() string {
return signature.Serialize().String()
}
// Serialize returns a 64 byte serialized signature
func (signature *SchnorrSignature) Serialize() *SerializedSchnorrSignature {
ret := SerializedSchnorrSignature(signature.signature)
return &ret
}
// DeserializeSchnorrSignature deserializes a 64 byte serialized schnorr signature into a SchnorrSignature type.
func DeserializeSchnorrSignature(serializedSignature *SerializedSchnorrSignature) *SchnorrSignature {
return &SchnorrSignature{signature: *serializedSignature}
}
// DeserializeSchnorrSignatureFromSlice returns a SchnorrSignature type from a serialized signature slice.
// will verify that it's SerializedSchnorrSignatureSize bytes long
func DeserializeSchnorrSignatureFromSlice(data []byte) (signature *SchnorrSignature, err error) {
if len(data) != SerializedSchnorrSignatureSize {
return nil, errors.Errorf("invalid schnorr signature length got %d, expected %d", len(data),
SerializedSchnorrSignatureSize)
}
signature = &SchnorrSignature{}
copy(signature.signature[:], data)
return
}