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 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
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: 76 additions & 58 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 @@ -859,6 +866,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 @@ -938,21 +950,32 @@ 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;

use crate::traits::ByteConversion;
use crate::unsigned_integer::element::{UnsignedInteger, U384};

use proptest::prelude::*;

const N_LIMBS: usize = 6;
type Uint = U384;

#[cfg(feature = "proptest")]
proptest! {
#[test]
fn bitand(a in any::<[u64; N_LIMBS]>(), b in any::<[u64; N_LIMBS]>()) {
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 @@ -961,57 +984,57 @@ 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);
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);
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);
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);
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);
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);
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 @@ -1996,79 +2019,74 @@ mod tests_u384 {

#[cfg(test)]
mod tests_u256 {
use core::ops::Shr;

use crate::unsigned_integer::element::{UnsignedInteger, U256};

use crate::unsigned_integer::element::ByteConversion;

use proptest::prelude::*;

const N_LIMBS: usize = 4;
type Uint = U256;

#[cfg(feature = "proptest")]
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);
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);
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);
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);
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);
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);
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);
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