Skip to content

Commit 537c053

Browse files
committed
ted448 point public fields.
1 parent f329f01 commit 537c053

File tree

4 files changed

+54
-67
lines changed

4 files changed

+54
-67
lines changed

.etc/golangci.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ linters-settings:
1818
statements: 80
1919
nestif:
2020
min-complexity: 6
21-
dogsled:
22-
max-blank-identifiers: 3
21+
2322
issues:
2423
max-issues-per-linter: 0
2524
max-same-issues: 0

ecc/decaf/decaf.go

+21-20
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
package decaf
1111

1212
import (
13+
"unsafe"
14+
1315
"github.com/cloudflare/circl/internal/ted448"
1416
fp "github.com/cloudflare/circl/math/fp448"
1517
)
@@ -55,19 +57,13 @@ func Mul(c *Elt, n *Scalar, a *Elt) { ted448.ScalarMult(&c.p, n, &a.p) }
5557
func MulGen(c *Elt, n *Scalar) { ted448.ScalarBaseMult(&c.p, n) }
5658

5759
// IsIdentity returns True if e is the identity of the group.
58-
func (e *Elt) IsIdentity() bool {
59-
x, y, _, _, z := e.p.Coordinates()
60-
return fp.IsZero(&x) && !fp.IsZero(&y) && !fp.IsZero(&z)
61-
}
60+
func (e *Elt) IsIdentity() bool { return fp.IsZero(&e.p.X) && !fp.IsZero(&e.p.Y) && !fp.IsZero(&e.p.Z) }
6261

