Skip to content

Commit 0a82375

Browse files
committed
Add basic test for LazyField, EagerField
1 parent 8c2f299 commit 0a82375

File tree

4 files changed

+65
-6
lines changed

4 files changed

+65
-6
lines changed

curve25519-dalek/src/hazmat.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub mod lazy_field;
1818
mod lazy_field25519;
1919
pub(crate) use lazy_field25519::UnderlyingCapacity;
2020

21+
/// An opaque view of the field element backend.
2122
/*
2223
The `Underlying` struct is exposed via the `LazyField` trait. As the underlying field
2324
implementations don't have safe arithmetic, we don't want to expose their arithmetic, but we must

curve25519-dalek/src/hazmat/lazy_field.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
33
use core::{fmt::Debug, ops::Add};
44

5-
use typenum::{B1, U0, Unsigned, type_operators::IsLessOrEqual};
5+
use typenum::{
6+
B1, U0, Unsigned,
7+
type_operators::{Cmp, IsLessOrEqual},
8+
};
69

710
use ff::Field;
811

@@ -47,7 +50,12 @@ pub trait LazyField<CapacityUsed: Unsigned>:
4750
>(
4851
self,
4952
other: &T,
50-
) -> impl LazyField<<V as Add<CapacityUsed>>::Output, Capacity = Self::Capacity>;
53+
) -> impl Reducible<Output = <Self as Reducible>::Output>
54+
+ LazyField<
55+
<V as Add<CapacityUsed>>::Output,
56+
Capacity = Self::Capacity,
57+
Underlying = Self::Underlying,
58+
>;
5159

5260
/// Multiply two lazy elements.
5361
///
@@ -65,5 +73,5 @@ pub trait LazyField<CapacityUsed: Unsigned>:
6573
///
6674
/// `LazyFieldWithCapacity<U1>` is _recommended_ due to the widespread popularity of 255-bit
6775
/// fields.
68-
pub trait LazyFieldWithCapacity<U: Unsigned> {}
69-
impl<U: Unsigned, F: LazyField<U0>> LazyFieldWithCapacity<U> for F {}
76+
pub trait LazyFieldWithCapacity<U: Unsigned + Cmp<Self::Capacity>>: LazyField<U0> {}
77+
impl<U: Unsigned + Cmp<Self::Capacity>, F: LazyField<U0>> LazyFieldWithCapacity<U> for F {}

curve25519-dalek/src/hazmat/lazy_field/eager.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,12 @@ impl<CapacityUsed: Unsigned + IsLessOrEqual<U256, Output = B1>, F: Field> LazyFi
220220
>(
221221
self,
222222
other: &T,
223-
) -> impl LazyField<<V as Add<CapacityUsed>>::Output, Capacity = Self::Capacity> {
223+
) -> impl Reducible<Output = <Self as Reducible>::Output>
224+
+ LazyField<
225+
<V as Add<CapacityUsed>>::Output,
226+
Capacity = Self::Capacity,
227+
Underlying = Self::Underlying,
228+
> {
224229
EagerField::<<V as Add<CapacityUsed>>::Output, F>(
225230
self.0 + other.as_underlying(),
226231
PhantomData,

curve25519-dalek/src/hazmat/lazy_field25519.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ impl<CapacityUsed: Unsigned> LazyField<CapacityUsed> for FieldElement<CapacityUs
4444
>(
4545
self,
4646
other: &T,
47-
) -> impl LazyField<<V as Add<CapacityUsed>>::Output, Capacity = Self::Capacity> {
47+
) -> impl Reducible<Output = <Self as Reducible>::Output>
48+
+ LazyField<
49+
<V as Add<CapacityUsed>>::Output,
50+
Capacity = Self::Capacity,
51+
Underlying = Self::Underlying,
52+
> {
4853
FieldElement::<<V as Add<CapacityUsed>>::Output>::from(&self.0.0 + &other.as_underlying().0)
4954
}
5055

@@ -56,3 +61,43 @@ impl<CapacityUsed: Unsigned> LazyField<CapacityUsed> for FieldElement<CapacityUs
5661
FieldElement::from(Underlying::from_bytes(&unreduced.to_bytes()))
5762
}
5863
}
64+
65+
#[cfg(test)]
66+
mod tests {
67+
use rand_core::{OsRng, TryRngCore};
68+
use typenum::U3;
69+
70+
use crate::hazmat::lazy_field::{EagerField, LazyField, LazyFieldWithCapacity, Reducible};
71+
72+
#[test]
73+
fn three_add_and_then_mul() {
74+
use crate::hazmat::FieldElement;
75+
use core::marker::PhantomData;
76+
use ff::Field;
77+
78+
let mut rng = OsRng.unwrap_err();
79+
80+
let a = FieldElement::random(&mut rng);
81+
let b = FieldElement::random(&mut rng);
82+
let c = FieldElement::random(&mut rng);
83+
let d = FieldElement::random(&mut rng);
84+
let e = FieldElement::random(&mut rng);
85+
let f = FieldElement::random(&mut rng);
86+
let expected = (a + b + c) * (d + e + f);
87+
88+
assert_eq!(a.add(&b).add(&c).mul(&d.add(&e).add(&f)), expected);
89+
90+
assert_eq!(
91+
EagerField(a, PhantomData::<typenum::U0>)
92+
.add(&EagerField(b, PhantomData::<typenum::U0>))
93+
.add(&EagerField(c, PhantomData::<typenum::U0>))
94+
.mul(
95+
&EagerField(d, PhantomData::<typenum::U0>)
96+
.add(&EagerField(e, PhantomData::<typenum::U0>))
97+
.add(&EagerField(f, PhantomData::<typenum::U0>))
98+
)
99+
.0,
100+
expected
101+
);
102+
}
103+
}

0 commit comments

Comments
 (0)