Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(proptest): Implement proptest::Arbitrary for UnsignedInteger #526

Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 4 additions & 2 deletions math/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ license.workspace = true
thiserror = { version = "1.0", optional = true }
serde = { version = "1.0", features = ["derive"], optional = true }
serde_json = { version = "1.0", optional = true }
proptest = { version = "1.1.0", optional = true }

# rayon
rayon = { version = "1.7", optional = true }
Expand All @@ -25,17 +26,18 @@ cudarc = { version = "0.9.7", optional = true }
lambdaworks-gpu = { workspace = true, optional = true }

[dev-dependencies]
rand = { version = "0.8.5", default-features = false}
proptest = "1.1.0"
rand = { version = "0.8.5", default-features = false }
criterion = "0.4"
const-random = "0.1.15"
iai-callgrind.workspace = true
proptest = "1.1.0"

[features]
rayon = ["dep:rayon"]
default = ["rayon", "std"]
std = ["dep:thiserror"]
lambdaworks-serde = ["dep:serde", "dep:serde_json", "std"]
proptest = ["dep:proptest"]

# gpu
metal = [
Expand Down
134 changes: 88 additions & 46 deletions math/src/unsigned_integer/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ use core::ops::{
Sub,
};

#[cfg(feature = "proptest")]
use proptest::{
arbitrary::Arbitrary,
prelude::any,
strategy::{SBoxedStrategy, Strategy},
};

use crate::errors::ByteConversionError;
use crate::errors::CreationError;
use crate::traits::ByteConversion;
Expand Down Expand Up @@ -848,6 +855,11 @@ impl<const NUM_LIMBS: usize> UnsignedInteger<NUM_LIMBS> {
}
Ok(res)
}

#[cfg(feature = "proptest")]
pub fn nonzero_uint() -> impl Strategy<Value = UnsignedInteger<NUM_LIMBS>> {
any_uint::<NUM_LIMBS>().prop_filter("is_zero", |&x| x != UnsignedInteger::from_u64(0))
}
}

impl<const NUM_LIMBS: usize> IsUnsignedInteger for UnsignedInteger<NUM_LIMBS> {}
Expand Down Expand Up @@ -927,6 +939,22 @@ impl<const NUM_LIMBS: usize> From<UnsignedInteger<NUM_LIMBS>> for u16 {
}
}

#[cfg(feature = "proptest")]
fn any_uint<const NUM_LIMBS: usize>() -> impl Strategy<Value = UnsignedInteger<NUM_LIMBS>> {
any::<[u64; NUM_LIMBS]>().prop_map(|limbs| UnsignedInteger::from_limbs(limbs))
}

#[cfg(feature = "proptest")]
impl<const NUM_LIMBS: usize> Arbitrary for UnsignedInteger<NUM_LIMBS> {
type Parameters = ();

fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
any_uint::<NUM_LIMBS>().sboxed()
}

type Strategy = SBoxedStrategy<Self>;
}

