Skip to content

Commit f3e6291

Browse files
authored
Revert "Use traits implemented for all Uint/Int types" (#1015)
This reverts commit f268e8d (#1008) See discussion on #1008 for context
1 parent e322137 commit f3e6291

File tree

23 files changed

+573
-400
lines changed

23 files changed

+573
-400
lines changed

benches/int.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rand_core::SeedableRng;
44
use std::hint::black_box;
55
use std::ops::Div;
66

7-
use crypto_bigint::{I128, I256, I512, I1024, I2048, I4096, NonZero, Random, nlimbs};
7+
use crypto_bigint::{I128, I256, I512, I1024, I2048, I4096, NonZero, Random};
88

99
fn bench_mul(c: &mut Criterion) {
1010
let mut rng = ChaCha8Rng::from_seed([7u8; 32]);
@@ -66,47 +66,47 @@ fn bench_concatenating_mul(c: &mut Criterion) {
6666
group.bench_function("concatenating_mul, I128xI128", |b| {
6767
b.iter_batched(
6868
|| (I128::random(&mut rng), I128::random(&mut rng)),
69-
|(x, y)| black_box(x.concatenating_mul::<{ I128::LIMBS }, { I256::LIMBS }>(&y)),
69+
|(x, y)| black_box(x.concatenating_mul(&y)),
7070
BatchSize::SmallInput,
7171
)
7272
});
7373

7474
group.bench_function("concatenating_mul, I256xI256", |b| {
7575
b.iter_batched(
7676
|| (I256::random(&mut rng), I256::random(&mut rng)),
77-
|(x, y)| black_box(x.concatenating_mul::<{ I256::LIMBS }, { I512::LIMBS }>(&y)),
77+
|(x, y)| black_box(x.concatenating_mul(&y)),
7878
BatchSize::SmallInput,
7979
)
8080
});
8181

8282
group.bench_function("concatenating_mul, I512xI512", |b| {
8383
b.iter_batched(
8484
|| (I512::random(&mut rng), I512::random(&mut rng)),
85-
|(x, y)| black_box(x.concatenating_mul::<{ I512::LIMBS }, { I1024::LIMBS }>(&y)),
85+
|(x, y)| black_box(x.concatenating_mul(&y)),
8686
BatchSize::SmallInput,
8787
)
8888
});
8989

9090
group.bench_function("concatenating_mul, I1024xI1024", |b| {
9191
b.iter_batched(
9292
|| (I1024::random(&mut rng), I1024::random(&mut rng)),
93-
|(x, y)| black_box(x.concatenating_mul::<{ I1024::LIMBS }, { I2048::LIMBS }>(&y)),
93+
|(x, y)| black_box(x.concatenating_mul(&y)),
9494
BatchSize::SmallInput,
9595
)
9696
});
9797

9898
group.bench_function("concatenating_mul, I2048xI2048", |b| {
9999
b.iter_batched(
100100
|| (I2048::random(&mut rng), I2048::random(&mut rng)),
101-
|(x, y)| black_box(x.concatenating_mul::<{ I2048::LIMBS }, { I4096::LIMBS }>(&y)),
101+
|(x, y)| black_box(x.concatenating_mul(&y)),
102102
BatchSize::SmallInput,
103103
)
104104
});
105105

106106
group.bench_function("concatenating_mul, I4096xI4096", |b| {
107107
b.iter_batched(
108108
|| (I4096::random(&mut rng), I4096::random(&mut rng)),
109-
|(x, y)| black_box(x.concatenating_mul::<{ I4096::LIMBS }, { nlimbs!(8192) }>(&y)),
109+
|(x, y)| black_box(x.concatenating_mul(&y)),
110110
BatchSize::SmallInput,
111111
)
112112
});

src/int/gcd.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ impl<const LIMBS: usize> Xgcd for OddInt<LIMBS> {
312312
#[cfg(all(test, not(miri)))]
313313
mod tests {
314314
use crate::int::gcd::{IntXgcdOutput, NonZeroIntXgcdOutput, OddIntXgcdOutput};
315-
use crate::{ConcatenatingMul, Gcd, Int, Uint};
315+
use crate::{ConcatMixed, Gcd, Int, Uint};
316316
use num_traits::Zero;
317317

318318
impl<const LIMBS: usize> From<NonZeroIntXgcdOutput<LIMBS>> for IntXgcdOutput<LIMBS> {
@@ -409,7 +409,7 @@ mod tests {
409409
rhs: Int<LIMBS>,
410410
output: IntXgcdOutput<LIMBS>,
411411
) where
412-
Uint<LIMBS>: ConcatenatingMul<Uint<DOUBLE>>,
412+
Uint<LIMBS>: ConcatMixed<Uint<LIMBS>, MixedOutput = Uint<DOUBLE>>,
413413
{
414414
let gcd = lhs.gcd(&rhs);
415415
assert_eq!(gcd, output.gcd);
@@ -437,28 +437,28 @@ mod tests {
437437
assert_eq!(
438438
x.concatenating_mul(&lhs)
439439
.wrapping_add(&y.concatenating_mul(&rhs)),
440-
*gcd.resize::<DOUBLE>().as_int()
440+
*gcd.resize().as_int()
441441
);
442442
}
443443

444444
mod test_int_xgcd {
445445
use crate::int::gcd::tests::xgcd_test;
446446
use crate::{
447-
ConcatenatingMul, Gcd, Int, U64, U128, U192, U256, U384, U512, U768, U1024, U2048,
448-
U4096, U8192, Uint,
447+
ConcatMixed, Gcd, Int, U64, U128, U192, U256, U384, U512, U768, U1024, U2048, U4096,
448+
U8192, Uint,
449449
};
450450

451451
fn test<const LIMBS: usize, const DOUBLE: usize>(lhs: Int<LIMBS>, rhs: Int<LIMBS>)
452452
where
453-
Uint<LIMBS>: ConcatenatingMul<Uint<DOUBLE>>,
453+
Uint<LIMBS>: ConcatMixed<Uint<LIMBS>, MixedOutput = Uint<DOUBLE>>,
454454
Int<LIMBS>: Gcd<Output = Uint<LIMBS>>,
455455
{
456456
xgcd_test(lhs, rhs, lhs.xgcd(&rhs))
457457
}
458458

459459
fn run_tests<const LIMBS: usize, const DOUBLE: usize>()
460460
where
461-
Uint<LIMBS>: ConcatenatingMul<Uint<DOUBLE>>,
461+
Uint<LIMBS>: ConcatMixed<Uint<LIMBS>, MixedOutput = Uint<DOUBLE>>,
462462
Int<LIMBS>: Gcd<Output = Uint<LIMBS>>,
463463
{
464464
test(Int::MIN, Int::MIN);
@@ -505,21 +505,21 @@ mod tests {
505505
mod test_nonzero_int_xgcd {
506506
use crate::int::gcd::tests::xgcd_test;
507507
use crate::{
508-
ConcatenatingMul, Int, U64, U128, U192, U256, U384, U512, U768, U1024, U2048, U4096,
509-
U8192, Uint,
508+
ConcatMixed, Int, U64, U128, U192, U256, U384, U512, U768, U1024, U2048, U4096, U8192,
509+
Uint,
510510
};
511511

512512
fn test<const LIMBS: usize, const DOUBLE: usize>(lhs: Int<LIMBS>, rhs: Int<LIMBS>)
513513
where
514-
Uint<LIMBS>: ConcatenatingMul<Uint<DOUBLE>>,
514+
Uint<LIMBS>: ConcatMixed<Uint<LIMBS>, MixedOutput = Uint<DOUBLE>>,
515515
{
516516
let output = lhs.to_nz().unwrap().xgcd(&rhs.to_nz().unwrap());
517517
xgcd_test(lhs, rhs, output.into());
518518
}
519519

520520
fn run_tests<const LIMBS: usize, const DOUBLE: usize>()
521521
where
522-
Uint<LIMBS>: ConcatenatingMul<Uint<DOUBLE>>,
522+
Uint<LIMBS>: ConcatMixed<Uint<LIMBS>, MixedOutput = Uint<DOUBLE>>,
523523
{
524524
test(Int::MIN, Int::MIN);
525525
test(Int::MIN, Int::MINUS_ONE);
@@ -556,21 +556,21 @@ mod tests {
556556
mod test_odd_int_xgcd {
557557
use crate::int::gcd::tests::xgcd_test;
558558
use crate::{
559-
ConcatenatingMul, Int, U64, U128, U192, U256, U384, U512, U768, U1024, U2048, U4096,
560-
U8192, Uint,
559+
ConcatMixed, Int, U64, U128, U192, U256, U384, U512, U768, U1024, U2048, U4096, U8192,
560+
Uint,
561561
};
562562

563563
fn test<const LIMBS: usize, const DOUBLE: usize>(lhs: Int<LIMBS>, rhs: Int<LIMBS>)
564564
where
565-
Uint<LIMBS>: ConcatenatingMul<Uint<DOUBLE>>,
565+
Uint<LIMBS>: ConcatMixed<Uint<LIMBS>, MixedOutput = Uint<DOUBLE>>,
566566
{
567567
let output = lhs.to_odd().unwrap().xgcd(&rhs.to_nz().unwrap());
568568
xgcd_test(lhs, rhs, output.into());
569569
}
570570

571571
fn run_tests<const LIMBS: usize, const DOUBLE: usize>()
572572
where
573-
Uint<LIMBS>: ConcatenatingMul<Uint<DOUBLE>>,
573+
Uint<LIMBS>: ConcatMixed<Uint<LIMBS>, MixedOutput = Uint<DOUBLE>>,
574574
{
575575
let neg_max = Int::MAX.wrapping_neg();
576576
test(neg_max, neg_max);

src/int/mul.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use core::ops::{Mul, MulAssign};
44
use num_traits::WrappingMul;
55
use subtle::CtOption;
66

7-
use crate::{Checked, CheckedMul, ConstChoice, ConstCtOption, Int, Uint, Zero};
7+
use crate::{Checked, CheckedMul, ConcatMixed, ConstChoice, ConstCtOption, Int, Uint, Zero};
88

99
impl<const LIMBS: usize> Int<LIMBS> {
1010
/// Compute "wide" multiplication as a 3-tuple `(lo, hi, negate)`.
@@ -51,7 +51,10 @@ impl<const LIMBS: usize> Int<LIMBS> {
5151
pub const fn concatenating_mul<const RHS_LIMBS: usize, const WIDE_LIMBS: usize>(
5252
&self,
5353
rhs: &Int<RHS_LIMBS>,
54-
) -> Int<WIDE_LIMBS> {
54+
) -> Int<WIDE_LIMBS>
55+
where
56+
Uint<LIMBS>: ConcatMixed<Uint<RHS_LIMBS>, MixedOutput = Uint<WIDE_LIMBS>>,
57+
{
5558
let (lhs_abs, lhs_sign) = self.abs_sign();
5659
let (rhs_abs, rhs_sign) = rhs.abs_sign();
5760
let product_abs = lhs_abs.concatenating_mul(&rhs_abs);
@@ -73,7 +76,10 @@ impl<const LIMBS: usize> Int<LIMBS> {
7376
/// Squaring operations.
7477
impl<const LIMBS: usize> Int<LIMBS> {
7578
/// Square self, returning a concatenated "wide" result.
76-
pub fn widening_square<const WIDE_LIMBS: usize>(&self) -> Uint<WIDE_LIMBS> {
79+
pub fn widening_square<const WIDE_LIMBS: usize>(&self) -> Uint<WIDE_LIMBS>
80+
where
81+
Uint<LIMBS>: ConcatMixed<Uint<LIMBS>, MixedOutput = Uint<WIDE_LIMBS>>,
82+
{
7783
self.abs().widening_square()
7884
}
7985

src/int/mul_uint.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use core::ops::Mul;
22
use subtle::CtOption;
33

4-
use crate::{CheckedMul, ConstChoice, ConstCtOption, Int, Uint};
4+
use crate::{CheckedMul, ConcatMixed, ConstChoice, ConstCtOption, Int, Uint};
55

66
impl<const LIMBS: usize> Int<LIMBS> {
77
/// Compute "wide" multiplication between an [`Int`] and [`Uint`] as 3-tuple `(lo, hi, negate)`.
@@ -70,7 +70,10 @@ impl<const LIMBS: usize> Int<LIMBS> {
7070
pub const fn concatenating_mul_uint<const RHS_LIMBS: usize, const WIDE_LIMBS: usize>(
7171
&self,
7272
rhs: &Uint<RHS_LIMBS>,
73-
) -> Int<WIDE_LIMBS> {
73+
) -> Int<WIDE_LIMBS>
74+
where
75+
Uint<LIMBS>: ConcatMixed<Uint<RHS_LIMBS>, MixedOutput = Uint<WIDE_LIMBS>>,
76+
{
7477
let (lhs_abs, lhs_sign) = self.abs_sign();
7578
let product_abs = lhs_abs.concatenating_mul(rhs);
7679

src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,6 @@
167167
#[macro_use]
168168
extern crate alloc;
169169

170-
pub use uint::encoding::{EncodedUint, TryFromSliceError};
171-
172170
#[cfg(feature = "rand_core")]
173171
pub use rand_core;
174172
#[cfg(feature = "rlp")]

src/modular.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub trait Retrieve {
5555
#[cfg(test)]
5656
mod tests {
5757
use crate::{
58-
NonZero, U64, U128, U256, U512, Uint, const_monty_params,
58+
NonZero, U64, U128, U256, Uint, const_monty_params,
5959
modular::{
6060
const_monty_form::{ConstMontyForm, ConstMontyParams},
6161
mul::{mul_montgomery_form, square_montgomery_form},
@@ -122,7 +122,7 @@ mod tests {
122122
#[test]
123123
fn test_reducing_r2_wide() {
124124
// Divide the value ONE^2 by R, which should equal ONE
125-
let (lo, hi) = Modulus256::PARAMS.one.square::<{ nlimbs!(512) }>().split();
125+
let (lo, hi) = Modulus256::PARAMS.one.square().split();
126126
assert_eq!(
127127
montgomery_reduction::<{ Modulus256::LIMBS }>(
128128
&(lo, hi),
@@ -158,7 +158,7 @@ mod tests {
158158

159159
// Computing xR mod modulus without Montgomery reduction
160160
let (lo, hi) = x.widening_mul(&Modulus256::PARAMS.one);
161-
let c: U512 = lo.concat(&hi);
161+
let c = lo.concat(&hi);
162162
let red =
163163
c.rem_vartime(&NonZero::new(Modulus256::PARAMS.modulus.0.concat(&U256::ZERO)).unwrap());
164164
let (lo, hi) = red.split();
@@ -287,7 +287,7 @@ mod tests {
287287

288288
// Computing xR mod modulus without Montgomery reduction
289289
let (lo, hi) = x.widening_mul(&Modulus256::PARAMS.one);
290-
let c: U512 = lo.concat(&hi);
290+
let c = lo.concat(&hi);
291291
let red =
292292
c.rem_vartime(&NonZero::new(Modulus256::PARAMS.modulus.0.concat(&U256::ZERO)).unwrap());
293293
let (lo, hi) = red.split();

0 commit comments

Comments
 (0)