/
falconWrapper.go
67 lines (55 loc) · 2.43 KB
/
falconWrapper.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
package stateproofcrypto
import (
cfalcon "github.com/algorand/falcon"
)
const (
// FalconSeedSize Represents the size in bytes of the random bytes used to generate Falcon keys
FalconSeedSize = 48
// FalconMaxSignatureSize Represents the max possible size in bytes of a falcon signature
FalconMaxSignatureSize = cfalcon.CTSignatureSize
)
type (
// FalconPublicKey is a wrapper for cfalcon.PublicKeySizey (used for packing)
FalconPublicKey [cfalcon.PublicKeySize]byte
// FalconPrivateKey is a wrapper for cfalcon.PrivateKeySize (used for packing)
FalconPrivateKey [cfalcon.PrivateKeySize]byte
// FalconSeed represents the seed which is being used to generate Falcon keys
FalconSeed [FalconSeedSize]byte
// FalconSignature represents a Falcon signature in a compressed-form
//msgp:allocbound FalconSignature FalconMaxSignatureSize
FalconSignature []byte
)
// FalconVerifier implements the type Verifier interface for the falcon signature scheme.
type FalconVerifier struct {
_struct struct{} `codec:",omitempty,omitemptyarray"`
PublicKey FalconPublicKey `codec:"k"`
}
// MsgIsZero returns whether this is a zero value
func (d *FalconVerifier) MsgIsZero() bool {
return ((*d).PublicKey == (FalconPublicKey{}))
}
// VerifyBytes follows falcon algorithm to verify a signature.
func (d *FalconVerifier) VerifyBytes(data []byte, sig FalconSignature) error {
// The wrapper, currently, support only the compress form signature. so we can
// assume that the signature given is in a compress form
falconSig := cfalcon.CompressedSignature(sig)
return (*cfalcon.PublicKey)(&d.PublicKey).Verify(falconSig, data)
}
// GetFixedLengthHashableRepresentation is used to fetch a plain serialized version of the public data (without the use of the msgpack).
func (d *FalconVerifier) GetFixedLengthHashableRepresentation() []byte {
return d.PublicKey[:]
}
// MsgIsZero returns whether this is a zero value
func (s FalconSignature) MsgIsZero() bool {
return len(s) == 0
}
// GetFixedLengthHashableRepresentation returns a serialized version of the signature
func (s FalconSignature) GetFixedLengthHashableRepresentation() ([]byte, error) {
compressedSignature := cfalcon.CompressedSignature(s)
ctSignature, err := compressedSignature.ConvertToCT()
return ctSignature[:], err
}
// IsSaltVersionEqual of the signature matches the given version
func (s FalconSignature) IsSaltVersionEqual(version byte) bool {
return (*cfalcon.CompressedSignature)(&s).SaltVersion() == version
}