#[cfg(test)]
mod tests_u384 {
use core::ops::Shr;
Expand All @@ -941,7 +969,8 @@ mod tests_u384 {

proptest! {
#[test]
fn bitand(a in any::<[u64; N_LIMBS]>(), b in any::<[u64; N_LIMBS]>()) {
#[cfg(feature = "proptest")]
Oppen marked this conversation as resolved.
Show resolved Hide resolved
fn bitand(a in any::<Uint>(), b in any::<Uint>()) {
let result = Uint::from_limbs(a) & Uint::from_limbs(b);

for i in 0..N_LIMBS {
Expand All @@ -950,57 +979,63 @@ mod tests_u384 {
}

#[test]
fn bitand_assign(a in any::<[u64; N_LIMBS]>(), b in any::<[u64; N_LIMBS]>()) {
let mut result = Uint::from_limbs(a);
result &= Uint::from_limbs(b);
#[cfg(feature = "proptest")]
fn bitand_assign(a in any::<Uint>(), b in any::<Uint>()) {
let mut result = a;
result &= b;

for i in 0..N_LIMBS {
assert_eq!(result.limbs[i], a[i] & b[i]);
assert_eq!(result.limbs[i], a.limbs[i] & b.limbs[i]);
}
}

#[test]
fn bitor(a in any::<[u64; N_LIMBS]>(), b in any::<[u64; N_LIMBS]>()) {
let result = Uint::from_limbs(a) | Uint::from_limbs(b);
#[cfg(feature = "proptest")]
fn bitor(a in any::<Uint>(), b in any::<Uint>()) {
let result = a | b;

for i in 0..N_LIMBS {
assert_eq!(result.limbs[i], a[i] | b[i]);
assert_eq!(result.limbs[i], a.limbs[i] | b.limbs[i]);
}
}

#[test]
fn bitor_assign(a in any::<[u64; N_LIMBS]>(), b in any::<[u64; N_LIMBS]>()) {
let mut result = Uint::from_limbs(a);
result |= Uint::from_limbs(b);
#[cfg(feature = "proptest")]
fn bitor_assign(a in any::<Uint>(), b in any::<Uint>()) {
let mut result = a;
result |= b;

for i in 0..N_LIMBS {
assert_eq!(result.limbs[i], a[i] | b[i]);
assert_eq!(result.limbs[i], a.limbs[i] | b.limbs[i]);
}
}

#[test]
fn bitxor(a in any::<[u64; N_LIMBS]>(), b in any::<[u64; N_LIMBS]>()) {
let result = Uint::from_limbs(a) ^ Uint::from_limbs(b);
#[cfg(feature = "proptest")]
fn bitxor(a in any::<Uint>(), b in any::<Uint>()) {
let result = a ^ b;

for i in 0..N_LIMBS {
assert_eq!(result.limbs[i], a[i] ^ b[i]);
assert_eq!(result.limbs[i], a.limbs[i] ^ b.limbs[i]);
}
}

#[test]
fn bitxor_assign(a in any::<[u64; N_LIMBS]>(), b in any::<[u64; N_LIMBS]>()) {
let mut result = Uint::from_limbs(a);
result ^= Uint::from_limbs(b);
#[cfg(feature = "proptest")]
fn bitxor_assign(a in any::<Uint>(), b in any::<Uint>()) {
let mut result = a;
result ^= b;

for i in 0..N_LIMBS {
assert_eq!(result.limbs[i], a[i] ^ b[i]);
assert_eq!(result.limbs[i], a.limbs[i] ^ b.limbs[i]);
}
}

#[test]
fn div_rem(a in any::<[u64; N_LIMBS]>(), b in any::<[u64; N_LIMBS]>()) {
let a = Uint::from_limbs(a).shr(256);
let b = Uint::from_limbs(b).shr(256);
#[cfg(feature = "proptest")]
fn div_rem(a in any::<Uint>(), b in any::<Uint>()) {
let a = a.shr(256);
let b = b.shr(256);
assert_eq!((a * b).div_rem(&b), (a, Uint::from_u64(0)));
}
}
Expand Down Expand Up @@ -1992,66 +2027,73 @@ mod tests_u256 {

proptest! {
#[test]
fn bitand(a in any::<[u64; N_LIMBS]>(), b in any::<[u64; N_LIMBS]>()) {
let result = Uint::from_limbs(a) & Uint::from_limbs(b);
#[cfg(feature = "proptest")]
fn bitand(a in any::<Uint>(), b in any::<Uint>()) {
let result = a & b;

for i in 0..N_LIMBS {
assert_eq!(result.limbs[i], a[i] & b[i]);
assert_eq!(result.limbs[i], a.limbs[i] & b.limbs[i]);
}
}

#[test]
fn bitand_assign(a in any::<[u64; N_LIMBS]>(), b in any::<[u64; N_LIMBS]>()) {
let mut result = Uint::from_limbs(a);
result &= Uint::from_limbs(b);
#[cfg(feature = "proptest")]
fn bitand_assign(a in any::<Uint>(), b in any::<Uint>()) {
let mut result = a;
result &= b;

for i in 0..N_LIMBS {
assert_eq!(result.limbs[i], a[i] & b[i]);
assert_eq!(result.limbs[i], a.limbs[i] & b.limbs[i]);
}
}

#[test]
fn bitor(a in any::<[u64; N_LIMBS]>(), b in any::<[u64; N_LIMBS]>()) {
let result = Uint::from_limbs(a) | Uint::from_limbs(b);
#[cfg(feature = "proptest")]
fn bitor(a in any::<Uint>(), b in any::<Uint>()) {
let result = a | b;

for i in 0..N_LIMBS {
assert_eq!(result.limbs[i], a[i] | b[i]);
assert_eq!(result.limbs[i], a.limbs[i] | b.limbs[i]);
}
}

#[test]
fn bitor_assign(a in any::<[u64; N_LIMBS]>(), b in any::<[u64; N_LIMBS]>()) {
let mut result = Uint::from_limbs(a);
result |= Uint::from_limbs(b);
#[cfg(feature = "proptest")]
fn bitor_assign(a in any::<Uint>(), b in any::<Uint>()) {
let mut result = a;
result |= b;

for i in 0..N_LIMBS {
assert_eq!(result.limbs[i], a[i] | b[i]);
assert_eq!(result.limbs[i], a.limbs[i] | b.limbs[i]);
}
}

#[test]
fn bitxor(a in any::<[u64; N_LIMBS]>(), b in any::<[u64; N_LIMBS]>()) {
let result = Uint::from_limbs(a) ^ Uint::from_limbs(b);
#[cfg(feature = "proptest")]
fn bitxor(a in any::<Uint>(), b in any::<Uint>()) {
let result = a ^ b;

for i in 0..N_LIMBS {
assert_eq!(result.limbs[i], a[i] ^ b[i]);
assert_eq!(result.limbs[i], a.limbs[i] ^ b.limbs[i]);
}
}

#[test]
fn bitxor_assign(a in any::<[u64; N_LIMBS]>(), b in any::<[u64; N_LIMBS]>()) {
let mut result = Uint::from_limbs(a);
result ^= Uint::from_limbs(b);
#[cfg(feature = "proptest")]
fn bitxor_assign(a in any::<Uint>(), b in any::<Uint>()) {
let mut result = a;
result ^= b;

for i in 0..N_LIMBS {
assert_eq!(result.limbs[i], a[i] ^ b[i]);
assert_eq!(result.limbs[i], a.limbs[i] ^ b.limbs[i]);
}
}

#[test]
fn div_rem(a in any::<[u64; N_LIMBS]>(), b in any::<[u64; N_LIMBS]>()) {
let a = Uint::from_limbs(a).shr(128);
let b = Uint::from_limbs(b).shr(128);
#[cfg(feature = "proptest")]
fn div_rem(a in any::<Uint>(), b in any::<Uint>()) {
let a = a.shr(128);
let b = b.shr(128);
assert_eq!((a * b).div_rem(&b), (a, Uint::from_u64(0)));
}
}
Expand Down
Loading