Skip to content

Commit 84d5495

Browse files
authored
tests: extract common module (#504)
Extracts a module containing shared functionality which can be reused between various tests. Currently it just contains `to_biguint` (which has been adapted to work with both `Uint` and `BoxedUint`)
1 parent 2792b2c commit 84d5495

File tree

8 files changed

+58
-42
lines changed

8 files changed

+58
-42
lines changed

src/odd.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
//! Wrapper type for non-zero integers.
22
3-
use crate::{Integer, NonZero, Uint};
3+
use crate::{Integer, Limb, NonZero, Uint};
44
use core::{cmp::Ordering, ops::Deref};
55
use subtle::{Choice, ConditionallySelectable, CtOption};
66

77
#[cfg(feature = "alloc")]
88
use crate::BoxedUint;
99

1010
#[cfg(feature = "rand_core")]
11-
use {
12-
crate::{Limb, Random},
13-
rand_core::CryptoRngCore,
14-
};
11+
use {crate::Random, rand_core::CryptoRngCore};
1512

1613
/// Wrapper type for odd integers.
1714
///
@@ -75,6 +72,15 @@ impl<T> AsRef<T> for Odd<T> {
7572
}
7673
}
7774

75+
impl<T> AsRef<[Limb]> for Odd<T>
76+
where
77+
T: AsRef<[Limb]>,
78+
{
79+
fn as_ref(&self) -> &[Limb] {
80+
self.0.as_ref()
81+
}
82+
}
83+
7884
impl<T> AsRef<NonZero<T>> for Odd<T> {
7985
fn as_ref(&self) -> &NonZero<T> {
8086
self.as_nz_ref()

tests/bernstein_yang.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
//! Equivalence tests for Bernstein-Yang inversions.
22
3-
use crypto_bigint::{Encoding, Inverter, Odd, PrecomputeInverter, U256};
3+
mod common;
4+
5+
use common::to_biguint;
6+
use crypto_bigint::{Inverter, Odd, PrecomputeInverter, U256};
47
use num_bigint::BigUint;
58
use num_integer::Integer;
69
use num_traits::One;
@@ -13,15 +16,6 @@ use crypto_bigint::BoxedUint;
1316
const P: Odd<U256> =
1417
Odd::<U256>::from_be_hex("ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551");
1518

16-
fn to_biguint(uint: &U256) -> BigUint {
17-
BigUint::from_bytes_le(uint.to_le_bytes().as_ref())
18-
}
19-
20-
#[cfg(feature = "alloc")]
21-
fn to_biguint_boxed(boxed_uint: &BoxedUint) -> BigUint {
22-
BigUint::from_bytes_le(boxed_uint.to_le_bytes().as_ref())
23-
}
24-
2519
prop_compose! {
2620
fn uint()(bytes in any::<[u8; 32]>()) -> U256 {
2721
U256::from_le_slice(&bytes)
@@ -53,7 +47,7 @@ proptest! {
5347

5448
prop_assert_eq!(expected_is_some, bool::from(actual.is_some()));
5549

56-
if let Some(actual) = actual.into() {
50+
if let Some(actual) = Option::<U256>::from(actual) {
5751
let inv_bi = to_biguint(&actual);
5852
let res = (inv_bi * x_bi) % p_bi;
5953
prop_assert_eq!(res, BigUint::one());
@@ -66,7 +60,7 @@ proptest! {
6660
let p = Odd::<BoxedUint>::from(&P);
6761
let x = x.rem_vartime(p.as_nz_ref()).widen(p.bits_precision());
6862

69-
let x_bi = to_biguint_boxed(&x);
63+
let x_bi = to_biguint(&x);
7064
let p_bi = to_biguint(&P);
7165

7266
let expected_is_some = x_bi.gcd(&p_bi) == BigUint::one();
@@ -75,8 +69,8 @@ proptest! {
7569

7670
prop_assert_eq!(expected_is_some, bool::from(actual.is_some()));
7771

78-
if let Some(actual) = actual.into() {
79-
let inv_bi = to_biguint_boxed(&actual);
72+
if let Some(actual) = Option::<BoxedUint>::from(actual) {
73+
let inv_bi = to_biguint(&actual);
8074
let res = (inv_bi * x_bi) % p_bi;
8175
prop_assert_eq!(res, BigUint::one());
8276
}

tests/boxed_monty_form.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
33
#![cfg(feature = "alloc")]
44

5+
mod common;
6+
7+
use common::to_biguint;
58
use crypto_bigint::{
69
modular::{BoxedMontyForm, BoxedMontyParams},
710
BoxedUint, Integer, Inverter, Limb, NonZero, Odd, PrecomputeInverter,
@@ -11,10 +14,6 @@ use num_modular::ModularUnaryOps;
1114
use proptest::prelude::*;
1215
use std::cmp::Ordering;
1316

14-
fn to_biguint(uint: &BoxedUint) -> BigUint {
15-
BigUint::from_bytes_be(&uint.to_be_bytes())
16-
}
17-
1817
fn retrieve_biguint(monty_form: &BoxedMontyForm) -> BigUint {
1918
to_biguint(&monty_form.retrieve())
2019
}

tests/boxed_uint.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
33
#![cfg(feature = "alloc")]
44

5+
mod common;
6+
7+
use common::to_biguint;
58
use core::cmp::Ordering;
69
use crypto_bigint::{BoxedUint, CheckedAdd, Gcd, Integer, Limb, NonZero};
710
use num_bigint::BigUint;
@@ -10,10 +13,6 @@ use num_modular::ModularUnaryOps;
1013
use num_traits::identities::One;
1114
use proptest::prelude::*;
1215

13-
fn to_biguint(uint: &BoxedUint) -> BigUint {
14-
BigUint::from_bytes_be(&uint.to_be_bytes())
15-
}
16-
1716
fn to_uint(big_uint: BigUint) -> BoxedUint {
1817
let bytes = big_uint.to_bytes_be();
1918
let pad_count = Limb::BYTES - (bytes.len() % Limb::BYTES);
@@ -170,7 +169,7 @@ proptest! {
170169
let a_bi = to_biguint(&a);
171170
let b_bi = to_biguint(&b);
172171
let expected = a_bi.invm(&b_bi);
173-
let actual = Option::from(a.inv_odd_mod(&b));
172+
let actual = Option::<BoxedUint>::from(a.inv_odd_mod(&b));
174173

175174
match (expected, actual) {
176175
(Some(exp), Some(act)) => prop_assert_eq!(exp, to_biguint(&act).into()),

tests/common/mod.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//! Common functionality shared between tests.
2+
3+
// Different tests may use only a subset of the available functionality
4+
#![allow(dead_code)]
5+
6+
use crypto_bigint::{Encoding, Limb};
7+
use num_bigint::BigUint;
8+
9+
/// `Uint` to `num_bigint::BigUint`
10+
pub fn to_biguint<T>(uint: &T) -> BigUint
11+
where
12+
T: AsRef<[Limb]>,
13+
{
14+
let mut bytes = Vec::with_capacity(uint.as_ref().len() * Limb::BYTES);
15+
16+
for limb in uint.as_ref() {
17+
bytes.extend_from_slice(&limb.to_le_bytes());
18+
}
19+
20+
BigUint::from_bytes_le(&bytes)
21+
}

tests/const_monty_form.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
//! Equivalence tests between `crypto_bigint::ConstMontyForm` and `num-bigint`.
22
3-
use crypto_bigint::{impl_modulus, modular::ConstMontyParams, Encoding, Invert, Inverter, U256};
3+
mod common;
4+
5+
use common::to_biguint;
6+
use crypto_bigint::{impl_modulus, modular::ConstMontyParams, Invert, Inverter, U256};
47
use num_bigint::BigUint;
58
use num_modular::ModularUnaryOps;
69
use proptest::prelude::*;
@@ -13,10 +16,6 @@ impl_modulus!(
1316

1417
type ConstMontyForm = crypto_bigint::modular::ConstMontyForm<Modulus, { U256::LIMBS }>;
1518

16-
fn to_biguint(uint: &U256) -> BigUint {
17-
BigUint::from_bytes_le(uint.to_le_bytes().as_ref())
18-
}
19-
2019
fn retrieve_biguint(monty_form: &ConstMontyForm) -> BigUint {
2120
to_biguint(&monty_form.retrieve())
2221
}

tests/monty_form.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
//! Equivalence tests between `crypto_bigint::MontyForm` and `num-bigint`.
22
3-
use crypto_bigint::{Encoding, Integer, Invert, Inverter, NonZero, Odd, PrecomputeInverter, U256};
3+
mod common;
4+
5+
use common::to_biguint;
6+
use crypto_bigint::{Integer, Invert, Inverter, NonZero, Odd, PrecomputeInverter, U256};
47
use num_bigint::BigUint;
58
use num_modular::ModularUnaryOps;
69
use proptest::prelude::*;
710

811
type MontyForm = crypto_bigint::modular::MontyForm<{ U256::LIMBS }>;
912
type MontyParams = crypto_bigint::modular::MontyParams<{ U256::LIMBS }>;
1013

11-
fn to_biguint(uint: &U256) -> BigUint {
12-
BigUint::from_bytes_le(uint.to_le_bytes().as_ref())
13-
}
14-
1514
fn retrieve_biguint(monty_form: &MontyForm) -> BigUint {
1615
to_biguint(&monty_form.retrieve())
1716
}

tests/uint.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
//! Equivalence tests between `crypto_bigint::Uint` and `num_bigint::BigUint`.
22
3+
mod common;
4+
5+
use common::to_biguint;
36
use crypto_bigint::{
47
modular::{MontyForm, MontyParams},
58
Encoding, Integer, Limb, NonZero, Odd, Word, U256,
@@ -14,10 +17,6 @@ use std::mem;
1417
const P: Odd<U256> =
1518
Odd::<U256>::from_be_hex("ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551");
1619

17-
fn to_biguint(uint: &U256) -> BigUint {
18-
BigUint::from_bytes_le(uint.to_le_bytes().as_ref())
19-
}
20-
2120
fn to_uint(big_uint: BigUint) -> U256 {
2221
let mut input = [0u8; U256::BYTES];
2322
let encoded = big_uint.to_bytes_le();

0 commit comments

Comments
 (0)