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
24 changes: 12 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "gridiron"
version = "0.12.0"
version = "0.12.1"
edition = "2024"
authors = ["IronCore Labs <code at ironcorelabs.com>"]
repository = "https://github.com/IronCoreLabs/gridiron"
Expand All @@ -12,27 +12,27 @@ license = "Apache-2.0"
keywords = ["finite", "field", "crypto", "math"]
rust-version = "1.88.0"

[features]
default = ["limb62"]
limb31 = []
limb62 = []
unstable = []

[dependencies]
num-traits = "0.2.11"

[dev-dependencies]
criterion = { version = "0.8", default-features = false }
proptest = "1"
rand = "0.9"

[profile.test]
opt-level = 2
debug = 2
debug-assertions = true
incremental = true
overflow-checks = true

[dev-dependencies]
proptest = "1"
rand = "0.9"
criterion = { version = "0.8", default-features = false }

[features]
default = ["limb62"]
limb31 = []
limb62 = []
unstable = []

[profile.bench]
opt-level = 3

Expand Down
12 changes: 6 additions & 6 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions proptest-regressions/lib.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@
# everyone who runs the test benefits from these saved cases.
xs 629389273 2515883728 747658385 3311699969 # shrinks to a = Fp256(0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0), b = Fp256(0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
xs 3203875714 2202784005 182194810 851240316 # shrinks to a = Fp480(0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0), b = Fp480(0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
cc 3dc44b48833d2d3e530602416071098b9e1c09aa62820eee1fc38eceded4cee9 # shrinks to a = Fp256(0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
cc 4afec65eb01d596f48036ad8f748440a500e8d693ff3987944c0f4929c200b55 # shrinks to a = Fp256(0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0), b = Fp256(0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
cc aad1a67dfcbdb3a326ed2b63815d6accec8e7b94eda35b2af7f491b627d2af59 # shrinks to a = Fp256(0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0), b = Fp256(0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
cc 52df40fe34a6ff4f0ee2e46fa4113c860fd6f41a2f932b2bd738b7475b5698bf # shrinks to a = Fp480(0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
1 change: 1 addition & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[toolchain]
profile = "default"
channel = "1.92.0"
components = ["rust-src", "rust-analyzer"]
80 changes: 43 additions & 37 deletions src/digits/ff31.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1103,8 +1103,6 @@ macro_rules! fp31 {
use super::*;
// use limb_math;
use proptest::prelude::*;
use rand::TryRngCore;
use rand::rngs::OsRng;
use $crate::digits::constant_time_primitives::ConstantSwap;

#[test]
Expand Down Expand Up @@ -1138,25 +1136,24 @@ macro_rules! fp31 {
assert_eq!(0, x_iter.endindex) // but don't fall off the end!
}

prop_compose! {
fn arb_fp()(seed in any::<u32>()) -> $classname {
if seed == 0 {
$classname::zero()
} else if seed == 1 {
$classname::one()
} else {
let mut limbs = [0u32; NUMLIMBS];
for limb in limbs.iter_mut() {
*limb = OsRng.try_next_u32().unwrap() & 0x7FFFFFFFu32;
}
limbs[NUMLIMBS - 1] &= (1u32 << (PRIMEBITS % 31)) - 1;
$classname {
limbs: limbs
}.normalize_little()
}
}
/// Static test for negation of small values. This is a sanity check that
/// doesn't depend on arb_fp(), since arb_fp() uses negation for edge cases.
#[test]
fn negation_of_small_values() {
// -1 should be p - 1, so -1 + 1 = 0
assert_eq!(-$classname::one() + $classname::one(), $classname::zero());
// -2 should be p - 2, so -2 + 2 = 0
assert_eq!(
-$classname::from(2u8) + $classname::from(2u8),
$classname::zero()
);
// Double negation should be identity
assert_eq!(-(-$classname::one()), $classname::one());
assert_eq!(-(-$classname::from(2u8)), $classname::from(2u8));
}

$crate::define_arb_fp!($classname, 31, u32, 0x7FFFFFFFu32);

proptest! {
#[test]
fn from_u32(a in any::<u32>()) {
Expand All @@ -1174,8 +1171,6 @@ macro_rules! fp31 {

#[test]
fn identity(a in arb_fp()) {
// prop_assert_eq!(a * 1, a);

prop_assert_eq!(a * $classname::one(), a);
prop_assert_eq!($classname::one() * a, a);

Expand All @@ -1185,7 +1180,9 @@ macro_rules! fp31 {
prop_assert_eq!(a - $classname::zero(), a);
prop_assert_eq!($classname::zero() - a, -a);

prop_assert_eq!(a / a, $classname::one());
if !a.is_zero() {
prop_assert_eq!(a / a, $classname::one());
}
prop_assert_eq!(a.pow(0), $classname::one());
prop_assert_eq!(a.pow(1), a);
}
Expand Down Expand Up @@ -1225,17 +1222,21 @@ macro_rules! fp31 {

#[test]
fn mul_equals_div(a in arb_fp(), b in arb_fp()) {
prop_assume!(!a.is_zero() && !b.is_zero());
let c = a * b;
prop_assert_eq!(c / a, b);
prop_assert_eq!(c / b, a);
if !a.is_zero() {
prop_assert_eq!(c / a, b);
}
if !b.is_zero() {
prop_assert_eq!(c / b, a);
}
}

#[test]
fn mul_equals_div_numerator_can_be_zero(a in arb_fp(), b in arb_fp()) {
prop_assume!(!b.is_zero());
let c = a * b;
prop_assert_eq!(c / b, a);
if !b.is_zero() {
let c = a * b;
prop_assert_eq!(c / b, a);
}
}

#[test]
Expand All @@ -1245,9 +1246,10 @@ macro_rules! fp31 {

#[test]
fn div_zero_by_anything_should_be_zero(a in arb_fp()) {
prop_assume!(!a.is_zero());
let result = $classname::zero()/a;
assert!(result.is_zero())
if !a.is_zero() {
let result = $classname::zero()/a;
assert!(result.is_zero())
}
}

#[test]
Expand Down Expand Up @@ -1327,18 +1329,22 @@ macro_rules! fp31 {
}
#[test]
fn monty_inv_same_as_div(a in arb_fp(), b in arb_fp()) {
let div_result = a/b;
let result = (a.to_monty() * b.to_monty().inv()).to_norm();
if !b.is_zero() {
let div_result = a/b;
let result = (a.to_monty() * b.to_monty().inv()).to_norm();

prop_assert_eq!(div_result, result)
prop_assert_eq!(div_result, result)
}
}

#[test]
fn monty_div_same_as_div(a in arb_fp(), b in arb_fp()) {
let div_result = a/b;
let monty_result = (a.to_monty()/b.to_monty()).to_norm();
if !b.is_zero() {
let div_result = a/b;
let monty_result = (a.to_monty()/b.to_monty()).to_norm();

prop_assert_eq!(div_result, monty_result)
prop_assert_eq!(div_result, monty_result)
}
}

#[test]
Expand Down
80 changes: 43 additions & 37 deletions src/digits/ff62.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1117,8 +1117,6 @@ macro_rules! fp62 {
use super::*;
// use limb_math;
use proptest::prelude::*;
use rand::TryRngCore;
use rand::rngs::OsRng;
use $crate::digits::constant_time_primitives::ConstantSwap;

#[test]
Expand Down Expand Up @@ -1157,25 +1155,24 @@ macro_rules! fp62 {
assert_eq!(0, x_iter.endindex) // but don't fall off the end!
}

prop_compose! {
fn arb_fp()(seed in any::<u32>()) -> $classname {
if seed == 0 {
$classname::zero()
} else if seed == 1 {
$classname::one()
} else {
let mut limbs = [0u64; NUMLIMBS];
for limb in limbs.iter_mut() {
*limb = OsRng.try_next_u64().unwrap() & 0x3FFFFFFFFFFFFFFF;
}
limbs[NUMLIMBS - 1] &= (1u64 << (PRIMEBITS % 62)) - 1;
$classname {
limbs: limbs
}.normalize_little()
}
}
/// Static test for negation of small values. This is a sanity check that
/// doesn't depend on arb_fp(), since arb_fp() uses negation for edge cases.
#[test]
fn negation_of_small_values() {
// -1 should be p - 1, so -1 + 1 = 0
assert_eq!(-$classname::one() + $classname::one(), $classname::zero());
// -2 should be p - 2, so -2 + 2 = 0
assert_eq!(
-$classname::from(2u8) + $classname::from(2u8),
$classname::zero()
);
// Double negation should be identity
assert_eq!(-(-$classname::one()), $classname::one());
assert_eq!(-(-$classname::from(2u8)), $classname::from(2u8));
}

$crate::define_arb_fp!($classname, 62, u64, 0x3FFFFFFFFFFFFFFFu64);

proptest! {
#[test]
fn from_u32(a in any::<u32>()) {
Expand All @@ -1186,8 +1183,6 @@ macro_rules! fp62 {

#[test]
fn identity(a in arb_fp()) {
// prop_assert_eq!(a * 1, a);

prop_assert_eq!(a * $classname::one(), a);
prop_assert_eq!($classname::one() * a, a);

Expand All @@ -1197,7 +1192,9 @@ macro_rules! fp62 {
prop_assert_eq!(a - $classname::zero(), a);
prop_assert_eq!($classname::zero() - a, -a);

prop_assert_eq!(a / a, $classname::one());
if !a.is_zero() {
prop_assert_eq!(a / a, $classname::one());
}
prop_assert_eq!(a.pow(0), $classname::one());
prop_assert_eq!(a.pow(1), a);
}
Expand Down Expand Up @@ -1237,17 +1234,21 @@ macro_rules! fp62 {

#[test]
fn mul_equals_div(a in arb_fp(), b in arb_fp()) {
prop_assume!(!a.is_zero() && !b.is_zero());
let c = a * b;
prop_assert_eq!(c / a, b);
prop_assert_eq!(c / b, a);
if !a.is_zero() {
prop_assert_eq!(c / a, b);
}
if !b.is_zero() {
prop_assert_eq!(c / b, a);
}
}

#[test]
fn mul_equals_div_numerator_can_be_zero(a in arb_fp(), b in arb_fp()) {
prop_assume!(!b.is_zero());
let c = a * b;
prop_assert_eq!(c / b, a);
if !b.is_zero() {
let c = a * b;
prop_assert_eq!(c / b, a);
}
}

#[test]
Expand All @@ -1257,9 +1258,10 @@ macro_rules! fp62 {

#[test]
fn div_zero_by_anything_should_be_zero(a in arb_fp()) {
prop_assume!(!a.is_zero());
let result = $classname::zero()/a;
assert!(result.is_zero())
if !a.is_zero() {
let result = $classname::zero()/a;
assert!(result.is_zero())
}
}

#[test]
Expand Down Expand Up @@ -1339,18 +1341,22 @@ macro_rules! fp62 {
}
#[test]
fn monty_inv_same_as_div(a in arb_fp(), b in arb_fp()) {
let div_result = a/b;
let result = (a.to_monty() * b.to_monty().inv()).to_norm();
if !b.is_zero() {
let div_result = a/b;
let result = (a.to_monty() * b.to_monty().inv()).to_norm();

prop_assert_eq!(div_result, result)
prop_assert_eq!(div_result, result)
}
}

#[test]
fn monty_div_same_as_div(a in arb_fp(), b in arb_fp()) {
let div_result = a/b;
let monty_result = (a.to_monty()/b.to_monty()).to_norm();
if !b.is_zero() {
let div_result = a/b;
let monty_result = (a.to_monty()/b.to_monty()).to_norm();

prop_assert_eq!(div_result, monty_result)
prop_assert_eq!(div_result, monty_result)
}
}

#[test]
Expand Down
Loading