| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
package ristretto255 |
| 14 |
|
| 15 |
import ( |
| 16 |
"bytes" |
| 17 |
"encoding/base64" |
| 18 |
"errors" |
| 19 |
|
| 20 |
"filippo.io/edwards25519" |
| 21 |
"filippo.io/edwards25519/field" |
| 22 |
) |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
var ( |
| 27 |
d, _ = new(field.Element).SetBytes([]byte{ |
| 28 |
0xa3, 0x78, 0x59, 0x13, 0xca, 0x4d, 0xeb, 0x75, |
| 29 |
0xab, 0xd8, 0x41, 0x41, 0x4d, 0x0a, 0x70, 0x00, |
| 30 |
0x98, 0xe8, 0x79, 0x77, 0x79, 0x40, 0xc7, 0x8c, |
| 31 |
0x73, 0xfe, 0x6f, 0x2b, 0xee, 0x6c, 0x03, 0x52, |
| 32 |
}) |
| 33 |
sqrtM1, _ = new(field.Element).SetBytes([]byte{ |
| 34 |
0xb0, 0xa0, 0x0e, 0x4a, 0x27, 0x1b, 0xee, 0xc4, |
| 35 |
0x78, 0xe4, 0x2f, 0xad, 0x06, 0x18, 0x43, 0x2f, |
| 36 |
0xa7, 0xd7, 0xfb, 0x3d, 0x99, 0x00, 0x4d, 0x2b, |
| 37 |
0x0b, 0xdf, 0xc1, 0x4f, 0x80, 0x24, 0x83, 0x2b, |
| 38 |
}) |
| 39 |
sqrtADMinusOne, _ = new(field.Element).SetBytes([]byte{ |
| 40 |
0x1b, 0x2e, 0x7b, 0x49, 0xa0, 0xf6, 0x97, 0x7e, |
| 41 |
0xbd, 0x54, 0x78, 0x1b, 0x0c, 0x8e, 0x9d, 0xaf, |
| 42 |
0xfd, 0xd1, 0xf5, 0x31, 0xc9, 0xfc, 0x3c, 0x0f, |
| 43 |
0xac, 0x48, 0x83, 0x2b, 0xbf, 0x31, 0x69, 0x37, |
| 44 |
}) |
| 45 |
invSqrtAMinusD, _ = new(field.Element).SetBytes([]byte{ |
| 46 |
0xea, 0x40, 0x5d, 0x80, 0xaa, 0xfd, 0xc8, 0x99, |
| 47 |
0xbe, 0x72, 0x41, 0x5a, 0x17, 0x16, 0x2f, 0x9d, |
| 48 |
0x40, 0xd8, 0x01, 0xfe, 0x91, 0x7b, 0xc2, 0x16, |
| 49 |
0xa2, 0xfc, 0xaf, 0xcf, 0x05, 0x89, 0x6c, 0x78, |
| 50 |
}) |
| 51 |
oneMinusDSQ, _ = new(field.Element).SetBytes([]byte{ |
| 52 |
0x76, 0xc1, 0x5f, 0x94, 0xc1, 0x09, 0x7c, 0xe2, |
| 53 |
0x0f, 0x35, 0x5e, 0xcd, 0x38, 0xa1, 0x81, 0x2c, |
| 54 |
0xe4, 0xdf, 0x70, 0xbe, 0xdd, 0xab, 0x94, 0x99, |
| 55 |
0xd7, 0xe0, 0xb3, 0xb2, 0xa8, 0x72, 0x90, 0x02, |
| 56 |
}) |
| 57 |
dMinusOneSQ, _ = new(field.Element).SetBytes([]byte{ |
| 58 |
0x20, 0x4d, 0xed, 0x44, 0xaa, 0x5a, 0xad, 0x31, |
| 59 |
0x99, 0x19, 0x1e, 0xb0, 0x2c, 0x4a, 0x9e, 0xd2, |
| 60 |
0xeb, 0x4e, 0x9b, 0x52, 0x2f, 0xd3, 0xdc, 0x4c, |
| 61 |
0x41, 0x22, 0x6c, 0xf6, 0x7a, 0xb3, 0x68, 0x59, |
| 62 |
}) |
| 63 |
) |
| 64 |
|
| 65 |
var ( |
| 66 |
zero = new(field.Element) |
| 67 |
one = new(field.Element).One() |
| 68 |
two = new(field.Element).Add(one, one) |
| 69 |
minusOne = new(field.Element).Subtract(zero, one) |
| 70 |
) |
| 71 |
|
| 72 |
|
| 73 |
type Element struct { |
| 74 |
r edwards25519.Point |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
func NewElement() *Element { |
| 81 |
return NewIdentityElement() |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
func NewIdentityElement() *Element { |
| 86 |
e := &Element{} |
| 87 |
e.r.Set(edwards25519.NewIdentityPoint()) |
| 88 |
return e |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
func NewGeneratorElement() *Element { |
| 93 |
e := &Element{} |
| 94 |
e.r.Set(edwards25519.NewGeneratorPoint()) |
| 95 |
return e |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
func (e *Element) Set(x *Element) *Element { |
| 100 |
*e = *x |
| 101 |
return e |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
func (e *Element) Equal(ee *Element) int { |
| 108 |
X1, Y1, _, _ := e.r.ExtendedCoordinates() |
| 109 |
X2, Y2, _, _ := ee.r.ExtendedCoordinates() |
| 110 |
|
| 111 |
var f0, f1 field.Element |
| 112 |
|
| 113 |
f0.Multiply(X1, Y2) |
| 114 |
f1.Multiply(Y1, X2) |
| 115 |
out := f0.Equal(&f1) |
| 116 |
|
| 117 |
f0.Multiply(Y1, Y2) |
| 118 |
f1.Multiply(X1, X2) |
| 119 |
out = out | f0.Equal(&f1) |
| 120 |
|
| 121 |
return out |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
func (e *Element) FromUniformBytes(b []byte) *Element { |
| 130 |
if _, err := e.SetUniformBytes(b); err != nil { |
| 131 |
panic(err.Error()) |
| 132 |
} |
| 133 |
return e |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
func (e *Element) SetUniformBytes(b []byte) (*Element, error) { |
| 144 |
if len(b) != 64 { |
| 145 |
return nil, errors.New("ristretto255: SetUniformBytes input is not 64 bytes long") |
| 146 |
} |
| 147 |
|
| 148 |
f := &field.Element{} |
| 149 |
|
| 150 |
f.SetBytes(b[:32]) |
| 151 |
point1 := &Element{} |
| 152 |
mapToPoint(&point1.r, f) |
| 153 |
|
| 154 |
f.SetBytes(b[32:]) |
| 155 |
point2 := &Element{} |
| 156 |
mapToPoint(&point2.r, f) |
| 157 |
|
| 158 |
return e.Add(point1, point2), nil |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
func mapToPoint(out *edwards25519.Point, t *field.Element) { |
| 163 |
|
| 164 |
r := &field.Element{} |
| 165 |
r.Multiply(sqrtM1, r.Square(t)) |
| 166 |
|
| 167 |
|
| 168 |
u := &field.Element{} |
| 169 |
u.Multiply(u.Add(r, one), oneMinusDSQ) |
| 170 |
|
| 171 |
|
| 172 |
c := &field.Element{} |
| 173 |
c.Set(minusOne) |
| 174 |
|
| 175 |
|
| 176 |
rPlusD := &field.Element{} |
| 177 |
rPlusD.Add(r, d) |
| 178 |
v := &field.Element{} |
| 179 |
v.Multiply(v.Subtract(c, v.Multiply(r, d)), rPlusD) |
| 180 |
|
| 181 |
|
| 182 |
s := &field.Element{} |
| 183 |
_, wasSquare := s.SqrtRatio(u, v) |
| 184 |
|
| 185 |
|
| 186 |
sPrime := &field.Element{} |
| 187 |
sPrime.Negate(sPrime.Absolute(sPrime.Multiply(s, t))) |
| 188 |
|
| 189 |
|
| 190 |
s.Select(s, sPrime, wasSquare) |
| 191 |
|
| 192 |
c.Select(c, r, wasSquare) |
| 193 |
|
| 194 |
|
| 195 |
N := &field.Element{} |
| 196 |
N.Multiply(c, N.Subtract(r, one)) |
| 197 |
N.Subtract(N.Multiply(N, dMinusOneSQ), v) |
| 198 |
|
| 199 |
s2 := &field.Element{} |
| 200 |
s2.Square(s) |
| 201 |
|
| 202 |
|
| 203 |
w0 := &field.Element{} |
| 204 |
w0.Add(w0, w0.Multiply(s, v)) |
| 205 |
|
| 206 |
w1 := &field.Element{} |
| 207 |
w1.Multiply(N, sqrtADMinusOne) |
| 208 |
|
| 209 |
w2 := &field.Element{} |
| 210 |
w2.Subtract(one, s2) |
| 211 |
|
| 212 |
w3 := &field.Element{} |
| 213 |
w3.Add(one, s2) |
| 214 |
|
| 215 |
|
| 216 |
var X, Y, Z, T field.Element |
| 217 |
X.Multiply(w0, w3) |
| 218 |
Y.Multiply(w2, w1) |
| 219 |
Z.Multiply(w1, w3) |
| 220 |
T.Multiply(w0, w2) |
| 221 |
if _, err := out.SetExtendedCoordinates(&X, &Y, &Z, &T); err != nil { |
| 222 |
panic("ristretto255: internal error: MAP generated invalid coordinates") |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
func (e *Element) Encode(b []byte) []byte { |
| 231 |
ret, out := sliceForAppend(b, 32) |
| 232 |
e.bytes(out) |
| 233 |
return ret |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
func sliceForAppend(in []byte, n int) (head, tail []byte) { |
| 241 |
if total := len(in) + n; cap(in) >= total { |
| 242 |
head = in[:total] |
| 243 |
} else { |
| 244 |
head = make([]byte, total) |
| 245 |
copy(head, in) |
| 246 |
} |
| 247 |
tail = head[len(in):] |
| 248 |
return |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
func (e *Element) Bytes() []byte { |
| 255 |
|
| 256 |
b := make([]byte, 32) |
| 257 |
return e.bytes(b) |
| 258 |
} |
| 259 |
|
| 260 |
func (e *Element) bytes(b []byte) []byte { |
| 261 |
X, Y, Z, T := e.r.ExtendedCoordinates() |
| 262 |
tmp := &field.Element{} |
| 263 |
|
| 264 |
|
| 265 |
u1 := &field.Element{} |
| 266 |
u1.Add(Z, Y).Multiply(u1, tmp.Subtract(Z, Y)) |
| 267 |
|
| 268 |
|
| 269 |
u2 := &field.Element{} |
| 270 |
u2.Multiply(X, Y) |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
invSqrt := &field.Element{} |
| 275 |
invSqrt.SqrtRatio(one, tmp.Square(u2).Multiply(tmp, u1)) |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
den1, den2 := &field.Element{}, &field.Element{} |
| 280 |
den1.Multiply(invSqrt, u1) |
| 281 |
den2.Multiply(invSqrt, u2) |
| 282 |
|
| 283 |
zInv := &field.Element{} |
| 284 |
zInv.Multiply(den1, den2).Multiply(zInv, T) |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
ix0, iy0 := &field.Element{}, &field.Element{} |
| 289 |
ix0.Multiply(X, sqrtM1) |
| 290 |
iy0.Multiply(Y, sqrtM1) |
| 291 |
|
| 292 |
enchantedDenominator := &field.Element{} |
| 293 |
enchantedDenominator.Multiply(den1, invSqrtAMinusD) |
| 294 |
|
| 295 |
|
| 296 |
rotate := tmp.Multiply(T, zInv).IsNegative() |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
x, y := &field.Element{}, &field.Element{} |
| 301 |
x.Select(iy0, X, rotate) |
| 302 |
y.Select(ix0, Y, rotate) |
| 303 |
|
| 304 |
z := Z |
| 305 |
|
| 306 |
denInv := &field.Element{} |
| 307 |
denInv.Select(enchantedDenominator, den2, rotate) |
| 308 |
|
| 309 |
|
| 310 |
isNegative := tmp.Multiply(x, zInv).IsNegative() |
| 311 |
y.Select(tmp.Negate(y), y, isNegative) |
| 312 |
|
| 313 |
|
| 314 |
s := tmp.Subtract(z, y).Multiply(tmp, denInv).Absolute(tmp) |
| 315 |
|
| 316 |
|
| 317 |
copy(b, s.Bytes()) |
| 318 |
return b |
| 319 |
} |
| 320 |
|
| 321 |
var errInvalidEncoding = errors.New("ristretto255: invalid element encoding") |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
func (e *Element) Decode(in []byte) error { |
| 328 |
_, err := e.SetCanonicalBytes(in) |
| 329 |
return err |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
func (e *Element) SetCanonicalBytes(in []byte) (*Element, error) { |
| 338 |
if len(in) != 32 { |
| 339 |
return nil, errInvalidEncoding |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
s := &field.Element{} |
| 344 |
s.SetBytes(in) |
| 345 |
|
| 346 |
|
| 347 |
if !bytes.Equal(s.Bytes(), in) { |
| 348 |
return nil, errInvalidEncoding |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
if s.IsNegative() == 1 { |
| 353 |
return nil, errInvalidEncoding |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
sSqr := &field.Element{} |
| 358 |
sSqr.Square(s) |
| 359 |
|
| 360 |
|
| 361 |
u1 := &field.Element{} |
| 362 |
u1.Subtract(one, sSqr) |
| 363 |
|
| 364 |
|
| 365 |
u2 := &field.Element{} |
| 366 |
u2.Add(one, sSqr) |
| 367 |
|
| 368 |
|
| 369 |
u2Sqr := &field.Element{} |
| 370 |
u2Sqr.Square(u2) |
| 371 |
|
| 372 |
|
| 373 |
v := &field.Element{} |
| 374 |
v.Square(u1).Multiply(v, d).Negate(v).Subtract(v, u2Sqr) |
| 375 |
|
| 376 |
|
| 377 |
invSqrt, tmp := &field.Element{}, &field.Element{} |
| 378 |
_, wasSquare := invSqrt.SqrtRatio(one, tmp.Multiply(v, u2Sqr)) |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
denX, denY := &field.Element{}, &field.Element{} |
| 383 |
denX.Multiply(invSqrt, u2) |
| 384 |
denY.Multiply(invSqrt, denX).Multiply(denY, v) |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
var X, Y, Z, T field.Element |
| 390 |
X.Multiply(two, s).Multiply(&X, denX).Absolute(&X) |
| 391 |
Y.Multiply(u1, denY) |
| 392 |
Z.One() |
| 393 |
T.Multiply(&X, &Y) |
| 394 |
|
| 395 |
|
| 396 |
if wasSquare == 0 || T.IsNegative() == 1 || Y.Equal(zero) == 1 { |
| 397 |
return nil, errInvalidEncoding |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
if _, err := e.r.SetExtendedCoordinates(&X, &Y, &Z, &T); err != nil { |
| 402 |
panic("ristretto255: internal error: DECODE generated invalid coordinates") |
| 403 |
} |
| 404 |
return e, nil |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
func (e *Element) ScalarBaseMult(s *Scalar) *Element { |
| 409 |
e.r.ScalarBaseMult(&s.s) |
| 410 |
return e |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
func (e *Element) ScalarMult(s *Scalar, p *Element) *Element { |
| 415 |
e.r.ScalarMult(&s.s, &p.r) |
| 416 |
return e |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
func (e *Element) MultiScalarMult(s []*Scalar, p []*Element) *Element { |
| 423 |
if len(p) != len(s) { |
| 424 |
panic("ristretto255: MultiScalarMult invoked with mismatched slice lengths") |
| 425 |
} |
| 426 |
points := make([]*edwards25519.Point, len(p)) |
| 427 |
scalars := make([]*edwards25519.Scalar, len(s)) |
| 428 |
for i := range s { |
| 429 |
points[i] = &p[i].r |
| 430 |
scalars[i] = &s[i].s |
| 431 |
} |
| 432 |
e.r.MultiScalarMult(scalars, points) |
| 433 |
return e |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
func (e *Element) VarTimeMultiScalarMult(s []*Scalar, p []*Element) *Element { |
| 440 |
if len(p) != len(s) { |
| 441 |
panic("ristretto255: VarTimeMultiScalarMult invoked with mismatched slice lengths") |
| 442 |
} |
| 443 |
points := make([]*edwards25519.Point, len(p)) |
| 444 |
scalars := make([]*edwards25519.Scalar, len(s)) |
| 445 |
for i := range s { |
| 446 |
points[i] = &p[i].r |
| 447 |
scalars[i] = &s[i].s |
| 448 |
} |
| 449 |
e.r.VarTimeMultiScalarMult(scalars, points) |
| 450 |
return e |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
func (e *Element) VarTimeDoubleScalarBaseMult(a *Scalar, A *Element, b *Scalar) *Element { |
| 458 |
e.r.VarTimeDoubleScalarBaseMult(&a.s, &A.r, &b.s) |
| 459 |
return e |
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
func (e *Element) Add(p, q *Element) *Element { |
| 464 |
e.r.Add(&p.r, &q.r) |
| 465 |
return e |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
func (e *Element) Subtract(p, q *Element) *Element { |
| 470 |
e.r.Subtract(&p.r, &q.r) |
| 471 |
return e |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
func (e *Element) Negate(p *Element) *Element { |
| 476 |
e.r.Negate(&p.r) |
| 477 |
return e |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
func (e *Element) Zero() *Element { |
| 484 |
return e.Set(NewIdentityElement()) |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
func (e *Element) Base() *Element { |
| 491 |
return e.Set(NewGeneratorElement()) |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
func (e *Element) MarshalText() (text []byte, err error) { |
| 496 |
b := e.Encode([]byte{}) |
| 497 |
return []byte(base64.StdEncoding.EncodeToString(b)), nil |
| 498 |
} |
| 499 |
|
| 500 |
|
| 501 |
func (e *Element) UnmarshalText(text []byte) error { |
| 502 |
eb, err := base64.StdEncoding.DecodeString(string(text)) |
| 503 |
if err == nil { |
| 504 |
return e.Decode(eb) |
| 505 |
} |
| 506 |
return err |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
func (e *Element) String() string { |
| 511 |
result, _ := e.MarshalText() |
| 512 |
return string(result) |
| 513 |
} |