forked from rot256/pblind
-
Notifications
You must be signed in to change notification settings - Fork 1
/
signer.go
110 lines (81 loc) · 2.09 KB
/
signer.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
package pblind
// https://link.springer.com/content/pdf/10.1007/3-540-44598-6_17.pdf
import (
"crypto/elliptic"
"crypto/rand"
"math/big"
)
const (
stateSignerFresh = iota
stateSignerMsg1Created
stateSignerMsg2Processed
stateSignerMsg3Created
)
type StateSigner struct {
state int
info Info // shared info for exchange
curve elliptic.Curve // domain
sk SecretKey // secret key
u *big.Int // scalar
s *big.Int // scalar
d *big.Int // scalar
e *big.Int // scalar
}
func CreateSigner(sk SecretKey, info Info) (*StateSigner, error) {
st := StateSigner{
state: stateSignerFresh,
sk: sk,
curve: sk.curve,
info: info,
}
order := st.curve.Params().N
var err error
if st.u, err = rand.Int(rand.Reader, order); err != nil {
return nil, err
}
if st.s, err = rand.Int(rand.Reader, order); err != nil {
return nil, err
}
if st.d, err = rand.Int(rand.Reader, order); err != nil {
return nil, err
}
return &st, nil
}
func (st *StateSigner) CreateMessage1() (Message1, error) {
var msg Message1
if st.state != stateSignerFresh {
return msg, ErrorInvalidSignerState
}
t1x, t1y := st.curve.ScalarMult(st.info.x, st.info.y, st.d.Bytes())
t2x, t2y := st.curve.ScalarBaseMult(st.s.Bytes())
msg.Ax, msg.Ay = st.curve.ScalarBaseMult(st.u.Bytes())
msg.Bx, msg.By = st.curve.Add(t1x, t1y, t2x, t2y)
st.state = stateSignerMsg1Created
return msg, nil
}
func (st *StateSigner) ProcessMessage2(msg Message2) error {
if st.state != stateSignerMsg1Created {
return ErrorInvalidSignerState
}
if isScalarBad(st.curve.Params(), msg.E) {
return ErrorInvalidScalar
}
st.e = msg.E
st.state = stateSignerMsg2Processed
return nil
}
func (st *StateSigner) CreateMessage3() (Message3, error) {
if st.state != stateSignerMsg2Processed {
return Message3{}, ErrorInvalidSignerState
}
params := st.curve.Params()
c := big.NewInt(0)
c.Sub(st.e, st.d)
c.Mod(c, params.N)
r := big.NewInt(0)
r.Mul(c, st.sk.scalar)
r.Sub(st.u, r)
r.Mod(r, params.N)
st.state = stateSignerMsg3Created
return Message3{R: r, C: c, S: st.s}, nil
}