Skip to content

Commit f53dbc0

Browse files
committed
Reduce after calling from_bytes_wide
Restores bound of `3` for 32-bit platforms.
1 parent d600742 commit f53dbc0

File tree

4 files changed

+33
-14
lines changed

4 files changed

+33
-14
lines changed

curve25519-dalek/src/backend/serial/fiat_u32/field.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,5 +272,5 @@ impl FieldElement2625 {
272272

273273
#[cfg(feature = "hazmat")]
274274
impl crate::hazmat::UnderlyingCapacity for FieldElement2625 {
275-
type Capacity = typenum::U2;
275+
type Capacity = typenum::U3;
276276
}

curve25519-dalek/src/backend/serial/u32/field.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,5 +608,5 @@ impl FieldElement2625 {
608608

609609
#[cfg(feature = "hazmat")]
610610
impl crate::hazmat::UnderlyingCapacity for FieldElement2625 {
611-
type Capacity = typenum::U2;
611+
type Capacity = typenum::U3;
612612
}

curve25519-dalek/src/hazmat.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub struct OpaqueFieldElement(Underlying);
3434
/// implementations. Its size and internals are not guaranteed to have
3535
/// any specific properties and are not covered by semver.
3636
///
37-
/// Usage is recommended to be done via `LazyFieldWithCapacity<U2>` which is
37+
/// Usage is recommended to be done via `LazyFieldWithCapacity<U3>` which is
3838
/// comprehensive to all backends.
3939
#[derive(Copy)]
4040
pub struct FieldElement<U: Unsigned = U1>(pub(crate) OpaqueFieldElement, pub(crate) PhantomData<U>);
@@ -217,7 +217,7 @@ impl Field for FieldElement {
217217
fn try_from_rng<R: rand_core::TryRngCore + ?Sized>(rng: &mut R) -> Result<Self, R::Error> {
218218
let mut bytes = [0; 64];
219219
rng.try_fill_bytes(&mut bytes)?;
220-
Ok(Self::from(Underlying::from_bytes_wide(&bytes)))
220+
Ok(Self::from_uniform_bytes(&bytes))
221221
}
222222

223223
fn square(&self) -> Self {
@@ -315,7 +315,9 @@ impl From<u64> for FieldElement {
315315

316316
impl FromUniformBytes<64> for FieldElement {
317317
fn from_uniform_bytes(bytes: &[u8; 64]) -> Self {
318-
Self::from(Underlying::from_bytes_wide(bytes))
318+
Self::from(Underlying::from_bytes(
319+
&Underlying::from_bytes_wide(bytes).to_bytes(),
320+
))
319321
}
320322
}
321323

curve25519-dalek/src/hazmat/lazy_field25519.rs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,25 @@ impl<CapacityUsed: Unsigned> LazyField<CapacityUsed> for FieldElement<CapacityUs
6565
#[cfg(test)]
6666
mod tests {
6767
use crate::hazmat::lazy_field::{EagerField, LazyField, LazyFieldWithCapacity, Reducible};
68-
use typenum::{B1, U2, type_operators::IsLessOrEqual};
68+
use typenum::{B1, U2, U3, type_operators::IsLessOrEqual};
6969

70-
fn add_pair_then_mul<F: LazyFieldWithCapacity<U2>>(
70+
fn add_triple_then_mul<F: LazyFieldWithCapacity<U3>>(
7171
a: F,
7272
b: F,
7373
c: F,
7474
d: F,
75+
e: F,
76+
f: F,
7577
) -> <F as Reducible>::Output
7678
where
7779
U2: IsLessOrEqual<F::Capacity, Output = B1>,
80+
U3: IsLessOrEqual<F::Capacity, Output = B1>,
7881
{
7982
let ab = a.add(&b);
80-
let cd = c.add(&d);
81-
ab.mul(&cd)
83+
let abc = ab.add(&c);
84+
let de = d.add(&e);
85+
let def = de.add(&f);
86+
abc.mul(&def)
8287
}
8388

8489
#[test]
@@ -95,21 +100,33 @@ mod tests {
95100
let b = FieldElement::random(&mut rng);
96101
let c = FieldElement::random(&mut rng);
97102
let d = FieldElement::random(&mut rng);
98-
let expected = (a + b) * (c + d);
103+
let e = FieldElement::random(&mut rng);
104+
let f = FieldElement::random(&mut rng);
105+
let expected = (a + b + c) * (d + e + f);
99106

100-
assert_eq!(LazyField::add(a, &b).mul(&LazyField::add(c, &d)), expected);
101-
assert_eq!(add_pair_then_mul(a, b, c, d), expected);
107+
assert_eq!(
108+
LazyField::add(a, &b)
109+
.add(&c)
110+
.mul(&LazyField::add(d, &e).add(&f)),
111+
expected
112+
);
113+
assert_eq!(add_triple_then_mul(a, b, c, d, e, f), expected);
102114

103115
let a = EagerField(a, PhantomData::<typenum::U1>);
104116
let b = EagerField(b, PhantomData::<typenum::U1>);
105117
let c = EagerField(c, PhantomData::<typenum::U1>);
106118
let d = EagerField(d, PhantomData::<typenum::U1>);
119+
let e = EagerField(e, PhantomData::<typenum::U1>);
120+
let f = EagerField(f, PhantomData::<typenum::U1>);
107121

108122
assert_eq!(
109-
LazyField::add(a, &b).mul(&LazyField::add(c, &d)).0,
123+
LazyField::add(a, &b)
124+
.add(&c)
125+
.mul(&LazyField::add(d, &e).add(&f))
126+
.0,
110127
expected
111128
);
112-
assert_eq!(add_pair_then_mul(a, b, c, d).0, expected);
129+
assert_eq!(add_triple_then_mul(a, b, c, d, e, f).0, expected);
113130
}
114131
}
115132
}

0 commit comments

Comments
 (0)