Skip to content

Commit 7d3f219

Browse files
committed
Simplify double-and-add code
1 parent 8bd7fbe commit 7d3f219

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 super::affine::AffinePoint;
55
use super::extended::ExtendedPoint;
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)
@@ -31,6 +31,17 @@ impl ConstantTimeEq for ExtensiblePoint {
3131
XZ.ct_eq(&ZX) & YZ.ct_eq(&ZY)
3232
}
3333
}
34+
impl ConditionallySelectable for ExtensiblePoint {
35+
fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
36+
Self {
37+
X: FieldElement::conditional_select(&a.X, &b.X, choice),
38+
Y: FieldElement::conditional_select(&a.Y, &b.Y, choice),
39+
Z: FieldElement::conditional_select(&a.Z, &b.Z, choice),
40+
T1: FieldElement::conditional_select(&a.T1, &b.T1, choice),
41+
T2: FieldElement::conditional_select(&a.T2, &b.T2, choice),
42+
}
43+
}
44+
}
3445
impl PartialEq for ExtensiblePoint {
3546
fn eq(&self, other: &ExtensiblePoint) -> bool {
3647
self.ct_eq(other).into()

0 commit comments

Comments
 (0)