Skip to content

Commit 9d98b18

Browse files
committed
Test LazyFieldWithCapacity
1 parent 67e63ee commit 9d98b18

File tree

3 files changed

+57
-24
lines changed

3 files changed

+57
-24
lines changed

curve25519-dalek/src/hazmat/lazy_field.rs

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

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

107
use ff::Field;
118

@@ -43,18 +40,21 @@ pub trait LazyField<CapacityUsed: Unsigned>:
4340
/// The underlying type is allowed to have undefined semantics and MUST NOT be used directly.
4441
fn as_underlying(&self) -> &Self::Underlying;
4542

43+
// The type corresponding to a certain usage of capacity.
44+
// type ForCapacityUsed<TheoreticalCapacityUsed: Unsigned>: LazyField<TheoreticalCapacityUsed, >
45+
4646
/// Add two lazy elements where the result remains within the capacity.
4747
fn add<
4848
V: Unsigned + Add<CapacityUsed, Output: Unsigned + IsLessOrEqual<Self::Capacity, Output = B1>>,
4949
T: LazyField<V, Underlying = Self::Underlying>,
5050
>(
5151
self,
5252
other: &T,
53-
) -> impl Reducible<Output = <Self as Reducible>::Output>
54-
+ LazyField<
53+
) -> impl LazyField<
5554
<V as Add<CapacityUsed>>::Output,
5655
Capacity = Self::Capacity,
5756
Underlying = Self::Underlying,
57+
Output = <Self as Reducible>::Output,
5858
>;
5959

6060
/// Multiply two lazy elements.
@@ -73,5 +73,11 @@ pub trait LazyField<CapacityUsed: Unsigned>:
7373
///
7474
/// `LazyFieldWithCapacity<U1>` is _recommended_ due to the widespread popularity of 255-bit
7575
/// fields.
76-
pub trait LazyFieldWithCapacity<U: Unsigned + Cmp<Self::Capacity>>: LazyField<U1> {}
77-
impl<U: Unsigned + Cmp<Self::Capacity>, F: LazyField<U1>> LazyFieldWithCapacity<U> for F {}
76+
pub trait LazyFieldWithCapacity<U: Unsigned + IsLessOrEqual<Self::Capacity, Output = B1>>:
77+
LazyField<U1>
78+
{
79+
}
80+
impl<U: Unsigned + IsLessOrEqual<Self::Capacity, Output = B1>, F: LazyField<U1>>
81+
LazyFieldWithCapacity<U> for F
82+
{
83+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,11 @@ impl<CapacityUsed: Unsigned + IsLessOrEqual<U256, Output = B1>, F: Field> LazyFi
220220
>(
221221
self,
222222
other: &T,
223-
) -> impl Reducible<Output = <Self as Reducible>::Output>
224-
+ LazyField<
223+
) -> impl LazyField<
225224
<V as Add<CapacityUsed>>::Output,
226225
Capacity = Self::Capacity,
227226
Underlying = Self::Underlying,
227+
Output = <Self as Reducible>::Output,
228228
> {
229229
EagerField::<<V as Add<CapacityUsed>>::Output, F>(
230230
self.0 + other.as_underlying(),

curve25519-dalek/src/hazmat/lazy_field25519.rs

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ impl<CapacityUsed: Unsigned> LazyField<CapacityUsed> for FieldElement<CapacityUs
4444
>(
4545
self,
4646
other: &T,
47-
) -> impl Reducible<Output = <Self as Reducible>::Output>
48-
+ LazyField<
47+
) -> impl LazyField<
4948
<V as Add<CapacityUsed>>::Output,
5049
Capacity = Self::Capacity,
5150
Underlying = Self::Underlying,
51+
Output = <Self as Reducible>::Output,
5252
> {
5353
FieldElement::<<V as Add<CapacityUsed>>::Output>::from(&self.0.0 + &other.as_underlying().0)
5454
}
@@ -64,16 +64,34 @@ impl<CapacityUsed: Unsigned> LazyField<CapacityUsed> for FieldElement<CapacityUs
6464

6565
#[cfg(test)]
6666
mod tests {
67-
use rand_core::{OsRng, TryRngCore};
68-
use typenum::U3;
69-
7067
use crate::hazmat::lazy_field::{EagerField, LazyField, LazyFieldWithCapacity, Reducible};
68+
use typenum::{B1, U2, U3, type_operators::IsLessOrEqual};
69+
70+
fn add_triple_then_mul<F: LazyFieldWithCapacity<U3>>(
71+
a: F,
72+
b: F,
73+
c: F,
74+
d: F,
75+
e: F,
76+
f: F,
77+
) -> <F as Reducible>::Output
78+
where
79+
U2: IsLessOrEqual<F::Capacity, Output = B1>,
80+
U3: IsLessOrEqual<F::Capacity, Output = B1>,
81+
{
82+
let ab = a.add(&b);
83+
let abc = ab.add(&c);
84+
let de = d.add(&e);
85+
let def = de.add(&f);
86+
abc.mul(&def)
87+
}
7188

7289
#[test]
7390
fn lazy_add_then_mul() {
7491
use crate::hazmat::FieldElement;
7592
use core::marker::PhantomData;
7693
use ff::Field;
94+
use rand_core::{OsRng, TryRngCore};
7795

7896
let mut rng = OsRng.unwrap_err();
7997

@@ -85,19 +103,28 @@ mod tests {
85103
let f = FieldElement::random(&mut rng);
86104
let expected = (a + b + c) * (d + e + f);
87105

88-
assert_eq!(a.add(&b).add(&c).mul(&d.add(&e).add(&f)), expected);
106+
assert_eq!(
107+
LazyField::add(a, &b)
108+
.add(&c)
109+
.mul(&LazyField::add(d, &e).add(&f)),
110+
expected
111+
);
112+
assert_eq!(add_triple_then_mul(a, b, c, d, e, f), expected);
113+
114+
let a = EagerField(a, PhantomData::<typenum::U1>);
115+
let b = EagerField(b, PhantomData::<typenum::U1>);
116+
let c = EagerField(c, PhantomData::<typenum::U1>);
117+
let d = EagerField(d, PhantomData::<typenum::U1>);
118+
let e = EagerField(e, PhantomData::<typenum::U1>);
119+
let f = EagerField(f, PhantomData::<typenum::U1>);
89120

90121
assert_eq!(
91-
EagerField(a, PhantomData::<typenum::U1>)
92-
.add(&EagerField(b, PhantomData::<typenum::U1>))
93-
.add(&EagerField(c, PhantomData::<typenum::U1>))
94-
.mul(
95-
&EagerField(d, PhantomData::<typenum::U1>)
96-
.add(&EagerField(e, PhantomData::<typenum::U1>))
97-
.add(&EagerField(f, PhantomData::<typenum::U1>))
98-
)
122+
LazyField::add(a, &b)
123+
.add(&c)
124+
.mul(&LazyField::add(d, &e).add(&f))
99125
.0,
100126
expected
101127
);
128+
assert_eq!(add_triple_then_mul(a, b, c, d, e, f).0, expected);
102129
}
103130
}

0 commit comments

Comments
 (0)