Skip to content

Commit c23ba25

Browse files
committed
Reuse Edwards windowed scalar multiplication for Decaf
1 parent 46a6de6 commit c23ba25

File tree

4 files changed

+25
-28
lines changed

4 files changed

+25
-28
lines changed
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
pub(crate) mod double_and_add;
21
// pub(crate) mod double_base;
32
pub(crate) mod variable_base;
43
pub(crate) mod window;
54

6-
pub(crate) use double_and_add::double_and_add;
75
pub(crate) use variable_base::variable_base;

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

Lines changed: 0 additions & 20 deletions
This file was deleted.

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#![allow(non_snake_case)]
22

33
use super::window::wnaf::LookupTable;
4-
use crate::EdwardsScalar;
4+
use crate::Scalar;
55
use crate::curve::twedwards::{extended::ExtendedPoint, extensible::ExtensiblePoint};
6+
use crate::field::CurveWithScalar;
67
use subtle::{Choice, ConditionallyNegatable};
78

8-
pub fn variable_base(point: &ExtendedPoint, s: &EdwardsScalar) -> ExtensiblePoint {
9+
pub fn variable_base<C: CurveWithScalar>(point: &ExtendedPoint, s: &Scalar<C>) -> ExtensiblePoint {
910
let mut result = ExtensiblePoint::IDENTITY;
1011

1112
// Recode Scalar
@@ -37,12 +38,30 @@ pub fn variable_base(point: &ExtendedPoint, s: &EdwardsScalar) -> ExtensiblePoin
3738
#[cfg(test)]
3839
mod test {
3940
use super::*;
41+
use crate::EdwardsScalar;
4042
use crate::TWISTED_EDWARDS_BASE_POINT;
41-
use crate::curve::scalar_mul::double_and_add;
4243
use elliptic_curve::bigint::U448;
44+
use subtle::ConditionallySelectable;
4345

4446
#[test]
4547
fn test_scalar_mul() {
48+
/// Traditional double and add algorithm
49+
fn double_and_add(point: &ExtendedPoint, s_bits: [bool; 448]) -> ExtensiblePoint {
50+
let mut result = ExtensiblePoint::IDENTITY;
51+
52+
// NB, we reverse here, so we are going from MSB to LSB
53+
// XXX: Would be great if subtle had a From<u32> for Choice. But maybe that is not it's purpose?
54+
for bit in s_bits.into_iter().rev() {
55+
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);
60+
}
61+
62+
result
63+
}
64+
4665
// XXX: In the future use known multiples from Sage in bytes form?
4766
let twisted_point = TWISTED_EDWARDS_BASE_POINT;
4867
let scalar = EdwardsScalar::new(U448::from_be_hex(

ed448-goldilocks/src/decaf/ops.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use crate::{DecafAffinePoint, DecafScalar, curve::scalar_mul::double_and_add};
1+
use crate::curve::scalar_mul::variable_base;
2+
use crate::{DecafAffinePoint, DecafScalar};
23
use core::{
34
borrow::Borrow,
45
iter::Sum,
@@ -13,8 +14,7 @@ impl Mul<&DecafScalar> for &DecafPoint {
1314
type Output = DecafPoint;
1415

1516
fn mul(self, scalar: &DecafScalar) -> DecafPoint {
16-
// XXX: We can do better than double and add
17-
DecafPoint(double_and_add(&self.0, scalar.bits()).to_extended())
17+
DecafPoint(variable_base(&self.0, scalar).to_extended())
1818
}
1919
}
2020

0 commit comments

Comments
 (0)