6362
// IsEqual returns True if e=a, where = is an equivalence relation.
6463
func (e *Elt) IsEqual(a *Elt) bool {
65-
x1, y1, _, _, _ := e.p.Coordinates()
66-
x2, y2, _, _, _ := a.p.Coordinates()
67-
6864
l, r := &fp.Elt{}, &fp.Elt{}
69-
fp.Mul(l, &x1, &y2)
70-
fp.Mul(r, &x2, &y1)
65+
fp.Mul(l, &e.p.X, &a.p.Y)
66+
fp.Mul(r, &a.p.X, &e.p.Y)
7167
fp.Sub(l, l, r)
7268
return fp.IsZero(l)
7369
}
@@ -116,22 +112,27 @@ func (e *Elt) UnmarshalBinary(data []byte) error {
116112
fp.Mul(y, y, t0) // y = (1 - a*s^2)*isr*den
117113

118114
isValid := isPositiveS && isLessThanP && isQR
119-
P, err := ted448.NewPoint(x, y)
120-
if !isValid || err != nil {
121-
return ErrInvalidDecoding
115+
b := uint(*((*byte)(unsafe.Pointer(&isValid))))
116+
fp.Cmov(&e.p.X, x, b)
117+
fp.Cmov(&e.p.Y, y, b)
118+
fp.Cmov(&e.p.Ta, x, b)
119+
fp.Cmov(&e.p.Tb, y, b)
120+
fp.Cmov(&e.p.Z, &one, b)
121+
var err error
122+
if !isValid {
123+
err = ErrInvalidDecoding
122124
}
123-
e.p = *P
124-
return nil
125+
return err
125126
}
126127

127128
// MarshalBinary returns a unique encoding of the element e.
128129
func (e *Elt) MarshalBinary() ([]byte, error) {
129-
x, _, ta, tb, z := e.p.Coordinates()
130+
x, ta, tb, z := &e.p.X, &e.p.Ta, &e.p.Tb, &e.p.Z
130131
one, t, t2, s := &fp.Elt{}, &fp.Elt{}, &fp.Elt{}, &fp.Elt{}
131132
fp.SetOne(one)
132-
fp.Mul(t, &ta, &tb) // t = ta*tb
133-
t0, t1 := x, *t // (t0,t1) = (x,t)
134-
fp.Sqr(t2, &x) // t2 = x^2
133+
fp.Mul(t, ta, tb) // t = ta*tb
134+
t0, t1 := *x, *t // (t0,t1) = (x,t)
135+
fp.Sqr(t2, x) // t2 = x^2
135136
fp.AddSub(&t0, &t1) // (t0,t1) = (x+t,x-t)
136137
fp.Mul(&t1, &t0, &t1) // t1 = (x+t)*(x-t)
137138
fp.Mul(&t0, &t1, &aMinusDTwist) // t0 = (a-d)*(x+t)*(x-t)
@@ -142,9 +143,9 @@ func (e *Elt) MarshalBinary() ([]byte, error) {
142143
isNeg := fp.Parity(t2) // isNeg = sgn(t2)
143144
fp.Neg(t2, &t1) // t2 = -t1
144145
fp.Cmov(&t1, t2, uint(isNeg)) // if t2 is negative then t1 = -t1
145-
fp.Mul(s, &t1, &z) // s = t1*z
146+
fp.Mul(s, &t1, z) // s = t1*z
146147
fp.Sub(s, s, t) // s = t1*z - t
147-
fp.Mul(s, s, &x) // s = x*(t1*z - t)
148+
fp.Mul(s, s, x) // s = x*(t1*z - t)
148149
fp.Mul(s, s, &t0) // s = isr*x*(t1*z - t)
149150
fp.Mul(s, s, &aMinusDTwist) // s = (a-d)*isr*x*(t1*z - t)
150151
isNeg = fp.Parity(s) // isNeg = sgn(s)

internal/ted448/curve.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import (
1010
)
1111

1212
// Identity returns the identity point.
13-
func Identity() Point { return Point{y: fp.One(), z: fp.One()} }
13+
func Identity() Point { return Point{Y: fp.One(), Z: fp.One()} }
1414

1515
// Generator returns the generator point.
16-
func Generator() Point { return Point{x: genX, y: genY, z: fp.One(), ta: genX, tb: genY} }
16+
func Generator() Point { return Point{X: genX, Y: genY, Z: fp.One(), Ta: genX, Tb: genY} }
1717

1818
// Order returns the number of points in the prime subgroup.
1919
func Order() Scalar { return order }
@@ -23,18 +23,18 @@ func IsOnCurve(P *Point) bool {
2323
eq0 := *P != Point{}
2424
x2, y2, t, t2, z2 := &fp.Elt{}, &fp.Elt{}, &fp.Elt{}, &fp.Elt{}, &fp.Elt{}
2525
rhs, lhs := &fp.Elt{}, &fp.Elt{}
26-
fp.Mul(t, &P.ta, &P.tb) // t = ta*tb
27-
fp.Sqr(x2, &P.x) // x^2
28-
fp.Sqr(y2, &P.y) // y^2
29-
fp.Sqr(z2, &P.z) // z^2
26+
fp.Mul(t, &P.Ta, &P.Tb) // t = ta*tb
27+
fp.Sqr(x2, &P.X) // x^2
28+
fp.Sqr(y2, &P.Y) // y^2
29+
fp.Sqr(z2, &P.Z) // z^2
3030
fp.Sqr(t2, t) // t^2
3131
fp.Sub(lhs, y2, x2) // -x^2 + y^2, since a=-1
3232
fp.Mul(rhs, t2, &ParamD) // dt^2
3333
fp.Add(rhs, rhs, z2) // z^2 + dt^2
3434
fp.Sub(lhs, lhs, rhs) // ax^2 + y^2 - (z^2 + dt^2)
3535
eq1 := fp.IsZero(lhs)
36-
fp.Mul(lhs, &P.x, &P.y) // xy
37-
fp.Mul(rhs, t, &P.z) // tz
36+
fp.Mul(lhs, &P.X, &P.Y) // xy
37+
fp.Mul(rhs, t, &P.Z) // tz
3838
fp.Sub(lhs, lhs, rhs) // xy - tz
3939
eq2 := fp.IsZero(lhs)
4040
return eq0 && eq1 && eq2

internal/ted448/point.go

+24-37
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package ted448
22

33
import (
4-
"errors"
54
"fmt"
65

76
fp "github.com/cloudflare/circl/math/fp448"
87
)
98

109
// Point defines a point on the ted448 curve.
11-
type Point struct{ x, y, z, ta, tb fp.Elt }
10+
type Point struct{ X, Y, Z, Ta, Tb fp.Elt }
1211

1312
type prePointAffine struct{ addYX, subYX, dt2 fp.Elt }
1413

@@ -18,36 +17,24 @@ type prePointProy struct {
1817
}
1918

2019
func (P Point) String() string {
21-
return fmt.Sprintf("x: %v\ny: %v\nta: %v\ntb: %v\nz: %v", P.x, P.y, P.ta, P.tb, P.z)
20+
return fmt.Sprintf("x: %v\ny: %v\nta: %v\ntb: %v\nz: %v", P.X, P.Y, P.Ta, P.Tb, P.Z)
2221
}
2322

24-
// NewPoint creates a point from affine coordinates.
25-
func NewPoint(x, y *fp.Elt) (*Point, error) {
26-
P := &Point{x: *x, y: *y, ta: *x, tb: *y, z: fp.One()}
27-
if !IsOnCurve(P) {
28-
return nil, errors.New("invalid point")
29-
}
30-
return P, nil
31-
}
32-
33-
// Coordinates returns a copy of the coordinates.
34-
func (P *Point) Coordinates() (x, y, ta, tb, z fp.Elt) { return P.x, P.y, P.ta, P.tb, P.z }
35-
3623
// cneg conditionally negates the point if b=1.
3724
func (P *Point) cneg(b uint) {
3825
t := &fp.Elt{}
39-
fp.Neg(t, &P.x)
40-
fp.Cmov(&P.x, t, b)
41-
fp.Neg(t, &P.ta)
42-
fp.Cmov(&P.ta, t, b)
26+
fp.Neg(t, &P.X)
27+
fp.Cmov(&P.X, t, b)
28+
fp.Neg(t, &P.Ta)
29+
fp.Cmov(&P.Ta, t, b)
4330
}
4431

4532
// Double updates P with 2P.
4633
func (P *Point) Double() {
4734
// This is formula (7) from "ed448 Edwards Curves Revisited" by
4835
// Hisil H., Wong K.KH., Carter G., Dawson E. (2008)
4936
// https://doi.org/10.1007/978-3-540-89255-7_20
50-
Px, Py, Pz, Pta, Ptb := &P.x, &P.y, &P.z, &P.ta, &P.tb
37+
Px, Py, Pz, Pta, Ptb := &P.X, &P.Y, &P.Z, &P.Ta, &P.Tb
5138
a, b, c, e, f, g, h := Px, Py, Pz, Pta, Px, Py, Ptb
5239
fp.Add(e, Px, Py) // x+y
5340
fp.Sqr(a, Px) // A = x^2
@@ -66,7 +53,7 @@ func (P *Point) Double() {
6653

6754
// mixAdd calulates P= P+Q, where Q is a precomputed448 point with Z_Q = 1.
6855
func (P *Point) mixAddZ1(Q *prePointAffine) {
69-
fp.Add(&P.z, &P.z, &P.z) // D = 2*z1 (z2=1)
56+
fp.Add(&P.Z, &P.Z, &P.Z) // D = 2*z1 (z2=1)
7057
P.coreAddition(Q)
7158
}
7259

@@ -75,7 +62,7 @@ func (P *Point) coreAddition(Q *prePointAffine) {
7562
// This is the formula following (5) from "ed448 Edwards Curves Revisited" by
7663
// Hisil H., Wong K.KH., Carter G., Dawson E. (2008)
7764
// https://doi.org/10.1007/978-3-540-89255-7_20
78-
Px, Py, Pz, Pta, Ptb := &P.x, &P.y, &P.z, &P.ta, &P.tb
65+
Px, Py, Pz, Pta, Ptb := &P.X, &P.Y, &P.Z, &P.Ta, &P.Tb
7966
addYX2, subYX2, dt2 := &Q.addYX, &Q.subYX, &Q.dt2
8067
a, b, c, d, e, f, g, h := Px, Py, &fp.Elt{}, Pz, Pta, Px, Py, Ptb
8168
fp.Mul(c, Pta, Ptb) // t1 = ta*tb
@@ -113,37 +100,37 @@ func (P *prePointAffine) cmov(Q *prePointAffine, b uint) {
113100

114101
// mixAdd calculates P= P+Q, where Q is a precomputed448 point with Z_Q != 1.
115102
func (P *Point) mixAdd(Q *prePointProy) {
116-
fp.Mul(&P.z, &P.z, &Q.z2) // D = 2*z1*z2
103+
fp.Mul(&P.Z, &P.Z, &Q.z2) // D = 2*z1*z2
117104
P.coreAddition(&Q.prePointAffine)
118105
}
119106

120107
// IsIdentity returns True is P is the identity.
121108
func (P *Point) IsIdentity() bool {
122-
return fp.IsZero(&P.x) && !fp.IsZero(&P.y) && !fp.IsZero(&P.z) && P.y == P.z
109+
return fp.IsZero(&P.X) && !fp.IsZero(&P.Y) && !fp.IsZero(&P.Z) && P.Y == P.Z
123110
}
124111

125112
// IsEqual returns True if P is equivalent to Q.
126113
func (P *Point) IsEqual(Q *Point) bool {
127114
l, r := &fp.Elt{}, &fp.Elt{}
128-
fp.Mul(l, &P.x, &Q.z)
129-
fp.Mul(r, &Q.x, &P.z)
115+
fp.Mul(l, &P.X, &Q.Z)
116+
fp.Mul(r, &Q.X, &P.Z)
130117
fp.Sub(l, l, r)
131118
b := fp.IsZero(l)
132-
fp.Mul(l, &P.y, &Q.z)
133-
fp.Mul(r, &Q.y, &P.z)
119+
fp.Mul(l, &P.Y, &Q.Z)
120+
fp.Mul(r, &Q.Y, &P.Z)
134121
fp.Sub(l, l, r)
135122
b = b && fp.IsZero(l)
136-
fp.Mul(l, &P.ta, &P.tb)
137-
fp.Mul(l, l, &Q.z)
138-
fp.Mul(r, &Q.ta, &Q.tb)
139-
fp.Mul(r, r, &P.z)
123+
fp.Mul(l, &P.Ta, &P.Tb)
124+
fp.Mul(l, l, &Q.Z)
125+
fp.Mul(r, &Q.Ta, &Q.Tb)
126+
fp.Mul(r, r, &P.Z)
140127
fp.Sub(l, l, r)
141128
b = b && fp.IsZero(l)
142129
return b
143130
}
144131

145132
// Neg obtains the inverse of P.
146-
func (P *Point) Neg() { fp.Neg(&P.x, &P.x); fp.Neg(&P.ta, &P.ta) }
133+
func (P *Point) Neg() { fp.Neg(&P.X, &P.X); fp.Neg(&P.Ta, &P.Ta) }
147134

148135
// Add calculates P = P+Q.
149136
func (P *Point) Add(Q *Point) {
@@ -176,10 +163,10 @@ func (P *prePointProy) cmov(Q *prePointProy, b uint) {
176163

177164
// FromPoint precomputes some coordinates of Q for mised addition.
178165
func (P *prePointProy) FromPoint(Q *Point) {
179-
fp.Add(&P.addYX, &Q.y, &Q.x) // addYX = X + Y
180-
fp.Sub(&P.subYX, &Q.y, &Q.x) // subYX = Y - X
181-
fp.Mul(&P.dt2, &Q.ta, &Q.tb) // T = ta*tb
166+
fp.Add(&P.addYX, &Q.Y, &Q.X) // addYX = X + Y
167+
fp.Sub(&P.subYX, &Q.Y, &Q.X) // subYX = Y - X
168+
fp.Mul(&P.dt2, &Q.Ta, &Q.Tb) // T = ta*tb
182169
fp.Mul(&P.dt2, &P.dt2, &ParamD) // D*T
183170
fp.Add(&P.dt2, &P.dt2, &P.dt2) // dt2 = 2*D*T
184-
fp.Add(&P.z2, &Q.z, &Q.z) // z2 = 2*Z
171+
fp.Add(&P.z2, &Q.Z, &Q.Z) // z2 = 2*Z
185172
}

0 commit comments

Comments
 (0)