Skip to content

Commit 0a3da6a

Browse files
committedMay 14, 2020
Adding goldilocks documentation.
1 parent 1a58619 commit 0a3da6a

File tree

3 files changed

+84
-26
lines changed

3 files changed

+84
-26
lines changed
 

‎ecc/goldilocks/curve.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
// Package goldilocks provides elliptic curve operations over the goldilocks curve.
21
package goldilocks
32

43
import fp "github.com/cloudflare/circl/math/fp448"
54

6-
// Curve is the Goldilocks curve x^2+y^2=z^2-39081x^2y^2.
5+
// Curve provides operations on the Goldilocks curve.
6+
// Curve is a zero-length datatype.
77
type Curve struct{}
88

99
// Identity returns the identity point.
@@ -31,8 +31,8 @@ func (Curve) IsOnCurve(P *Point) bool { return isOnCurve(&P.x, &P.y, &P.ta, &P.t
3131
// Order returns the number of points in the prime subgroup.
3232
func (Curve) Order() Scalar { return order }
3333

34-
// Double returns 2P.
35-
func (Curve) Double(P *Point) *Point { R := *P; R.Double(); return &R }
34+
// Double returns R = 2P.
35+
func (Curve) Double(R, P *Point) *Point { R := *P; R.Double(); return &R }
3636

3737
// Add returns P+Q.
3838
func (Curve) Add(P, Q *Point) *Point { R := *P; R.Add(Q); return &R }

‎ecc/goldilocks/decaf.go

+27-22
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,53 @@
11
package goldilocks
22

3-
import (
4-
fp "github.com/cloudflare/circl/math/fp448"
5-
)
3+
import fp "github.com/cloudflare/circl/math/fp448"
64

7-
// Decaf provides a prime-order group.
8-
// Internally, the implementation uses the twist of goldilocks curve.
5+
// DecafEncodingSize is the size (in bytes) for storing a decaf element.
6+
const DecafEncodingSize = fp.Size
7+
8+
// Decaf provides operations of a prime-order group from goldilocks curve.
9+
// Its internal implementation uses the twist of the goldilocks curve.
10+
// This uses version 1.1 of the encoding. Decaf is a zero-length datatype.
911
type Decaf struct{ c twistCurve }
1012

11-
// Elt is an element of the decaf group.
13+
// Elt is an element of the Decaf group. It must be always initialized using
14+
// one of the Decaf functions.
1215
type Elt struct{ p twistPoint }
1316

1417
func (e Elt) String() string { return e.p.String() }
1518

16-
// IsValid is
19+
// IsValid returns True if a is a valid element of the group.
1720
func (d Decaf) IsValid(a *Elt) bool { return d.c.IsOnCurve(&a.p) }
1821

19-
// IsIdentity is
22+
// IsIdentity returns True if a is the identity of the group.
2023
func (d Decaf) IsIdentity(a *Elt) bool { return fp.IsZero(&a.p.x) }
2124

22-
// Identity is
25+
// Identity returns the identity element of the group.
2326
func (d Decaf) Identity() *Elt { return &Elt{*d.c.Identity()} }
2427

25-
// Generator is
28+
// Generator returns the generator element of the group.
2629
func (d Decaf) Generator() *Elt { return &Elt{*d.c.pull(Curve{}.Generator())} }
2730

28-
// Order is
31+
// Order returns a scalar with the order of the group.
2932
func (d Decaf) Order() Scalar { return order }
3033

31-
// Add is
34+
// Add calculates c=a+b, where + is the group operation.
3235
func (d Decaf) Add(c, a, b *Elt) { c.p = a.p; c.p.Add(&b.p) }
3336

34-
// Double is
37+
// Double calculates c=a+a, where + is the group operation.
3538
func (d Decaf) Double(c, a *Elt) { c.p = a.p; c.p.Double() }
3639

37-
// Neg is
40+
// Neg calculates c=-a, where - is the inverse of the group operation.
3841
func (d Decaf) Neg(c, a *Elt) { c.p = a.p; c.p.cneg(1) }
3942

40-
// Mul is
43+
// Mul calculates c=n*a, where * is scalar multiplication on the group.
4144
func (d Decaf) Mul(c *Elt, n *Scalar, a *Elt) { c.p = *d.c.ScalarMult(n, &a.p) }
4245

43-
// MulGen is
46+
// MulGen calculates c=n*g, where * is scalar multiplication on the group,
47+
// and g is the generator of the group.
4448
func (d Decaf) MulGen(c *Elt, n *Scalar) { c.p = *d.c.ScalarBaseMult(n) }
4549

46-
// AreEqual is
50+
// AreEqual returns True if a=b, where = is an equivalence relation.
4751
func (d Decaf) AreEqual(a, b *Elt) bool {
4852
l, r := &fp.Elt{}, &fp.Elt{}
4953
fp.Mul(l, &a.p.x, &b.p.y)
@@ -52,7 +56,7 @@ func (d Decaf) AreEqual(a, b *Elt) bool {
5256
return fp.IsZero(l)
5357
}
5458

55-
// Marshal is
59+
// Marshal returns a unique encoding of the element e.
5660
func (e *Elt) Marshal() []byte {
5761
x, ta, tb, z := e.p.x, e.p.ta, e.p.tb, e.p.z
5862
one, t, t2, s := &fp.Elt{}, &fp.Elt{}, &fp.Elt{}, &fp.Elt{}
@@ -84,17 +88,18 @@ func (e *Elt) Marshal() []byte {
8488
return encS[:]
8589
}
8690

87-
// Unmarshal is
91+
// Unmarshal if succeeds returns nil and constructs an element e from an
92+
// encoding stored in a slice b of DecafEncodingSize bytes.
8893
func (e *Elt) Unmarshal(b []byte) error {
89-
if len(b) < fp.Size {
94+
if len(b) < DecafEncodingSize {
9095
return errInvalidDecoding
9196
}
9297

9398
s := &fp.Elt{}
94-
copy(s[:], b[:fp.Size])
99+
copy(s[:], b[:DecafEncodingSize])
95100
isNeg := fp.Parity(s)
96101
p := fp.P()
97-
if isNeg == 1 || !isLessThan(b[:fp.Size], p[:]) {
102+
if isNeg == 1 || !isLessThan(b[:DecafEncodingSize], p[:]) {
98103
return errInvalidDecoding
99104
}
100105

‎ecc/goldilocks/doc.go

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Package goldilocks provides elliptic curve operations over the goldilocks
2+
// curve and the decaf group.
3+
//
4+
// Goldilocks Curve
5+
//
6+
// The goldilocks curve is defined over GF(2^448-2^224-1) by the twisted Edwards
7+
// curve
8+
// Goldilocks: ax^2+y^2 = 1 + dx^2y^2, where a=1 and d=-39081.
9+
// This curve was proposed by Hamburg (1) and is also known as edwards448
10+
// after RFC-7748 (2).
11+
//
12+
// order = 4*(2^446 - 0x8335dc163bb124b65129c96fde933d8d723a70aadc873d6d54a7bb0d)
13+
// G = (x,y) =
14+
// (224580040295924300187604334099896036246789641632564134246125461
15+
// 686950415467406032909029192869357953282578032075146446173674602635
16+
// 247710
17+
//
18+
// Y(P) 298819210078481492676017930443930673437544040154080242095928241
19+
// 372331506189835876003536878655418784733982303233503462500531545062
20+
// 832660
21+
22+
// The datatypes Curve, Point, and Scalar provide methods to perform arithmetic
23+
// operations on the Goldilocks curve.
24+
//
25+
// Decaf Group
26+
//
27+
// Decaf (3) is a prime-order group constructed as a quotient of groups. A Decaf
28+
// element can be represented by any point in the coset P+J[2], where J is a
29+
// Jacobi quartic and J[2] are its 2-torsion points.
30+
// Since P+J[2] has four points, Decaf specifies rules to choose one canonical
31+
// representative, which has a unique encoding. Two representations are
32+
// equivalent if they belong to the same coset.
33+
//
34+
// The types Decaf, Elt, and Scalar provide methods to perform arithmetic
35+
// operations on the Decaf group.
36+
//
37+
// Internals
38+
//
39+
// Both Goldilocks and Decaf use as internal representation the curve
40+
// 4Iso-Goldilocks: ax^2+y^2 = 1 + dx^2y^2, where a=-1 and d=-39082.
41+
// This curve is 4-degree isogeous to the Goldilocks curve, and 2-degree
42+
// isogeneous to the Jacobi quartic. The 4Iso-Goldilocks curve was chosen as
43+
// provides faster arithmetic operations.
44+
//
45+
// References
46+
//
47+
// (1) https://www.shiftleft.org/papers/goldilocks
48+
//
49+
// (2) https://tools.ietf.org/html/rfc7748
50+
//
51+
// (3) https://doi.org/10.1007/978-3-662-47989-6_34
52+
//
53+
package goldilocks

0 commit comments

Comments
 (0)