Skip to content

Commit c7a2b4f

Browse files
committed
Introduce NewConcat/NewConcatMixed
1 parent 9cbea3a commit c7a2b4f

14 files changed

Lines changed: 171 additions & 162 deletions

File tree

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};
7+
use crypto_bigint::{I128, I256, I512, I1024, I2048, I4096, NonZero, Random, nlimbs};
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(&y)),
69+
|(x, y)| black_box(x.concatenating_mul::<{ I128::LIMBS }, { I256::LIMBS }>(&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(&y)),
77+
|(x, y)| black_box(x.concatenating_mul::<{ I256::LIMBS }, { I512::LIMBS }>(&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(&y)),
85+
|(x, y)| black_box(x.concatenating_mul::<{ I512::LIMBS }, { I1024::LIMBS }>(&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(&y)),
93+
|(x, y)| black_box(x.concatenating_mul::<{ I1024::LIMBS }, { I2048::LIMBS }>(&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(&y)),
101+
|(x, y)| black_box(x.concatenating_mul::<{ I2048::LIMBS }, { I4096::LIMBS }>(&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(&y)),
109+
|(x, y)| black_box(x.concatenating_mul::<{ I4096::LIMBS }, { nlimbs!(8192) }>(&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::{ConcatMixed, Gcd, Int, Uint};
315+
use crate::{ConcatenatingMul, 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>: ConcatMixed<Uint<LIMBS>, MixedOutput = Uint<DOUBLE>>,
412+
Uint<LIMBS>: ConcatenatingMul<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().as_int()
440+
*gcd.resize::<DOUBLE>().as_int()
441441
);
442442
}
443443

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

451451
fn test<const LIMBS: usize, const DOUBLE: usize>(lhs: Int<LIMBS>, rhs: Int<LIMBS>)
452452
where
453-
Uint<LIMBS>: ConcatMixed<Uint<LIMBS>, MixedOutput = Uint<DOUBLE>>,
453+
Uint<LIMBS>: ConcatenatingMul<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>: ConcatMixed<Uint<LIMBS>, MixedOutput = Uint<DOUBLE>>,
461+
Uint<LIMBS>: ConcatenatingMul<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-
ConcatMixed, Int, U64, U128, U192, U256, U384, U512, U768, U1024, U2048, U4096, U8192,
509-
Uint,
508+
ConcatenatingMul, Int, U64, U128, U192, U256, U384, U512, U768, U1024, U2048, U4096,
509+
U8192, Uint,
510510
};
511511

512512
fn test<const LIMBS: usize, const DOUBLE: usize>(lhs: Int<LIMBS>, rhs: Int<LIMBS>)
513513
where
514-
Uint<LIMBS>: ConcatMixed<Uint<LIMBS>, MixedOutput = Uint<DOUBLE>>,
514+
Uint<LIMBS>: ConcatenatingMul<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>: ConcatMixed<Uint<LIMBS>, MixedOutput = Uint<DOUBLE>>,
522+
Uint<LIMBS>: ConcatenatingMul<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-
ConcatMixed, Int, U64, U128, U192, U256, U384, U512, U768, U1024, U2048, U4096, U8192,
560-
Uint,
559+
ConcatenatingMul, Int, U64, U128, U192, U256, U384, U512, U768, U1024, U2048, U4096,
560+
U8192, Uint,
561561
};
562562

563563
fn test<const LIMBS: usize, const DOUBLE: usize>(lhs: Int<LIMBS>, rhs: Int<LIMBS>)
564564
where
565-
Uint<LIMBS>: ConcatMixed<Uint<LIMBS>, MixedOutput = Uint<DOUBLE>>,
565+
Uint<LIMBS>: ConcatenatingMul<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>: ConcatMixed<Uint<LIMBS>, MixedOutput = Uint<DOUBLE>>,
573+
Uint<LIMBS>: ConcatenatingMul<Uint<DOUBLE>>,
574574
{
575575
let neg_max = Int::MAX.wrapping_neg();
576576
test(neg_max, neg_max);

src/int/mul.rs

Lines changed: 3 additions & 9 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, ConcatMixed, ConstChoice, ConstCtOption, Int, Uint, Zero};
7+
use crate::{Checked, CheckedMul, 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,10 +51,7 @@ 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>
55-
where
56-
Uint<LIMBS>: ConcatMixed<Uint<RHS_LIMBS>, MixedOutput = Uint<WIDE_LIMBS>>,
57-
{
54+
) -> Int<WIDE_LIMBS> {
5855
let (lhs_abs, lhs_sign) = self.abs_sign();
5956
let (rhs_abs, rhs_sign) = rhs.abs_sign();
6057
let product_abs = lhs_abs.concatenating_mul(&rhs_abs);
@@ -76,10 +73,7 @@ impl<const LIMBS: usize> Int<LIMBS> {
7673
/// Squaring operations.
7774
impl<const LIMBS: usize> Int<LIMBS> {
7875
/// Square self, returning a concatenated "wide" result.
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-
{
76+
pub fn widening_square<const WIDE_LIMBS: usize>(&self) -> Uint<WIDE_LIMBS> {
8377
self.abs().widening_square()
8478
}
8579

src/int/mul_uint.rs

Lines changed: 2 additions & 5 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, ConcatMixed, ConstChoice, ConstCtOption, Int, Uint};
4+
use crate::{CheckedMul, 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,10 +70,7 @@ 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>
74-
where
75-
Uint<LIMBS>: ConcatMixed<Uint<RHS_LIMBS>, MixedOutput = Uint<WIDE_LIMBS>>,
76-
{
73+
) -> Int<WIDE_LIMBS> {
7774
let (lhs_abs, lhs_sign) = self.abs_sign();
7875
let product_abs = lhs_abs.concatenating_mul(rhs);
7976

src/modular.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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().split();
125+
let (lo, hi) = Modulus256::PARAMS.one.square::<{ nlimbs!(512) }>().split();
126126
assert_eq!(
127127
montgomery_reduction::<{ Modulus256::LIMBS }>(
128128
&(lo, hi),

src/modular/bingcd/xgcd.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
369369
#[cfg(all(test, not(miri)))]
370370
mod tests {
371371
use crate::modular::bingcd::xgcd::PatternXgcdOutput;
372-
use crate::{ConcatMixed, Gcd, Uint};
372+
use crate::{ConcatenatingMul, Gcd, Uint};
373373
use core::ops::Div;
374374
use num_traits::Zero;
375375

@@ -584,7 +584,7 @@ mod tests {
584584
rhs: Uint<LIMBS>,
585585
output: PatternXgcdOutput<LIMBS>,
586586
) where
587-
Uint<LIMBS>: ConcatMixed<Uint<LIMBS>, MixedOutput = Uint<DOUBLE>>,
587+
Uint<LIMBS>: ConcatenatingMul<Uint<DOUBLE>>,
588588
{
589589
// Test the gcd
590590
assert_eq!(lhs.gcd(&rhs), output.gcd, "{lhs} {rhs}");
@@ -598,7 +598,7 @@ mod tests {
598598
let (x, y) = output.bezout_coefficients();
599599
assert_eq!(
600600
x.concatenating_mul_uint(&lhs) + y.concatenating_mul_uint(&rhs),
601-
*output.gcd.resize().as_int(),
601+
*output.gcd.resize::<DOUBLE>().as_int(),
602602
);
603603

604604
// Test the Bezout coefficients for minimality
@@ -613,23 +613,23 @@ mod tests {
613613
mod test_binxgcd_nz {
614614
use crate::modular::bingcd::xgcd::tests::test_xgcd;
615615
use crate::{
616-
ConcatMixed, Int, U64, U128, U192, U256, U384, U512, U768, U1024, U2048, U4096, U8192,
617-
Uint,
616+
ConcatenatingMul, Int, U64, U128, U192, U256, U384, U512, U768, U1024, U2048, U4096,
617+
U8192, Uint,
618618
};
619619

620620
fn binxgcd_nz_test<const LIMBS: usize, const DOUBLE: usize>(
621621
lhs: Uint<LIMBS>,
622622
rhs: Uint<LIMBS>,
623623
) where
624-
Uint<LIMBS>: ConcatMixed<Uint<LIMBS>, MixedOutput = Uint<DOUBLE>>,
624+
Uint<LIMBS>: ConcatenatingMul<Uint<DOUBLE>>,
625625
{
626626
let output = lhs.to_odd().unwrap().binxgcd_nz(&rhs.to_nz().unwrap());
627627
test_xgcd(lhs, rhs, output);
628628
}
629629

630630
fn binxgcd_nz_tests<const LIMBS: usize, const DOUBLE: usize>()
631631
where
632-
Uint<LIMBS>: ConcatMixed<Uint<LIMBS>, MixedOutput = Uint<DOUBLE>>,
632+
Uint<LIMBS>: ConcatenatingMul<Uint<DOUBLE>>,
633633
{
634634
let max_int = *Int::MAX.as_uint();
635635
let int_abs_min = Int::MIN.abs();
@@ -665,15 +665,15 @@ mod tests {
665665
mod test_classic_binxgcd {
666666
use crate::modular::bingcd::xgcd::tests::test_xgcd;
667667
use crate::{
668-
ConcatMixed, Int, U64, U128, U192, U256, U384, U512, U768, U1024, U2048, U4096, U8192,
669-
Uint,
668+
ConcatenatingMul, Int, U64, U128, U192, U256, U384, U512, U768, U1024, U2048, U4096,
669+
U8192, Uint,
670670
};
671671

672672
fn classic_binxgcd_test<const LIMBS: usize, const DOUBLE: usize>(
673673
lhs: Uint<LIMBS>,
674674
rhs: Uint<LIMBS>,
675675
) where
676-
Uint<LIMBS>: ConcatMixed<Uint<LIMBS>, MixedOutput = Uint<DOUBLE>>,
676+
Uint<LIMBS>: ConcatenatingMul<Uint<DOUBLE>>,
677677
{
678678
let output = lhs
679679
.to_odd()
@@ -685,7 +685,7 @@ mod tests {
685685

686686
fn classic_binxgcd_tests<const LIMBS: usize, const DOUBLE: usize>()
687687
where
688-
Uint<LIMBS>: ConcatMixed<Uint<LIMBS>, MixedOutput = Uint<DOUBLE>>,
688+
Uint<LIMBS>: ConcatenatingMul<Uint<DOUBLE>>,
689689
{
690690
let max_int = *Int::MAX.as_uint();
691691

@@ -718,13 +718,13 @@ mod tests {
718718
use crate::modular::bingcd::xgcd::tests::test_xgcd;
719719
use crate::modular::bingcd::xgcd::{DOUBLE_SUMMARY_LIMBS, SUMMARY_BITS, SUMMARY_LIMBS};
720720
use crate::{
721-
ConcatMixed, Int, U64, U128, U192, U256, U384, U512, U768, U1024, U2048, U4096, U8192,
722-
Uint,
721+
ConcatenatingMul, Int, U64, U128, U192, U256, U384, U512, U768, U1024, U2048, U4096,
722+
U8192, Uint,
723723
};
724724

725725
fn test<const LIMBS: usize, const DOUBLE: usize>(lhs: Uint<LIMBS>, rhs: Uint<LIMBS>)
726726
where
727-
Uint<LIMBS>: ConcatMixed<Uint<LIMBS>, MixedOutput = Uint<DOUBLE>>,
727+
Uint<LIMBS>: ConcatenatingMul<Uint<DOUBLE>>,
728728
{
729729
let output = lhs
730730
.to_odd()
@@ -736,7 +736,7 @@ mod tests {
736736

737737
fn run_tests<const LIMBS: usize, const DOUBLE: usize>()
738738
where
739-
Uint<LIMBS>: ConcatMixed<Uint<LIMBS>, MixedOutput = Uint<DOUBLE>>,
739+
Uint<LIMBS>: ConcatenatingMul<Uint<DOUBLE>>,
740740
{
741741
let upper_bound = *Int::MAX.as_uint();
742742
test(Uint::ONE, Uint::ONE);
@@ -771,10 +771,10 @@ mod tests {
771771
a.compact::<SUMMARY_BITS, DOUBLE_SUMMARY_LIMBS>(U256::BITS)
772772
< b.compact::<SUMMARY_BITS, DOUBLE_SUMMARY_LIMBS>(U256::BITS)
773773
);
774-
test(a, b);
774+
test::<{ U256::LIMBS }, { U512::LIMBS }>(a, b);
775775

776776
// Case #2: a < b but a.compact() > b.compact()
777-
test(b, a);
777+
test::<{ U256::LIMBS }, { U512::LIMBS }>(b, a);
778778

779779
// Case #3: a > b but a.compact() = b.compact()
780780
let a = U256::from_be_hex(
@@ -788,10 +788,10 @@ mod tests {
788788
a.compact::<SUMMARY_BITS, DOUBLE_SUMMARY_LIMBS>(U256::BITS),
789789
b.compact::<SUMMARY_BITS, DOUBLE_SUMMARY_LIMBS>(U256::BITS)
790790
);
791-
test(a, b);
791+
test::<{ U256::LIMBS }, { U512::LIMBS }>(a, b);
792792

793793
// Case #4: a < b but a.compact() = b.compact()
794-
test(b, a);
794+
test::<{ U256::LIMBS }, { U512::LIMBS }>(b, a);
795795
}
796796

797797
#[test]

0 commit comments

Comments
 (0)