-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsign.go
133 lines (111 loc) · 2.56 KB
/
sign.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package digisig
import (
"crypto/rand"
"math/big"
curve "github.com/dece2183/go-digisig/ellipticCurve"
"github.com/dece2183/go-stribog"
)
type Signature struct {
// external parameters
key *big.Int // (d)
p, a, q *big.Int
_P curve.Point
hashFunc *stribog.Stribog
// internal variables
hashSize int
curve *curve.Curve
}
func NewSignature(privateKey, p, a, q *big.Int, P curve.Point, hashFunc *stribog.Stribog) *Signature {
s := &Signature{
key: privateKey,
p: p,
a: a,
q: q,
_P: P,
hashFunc: hashFunc,
hashSize: hashFunc.Size(),
curve: curve.NewCurve(p, a),
}
return s
}
// GenerateKey generates the validation key based on the parameters passed in NewSignature.
//
// Generated key should be provided to the Validator.
func (s *Signature) GenerateKey() curve.Point {
return s.curve.Scalar(s.key, s._P)
}
// Sign generates the digital signature for the provided message.
//
// This function returns only signature without message.
func (s *Signature) Sign(msg []byte) ([]byte, error) {
hs := new(big.Int).SetBytes(s.hashFunc.CheckSum(msg))
var e, k, r, _s *big.Int
var c curve.Point
var err error
e = s.calcE(hs)
recalculate:
k, err = s.randK()
if k == nil {
return nil, err
}
c = s.genC(k)
r = s.calcR(c)
if r == nil {
goto recalculate
}
_s = s.calcS(e, k, r)
if _s == nil {
goto recalculate
}
return append(s.completion(r), s.completion(_s)...), nil
}
func (s *Signature) calcE(hs *big.Int) *big.Int {
e := hs.Mod(hs, s.q)
if e.Cmp(big.NewInt(0)) == 0 {
return big.NewInt(1)
}
return e
}
func (s *Signature) randK() (*big.Int, error) {
var err error
k := big.NewInt(1)
max := big.NewInt(1).Lsh(k, uint(len(s.q.Bytes()))*8)
for {
k, err = rand.Int(rand.Reader, max)
if err != nil {
return nil, err
}
if k.Cmp(s.q) < 0 {
return k, nil
}
}
}
func (s *Signature) genC(k *big.Int) curve.Point {
return s.curve.Scalar(k, s._P)
}
func (s *Signature) calcR(c curve.Point) *big.Int {
// c.X % s.q
r := new(big.Int).Mod(c.X, s.q)
if r.Cmp(big.NewInt(0)) == 0 || len(r.Bytes()) > s.hashSize {
return nil
}
return r
}
func (s *Signature) calcS(e, k, r *big.Int) *big.Int {
// _s = (r * s.key + k * e) % s.q;
_s := new(big.Int).Mul(r, s.key)
_s = _s.Add(_s, new(big.Int).Mul(k, e))
_s = _s.Mod(_s, s.q)
if _s.Cmp(big.NewInt(0)) == 0 || len(_s.Bytes()) > s.hashSize {
return nil
}
return _s
}
func (s *Signature) completion(num *big.Int) []byte {
expectedLen := s.hashSize
b := num.Bytes()
for len(b) < expectedLen {
b = append([]byte{0}, b...)
}
return b
}