Skip to content

Commit 7360c2a

Browse files
committed
Add optimized Edwards addition and doubling algorithms
1 parent 6ff0824 commit 7360c2a

File tree

1 file changed

+30
-30
lines changed

1 file changed

+30
-30
lines changed

ed448-goldilocks/src/edwards/extended.rs

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -341,41 +341,41 @@ impl EdwardsPoint {
341341
}
342342

343343
/// Add two points
344-
//https://iacr.org/archive/asiacrypt2008/53500329/53500329.pdf (3.1)
345-
// These formulas are unified, so for now we can use it for doubling. Will refactor later for speed
344+
// (3.1) https://iacr.org/archive/asiacrypt2008/53500329/53500329.pdf
346345
pub fn add(&self, other: &EdwardsPoint) -> Self {
347-
let aXX = self.X * other.X; // aX1X2
348-
let dTT = FieldElement::EDWARDS_D * self.T * other.T; // dT1T2
349-
let ZZ = self.Z * other.Z; // Z1Z2
350-
let YY = self.Y * other.Y;
351-
352-
let X = {
353-
let x_1 = (self.X * other.Y) + (self.Y * other.X);
354-
let x_2 = ZZ - dTT;
355-
x_1 * x_2
356-
};
357-
let Y = {
358-
let y_1 = YY - aXX;
359-
let y_2 = ZZ + dTT;
360-
y_1 * y_2
361-
};
362-
363-
let T = {
364-
let t_1 = YY - aXX;
365-
let t_2 = (self.X * other.Y) + (self.Y * other.X);
366-
t_1 * t_2
367-
};
368-
369-
let Z = { (ZZ - dTT) * (ZZ + dTT) };
370-
371-
EdwardsPoint { X, Y, Z, T }
346+
let A = self.X * other.X;
347+
let B = self.Y * other.Y;
348+
let C = self.T * other.T * FieldElement::EDWARDS_D;
349+
let D = self.Z * other.Z;
350+
let E = (self.X + self.Y) * (other.X + other.Y) - A - B;
351+
let F = D - C;
352+
let G = D + C;
353+
let H = B - A;
354+
Self {
355+
X: E * F,
356+
Y: G * H,
357+
Z: F * G,
358+
T: E * H,
359+
}
372360
}
373361

374362
/// Double this point
375-
// XXX: See comment on addition, the formula is unified, so this will do for now
376-
//https://iacr.org/archive/asiacrypt2008/53500329/53500329.pdf (3.1)
363+
// (3.3) https://iacr.org/archive/asiacrypt2008/53500329/53500329.pdf
377364
pub fn double(&self) -> Self {
378-
self.add(self)
365+
let A = self.X.square();
366+
let B = self.Y.square();
367+
let C = self.Z.square().double();
368+
let D = A;
369+
let E = (self.X + self.Y).square() - A - B;
370+
let G = D + B;
371+
let F = G - C;
372+
let H = D - B;
373+
Self {
374+
X: E * F,
375+
Y: G * H,
376+
Z: F * G,
377+
T: E * H,
378+
}
379379
}
380380

381381
/// Check if this point is on the curve

0 commit comments

Comments
 (0)