1
1
package goldilocks
2
2
3
- import (
4
- fp "github.com/cloudflare/circl/math/fp448"
5
- )
3
+ import fp "github.com/cloudflare/circl/math/fp448"
6
4
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.
9
11
type Decaf struct { c twistCurve }
10
12
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.
12
15
type Elt struct { p twistPoint }
13
16
14
17
func (e Elt ) String () string { return e .p .String () }
15
18
16
- // IsValid is
19
+ // IsValid returns True if a is a valid element of the group.
17
20
func (d Decaf ) IsValid (a * Elt ) bool { return d .c .IsOnCurve (& a .p ) }
18
21
19
- // IsIdentity is
22
+ // IsIdentity returns True if a is the identity of the group.
20
23
func (d Decaf ) IsIdentity (a * Elt ) bool { return fp .IsZero (& a .p .x ) }
21
24
22
- // Identity is
25
+ // Identity returns the identity element of the group.
23
26
func (d Decaf ) Identity () * Elt { return & Elt {* d .c .Identity ()} }
24
27
25
- // Generator is
28
+ // Generator returns the generator element of the group.
26
29
func (d Decaf ) Generator () * Elt { return & Elt {* d .c .pull (Curve {}.Generator ())} }
27
30
28
- // Order is
31
+ // Order returns a scalar with the order of the group.
29
32
func (d Decaf ) Order () Scalar { return order }
30
33
31
- // Add is
34
+ // Add calculates c=a+b, where + is the group operation.
32
35
func (d Decaf ) Add (c , a , b * Elt ) { c .p = a .p ; c .p .Add (& b .p ) }
33
36
34
- // Double is
37
+ // Double calculates c=a+a, where + is the group operation.
35
38
func (d Decaf ) Double (c , a * Elt ) { c .p = a .p ; c .p .Double () }
36
39
37
- // Neg is
40
+ // Neg calculates c=-a, where - is the inverse of the group operation.
38
41
func (d Decaf ) Neg (c , a * Elt ) { c .p = a .p ; c .p .cneg (1 ) }
39
42
40
- // Mul is
43
+ // Mul calculates c=n*a, where * is scalar multiplication on the group.
41
44
func (d Decaf ) Mul (c * Elt , n * Scalar , a * Elt ) { c .p = * d .c .ScalarMult (n , & a .p ) }
42
45
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.
44
48
func (d Decaf ) MulGen (c * Elt , n * Scalar ) { c .p = * d .c .ScalarBaseMult (n ) }
45
49
46
- // AreEqual is
50
+ // AreEqual returns True if a=b, where = is an equivalence relation.
47
51
func (d Decaf ) AreEqual (a , b * Elt ) bool {
48
52
l , r := & fp.Elt {}, & fp.Elt {}
49
53
fp .Mul (l , & a .p .x , & b .p .y )
@@ -52,7 +56,7 @@ func (d Decaf) AreEqual(a, b *Elt) bool {
52
56
return fp .IsZero (l )
53
57
}
54
58
55
- // Marshal is
59
+ // Marshal returns a unique encoding of the element e.
56
60
func (e * Elt ) Marshal () []byte {
57
61
x , ta , tb , z := e .p .x , e .p .ta , e .p .tb , e .p .z
58
62
one , t , t2 , s := & fp.Elt {}, & fp.Elt {}, & fp.Elt {}, & fp.Elt {}
@@ -84,17 +88,18 @@ func (e *Elt) Marshal() []byte {
84
88
return encS [:]
85
89
}
86
90
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.
88
93
func (e * Elt ) Unmarshal (b []byte ) error {
89
- if len (b ) < fp . Size {
94
+ if len (b ) < DecafEncodingSize {
90
95
return errInvalidDecoding
91
96
}
92
97
93
98
s := & fp.Elt {}
94
- copy (s [:], b [:fp . Size ])
99
+ copy (s [:], b [:DecafEncodingSize ])
95
100
isNeg := fp .Parity (s )
96
101
p := fp .P ()
97
- if isNeg == 1 || ! isLessThan (b [:fp . Size ], p [:]) {
102
+ if isNeg == 1 || ! isLessThan (b [:DecafEncodingSize ], p [:]) {
98
103
return errInvalidDecoding
99
104
}
100
105
0 commit comments