Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions benches/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rand_core::SeedableRng;
use std::hint::black_box;
use std::ops::Div;

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

fn bench_mul(c: &mut Criterion) {
let mut rng = ChaCha8Rng::from_seed([7u8; 32]);
Expand Down Expand Up @@ -66,47 +66,47 @@ fn bench_concatenating_mul(c: &mut Criterion) {
group.bench_function("concatenating_mul, I128xI128", |b| {
b.iter_batched(
|| (I128::random(&mut rng), I128::random(&mut rng)),
|(x, y)| black_box(x.concatenating_mul(&y)),
|(x, y)| black_box(x.concatenating_mul::<{ I128::LIMBS }, { I256::LIMBS }>(&y)),
BatchSize::SmallInput,
)
});

group.bench_function("concatenating_mul, I256xI256", |b| {
b.iter_batched(
|| (I256::random(&mut rng), I256::random(&mut rng)),
|(x, y)| black_box(x.concatenating_mul(&y)),
|(x, y)| black_box(x.concatenating_mul::<{ I256::LIMBS }, { I512::LIMBS }>(&y)),
BatchSize::SmallInput,
)
});

group.bench_function("concatenating_mul, I512xI512", |b| {
b.iter_batched(
|| (I512::random(&mut rng), I512::random(&mut rng)),
|(x, y)| black_box(x.concatenating_mul(&y)),
|(x, y)| black_box(x.concatenating_mul::<{ I512::LIMBS }, { I1024::LIMBS }>(&y)),
BatchSize::SmallInput,
)
});

group.bench_function("concatenating_mul, I1024xI1024", |b| {
b.iter_batched(
|| (I1024::random(&mut rng), I1024::random(&mut rng)),
|(x, y)| black_box(x.concatenating_mul(&y)),
|(x, y)| black_box(x.concatenating_mul::<{ I1024::LIMBS }, { I2048::LIMBS }>(&y)),
BatchSize::SmallInput,
)
});

group.bench_function("concatenating_mul, I2048xI2048", |b| {
b.iter_batched(
|| (I2048::random(&mut rng), I2048::random(&mut rng)),
|(x, y)| black_box(x.concatenating_mul(&y)),
|(x, y)| black_box(x.concatenating_mul::<{ I2048::LIMBS }, { I4096::LIMBS }>(&y)),
BatchSize::SmallInput,
)
});

group.bench_function("concatenating_mul, I4096xI4096", |b| {
b.iter_batched(
|| (I4096::random(&mut rng), I4096::random(&mut rng)),
|(x, y)| black_box(x.concatenating_mul(&y)),
|(x, y)| black_box(x.concatenating_mul::<{ I4096::LIMBS }, { nlimbs!(8192) }>(&y)),
BatchSize::SmallInput,
)
});
Expand Down
30 changes: 15 additions & 15 deletions src/int/gcd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ impl<const LIMBS: usize> Xgcd for OddInt<LIMBS> {
#[cfg(all(test, not(miri)))]
mod tests {
use crate::int::gcd::{IntXgcdOutput, NonZeroIntXgcdOutput, OddIntXgcdOutput};
use crate::{ConcatMixed, Gcd, Int, Uint};
use crate::{ConcatenatingMul, Gcd, Int, Uint};
use num_traits::Zero;

impl<const LIMBS: usize> From<NonZeroIntXgcdOutput<LIMBS>> for IntXgcdOutput<LIMBS> {
Expand Down Expand Up @@ -409,7 +409,7 @@ mod tests {
rhs: Int<LIMBS>,
output: IntXgcdOutput<LIMBS>,
) where
Uint<LIMBS>: ConcatMixed<Uint<LIMBS>, MixedOutput = Uint<DOUBLE>>,
Uint<LIMBS>: ConcatenatingMul<Uint<DOUBLE>>,
{
let gcd = lhs.gcd(&rhs);
assert_eq!(gcd, output.gcd);
Expand Down Expand Up @@ -437,28 +437,28 @@ mod tests {
assert_eq!(
x.concatenating_mul(&lhs)
.wrapping_add(&y.concatenating_mul(&rhs)),
*gcd.resize().as_int()
*gcd.resize::<DOUBLE>().as_int()
);
}

mod test_int_xgcd {
use crate::int::gcd::tests::xgcd_test;
use crate::{
ConcatMixed, Gcd, Int, U64, U128, U192, U256, U384, U512, U768, U1024, U2048, U4096,
U8192, Uint,
ConcatenatingMul, Gcd, Int, U64, U128, U192, U256, U384, U512, U768, U1024, U2048,
U4096, U8192, Uint,
};

fn test<const LIMBS: usize, const DOUBLE: usize>(lhs: Int<LIMBS>, rhs: Int<LIMBS>)
where
Uint<LIMBS>: ConcatMixed<Uint<LIMBS>, MixedOutput = Uint<DOUBLE>>,
Uint<LIMBS>: ConcatenatingMul<Uint<DOUBLE>>,
Int<LIMBS>: Gcd<Output = Uint<LIMBS>>,
{
xgcd_test(lhs, rhs, lhs.xgcd(&rhs))
}

fn run_tests<const LIMBS: usize, const DOUBLE: usize>()
where
Uint<LIMBS>: ConcatMixed<Uint<LIMBS>, MixedOutput = Uint<DOUBLE>>,
Uint<LIMBS>: ConcatenatingMul<Uint<DOUBLE>>,
Int<LIMBS>: Gcd<Output = Uint<LIMBS>>,
{
test(Int::MIN, Int::MIN);
Expand Down Expand Up @@ -505,21 +505,21 @@ mod tests {
mod test_nonzero_int_xgcd {
use crate::int::gcd::tests::xgcd_test;
use crate::{
ConcatMixed, Int, U64, U128, U192, U256, U384, U512, U768, U1024, U2048, U4096, U8192,
Uint,
ConcatenatingMul, Int, U64, U128, U192, U256, U384, U512, U768, U1024, U2048, U4096,
U8192, Uint,
};

fn test<const LIMBS: usize, const DOUBLE: usize>(lhs: Int<LIMBS>, rhs: Int<LIMBS>)
where
Uint<LIMBS>: ConcatMixed<Uint<LIMBS>, MixedOutput = Uint<DOUBLE>>,
Uint<LIMBS>: ConcatenatingMul<Uint<DOUBLE>>,
{
let output = lhs.to_nz().unwrap().xgcd(&rhs.to_nz().unwrap());
xgcd_test(lhs, rhs, output.into());
}

fn run_tests<const LIMBS: usize, const DOUBLE: usize>()
where
Uint<LIMBS>: ConcatMixed<Uint<LIMBS>, MixedOutput = Uint<DOUBLE>>,
Uint<LIMBS>: ConcatenatingMul<Uint<DOUBLE>>,
{
test(Int::MIN, Int::MIN);
test(Int::MIN, Int::MINUS_ONE);
Expand Down Expand Up @@ -556,21 +556,21 @@ mod tests {
mod test_odd_int_xgcd {
use crate::int::gcd::tests::xgcd_test;
use crate::{
ConcatMixed, Int, U64, U128, U192, U256, U384, U512, U768, U1024, U2048, U4096, U8192,
Uint,
ConcatenatingMul, Int, U64, U128, U192, U256, U384, U512, U768, U1024, U2048, U4096,
U8192, Uint,
};

fn test<const LIMBS: usize, const DOUBLE: usize>(lhs: Int<LIMBS>, rhs: Int<LIMBS>)
where
Uint<LIMBS>: ConcatMixed<Uint<LIMBS>, MixedOutput = Uint<DOUBLE>>,
Uint<LIMBS>: ConcatenatingMul<Uint<DOUBLE>>,
{
let output = lhs.to_odd().unwrap().xgcd(&rhs.to_nz().unwrap());
xgcd_test(lhs, rhs, output.into());
}

fn run_tests<const LIMBS: usize, const DOUBLE: usize>()
where
Uint<LIMBS>: ConcatMixed<Uint<LIMBS>, MixedOutput = Uint<DOUBLE>>,
Uint<LIMBS>: ConcatenatingMul<Uint<DOUBLE>>,
{
let neg_max = Int::MAX.wrapping_neg();
test(neg_max, neg_max);
Expand Down
12 changes: 3 additions & 9 deletions src/int/mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::ops::{Mul, MulAssign};
use num_traits::WrappingMul;
use subtle::CtOption;

use crate::{Checked, CheckedMul, ConcatMixed, ConstChoice, ConstCtOption, Int, Uint, Zero};
use crate::{Checked, CheckedMul, ConstChoice, ConstCtOption, Int, Uint, Zero};

impl<const LIMBS: usize> Int<LIMBS> {
/// Compute "wide" multiplication as a 3-tuple `(lo, hi, negate)`.
Expand Down Expand Up @@ -51,10 +51,7 @@ impl<const LIMBS: usize> Int<LIMBS> {
pub const fn concatenating_mul<const RHS_LIMBS: usize, const WIDE_LIMBS: usize>(
&self,
rhs: &Int<RHS_LIMBS>,
) -> Int<WIDE_LIMBS>
where
Uint<LIMBS>: ConcatMixed<Uint<RHS_LIMBS>, MixedOutput = Uint<WIDE_LIMBS>>,
{
) -> Int<WIDE_LIMBS> {
let (lhs_abs, lhs_sign) = self.abs_sign();
let (rhs_abs, rhs_sign) = rhs.abs_sign();
let product_abs = lhs_abs.concatenating_mul(&rhs_abs);
Expand All @@ -76,10 +73,7 @@ impl<const LIMBS: usize> Int<LIMBS> {
/// Squaring operations.
impl<const LIMBS: usize> Int<LIMBS> {
/// Square self, returning a concatenated "wide" result.
pub fn widening_square<const WIDE_LIMBS: usize>(&self) -> Uint<WIDE_LIMBS>
where
Uint<LIMBS>: ConcatMixed<Uint<LIMBS>, MixedOutput = Uint<WIDE_LIMBS>>,
{
pub fn widening_square<const WIDE_LIMBS: usize>(&self) -> Uint<WIDE_LIMBS> {
self.abs().widening_square()
}

Expand Down
7 changes: 2 additions & 5 deletions src/int/mul_uint.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::ops::Mul;
use subtle::CtOption;

use crate::{CheckedMul, ConcatMixed, ConstChoice, ConstCtOption, Int, Uint};
use crate::{CheckedMul, ConstChoice, ConstCtOption, Int, Uint};

impl<const LIMBS: usize> Int<LIMBS> {
/// Compute "wide" multiplication between an [`Int`] and [`Uint`] as 3-tuple `(lo, hi, negate)`.
Expand Down Expand Up @@ -70,10 +70,7 @@ impl<const LIMBS: usize> Int<LIMBS> {
pub const fn concatenating_mul_uint<const RHS_LIMBS: usize, const WIDE_LIMBS: usize>(
&self,
rhs: &Uint<RHS_LIMBS>,
) -> Int<WIDE_LIMBS>
where
Uint<LIMBS>: ConcatMixed<Uint<RHS_LIMBS>, MixedOutput = Uint<WIDE_LIMBS>>,
{
) -> Int<WIDE_LIMBS> {
let (lhs_abs, lhs_sign) = self.abs_sign();
let product_abs = lhs_abs.concatenating_mul(rhs);

Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@
#[macro_use]
extern crate alloc;

pub use uint::encoding::{EncodedUint, TryFromSliceError};

#[cfg(feature = "rand_core")]
pub use rand_core;
#[cfg(feature = "rlp")]
Expand Down
8 changes: 4 additions & 4 deletions src/modular.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub trait Retrieve {
#[cfg(test)]
mod tests {
use crate::{
NonZero, U64, U128, U256, Uint, const_monty_params,
NonZero, U64, U128, U256, U512, Uint, const_monty_params,
modular::{
const_monty_form::{ConstMontyForm, ConstMontyParams},
mul::{mul_montgomery_form, square_montgomery_form},
Expand Down Expand Up @@ -122,7 +122,7 @@ mod tests {
#[test]
fn test_reducing_r2_wide() {
// Divide the value ONE^2 by R, which should equal ONE
let (lo, hi) = Modulus256::PARAMS.one.square().split();
let (lo, hi) = Modulus256::PARAMS.one.square::<{ nlimbs!(512) }>().split();
assert_eq!(
montgomery_reduction::<{ Modulus256::LIMBS }>(
&(lo, hi),
Expand Down Expand Up @@ -158,7 +158,7 @@ mod tests {

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

// Computing xR mod modulus without Montgomery reduction
let (lo, hi) = x.widening_mul(&Modulus256::PARAMS.one);
let c = lo.concat(&hi);
let c: U512 = lo.concat(&hi);
let red =
c.rem_vartime(&NonZero::new(Modulus256::PARAMS.modulus.0.concat(&U256::ZERO)).unwrap());
let (lo, hi) = red.split();
Expand Down
Loading