Skip to content

Commit 710ce88

Browse files
committed
[WIP] wNAF
1 parent 7f601e0 commit 710ce88

6 files changed

Lines changed: 64 additions & 7 deletions

File tree

Cargo.lock

Lines changed: 3 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@ ed448-goldilocks = { path = "ed448-goldilocks" }
2626
hash2curve = { path = "hash2curve" }
2727
primefield = { path = "primefield" }
2828
primeorder = { path = "primeorder" }
29+
30+
rustcrypto-group = { git = "https://github.com/RustCrypto/group", branch = "wnaf-fix" }

p256/src/arithmetic/scalar.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,10 @@ impl PrimeField for Scalar {
305305
self.to_bytes()
306306
}
307307

308+
fn to_le_repr(&self) -> FieldBytes {
309+
self.0.to_le_byte_array()
310+
}
311+
308312
fn is_odd(&self) -> Choice {
309313
self.0.is_odd().into()
310314
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Seeds for failure cases proptest has generated in the past. It is
2+
# automatically read and these particular cases re-run before any
3+
# novel cases are generated.
4+
#
5+
# It is recommended to check this file in to source control so that
6+
# everyone who runs the test benefits from these saved cases.
7+
cc e19ee42c127b7289fbe7e42df47abf141eb644afcbd13ac141e39b9960362174 # shrinks to point = ProjectivePoint { x: FieldElement(0x823CD15F6DD3C71933565064513A6B2BD183E554C6A08622F713EBBBFACE98BE), y: FieldElement(0x55DF5D5850F47BAD82149139979369FE498A9022A412B5E0BEDD2CFC21C3ED91), z: FieldElement(0x4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5) }, scalar = Scalar(0x0000000000000000000000000000000000000000000000000000000000000001)

p256/tests/projective.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@ use elliptic_curve::{
88
consts::U32,
99
group::{GroupEncoding, ff::PrimeField},
1010
ops::{LinearCombination, Reduce, ReduceNonZero},
11-
point::NonIdentity,
11+
point::{AffineCoordinates, NonIdentity},
1212
sec1::{self, ToSec1Point},
1313
};
1414
use p256::{
1515
AffinePoint, FieldBytes, NonZeroScalar, ProjectivePoint, Scalar,
1616
test_vectors::group::{ADD_TEST_VECTORS, MUL_TEST_VECTORS},
1717
};
1818
use primeorder::test_projective_arithmetic;
19-
use proptest::{prelude::any, prop_compose, proptest};
19+
use proptest::{prelude::*, prop_compose, proptest};
20+
21+
#[cfg(feature = "alloc")]
22+
use elliptic_curve::group::Wnaf;
2023

2124
test_projective_arithmetic!(
2225
AffinePoint,
@@ -26,6 +29,26 @@ test_projective_arithmetic!(
2629
MUL_TEST_VECTORS
2730
);
2831

32+
#[cfg(feature = "alloc")]
33+
#[test]
34+
fn wnaf() {
35+
for (k, coords) in ADD_TEST_VECTORS.iter().enumerate() {
36+
let scalar = Scalar::from(k as u64 + 1);
37+
dbg!(&scalar, coords);
38+
39+
let mut wnaf = Wnaf::new();
40+
// let p = wnaf
41+
// .scalar(&scalar)
42+
// .base(ProjectivePoint::GENERATOR)
43+
// .to_affine();
44+
let mut wnaf_base = wnaf.base(ProjectivePoint::GENERATOR, 1);
45+
let p = wnaf_base.scalar(&scalar).to_affine();
46+
47+
let (x, _y) = (p.x(), p.y());
48+
assert_eq!(x.0, coords.0);
49+
}
50+
}
51+
2952
#[test]
3053
fn projective_identity_to_bytes() {
3154
// This is technically an invalid SEC1 encoding, but is preferable to panicking.
@@ -52,6 +75,17 @@ prop_compose! {
5275

5376
// TODO: move to `primeorder::test_projective_arithmetic`.
5477
proptest! {
78+
#[cfg(feature = "alloc")]
79+
#[test]
80+
fn wnaf_proptest(
81+
point in projective(),
82+
scalar in scalar(),
83+
) {
84+
let result = point * scalar;
85+
let wnaf_result = Wnaf::new().scalar(&scalar).base(point);
86+
prop_assert_eq!(result.to_affine(), wnaf_result.to_affine());
87+
}
88+
5589
#[test]
5690
fn batch_normalize(
5791
a in non_identity(),

primeorder/src/projective.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use elliptic_curve::{
3131
};
3232

3333
#[cfg(feature = "alloc")]
34-
use alloc::vec::Vec;
34+
use {alloc::vec::Vec, elliptic_curve::group::WnafGroup};
3535

3636
#[cfg(feature = "serde")]
3737
use serdect::serde::{Deserialize, Serialize, de, ser};
@@ -598,6 +598,17 @@ where
598598
}
599599
}
600600

601+
#[cfg(feature = "alloc")]
602+
impl<C> WnafGroup for ProjectivePoint<C>
603+
where
604+
C: PrimeCurveParams,
605+
FieldBytes<C>: Copy,
606+
{
607+
fn recommended_wnaf_for_num_scalars(num_scalars: usize) -> usize {
608+
2
609+
}
610+
}
611+
601612
//
602613
// `core::ops` trait impls
603614
//

0 commit comments

Comments
 (0)