Skip to content

Commit 9732c75

Browse files
committed
Simplify double-and-add code
1 parent c23ba25 commit 9732c75

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

ed448-goldilocks/src/curve/scalar_mul/variable_base.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ mod test {
5353
// XXX: Would be great if subtle had a From<u32> for Choice. But maybe that is not it's purpose?
5454
for bit in s_bits.into_iter().rev() {
5555
result = result.double();
56-
57-
let mut p = ExtendedPoint::IDENTITY;
58-
p.conditional_assign(point, Choice::from(bit as u8));
59-
result = result.to_extended().add_extended(&p);
56+
result.conditional_assign(
57+
&result.to_extended().add_extended(point),
58+
Choice::from(u8::from(bit)),
59+
);
6060
}
6161

6262
result

ed448-goldilocks/src/curve/twedwards/extensible.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use crate::curve::twedwards::{affine::AffinePoint, extended::ExtendedPoint};
55
use crate::edwards::EdwardsPoint as EdwardsExtendedPoint;
66
use crate::field::FieldElement;
7-
use subtle::{Choice, ConstantTimeEq};
7+
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq};
88

99
/// This is the representation that we will do most of the group operations on.
1010
// In affine (x,y) is the extensible point (X, Y, Z, T1, T2)
@@ -20,6 +20,17 @@ pub struct ExtensiblePoint {
2020
pub(crate) T2: FieldElement,
2121
}
2222

23+
impl ConditionallySelectable for ExtensiblePoint {
24+
fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
25+
Self {
26+
X: FieldElement::conditional_select(&a.X, &b.X, choice),
27+
Y: FieldElement::conditional_select(&a.Y, &b.Y, choice),
28+
Z: FieldElement::conditional_select(&a.Z, &b.Z, choice),
29+
T1: FieldElement::conditional_select(&a.T1, &b.T1, choice),
30+
T2: FieldElement::conditional_select(&a.T2, &b.T2, choice),
31+
}
32+
}
33+
}
2334
impl ConstantTimeEq for ExtensiblePoint {
2435
fn ct_eq(&self, other: &Self) -> Choice {
2536
let XZ = self.X * other.Z;

0 commit comments

Comments
 (0)