forked from taurushq-io/multi-party-sig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
edwards25519.go
227 lines (181 loc) · 4.83 KB
/
edwards25519.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package curve
import (
"errors"
"fmt"
"math/big"
"filippo.io/edwards25519"
"github.com/cronokirby/saferith"
)
var (
// l = 2 ^ 252 + 27742317777372353535851937790883648493
b, _ = new(big.Int).SetString("27742317777372353535851937790883648493", 10)
l = new(big.Int).Add(b, new(big.Int).Exp(big.NewInt(2), big.NewInt(252), nil))
edwards25519Order = saferith.ModulusFromNat(new(saferith.Nat).SetBig(l, 512))
)
type Edwards25519 struct{}
func (Edwards25519) NewPoint() Point {
return &Edwards25519Point{*edwards25519.NewIdentityPoint()}
}
func (Edwards25519) NewBasePoint() Point {
out := new(Edwards25519Point)
out.value = *edwards25519.NewGeneratorPoint()
return out
}
func (Edwards25519) NewScalar() Scalar {
return &Edwards25519Scalar{*edwards25519.NewScalar()}
}
func (Edwards25519) ScalarBits() int {
return 256
}
func (Edwards25519) SafeScalarBytes() int {
return 64
}
func (Edwards25519) Order() *saferith.Modulus {
return edwards25519Order
}
func (Edwards25519) Name() string {
return "edwards25519"
}
type Edwards25519Scalar struct {
value edwards25519.Scalar
}
func edwards25519CastScalar(generic Scalar) *Edwards25519Scalar {
return generic.(*Edwards25519Scalar)
}
func (*Edwards25519Scalar) Curve() Curve {
return Edwards25519{}
}
func (s *Edwards25519Scalar) MarshalBinary() ([]byte, error) {
data := s.value.Bytes()
return data[:], nil
}
func (s *Edwards25519Scalar) UnmarshalBinary(data []byte) error {
if len(data) != 32 {
return fmt.Errorf("invalid length for edwards25519 scalar: %d", len(data))
}
value, err := s.value.SetCanonicalBytes(data)
if err != nil {
return errors.New("invalid bytes for edwards25519 scalar")
}
s.value = *value
return nil
}
func (s *Edwards25519Scalar) Add(that Scalar) Scalar {
other := edwards25519CastScalar(that)
s.value.Add(&s.value, &other.value)
return s
}
func (s *Edwards25519Scalar) Sub(that Scalar) Scalar {
other := edwards25519CastScalar(that)
s.value.Subtract(&s.value, &other.value)
return s
}
func (s *Edwards25519Scalar) Mul(that Scalar) Scalar {
other := edwards25519CastScalar(that)
s.value.Multiply(&s.value, &other.value)
return s
}
func (s *Edwards25519Scalar) Invert() Scalar {
s.value.Invert(&s.value)
return s
}
func (s *Edwards25519Scalar) Negate() Scalar {
s.value.Negate(&s.value)
return s
}
func (s *Edwards25519Scalar) Equal(that Scalar) bool {
other := edwards25519CastScalar(that)
return s.value.Equal(&other.value) == 1
}
func (s *Edwards25519Scalar) IsZero() bool {
return s.value.Equal(edwards25519.NewScalar()) == 1
}
func (s *Edwards25519Scalar) Set(that Scalar) Scalar {
other := edwards25519CastScalar(that)
s.value.Set(&other.value)
return s
}
func (s *Edwards25519Scalar) SetNat(x *saferith.Nat) Scalar {
buf := make([]byte, 64)
buf = x.FillBytes(buf)
if len(buf) != 64 {
panic(len(buf))
}
s.value.SetUniformBytes(buf)
return s
}
func (s *Edwards25519Scalar) Act(that Point) Point {
other := edwards25519CastPoint(that)
out := new(Edwards25519Point)
out.value.ScalarMult(&s.value, &other.value)
return out
}
func (s *Edwards25519Scalar) ActOnBase() Point {
out := new(Edwards25519Point)
out.value.ScalarBaseMult(&s.value)
return out
}
func (s *Edwards25519Scalar) Bytes() []byte {
b, err := s.MarshalBinary()
if err != nil {
panic(err)
}
return b
}
type Edwards25519Point struct {
value edwards25519.Point
}
func edwards25519CastPoint(generic Point) *Edwards25519Point {
return generic.(*Edwards25519Point)
}
func (*Edwards25519Point) Curve() Curve {
return Edwards25519{}
}
func (p *Edwards25519Point) MarshalBinary() ([]byte, error) {
return p.value.Bytes(), nil
}
func (p *Edwards25519Point) UnmarshalBinary(data []byte) error {
if len(data) != 32 {
return fmt.Errorf("invalid edwards25519 point length: %d", len(data))
}
_, err := p.value.SetBytes(data)
return err
}
func (p *Edwards25519Point) Add(that Point) Point {
other := edwards25519CastPoint(that)
out := new(Edwards25519Point)
out.value.Add(&p.value, &other.value)
return out
}
func (p *Edwards25519Point) Sub(that Point) Point {
other := edwards25519CastPoint(that)
out := new(Edwards25519Point)
out.value.Subtract(&p.value, &other.value)
return out
}
func (p *Edwards25519Point) Set(that Point) Point {
other := edwards25519CastPoint(that)
p.value.Set(&other.value)
return p
}
func (p *Edwards25519Point) Negate() Point {
out := new(Edwards25519Point)
out.value.Negate(&p.value)
return out
}
func (p *Edwards25519Point) Equal(that Point) bool {
other := edwards25519CastPoint(that)
return p.value.Equal(&other.value) == 1
}
func (p *Edwards25519Point) IsIdentity() bool {
return p.value.Equal(edwards25519.NewIdentityPoint()) == 1
}
func (p *Edwards25519Point) HasEvenY() bool {
return false
}
func (p *Edwards25519Point) XScalar() Scalar {
return nil
}
func (p *Edwards25519Point) YScalar() Scalar {
return nil
}