This repository has been archived by the owner on Aug 7, 2023. It is now read-only.
forked from decred/dcrd
/
signature.go
75 lines (60 loc) · 1.9 KB
/
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
65
66
67
68
69
70
71
72
73
74
75
// Copyright (c) 2013-2014 The btcsuite developers
// Copyright (c) 2015-2016 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package schnorr
import (
"fmt"
"math/big"
)
// Signature is a type representing a Schnorr signature.
type Signature struct {
R *big.Int
S *big.Int
}
// SignatureSize is the size of an encoded Schnorr signature.
const SignatureSize = 64
// NewSignature instantiates a new signature given some R,S values.
func NewSignature(r, s *big.Int) *Signature {
return &Signature{r, s}
}
// Serialize returns the Schnorr signature in the more strict format.
//
// The signatures are encoded as
// sig[0:32] R, a point encoded as big endian
// sig[32:64] S, scalar multiplication/addition results = (ab+c) mod l
// encoded also as big endian
func (sig Signature) Serialize() []byte {
rBytes := BigIntToEncodedBytes(sig.R)
sBytes := BigIntToEncodedBytes(sig.S)
all := append(rBytes[:], sBytes[:]...)
return all
}
func parseSig(sigStr []byte) (*Signature, error) {
if len(sigStr) != SignatureSize {
return nil, fmt.Errorf("bad signature size; have %v, want %v",
len(sigStr), SignatureSize)
}
rBytes := copyBytes(sigStr[0:32])
r := EncodedBytesToBigInt(rBytes)
sBytes := copyBytes(sigStr[32:64])
s := EncodedBytesToBigInt(sBytes)
return &Signature{r, s}, nil
}
// ParseSignature parses a signature in BER format for the curve type `curve'
// into a Signature type, perfoming some basic sanity checks.
func ParseSignature(sigStr []byte) (*Signature, error) {
return parseSig(sigStr)
}
// GetR satisfies the chainec PublicKey interface.
func (sig Signature) GetR() *big.Int {
return sig.R
}
// GetS satisfies the chainec PublicKey interface.
func (sig Signature) GetS() *big.Int {
return sig.S
}
// GetType satisfies the chainec Signature interface.
func (sig Signature) GetType() int {
return ecTypeSecSchnorr
}