From 47790cb6d857d0710ee6a045eb4a6505e940c979 Mon Sep 17 00:00:00 2001 From: aritkulova Date: Mon, 28 Sep 2026 12:26:47 +0300 Subject: [PATCH 1/2] removed `safe_` prefixes from div functions --- docs/stdlib.json | 8 ++++---- simf/lib/u16/math.simf | 2 +- simf/lib/u32/math.simf | 2 +- simf/lib/u64/math.simf | 2 +- simf/lib/u8/math.simf | 2 +- simf/tests/u16/math.simf | 4 ++-- simf/tests/u32/math.simf | 4 ++-- simf/tests/u64/math.simf | 4 ++-- simf/tests/u8/math.simf | 4 ++-- tests/stdlib/common/uint.rs | 14 ++++++-------- 10 files changed, 22 insertions(+), 24 deletions(-) diff --git a/docs/stdlib.json b/docs/stdlib.json index 801701e..9f9d133 100644 --- a/docs/stdlib.json +++ b/docs/stdlib.json @@ -371,7 +371,7 @@ "description": "Divide the first `u8` value by the second. Return `Some` of the quotient, or `None` if the divisor is zero." }, { - "simplicityhl_name": "safe_div_8", + "simplicityhl_name": "div_8", "section": "`u8` arithmetic", "input_type": "u8, u8", "output_type": "u8", @@ -497,7 +497,7 @@ "description": "Divide the first `u16` value by the second. Return `Some` of the quotient, or `None` if the divisor is zero." }, { - "simplicityhl_name": "safe_div_16", + "simplicityhl_name": "div_16", "section": "`u16` arithmetic", "input_type": "u16, u16", "output_type": "u16", @@ -623,7 +623,7 @@ "description": "Divide the first `u32` value by the second. Return `Some` of the quotient, or `None` if the divisor is zero." }, { - "simplicityhl_name": "safe_div_32", + "simplicityhl_name": "div_32", "section": "`u32` arithmetic", "input_type": "u32, u32", "output_type": "u32", @@ -756,7 +756,7 @@ "description": "Divide the first `u64` value by the second. Return `Some` of the quotient, or `None` if the divisor is zero." }, { - "simplicityhl_name": "safe_div_64", + "simplicityhl_name": "div_64", "section": "`u64` arithmetic", "input_type": "u64, u64", "output_type": "u64", diff --git a/simf/lib/u16/math.simf b/simf/lib/u16/math.simf index cf3c1b6..40fedc4 100644 --- a/simf/lib/u16/math.simf +++ b/simf/lib/u16/math.simf @@ -54,7 +54,7 @@ pub fn checked_div_16(a: u16, b: u16) -> Option { } /// Returns the quotient of two u16 values, panics if the result overflows u16 -pub fn safe_div_16(a: u16, b: u16) -> u16 { +pub fn div_16(a: u16, b: u16) -> u16 { unwrap(checked_div_16(a, b)) } diff --git a/simf/lib/u32/math.simf b/simf/lib/u32/math.simf index cfce540..060f68b 100644 --- a/simf/lib/u32/math.simf +++ b/simf/lib/u32/math.simf @@ -54,7 +54,7 @@ pub fn checked_div_32(a: u32, b: u32) -> Option { } /// Returns the quotient of two u32 values, panics if the result overflows u32 -pub fn safe_div_32(a: u32, b: u32) -> u32 { +pub fn div_32(a: u32, b: u32) -> u32 { unwrap(checked_div_32(a, b)) } diff --git a/simf/lib/u64/math.simf b/simf/lib/u64/math.simf index eebddc8..7928e68 100644 --- a/simf/lib/u64/math.simf +++ b/simf/lib/u64/math.simf @@ -54,7 +54,7 @@ pub fn checked_div_64(a: u64, b: u64) -> Option { } /// Returns the quotient of two u64 values, panics if the result overflows u64 -pub fn safe_div_64(a: u64, b: u64) -> u64 { +pub fn div_64(a: u64, b: u64) -> u64 { unwrap(checked_div_64(a, b)) } diff --git a/simf/lib/u8/math.simf b/simf/lib/u8/math.simf index e2e9e88..9017838 100644 --- a/simf/lib/u8/math.simf +++ b/simf/lib/u8/math.simf @@ -54,7 +54,7 @@ pub fn checked_div_8(a: u8, b: u8) -> Option { } /// Returns the quotient of two u8 values, panics if the result overflows u8 -pub fn safe_div_8(a: u8, b: u8) -> u8 { +pub fn div_8(a: u8, b: u8) -> u8 { unwrap(checked_div_8(a, b)) } diff --git a/simf/tests/u16/math.simf b/simf/tests/u16/math.simf index 6357266..fb9e1e5 100644 --- a/simf/tests/u16/math.simf +++ b/simf/tests/u16/math.simf @@ -6,7 +6,7 @@ use crate::lib::u16::math::{ checked_mul_16, safe_mul_16, checked_div_16, - safe_div_16, + div_16, gt_16, ge_16 }; @@ -72,7 +72,7 @@ fn main() { }; match is_selected(7, fn_idx) { true => { - assert!(jet::eq_16(safe_div_16(a, b), unwrap(expected))); + assert!(jet::eq_16(div_16(a, b), unwrap(expected))); }, false => (), }; diff --git a/simf/tests/u32/math.simf b/simf/tests/u32/math.simf index cf35bfd..cc7f1d3 100644 --- a/simf/tests/u32/math.simf +++ b/simf/tests/u32/math.simf @@ -6,7 +6,7 @@ use crate::lib::u32::math::{ checked_mul_32, safe_mul_32, checked_div_32, - safe_div_32, + div_32, gt_32, ge_32 }; @@ -72,7 +72,7 @@ fn main() { }; match is_selected(7, fn_idx) { true => { - assert!(jet::eq_32(safe_div_32(a, b), unwrap(expected))); + assert!(jet::eq_32(div_32(a, b), unwrap(expected))); }, false => (), }; diff --git a/simf/tests/u64/math.simf b/simf/tests/u64/math.simf index f47bd9b..979ba8e 100644 --- a/simf/tests/u64/math.simf +++ b/simf/tests/u64/math.simf @@ -6,7 +6,7 @@ use crate::lib::u64::math::{ checked_mul_64, safe_mul_64, checked_div_64, - safe_div_64, + div_64, gt_64, ge_64 }; @@ -72,7 +72,7 @@ fn main() { }; match is_selected(7, fn_idx) { true => { - assert!(jet::eq_64(safe_div_64(a, b), unwrap(expected))); + assert!(jet::eq_64(div_64(a, b), unwrap(expected))); }, false => (), }; diff --git a/simf/tests/u8/math.simf b/simf/tests/u8/math.simf index f600e63..b817130 100644 --- a/simf/tests/u8/math.simf +++ b/simf/tests/u8/math.simf @@ -6,7 +6,7 @@ use crate::lib::u8::math::{ checked_mul_8, safe_mul_8, checked_div_8, - safe_div_8, + div_8, gt_8, ge_8 }; @@ -72,7 +72,7 @@ fn main() { }; match is_selected(7, fn_idx) { true => { - assert!(jet::eq_8(safe_div_8(a, b), unwrap(expected))); + assert!(jet::eq_8(div_8(a, b), unwrap(expected))); }, false => (), }; diff --git a/tests/stdlib/common/uint.rs b/tests/stdlib/common/uint.rs index f271a4b..5be4b76 100644 --- a/tests/stdlib/common/uint.rs +++ b/tests/stdlib/common/uint.rs @@ -25,7 +25,7 @@ pub enum CommonOp { CheckedMul, SafeMul, CheckedDiv, - SafeDiv, + Div, Gt, Ge, } @@ -235,21 +235,19 @@ pub fn checked_div_by_zero(context: simplex::TestContext) -> anyhow ) } -pub fn safe_div_fitting(context: simplex::TestContext) -> anyhow::Result<()> { +pub fn div_fitting(context: simplex::TestContext) -> anyhow::Result<()> { let a = rand::thread_rng().gen_range(T::ZERO..=T::MAX); let b = rand::thread_rng().gen_range(T::ONE..=T::MAX); run( &context, T::program(), - T::witness(op(CommonOp::SafeDiv), a, b, Some(a / b)), + T::witness(op(CommonOp::Div), a, b, Some(a / b)), Expect::Ok, ) } -pub fn safe_div_by_zero( - context: simplex::TestContext, -) -> anyhow::Result<()> { +pub fn div_by_zero(context: simplex::TestContext) -> anyhow::Result<()> { let a = rand::thread_rng().gen_range(T::ZERO..=T::MAX); let expect = if TypeId::of::() == TypeId::of::() @@ -263,7 +261,7 @@ pub fn safe_div_by_zero( run( &context, T::program(), - T::witness(op(CommonOp::SafeDiv), a, T::ZERO, None), + T::witness(op(CommonOp::Div), a, T::ZERO, None), expect, ) } @@ -378,7 +376,7 @@ macro_rules! uint_tests { checked_mul_fitting checked_mul_overflow safe_mul_fitting safe_mul_overflow checked_div_fitting checked_div_by_zero - safe_div_fitting safe_div_by_zero + div_fitting div_by_zero gt_greater gt_equal gt_less ge_greater ge_equal ge_less From 7194aa7341f89e0fc3a673ffe84a695e6c36c151 Mon Sep 17 00:00:00 2001 From: aritkulova Date: Fri, 2 Oct 2026 12:05:03 +0300 Subject: [PATCH 2/2] removed `_safe` prefixes; `add` became `add_carry`; `sub` - `sub_carry`; `mul` - `mul_wide`. --- docs/stdlib.json | 62 ++++++++-------- simf/lib/u128/math.simf | 32 ++++----- simf/lib/u128/mul_div.simf | 4 +- simf/lib/u16/math.simf | 6 +- simf/lib/u256/math.simf | 104 +++++++++++++-------------- simf/lib/u256/mul_div.simf | 35 +++++---- simf/lib/u32/math.simf | 6 +- simf/lib/u64/math.simf | 6 +- simf/lib/u8/math.simf | 6 +- simf/tests/u128/math/api.simf | 12 ++-- simf/tests/u128/math/primitives.simf | 20 +++--- simf/tests/u16/math.simf | 12 ++-- simf/tests/u256/math/add.simf | 6 +- simf/tests/u256/math/api.simf | 12 ++-- simf/tests/u256/math/sub_mul.simf | 24 +++---- simf/tests/u32/math.simf | 12 ++-- simf/tests/u64/math.simf | 12 ++-- simf/tests/u8/math.simf | 12 ++-- tests/stdlib/common/core.rs | 2 +- tests/stdlib/common/uint.rs | 36 +++++----- tests/stdlib/u128/math/primitives.rs | 59 ++++++++------- tests/stdlib/u256/math/add.rs | 20 +++--- tests/stdlib/u256/math/div.rs | 1 - tests/stdlib/u256/math/sub_mul.rs | 58 +++++++-------- 24 files changed, 278 insertions(+), 281 deletions(-) diff --git a/docs/stdlib.json b/docs/stdlib.json index 9f9d133..93503bf 100644 --- a/docs/stdlib.json +++ b/docs/stdlib.json @@ -329,7 +329,7 @@ "description": "Add two `u8` values. Return `Some` of the sum, or `None` if the result overflows `u8`." }, { - "simplicityhl_name": "safe_add_8", + "simplicityhl_name": "add_8", "section": "`u8` arithmetic", "input_type": "u8, u8", "output_type": "u8", @@ -343,7 +343,7 @@ "description": "Subtract the second `u8` value from the first. Return `Some` of the difference, or `None` if the result would overflow `u8`." }, { - "simplicityhl_name": "safe_sub_8", + "simplicityhl_name": "sub_8", "section": "`u8` arithmetic", "input_type": "u8, u8", "output_type": "u8", @@ -357,7 +357,7 @@ "description": "Multiply two `u8` values. Return `Some` of the product, or `None` if the result overflows `u8`." }, { - "simplicityhl_name": "safe_mul_8", + "simplicityhl_name": "mul_8", "section": "`u8` arithmetic", "input_type": "u8, u8", "output_type": "u8", @@ -455,7 +455,7 @@ "description": "Add two `u16` values. Return `Some` of the sum, or `None` if the result overflows `u16`." }, { - "simplicityhl_name": "safe_add_16", + "simplicityhl_name": "add_16", "section": "`u16` arithmetic", "input_type": "u16, u16", "output_type": "u16", @@ -469,7 +469,7 @@ "description": "Subtract the second `u16` value from the first. Return `Some` of the difference, or `None` if the result would overflow `u16`." }, { - "simplicityhl_name": "safe_sub_16", + "simplicityhl_name": "sub_16", "section": "`u16` arithmetic", "input_type": "u16, u16", "output_type": "u16", @@ -483,7 +483,7 @@ "description": "Multiply two `u16` values. Return `Some` of the product, or `None` if the result overflows `u16`." }, { - "simplicityhl_name": "safe_mul_16", + "simplicityhl_name": "mul_16", "section": "`u16` arithmetic", "input_type": "u16, u16", "output_type": "u16", @@ -581,7 +581,7 @@ "description": "Add two `u32` values. Return `Some` of the sum, or `None` if the result overflows `u32`." }, { - "simplicityhl_name": "safe_add_32", + "simplicityhl_name": "add_32", "section": "`u32` arithmetic", "input_type": "u32, u32", "output_type": "u32", @@ -595,7 +595,7 @@ "description": "Subtract the second `u32` value from the first. Return `Some` of the difference, or `None` if the result would overflow `u32`." }, { - "simplicityhl_name": "safe_sub_32", + "simplicityhl_name": "sub_32", "section": "`u32` arithmetic", "input_type": "u32, u32", "output_type": "u32", @@ -609,7 +609,7 @@ "description": "Multiply two `u32` values. Return `Some` of the product, or `None` if the result overflows `u32`." }, { - "simplicityhl_name": "safe_mul_32", + "simplicityhl_name": "mul_32", "section": "`u32` arithmetic", "input_type": "u32, u32", "output_type": "u32", @@ -714,7 +714,7 @@ "description": "Add two `u64` values. Return `Some` of the sum, or `None` if the result overflows `u64`." }, { - "simplicityhl_name": "safe_add_64", + "simplicityhl_name": "add_64", "section": "`u64` arithmetic", "input_type": "u64, u64", "output_type": "u64", @@ -728,7 +728,7 @@ "description": "Subtract the second `u64` value from the first. Return `Some` of the difference, or `None` if the result would overflow `u64`." }, { - "simplicityhl_name": "safe_sub_64", + "simplicityhl_name": "sub_64", "section": "`u64` arithmetic", "input_type": "u64, u64", "output_type": "u64", @@ -742,7 +742,7 @@ "description": "Multiply two `u64` values. Return `Some` of the product, or `None` if the result overflows `u64`." }, { - "simplicityhl_name": "safe_mul_64", + "simplicityhl_name": "mul_64", "section": "`u64` arithmetic", "input_type": "u64, u64", "output_type": "u64", @@ -847,14 +847,14 @@ "description": "Narrow a `u64` value to `u32`.\n\n## Panics\nPanics if the value does not fit in `u32`." }, { - "simplicityhl_name": "add_128", + "simplicityhl_name": "add_128_carry", "section": "`u128` arithmetic", "input_type": "u128, u128", "output_type": "(bool, u128)", "description": "Add two `u128` values. Return the carry bit and the sum." }, { - "simplicityhl_name": "add_128_64", + "simplicityhl_name": "add_128_64_carry", "section": "`u128` arithmetic", "input_type": "u128, u64", "output_type": "(bool, u128)", @@ -868,14 +868,14 @@ "description": "Add two `u128` values. Return `Some` of the sum, or `None` if the result overflows `u128`." }, { - "simplicityhl_name": "safe_add_128", + "simplicityhl_name": "add_128", "section": "`u128` arithmetic", "input_type": "u128, u128", "output_type": "u128", "description": "Add two `u128` values.\n\n## Panics\nPanics if the result overflows `u128`." }, { - "simplicityhl_name": "sub_128", + "simplicityhl_name": "sub_128_borrow", "section": "`u128` arithmetic", "input_type": "u128, u128", "output_type": "(bool, u128)", @@ -889,14 +889,14 @@ "description": "Subtract the second `u128` value from the first. Return `Some` of the difference, or `None` if the result would overflow `u128`." }, { - "simplicityhl_name": "safe_sub_128", + "simplicityhl_name": "sub_128", "section": "`u128` arithmetic", "input_type": "u128, u128", "output_type": "u128", "description": "Subtract the second `u128` value from the first.\n\n## Panics\nPanics if the result would overflow `u128`." }, { - "simplicityhl_name": "mul_128", + "simplicityhl_name": "mul_128_wide", "section": "`u128` arithmetic", "input_type": "u128, u128", "output_type": "u256", @@ -910,7 +910,7 @@ "description": "Multiply two `u128` values. Return `Some` of the product, or `None` if the result overflows `u128`." }, { - "simplicityhl_name": "safe_mul_128", + "simplicityhl_name": "mul_128", "section": "`u128` arithmetic", "input_type": "u128, u128", "output_type": "u128", @@ -959,7 +959,7 @@ "description": "Subtract the second `u128` value from the first, taking an incoming borrow bit. Return the outgoing borrow bit and the difference." }, { - "simplicityhl_name": "mul_128_64", + "simplicityhl_name": "mul_128_64_wide", "section": "`u128` arithmetic", "input_type": "u128, u64", "output_type": "u256", @@ -1127,14 +1127,14 @@ "description": "Narrow a `u128` value to `u64`.\n\n## Panics\nPanics if the value does not fit in `u64`." }, { - "simplicityhl_name": "add_256", + "simplicityhl_name": "add_256_carry", "section": "`u256` arithmetic", "input_type": "u256, u256", "output_type": "(bool, u256)", "description": "Add two `u256` values. Return the carry bit and the sum." }, { - "simplicityhl_name": "add_256_128", + "simplicityhl_name": "add_256_128_carry", "section": "`u256` arithmetic", "input_type": "u256, u128", "output_type": "(bool, u256)", @@ -1155,14 +1155,14 @@ "description": "Add two `u256` values. Return `Some` of the sum, or `None` if the result overflows `u256`." }, { - "simplicityhl_name": "safe_add_256", + "simplicityhl_name": "add_256", "section": "`u256` arithmetic", "input_type": "u256, u256", "output_type": "u256", "description": "Add two `u256` values.\n\n## Panics\nPanics if the result overflows `u256`." }, { - "simplicityhl_name": "sub_256", + "simplicityhl_name": "sub_256_borrow", "section": "`u256` arithmetic", "input_type": "u256, u256", "output_type": "(bool, u256)", @@ -1176,35 +1176,35 @@ "description": "Subtract the second `u256` value from the first. Return `Some` of the difference, or `None` if the result would overflow `u256`." }, { - "simplicityhl_name": "safe_sub_256", + "simplicityhl_name": "sub_256", "section": "`u256` arithmetic", "input_type": "u256, u256", "output_type": "u256", "description": "Subtract the second `u256` value from the first.\n\n## Panics\nPanics if the result would overflow `u256`." }, { - "simplicityhl_name": "mul_256", + "simplicityhl_name": "mul_256_wide", "section": "`u256` arithmetic", "input_type": "u256, u256", "output_type": "(u256, u256)", "description": "Multiply two `u256` values. The full, non-truncated product is returned as a pair of `u256` values, most-significant first, so this operation can never overflow." }, { - "simplicityhl_name": "mul_256_64", + "simplicityhl_name": "mul_256_64_wide", "section": "`u256` arithmetic", "input_type": "u256, u64", "output_type": "(u64, u256)", "description": "Multiply a `u256` value by a `u64` value. The full, non-truncated product is returned as a `u64`/`u256` pair, most-significant first, so this operation can never overflow." }, { - "simplicityhl_name": "mul_256_128", + "simplicityhl_name": "mul_256_128_wide", "section": "`u256` arithmetic", "input_type": "u256, u128", "output_type": "(u128, u256)", "description": "Multiply a `u256` value by a `u128` value. The full, non-truncated product is returned as a `u128`/`u256` pair, most-significant first, so this operation can never overflow." }, { - "simplicityhl_name": "mul_512_128", + "simplicityhl_name": "mul_512_128_wide", "section": "`u256` arithmetic", "input_type": "u256, u256, u128", "output_type": "(u128, u256, u256)", @@ -1218,14 +1218,14 @@ "description": "Multiply two `u256` values. Return `Some` of the product, or `None` if the result overflows `u256`." }, { - "simplicityhl_name": "safe_mul_256", + "simplicityhl_name": "mul_256", "section": "`u256` arithmetic", "input_type": "u256, u256", "output_type": "u256", "description": "Multiply two `u256` values.\n\n## Panics\nPanics if the result overflows `u256`." }, { - "simplicityhl_name": "safe_mul_256_128", + "simplicityhl_name": "mul_256_128", "section": "`u256` arithmetic", "input_type": "u256, u128", "output_type": "u256", diff --git a/simf/lib/u128/math.simf b/simf/lib/u128/math.simf index a842565..db0d26b 100644 --- a/simf/lib/u128/math.simf +++ b/simf/lib/u128/math.simf @@ -7,7 +7,7 @@ use crate::lib::u128::convert::{u128_to_u256, split_u128_into_u64}; /// Arithmetic /// Adds two integers and returns the carry -pub fn add_128(a: u128, b: u128) -> (bool, u128) { +pub fn add_128_carry(a: u128, b: u128) -> (bool, u128) { let (a_high, a_low): (u64, u64) = split_u128_into_u64(a); let (b_high, b_low): (u64, u64) = split_u128_into_u64(b); @@ -19,7 +19,7 @@ pub fn add_128(a: u128, b: u128) -> (bool, u128) { } /// Adds the 128-bit integer with the 64-bit integer and returns the carry -pub fn add_128_64(a: u128, b: u64) -> (bool, u128) { +pub fn add_128_64_carry(a: u128, b: u64) -> (bool, u128) { let (a_high, a_low): (u64, u64) = split_u128_into_u64(a); let (carry_low, res_low): (bool, u64) = jet::add_64(a_low, b); @@ -43,7 +43,7 @@ pub fn full_add_128(carry_in: bool, a: u128, b: u128) -> (bool, u128) { /// Returns the sum of two u128 values wrapped in Some, or None if the result overflows u128 pub fn checked_add_128(a: u128, b: u128) -> Option { - let (carry, sum): (bool, u128) = add_128(a, b); + let (carry, sum): (bool, u128) = add_128_carry(a, b); match carry { true => None, @@ -52,12 +52,12 @@ pub fn checked_add_128(a: u128, b: u128) -> Option { } /// Returns the sum of two u128 values, panics if the result overflows u128 -pub fn safe_add_128(a: u128, b: u128) -> u128 { +pub fn add_128(a: u128, b: u128) -> u128 { unwrap(checked_add_128(a, b)) } /// Subtracts the second integer from the first integer, and returns the borrow bit -pub fn sub_128(a: u128, b: u128) -> (bool, u128) { +pub fn sub_128_borrow(a: u128, b: u128) -> (bool, u128) { let (a_high, a_low): (u64, u64) = split_u128_into_u64(a); let (b_high, b_low): (u64, u64) = split_u128_into_u64(b); @@ -82,7 +82,7 @@ pub fn full_sub_128(borrow_in: bool, a: u128, b: u128) -> (bool, u128) { /// Returns the difference of two u128 values wrapped in Some, or None if the result overflows u128 pub fn checked_sub_128(a: u128, b: u128) -> Option { - let (borrow, diff): (bool, u128) = sub_128(a, b); + let (borrow, diff): (bool, u128) = sub_128_borrow(a, b); match borrow { true => None, @@ -91,7 +91,7 @@ pub fn checked_sub_128(a: u128, b: u128) -> Option { } /// Returns the difference of two u128 values, panics if the result overflows u128 -pub fn safe_sub_128(a: u128, b: u128) -> u128 { +pub fn sub_128(a: u128, b: u128) -> u128 { unwrap(checked_sub_128(a, b)) } @@ -100,7 +100,7 @@ pub fn safe_sub_128(a: u128, b: u128) -> u128 { /// so a = a_high * 2^64 + a_low. /// In the same way, b = b_high * 2^64 + b_low. /// Therefore, a * b = 2^128 * a_high * b_high + 2^64(a_high * b_low + a_low * b_high) + a_low * b_low. -pub fn mul_128(a: u128, b: u128) -> u256 { +pub fn mul_128_wide(a: u128, b: u128) -> u256 { let (a_high, a_low): (u64, u64) = split_u128_into_u64(a); let (b_high, b_low): (u64, u64) = split_u128_into_u64(b); @@ -111,7 +111,7 @@ pub fn mul_128(a: u128, b: u128) -> u256 { let product_1: u128 = jet::multiply_64(a_high, b_low); let product_2: u128 = jet::multiply_64(b_high, a_low); - let (carry_3a, middle): (bool, u128) = add_128(product_1, product_2); + let (carry_3a, middle): (bool, u128) = add_128_carry(product_1, product_2); let (middle_2, middle_1): (u64, u64) = split_u128_into_u64(middle); @@ -124,7 +124,7 @@ pub fn mul_128(a: u128, b: u128) -> u256 { // either factor is `u64::MAX`, and even in the extreme case where // a == b == u128::MAX, the total product still fits into u256. // Therefore, word_3 + carry_3a + carry_3b can not overflow, and `full_add_64` - // is used instead of `safe_add_64` to avoid the unnecessary overflow check + // is used instead of `add_64` to avoid the unnecessary overflow check let (_, res_3a): (bool, u64) = jet::full_add_64(carry_3a, word_3, 0); let (_, res_3): (bool, u64) = jet::full_add_64(carry_3b, res_3a, 0); @@ -138,7 +138,7 @@ pub fn mul_128(a: u128, b: u128) -> u256 { /// The idea is that u128-bit `a` divides into 64-bit `a_high` and `a_low`, /// so a = a_high * 2^64 + a_low. /// Therefore, a * b = 2^64 * a_high * b + a_low * b. -pub fn mul_128_64(a: u128, b: u64) -> u256 { +pub fn mul_128_64_wide(a: u128, b: u64) -> u256 { let (a_high, a_low): (u64, u64) = split_u128_into_u64(a); let highest: u128 = jet::multiply_64(a_high, b); @@ -149,7 +149,7 @@ pub fn mul_128_64(a: u128, b: u64) -> u256 { let (carry_2, res_1): (bool, u64) = jet::add_64(word_1, word_2); // a * b fits into u192, so addition below can not overflow and `full_add_64` - // is used instead of `safe_add_64` to avoid the unnecessary overflow check + // is used instead of `add_64` to avoid the unnecessary overflow check let (_, res_2): (bool, u64) = jet::full_add_64(carry_2, word_3, 0); <(u64, u64, u64, u64)>::into((0, res_2, res_1, word_0)) @@ -157,7 +157,7 @@ pub fn mul_128_64(a: u128, b: u64) -> u256 { /// Returns the product of two u128 values wrapped in Some, or None if the result overflows u128 pub fn checked_mul_128(a: u128, b: u128) -> Option { - let result: u256 = mul_128(a, b); + let result: u256 = mul_128_wide(a, b); let (high, low): (u128, u128) = ::into(result); match is_zero_128(high) { @@ -167,7 +167,7 @@ pub fn checked_mul_128(a: u128, b: u128) -> Option { } /// Returns the product of two u128 values, panics if the result overflows u128 -pub fn safe_mul_128(a: u128, b: u128) -> u128 { +pub fn mul_128(a: u128, b: u128) -> u128 { unwrap(checked_mul_128(a, b)) } @@ -216,7 +216,7 @@ fn normalize_to_threshold_128_63(a: u128, b: u128, is_b_u128: bool) -> (u256, u1 match jet::eq_64(norm, 1) { true => (u128_to_u256(a), b, norm), - false => (mul_128(a, norm_128), safe_mul_128(b, norm_128), norm), + false => (mul_128_wide(a, norm_128), mul_128(b, norm_128), norm), } } @@ -295,7 +295,7 @@ fn algorithm_d_128_128(dividend: u128, divisor: u128) -> (u64, u128) { let q: u64 = estimate_quotient_digit_base_64(u2, u1, u0, v1, v0); - let remainder: u128 = safe_sub_128(dividend, safe_mul_128(divisor, u64_to_u128(q))); + let remainder: u128 = sub_128(dividend, mul_128(divisor, u64_to_u128(q))); (q, remainder) } diff --git a/simf/lib/u128/mul_div.simf b/simf/lib/u128/mul_div.simf index 2b7bf3c..e3dfc1b 100644 --- a/simf/lib/u128/mul_div.simf +++ b/simf/lib/u128/mul_div.simf @@ -1,4 +1,4 @@ -use crate::lib::u128::math::mul_128; +use crate::lib::u128::math::mul_128_wide; use crate::lib::u128::convert::u128_to_u256; use crate::lib::u256::math::div_256; @@ -9,7 +9,7 @@ use crate::lib::u256::convert::safe_u256_to_u128; pub fn mul_div_128(a: u128, b: u128, denominator: u128) -> u128 { let denominator_256: u256 = u128_to_u256(denominator); - let result_256: u256 = div_256(mul_128(a, b), denominator_256); + let result_256: u256 = div_256(mul_128_wide(a, b), denominator_256); safe_u256_to_u128(result_256) } diff --git a/simf/lib/u16/math.simf b/simf/lib/u16/math.simf index 40fedc4..ce298c8 100644 --- a/simf/lib/u16/math.simf +++ b/simf/lib/u16/math.simf @@ -9,7 +9,7 @@ pub fn checked_add_16(a: u16, b: u16) -> Option { } /// Returns the sum of two u16 values, panics if the result overflows u16 -pub fn safe_add_16(a: u16, b: u16) -> u16 { +pub fn add_16(a: u16, b: u16) -> u16 { unwrap(checked_add_16(a, b)) } @@ -24,7 +24,7 @@ pub fn checked_sub_16(a: u16, b: u16) -> Option { } /// Returns the difference of two u16 values, panics if the result overflows u16 -pub fn safe_sub_16(a: u16, b: u16) -> u16 { +pub fn sub_16(a: u16, b: u16) -> u16 { unwrap(checked_sub_16(a, b)) } @@ -41,7 +41,7 @@ pub fn checked_mul_16(a: u16, b: u16) -> Option { } /// Returns the product of two u16 values, panics if the result overflows u16 -pub fn safe_mul_16(a: u16, b: u16) -> u16 { +pub fn mul_16(a: u16, b: u16) -> u16 { unwrap(checked_mul_16(a, b)) } diff --git a/simf/lib/u256/math.simf b/simf/lib/u256/math.simf index 8654bf9..8750b2f 100644 --- a/simf/lib/u256/math.simf +++ b/simf/lib/u256/math.simf @@ -2,13 +2,13 @@ use crate::lib::binary::{not, and, or}; use crate::lib::u64::convert::u64_to_u128; use crate::lib::u128::math::{ - add_128, + add_128_carry, full_add_128, - sub_128, + sub_128_borrow, full_sub_128, + mul_128_wide, + mul_128_64_wide, mul_128, - mul_128_64, - safe_mul_128, calculate_normalizer_base_64, estimate_quotient_digit_base_64, div_mod_128, @@ -24,11 +24,11 @@ use crate::lib::u256::convert::{split_u256_into_u64, split_u256_into_u128}; /// Arithmetic /// Adds two integers and returns the carry -pub fn add_256(a: u256, b: u256) -> (bool, u256) { +pub fn add_256_carry(a: u256, b: u256) -> (bool, u256) { let (a_high, a_low): (u128, u128) = split_u256_into_u128(a); let (b_high, b_low): (u128, u128) = split_u256_into_u128(b); - let (carry_low, sum_low): (bool, u128) = add_128(a_low, b_low); + let (carry_low, sum_low): (bool, u128) = add_128_carry(a_low, b_low); let (carry_high, sum_high): (bool, u128) = full_add_128(carry_low, a_high, b_high); let res: u256 = <(u128, u128)>::into((sum_high, sum_low)); @@ -36,10 +36,10 @@ pub fn add_256(a: u256, b: u256) -> (bool, u256) { } /// Adds the 256-bit integer with the 128-bit integer and returns the carry -pub fn add_256_128(a: u256, b: u128) -> (bool, u256) { +pub fn add_256_128_carry(a: u256, b: u128) -> (bool, u256) { let b: u256 = u128_to_u256(b); - add_256(a, b) + add_256_carry(a, b) } /// Adds two integers. Takes a carry-in and returns a carry-out @@ -56,7 +56,7 @@ pub fn full_add_256(carry_in: bool, a: u256, b: u256) -> (bool, u256) { /// Returns the sum of two u256 values wrapped in Some, or None if the result overflows u256 pub fn checked_add_256(a: u256, b: u256) -> Option { - let (carry, sum): (bool, u256) = add_256(a, b); + let (carry, sum): (bool, u256) = add_256_carry(a, b); match carry { true => None, @@ -65,16 +65,16 @@ pub fn checked_add_256(a: u256, b: u256) -> Option { } /// Returns the sum of two u256 values, panics if the result overflows u256 -pub fn safe_add_256(a: u256, b: u256) -> u256 { +pub fn add_256(a: u256, b: u256) -> u256 { unwrap(checked_add_256(a, b)) } /// Subtracts the second integer from the first integer, and returns the borrow bit -pub fn sub_256(a: u256, b: u256) -> (bool, u256) { +pub fn sub_256_borrow(a: u256, b: u256) -> (bool, u256) { let (a_high, a_low): (u128, u128) = split_u256_into_u128(a); let (b_high, b_low): (u128, u128) = split_u256_into_u128(b); - let (borrow_low, diff_low): (bool, u128) = sub_128(a_low, b_low); + let (borrow_low, diff_low): (bool, u128) = sub_128_borrow(a_low, b_low); let (borrow_high, diff_high): (bool, u128) = full_sub_128(borrow_low, a_high, b_high); let res: u256 = <(u128, u128)>::into((diff_high, diff_low)); @@ -83,7 +83,7 @@ pub fn sub_256(a: u256, b: u256) -> (bool, u256) { /// Returns the difference of two u256 values wrapped in Some, or None if the result overflows u256 pub fn checked_sub_256(a: u256, b: u256) -> Option { - let (borrow, diff): (bool, u256) = sub_256(a, b); + let (borrow, diff): (bool, u256) = sub_256_borrow(a, b); match borrow { true => None, @@ -92,7 +92,7 @@ pub fn checked_sub_256(a: u256, b: u256) -> Option { } /// Returns the difference of two u256 values, panics if the result overflows u256 -pub fn safe_sub_256(a: u256, b: u256) -> u256 { +pub fn sub_256(a: u256, b: u256) -> u256 { unwrap(checked_sub_256(a, b)) } @@ -101,31 +101,31 @@ pub fn safe_sub_256(a: u256, b: u256) -> u256 { /// so a = a_high * 2^128 + a_low. /// In the same way, b = b_high * 2^128 + b_low. /// Therefore, a * b = 2^256 * a_high * b_high + 2^128(a_high * b_low + a_low * b_high) + a_low * b_low. -pub fn mul_256(a: u256, b: u256) -> (u256, u256) { +pub fn mul_256_wide(a: u256, b: u256) -> (u256, u256) { let (a_high, a_low): (u128, u128) = split_u256_into_u128(a); let (b_high, b_low): (u128, u128) = split_u256_into_u128(b); - let highest: u256 = mul_128(a_high, b_high); - let lowest: u256 = mul_128(a_low, b_low); + let highest: u256 = mul_128_wide(a_high, b_high); + let lowest: u256 = mul_128_wide(a_low, b_low); let (word_1, word_0): (u128, u128) = split_u256_into_u128(lowest); let (word_3, word_2): (u128, u128) = split_u256_into_u128(highest); - let product_1: u256 = mul_128(a_high, b_low); - let product_2: u256 = mul_128(b_high, a_low); - let (carry_3a, middle): (bool, u256) = add_256(product_1, product_2); + let product_1: u256 = mul_128_wide(a_high, b_low); + let product_2: u256 = mul_128_wide(b_high, a_low); + let (carry_3a, middle): (bool, u256) = add_256_carry(product_1, product_2); let (middle_2, middle_1): (u128, u128) = split_u256_into_u128(middle); // fold the low-side carry directly into the word_2 addition via full_add_128, // then propagate any resulting carry into word_3 - let (carry_2, res_1): (bool, u128) = add_128(word_1, middle_1); + let (carry_2, res_1): (bool, u128) = add_128_carry(word_1, middle_1); let (carry_3b, res_2): (bool, u128) = full_add_128(carry_2, word_2, middle_2); // `word_3` is the upper half of a_high * b_high. It is at most `u128::MAX - 1` when // either factor is `u128::MAX`, and even in the extreme case where // a == b == u256::MAX, the total product still fits into two u256. // Therefore, word_3 + carry_3a + carry_3b can not overflow, and `full_add_128` - // is used instead of `safe_add_128` to avoid the unnecessary overflow check + // is used instead of `add_128` to avoid the unnecessary overflow check let (_, res_3a): (bool, u128) = full_add_128(carry_3a, word_3, 0); let (_, res_3): (bool, u128) = full_add_128(carry_3b, res_3a, 0); @@ -140,18 +140,18 @@ pub fn mul_256(a: u256, b: u256) -> (u256, u256) { /// The idea is that u256-bit `a` divides into 128-bit `a_high` and `a_low`, /// so a = a_high * 2^128 + a_low, /// a * b = 2^128 * a_high * b + a_low * b -pub fn mul_256_64(a: u256, b: u64) -> (u64, u256) { +pub fn mul_256_64_wide(a: u256, b: u64) -> (u64, u256) { let (a_high, a_low): (u128, u128) = split_u256_into_u128(a); - let lowest: u256 = mul_128_64(a_low, b); - let highest: u256 = mul_128_64(a_high, b); + let lowest: u256 = mul_128_64_wide(a_low, b); + let highest: u256 = mul_128_64_wide(a_high, b); let (word_1, word_0): (u128, u128) = split_u256_into_u128(lowest); let (word_3, word_2): (u128, u128) = split_u256_into_u128(highest); let (_, word_3): (u64, u64) = split_u128_into_u64(word_3); - let (carry_2, res_1): (bool, u128) = add_128(word_1, word_2); + let (carry_2, res_1): (bool, u128) = add_128_carry(word_1, word_2); // a * b fits into u320, so addition below can not overflow and // `full_add_64` is used to avoid the unnecessary overflow check let (_, res_2): (bool, u64) = jet::full_add_64(carry_2, word_3, 0); @@ -164,16 +164,16 @@ pub fn mul_256_64(a: u256, b: u64) -> (u64, u256) { /// The idea is that u256-bit `a` divides into 128-bit `a_high` and `a_low`, /// so a = a_high * 2^128 + a_low, /// and a * b = 2^128 * a_high * b + a_low * b -pub fn mul_256_128(a: u256, b: u128) -> (u128, u256) { +pub fn mul_256_128_wide(a: u256, b: u128) -> (u128, u256) { let (a_high, a_low): (u128, u128) = split_u256_into_u128(a); - let lowest: u256 = mul_128(a_low, b); - let highest: u256 = mul_128(a_high, b); + let lowest: u256 = mul_128_wide(a_low, b); + let highest: u256 = mul_128_wide(a_high, b); let (word_1, word_0): (u128, u128) = split_u256_into_u128(lowest); let (word_3, word_2): (u128, u128) = split_u256_into_u128(highest); - let (carry_2, res_1): (bool, u128) = add_128(word_1, word_2); + let (carry_2, res_1): (bool, u128) = add_128_carry(word_1, word_2); // a * b fits into u384, so addition below can not overflow and // `full_add_128` is used to avoid the unnecessary overflow check let (_, res_2): (bool, u128) = full_add_128(carry_2, word_3, 0); @@ -186,11 +186,11 @@ pub fn mul_256_128(a: u256, b: u128) -> (u128, u256) { /// The idea is that 512-bit `a` divides into 256-bit `a_high` and `a_low`, /// so a = a_high * 2^256 + a_low, /// and a * b = 2^256 * a_high * b + a_low * b -pub fn mul_512_128(a_high: u256, a_low: u256, b: u128) -> (u128, u256, u256) { - let (word_1, word_0): (u128, u256) = mul_256_128(a_low, b); - let (word_3, word_2): (u128, u256) = mul_256_128(a_high, b); +pub fn mul_512_128_wide(a_high: u256, a_low: u256, b: u128) -> (u128, u256, u256) { + let (word_1, word_0): (u128, u256) = mul_256_128_wide(a_low, b); + let (word_3, word_2): (u128, u256) = mul_256_128_wide(a_high, b); - let (carry, res_1): (bool, u256) = add_256_128(word_2, word_1); + let (carry, res_1): (bool, u256) = add_256_128_carry(word_2, word_1); // a * b fits into u640, so addition below can not overflow and // `full_add_128` is used to avoid the unnecessary overflow check @@ -201,7 +201,7 @@ pub fn mul_512_128(a_high: u256, a_low: u256, b: u128) -> (u128, u256, u256) { /// Returns the product of two u256 values wrapped in Some, or None if the result overflows u256 pub fn checked_mul_256(a: u256, b: u256) -> Option { - let (result_high, result_low): (u256, u256) = mul_256(a, b); + let (result_high, result_low): (u256, u256) = mul_256_wide(a, b); match is_zero_256(result_high) { true => Some(result_low), @@ -210,13 +210,13 @@ pub fn checked_mul_256(a: u256, b: u256) -> Option { } /// Returns the product of two u256 values, panics if the result overflows u256 -pub fn safe_mul_256(a: u256, b: u256) -> u256 { +pub fn mul_256(a: u256, b: u256) -> u256 { unwrap(checked_mul_256(a, b)) } /// Returns the product of u256 and u128 values, panics if the result overflows u256 -pub fn safe_mul_256_128(a: u256, b: u128) -> u256 { - let (result_high, result_low): (u128, u256) = mul_256_128(a, b); +pub fn mul_256_128(a: u256, b: u128) -> u256 { + let (result_high, result_low): (u128, u256) = mul_256_128_wide(a, b); assert!(is_zero_128(result_high)); result_low @@ -242,7 +242,7 @@ pub fn calculate_normalizer_base_128(b: u256) -> u128 { let ( _, norm - ): (bool, u128) = add_128( + ): (bool, u128) = add_128_carry( norm, 1 ); // norm <= 2^127, so norm + 1 can not overflow @@ -267,8 +267,8 @@ fn normalize_to_threshold_256_63(a: u256, b: u128, is_b_u128: bool) -> (u64, u25 match jet::eq_64(norm, 1) { true => (0, a, b, norm), false => { - let (high, low): (u64, u256) = mul_256_64(a, norm); - let b_norm: u128 = safe_mul_128(b, norm_128); + let (high, low): (u64, u256) = mul_256_64_wide(a, norm); + let b_norm: u128 = mul_128(b, norm_128); (high, low, b_norm, norm) }, @@ -287,9 +287,9 @@ fn normalize_to_threshold_256_127(a: u256, b: u256) -> (u128, u256, u256) { match eq_128(norm, 1) { true => (0, a, b), false => { - let (high, low): (u128, u256) = mul_256_128(a, norm); + let (high, low): (u128, u256) = mul_256_128_wide(a, norm); - (high, low, safe_mul_256_128(b, norm)) + (high, low, mul_256_128(b, norm)) }, } } @@ -334,9 +334,9 @@ pub fn div_mod_256_64(dividend: u256, divisor: u64) -> (u256, u64) { fn mul_and_sub_64(q: u64, u2: u64, u1: u64, u0: u64, v: u128) -> (u64, u64) { let u: u256 = <(u64, u64, u64, u64)>::into((0, u2, u1, u0)); - let q_v: u256 = mul_128_64(v, q); + let q_v: u256 = mul_128_64_wide(v, q); - let u_updated: u256 = safe_sub_256(u, q_v); + let u_updated: u256 = sub_256(u, q_v); let (_, _, u1, u0): (u64, u64, u64, u64) = split_u256_into_u64(u_updated); (u1, u0) @@ -407,25 +407,25 @@ fn algorithm_d_256_256(dividend: u256, divisor: u256) -> (u128, u256) { let (_, q_hat): (u128, u128) = split_u256_into_u128(q_hat); let r_hat_u0: u256 = <(u128, u128)>::into((r_hat, u0)); - let u_hat: u256 = mul_128(q_hat, v0); + let u_hat: u256 = mul_128_wide(q_hat, v0); // correcting estimation: q_hat is off by at most 2. let q: u128 = match lt_256(r_hat_u0, u_hat) { true => { // can not overflow because r_hat_u0 < q_hat * v0, so q_hat is at least 1 - let (_, q_hat): (bool, u128) = sub_128(q_hat, 1); - let (carry, r_hat): (bool, u128) = add_128(r_hat, v1); + let (_, q_hat): (bool, u128) = sub_128_borrow(q_hat, 1); + let (carry, r_hat): (bool, u128) = add_128_carry(r_hat, v1); match carry { true => q_hat, false => { let r_hat_u0: u256 = <(u128, u128)>::into((r_hat, u0)); - let (_, u_hat): (bool, u256) = sub_256(u_hat, u128_to_u256(v0)); + let (_, u_hat): (bool, u256) = sub_256_borrow(u_hat, u128_to_u256(v0)); match lt_256(r_hat_u0, u_hat) { true => { // can not overflow because r_hat_u0 < q_hat * v0, so q_hat is at least 1 - let (_, q_hat): (bool, u128) = sub_128(q_hat, 1); + let (_, q_hat): (bool, u128) = sub_128_borrow(q_hat, 1); q_hat }, @@ -437,7 +437,7 @@ fn algorithm_d_256_256(dividend: u256, divisor: u256) -> (u128, u256) { false => q_hat, }; - let remainder: u256 = safe_sub_256(dividend, safe_mul_256_128(divisor, q)); + let remainder: u256 = sub_256(dividend, mul_256_128(divisor, q)); (q, remainder) } @@ -464,7 +464,7 @@ pub fn div_mod_256(a: u256, b: u256) -> (u256, u256) { true => { // safe: !lt_256(a, b) and a_high == b_high, so a_low >= b_low, // and the subtraction can not overflow - let (_, diff): (bool, u128) = sub_128(a_low, b_low); + let (_, diff): (bool, u128) = sub_128_borrow(a_low, b_low); (1, u128_to_u256(diff)) }, false => match is_zero_128(b_high) { diff --git a/simf/lib/u256/mul_div.simf b/simf/lib/u256/mul_div.simf index 9aa765b..f4f9878 100644 --- a/simf/lib/u256/mul_div.simf +++ b/simf/lib/u256/mul_div.simf @@ -1,18 +1,18 @@ use crate::lib::binary::not; -use crate::lib::u128::math::{add_128, sub_128, full_sub_128, mul_128}; +use crate::lib::u128::math::{add_128_carry, sub_128_borrow, full_sub_128, mul_128_wide}; use crate::lib::u128::bit::eq_128; use crate::lib::u128::comparison::{is_zero_128, lt_128}; use crate::lib::u256::math::{ algorithm_d_256_128, - mul_256_128, + mul_256_128_wide, + sub_256_borrow, sub_256, - safe_sub_256, calculate_normalizer_base_128, - mul_512_128, - safe_mul_256_128, - mul_256, + mul_512_128_wide, + mul_256_128, + mul_256_wide, div_mod_256_128, div_256 }; @@ -46,7 +46,7 @@ fn estimate_quotient_digit_base_128(u2: u128, u1: u128, u0: u128, v1: u128, v0: // This means u2 == v1, q_hat = 2^128, and r_hat = u1. // Therefore, we need to decrement q and add v1 to r_hat. // r_hat = u1 + v1 may overflow u128, which means that the estimate is exact - let (carry, r_hat): (bool, u128) = add_128(u1, v1); + let (carry, r_hat): (bool, u128) = add_128_carry(u1, v1); let u128_max: u128 = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF; @@ -60,21 +60,21 @@ fn estimate_quotient_digit_base_128(u2: u128, u1: u128, u0: u128, v1: u128, v0: let r_hat_u0: u256 = <(u128, u128)>::into((r_hat, u0)); // correcting estimation: q_hat is off by at most 2. - match lt_256(r_hat_u0, mul_128(q_hat, v0)) { + match lt_256(r_hat_u0, mul_128_wide(q_hat, v0)) { true => { // can not overflow because r_hat_u0 < q_hat * v0, so q_hat is at least 1 - let (_, q_hat): (bool, u128) = sub_128(q_hat, 1); - let (carry, r_hat): (bool, u128) = add_128(r_hat, v1); + let (_, q_hat): (bool, u128) = sub_128_borrow(q_hat, 1); + let (carry, r_hat): (bool, u128) = add_128_carry(r_hat, v1); match carry { true => q_hat, false => { let r_hat_u0: u256 = <(u128, u128)>::into((r_hat, u0)); - match lt_256(r_hat_u0, mul_128(q_hat, v0)) { + match lt_256(r_hat_u0, mul_128_wide(q_hat, v0)) { true => { // can not overflow because r_hat_u0 < q_hat * v0, so q_hat is at least 1 - let (_, q_hat): (bool, u128) = sub_128(q_hat, 1); + let (_, q_hat): (bool, u128) = sub_128_borrow(q_hat, 1); q_hat }, @@ -101,9 +101,9 @@ fn normalize_to_threshold_512_127(a_high: u256, a_low: u256, b: u256) -> (u128, match eq_128(norm, 1) { true => (0, a_high, a_low, b), false => { - let (a_2, a_1, a_0): (u128, u256, u256) = mul_512_128(a_high, a_low, norm); + let (a_2, a_1, a_0): (u128, u256, u256) = mul_512_128_wide(a_high, a_low, norm); - (a_2, a_1, a_0, safe_mul_256_128(b, norm)) + (a_2, a_1, a_0, mul_256_128(b, norm)) }, } } @@ -114,9 +114,9 @@ fn normalize_to_threshold_512_127(a_high: u256, a_low: u256, b: u256) -> (u128, fn mul_and_sub_128(q: u128, u2: u128, u1: u128, u0: u128, v: u256) -> (u128, u128) { let u1_0: u256 = <(u128, u128)>::into((u1, u0)); - let (q_v_1, q_v_0): (u128, u256) = mul_256_128(v, q); + let (q_v_1, q_v_0): (u128, u256) = mul_256_128_wide(v, q); - let (borrow_low, u_updated_1_0): (bool, u256) = sub_256(u1_0, q_v_0); + let (borrow_low, u_updated_1_0): (bool, u256) = sub_256_borrow(u1_0, q_v_0); let (borrow, u_updated_2): (bool, u128) = full_sub_128(borrow_low, u2, q_v_1); assert!(not(borrow)); @@ -156,14 +156,13 @@ fn algorithm_d_512_256(dividend_high: u256, dividend_low: u256, divisor: u256) - pub fn mul_div_256(a: u256, b: u256, denominator: u256) -> u256 { assert!(not(is_zero_256(denominator))); - let (result_high, result_low): (u256, u256) = mul_256(a, b); + let (result_high, result_low): (u256, u256) = mul_256_wide(a, b); match is_zero_256(result_high) { true => div_256(result_low, denominator), false => { // if result_high >= denominator, result will overflow u256 assert!(lt_256(result_high, denominator)); - let (denom_high, denom_low): (u128, u128) = split_u256_into_u128(denominator); match is_zero_128(denom_high) { diff --git a/simf/lib/u32/math.simf b/simf/lib/u32/math.simf index 060f68b..0253b5e 100644 --- a/simf/lib/u32/math.simf +++ b/simf/lib/u32/math.simf @@ -9,7 +9,7 @@ pub fn checked_add_32(a: u32, b: u32) -> Option { } /// Returns the sum of two u32 values, panics if the result overflows u32 -pub fn safe_add_32(a: u32, b: u32) -> u32 { +pub fn add_32(a: u32, b: u32) -> u32 { unwrap(checked_add_32(a, b)) } @@ -24,7 +24,7 @@ pub fn checked_sub_32(a: u32, b: u32) -> Option { } /// Returns the difference of two u32 values, panics if the result overflows u32 -pub fn safe_sub_32(a: u32, b: u32) -> u32 { +pub fn sub_32(a: u32, b: u32) -> u32 { unwrap(checked_sub_32(a, b)) } @@ -41,7 +41,7 @@ pub fn checked_mul_32(a: u32, b: u32) -> Option { } /// Returns the product of two u32 values, panics if the result overflows u32 -pub fn safe_mul_32(a: u32, b: u32) -> u32 { +pub fn mul_32(a: u32, b: u32) -> u32 { unwrap(checked_mul_32(a, b)) } diff --git a/simf/lib/u64/math.simf b/simf/lib/u64/math.simf index 7928e68..05a8498 100644 --- a/simf/lib/u64/math.simf +++ b/simf/lib/u64/math.simf @@ -9,7 +9,7 @@ pub fn checked_add_64(a: u64, b: u64) -> Option { } /// Returns the sum of two u64 values, panics if the result overflows u64 -pub fn safe_add_64(a: u64, b: u64) -> u64 { +pub fn add_64(a: u64, b: u64) -> u64 { unwrap(checked_add_64(a, b)) } @@ -24,7 +24,7 @@ pub fn checked_sub_64(a: u64, b: u64) -> Option { } /// Returns the difference of two u64 values, panics if the result overflows u64 -pub fn safe_sub_64(a: u64, b: u64) -> u64 { +pub fn sub_64(a: u64, b: u64) -> u64 { unwrap(checked_sub_64(a, b)) } @@ -41,7 +41,7 @@ pub fn checked_mul_64(a: u64, b: u64) -> Option { } /// Returns the product of two u64 values, panics if the result overflows u64 -pub fn safe_mul_64(a: u64, b: u64) -> u64 { +pub fn mul_64(a: u64, b: u64) -> u64 { unwrap(checked_mul_64(a, b)) } diff --git a/simf/lib/u8/math.simf b/simf/lib/u8/math.simf index 9017838..1e56ee6 100644 --- a/simf/lib/u8/math.simf +++ b/simf/lib/u8/math.simf @@ -9,7 +9,7 @@ pub fn checked_add_8(a: u8, b: u8) -> Option { } /// Returns the sum of two u8 values, panics if the result overflows u8 -pub fn safe_add_8(a: u8, b: u8) -> u8 { +pub fn add_8(a: u8, b: u8) -> u8 { unwrap(checked_add_8(a, b)) } @@ -24,7 +24,7 @@ pub fn checked_sub_8(a: u8, b: u8) -> Option { } /// Returns the difference of two u8 values, panics if the result overflows u8 -pub fn safe_sub_8(a: u8, b: u8) -> u8 { +pub fn sub_8(a: u8, b: u8) -> u8 { unwrap(checked_sub_8(a, b)) } @@ -41,7 +41,7 @@ pub fn checked_mul_8(a: u8, b: u8) -> Option { } /// Returns the product of two u8 values, panics if the result overflows u8 -pub fn safe_mul_8(a: u8, b: u8) -> u8 { +pub fn mul_8(a: u8, b: u8) -> u8 { unwrap(checked_mul_8(a, b)) } diff --git a/simf/tests/u128/math/api.simf b/simf/tests/u128/math/api.simf index 4769750..5cf1585 100644 --- a/simf/tests/u128/math/api.simf +++ b/simf/tests/u128/math/api.simf @@ -1,10 +1,10 @@ use crate::lib::u128::math::{ checked_add_128, - safe_add_128, + add_128, checked_sub_128, - safe_sub_128, + sub_128, checked_mul_128, - safe_mul_128, + mul_128, checked_div_128, div_128 }; @@ -32,7 +32,7 @@ fn main() { }; match is_selected(1, fn_idx) { true => { - assert_eq_128(safe_add_128(a, b), unwrap(expected)); + assert_eq_128(add_128(a, b), unwrap(expected)); }, false => (), }; @@ -46,7 +46,7 @@ fn main() { }; match is_selected(3, fn_idx) { true => { - assert_eq_128(safe_sub_128(a, b), unwrap(expected)); + assert_eq_128(sub_128(a, b), unwrap(expected)); }, false => (), }; @@ -60,7 +60,7 @@ fn main() { }; match is_selected(5, fn_idx) { true => { - assert_eq_128(safe_mul_128(a, b), unwrap(expected)); + assert_eq_128(mul_128(a, b), unwrap(expected)); }, false => (), }; diff --git a/simf/tests/u128/math/primitives.simf b/simf/tests/u128/math/primitives.simf index c8a6aa0..e8edb36 100644 --- a/simf/tests/u128/math/primitives.simf +++ b/simf/tests/u128/math/primitives.simf @@ -1,11 +1,11 @@ use crate::lib::u128::math::{ - add_128, - add_128_64, + add_128_carry, + add_128_64_carry, full_add_128, - sub_128, + sub_128_borrow, full_sub_128, - mul_128, - mul_128_64, + mul_128_wide, + mul_128_64_wide, calculate_normalizer_base_64, estimate_quotient_digit_base_64, div_mod_128_64, @@ -30,14 +30,14 @@ fn main() { match is_selected(0, fn_idx) { true => { - assert_eq_uint_bool_128(add_128(a, b), unwrap(expected), expected_bool); + assert_eq_uint_bool_128(add_128_carry(a, b), unwrap(expected), expected_bool); }, false => (), }; match is_selected(1, fn_idx) { true => { let (_, b): (u64, u64) = ::into(b); - assert_eq_uint_bool_128(add_128_64(a, b), unwrap(expected), expected_bool); + assert_eq_uint_bool_128(add_128_64_carry(a, b), unwrap(expected), expected_bool); }, false => (), }; @@ -54,7 +54,7 @@ fn main() { match is_selected(3, fn_idx) { true => { - assert_eq_uint_bool_128(sub_128(a, b), unwrap(expected), expected_bool); + assert_eq_uint_bool_128(sub_128_borrow(a, b), unwrap(expected), expected_bool); }, false => (), }; @@ -71,7 +71,7 @@ fn main() { match is_selected(5, fn_idx) { true => { - let result: u256 = mul_128(a, b); + let result: u256 = mul_128_wide(a, b); let (result_high, result_low): (u128, u128) = ::into(result); @@ -84,7 +84,7 @@ fn main() { match is_selected(6, fn_idx) { true => { let (_, b): (u64, u64) = ::into(b); - let result: u256 = mul_128_64(a, b); + let result: u256 = mul_128_64_wide(a, b); let (result_high, result_low): (u128, u128) = ::into(result); diff --git a/simf/tests/u16/math.simf b/simf/tests/u16/math.simf index fb9e1e5..4736d91 100644 --- a/simf/tests/u16/math.simf +++ b/simf/tests/u16/math.simf @@ -1,10 +1,10 @@ use crate::lib::u16::math::{ checked_add_16, - safe_add_16, + add_16, checked_sub_16, - safe_sub_16, + sub_16, checked_mul_16, - safe_mul_16, + mul_16, checked_div_16, div_16, gt_16, @@ -30,7 +30,7 @@ fn main() { }; match is_selected(1, fn_idx) { true => { - assert!(jet::eq_16(safe_add_16(a, b), unwrap(expected))); + assert!(jet::eq_16(add_16(a, b), unwrap(expected))); }, false => (), }; @@ -44,7 +44,7 @@ fn main() { }; match is_selected(3, fn_idx) { true => { - assert!(jet::eq_16(safe_sub_16(a, b), unwrap(expected))); + assert!(jet::eq_16(sub_16(a, b), unwrap(expected))); }, false => (), }; @@ -58,7 +58,7 @@ fn main() { }; match is_selected(5, fn_idx) { true => { - assert!(jet::eq_16(safe_mul_16(a, b), unwrap(expected))); + assert!(jet::eq_16(mul_16(a, b), unwrap(expected))); }, false => (), }; diff --git a/simf/tests/u256/math/add.simf b/simf/tests/u256/math/add.simf index 398c4c5..7ba03e8 100644 --- a/simf/tests/u256/math/add.simf +++ b/simf/tests/u256/math/add.simf @@ -1,4 +1,4 @@ -use crate::lib::u256::math::{add_256, add_256_128, full_add_256}; +use crate::lib::u256::math::{add_256_carry, add_256_128_carry, full_add_256}; use crate::tests::support::expect::assert_eq_uint_bool_256; use crate::tests::support::dispatch::is_selected; @@ -16,7 +16,7 @@ fn main() { match is_selected(0, fn_idx) { true => { - assert_eq_uint_bool_256(add_256(a, b), unwrap(expected), expected_bool); + assert_eq_uint_bool_256(add_256_carry(a, b), unwrap(expected), expected_bool); }, false => (), }; @@ -24,7 +24,7 @@ fn main() { match is_selected(1, fn_idx) { true => { let (_, b): (u128, u128) = ::into(b); - assert_eq_uint_bool_256(add_256_128(a, b), unwrap(expected), expected_bool); + assert_eq_uint_bool_256(add_256_128_carry(a, b), unwrap(expected), expected_bool); }, false => (), }; diff --git a/simf/tests/u256/math/api.simf b/simf/tests/u256/math/api.simf index 8527fe0..7de1362 100644 --- a/simf/tests/u256/math/api.simf +++ b/simf/tests/u256/math/api.simf @@ -1,10 +1,10 @@ use crate::lib::u256::math::{ checked_add_256, - safe_add_256, + add_256, checked_sub_256, - safe_sub_256, + sub_256, checked_mul_256, - safe_mul_256, + mul_256, checked_div_256, div_256 }; @@ -32,7 +32,7 @@ fn main() { }; match is_selected(1, fn_idx) { true => { - assert_eq_256(safe_add_256(a, b), unwrap(expected)); + assert_eq_256(add_256(a, b), unwrap(expected)); }, false => (), }; @@ -46,7 +46,7 @@ fn main() { }; match is_selected(3, fn_idx) { true => { - assert_eq_256(safe_sub_256(a, b), unwrap(expected)); + assert_eq_256(sub_256(a, b), unwrap(expected)); }, false => (), }; @@ -60,7 +60,7 @@ fn main() { }; match is_selected(5, fn_idx) { true => { - assert_eq_256(safe_mul_256(a, b), unwrap(expected)); + assert_eq_256(mul_256(a, b), unwrap(expected)); }, false => (), }; diff --git a/simf/tests/u256/math/sub_mul.simf b/simf/tests/u256/math/sub_mul.simf index 4b525a0..2d47842 100644 --- a/simf/tests/u256/math/sub_mul.simf +++ b/simf/tests/u256/math/sub_mul.simf @@ -1,10 +1,10 @@ use crate::lib::u256::math::{ - sub_256, - mul_256, - mul_256_64, - mul_256_128, - mul_512_128, - safe_mul_256_128 + sub_256_borrow, + mul_256_wide, + mul_256_64_wide, + mul_256_128_wide, + mul_512_128_wide, + mul_256_128 }; use crate::lib::u256::convert::split_u256_into_u64; use crate::lib::asserts::{assert_eq_64, assert_eq_128, assert_eq_256}; @@ -28,14 +28,14 @@ fn main() { match is_selected(0, fn_idx) { true => { - assert_eq_uint_bool_256(sub_256(a, b), unwrap(expected), expected_bool); + assert_eq_uint_bool_256(sub_256_borrow(a, b), unwrap(expected), expected_bool); }, false => (), }; match is_selected(1, fn_idx) { true => { - let (result_high, result_low): (u256, u256) = mul_256(a, b); + let (result_high, result_low): (u256, u256) = mul_256_wide(a, b); assert_eq_256(result_high, unwrap(expected)); assert_eq_256(result_low, second_expected); @@ -48,7 +48,7 @@ fn main() { let (_, _, _, b): (u64, u64, u64, u64) = split_u256_into_u64(b); let (_, _, _, expected): (u64, u64, u64, u64) = split_u256_into_u64(unwrap(expected)); - let (result_high, result_low): (u64, u256) = mul_256_64(a, b); + let (result_high, result_low): (u64, u256) = mul_256_64_wide(a, b); assert_eq_64(result_high, expected); assert_eq_256(result_low, second_expected); @@ -61,7 +61,7 @@ fn main() { let (_, b): (u128, u128) = ::into(b); let (_, expected): (u128, u128) = ::into(unwrap(expected)); - let (result_high, result_low): (u128, u256) = mul_256_128(a, b); + let (result_high, result_low): (u128, u256) = mul_256_128_wide(a, b); assert_eq_128(result_high, expected); assert_eq_256(result_low, second_expected); @@ -71,7 +71,7 @@ fn main() { match is_selected(4, fn_idx) { true => { - let (result_2, result_1, result_0): (u128, u256, u256) = mul_512_128(a, b, c); + let (result_2, result_1, result_0): (u128, u256, u256) = mul_512_128_wide(a, b, c); let (_, expected): (u128, u128) = ::into(unwrap(expected)); @@ -85,7 +85,7 @@ fn main() { match is_selected(5, fn_idx) { true => { let (_, b): (u128, u128) = ::into(b); - let result: u256 = safe_mul_256_128(a, b); + let result: u256 = mul_256_128(a, b); assert_eq_256(result, unwrap(expected)); }, diff --git a/simf/tests/u32/math.simf b/simf/tests/u32/math.simf index cc7f1d3..103b12a 100644 --- a/simf/tests/u32/math.simf +++ b/simf/tests/u32/math.simf @@ -1,10 +1,10 @@ use crate::lib::u32::math::{ checked_add_32, - safe_add_32, + add_32, checked_sub_32, - safe_sub_32, + sub_32, checked_mul_32, - safe_mul_32, + mul_32, checked_div_32, div_32, gt_32, @@ -30,7 +30,7 @@ fn main() { }; match is_selected(1, fn_idx) { true => { - assert!(jet::eq_32(safe_add_32(a, b), unwrap(expected))); + assert!(jet::eq_32(add_32(a, b), unwrap(expected))); }, false => (), }; @@ -44,7 +44,7 @@ fn main() { }; match is_selected(3, fn_idx) { true => { - assert!(jet::eq_32(safe_sub_32(a, b), unwrap(expected))); + assert!(jet::eq_32(sub_32(a, b), unwrap(expected))); }, false => (), }; @@ -58,7 +58,7 @@ fn main() { }; match is_selected(5, fn_idx) { true => { - assert!(jet::eq_32(safe_mul_32(a, b), unwrap(expected))); + assert!(jet::eq_32(mul_32(a, b), unwrap(expected))); }, false => (), }; diff --git a/simf/tests/u64/math.simf b/simf/tests/u64/math.simf index 979ba8e..6f5f510 100644 --- a/simf/tests/u64/math.simf +++ b/simf/tests/u64/math.simf @@ -1,10 +1,10 @@ use crate::lib::u64::math::{ checked_add_64, - safe_add_64, + add_64, checked_sub_64, - safe_sub_64, + sub_64, checked_mul_64, - safe_mul_64, + mul_64, checked_div_64, div_64, gt_64, @@ -30,7 +30,7 @@ fn main() { }; match is_selected(1, fn_idx) { true => { - assert!(jet::eq_64(safe_add_64(a, b), unwrap(expected))); + assert!(jet::eq_64(add_64(a, b), unwrap(expected))); }, false => (), }; @@ -44,7 +44,7 @@ fn main() { }; match is_selected(3, fn_idx) { true => { - assert!(jet::eq_64(safe_sub_64(a, b), unwrap(expected))); + assert!(jet::eq_64(sub_64(a, b), unwrap(expected))); }, false => (), }; @@ -58,7 +58,7 @@ fn main() { }; match is_selected(5, fn_idx) { true => { - assert!(jet::eq_64(safe_mul_64(a, b), unwrap(expected))); + assert!(jet::eq_64(mul_64(a, b), unwrap(expected))); }, false => (), }; diff --git a/simf/tests/u8/math.simf b/simf/tests/u8/math.simf index b817130..9e3eace 100644 --- a/simf/tests/u8/math.simf +++ b/simf/tests/u8/math.simf @@ -1,10 +1,10 @@ use crate::lib::u8::math::{ checked_add_8, - safe_add_8, + add_8, checked_sub_8, - safe_sub_8, + sub_8, checked_mul_8, - safe_mul_8, + mul_8, checked_div_8, div_8, gt_8, @@ -30,7 +30,7 @@ fn main() { }; match is_selected(1, fn_idx) { true => { - assert!(jet::eq_8(safe_add_8(a, b), unwrap(expected))); + assert!(jet::eq_8(add_8(a, b), unwrap(expected))); }, false => (), }; @@ -44,7 +44,7 @@ fn main() { }; match is_selected(3, fn_idx) { true => { - assert!(jet::eq_8(safe_sub_8(a, b), unwrap(expected))); + assert!(jet::eq_8(sub_8(a, b), unwrap(expected))); }, false => (), }; @@ -58,7 +58,7 @@ fn main() { }; match is_selected(5, fn_idx) { true => { - assert!(jet::eq_8(safe_mul_8(a, b), unwrap(expected))); + assert!(jet::eq_8(mul_8(a, b), unwrap(expected))); }, false => (), }; diff --git a/tests/stdlib/common/core.rs b/tests/stdlib/common/core.rs index ada5e42..e11226b 100644 --- a/tests/stdlib/common/core.rs +++ b/tests/stdlib/common/core.rs @@ -14,7 +14,7 @@ pub enum Expect { Ok, /// A failed `assert!` in the contract. AssertFailed, - /// Execution reached a pruned branch (e.g. `unwrap(None)`, a `safe_*` overflow). + /// Execution reached a pruned branch (e.g. `unwrap(None)`). PrunedBranch, } diff --git a/tests/stdlib/common/uint.rs b/tests/stdlib/common/uint.rs index 5be4b76..1169c33 100644 --- a/tests/stdlib/common/uint.rs +++ b/tests/stdlib/common/uint.rs @@ -19,11 +19,11 @@ use super::core::{Expect, run}; /// program under `simf/tests`. pub enum CommonOp { CheckedAdd, - SafeAdd, + Add, CheckedSub, - SafeSub, + Sub, CheckedMul, - SafeMul, + Mul, CheckedDiv, Div, Gt, @@ -90,25 +90,25 @@ pub fn checked_add_overflow(context: simplex::TestContext) -> anyho ) } -pub fn safe_add_fitting(context: simplex::TestContext) -> anyhow::Result<()> { +pub fn add_fitting(context: simplex::TestContext) -> anyhow::Result<()> { let a = rand::thread_rng().gen_range(T::ZERO..=T::HALF_MAX); let b = rand::thread_rng().gen_range(T::ZERO..=T::HALF_MAX); run( &context, T::program(), - T::witness(op(CommonOp::SafeAdd), a, b, Some(a + b)), + T::witness(op(CommonOp::Add), a, b, Some(a + b)), Expect::Ok, ) } -pub fn safe_add_overflow(context: simplex::TestContext) -> anyhow::Result<()> { +pub fn add_overflow(context: simplex::TestContext) -> anyhow::Result<()> { let b = rand::thread_rng().gen_range(T::ONE..=T::MAX); run( &context, T::program(), - T::witness(op(CommonOp::SafeAdd), T::MAX, b, None), + T::witness(op(CommonOp::Add), T::MAX, b, None), Expect::PrunedBranch, ) } @@ -138,26 +138,26 @@ pub fn checked_sub_overflow(context: simplex::TestContext) -> anyho ) } -pub fn safe_sub_fitting(context: simplex::TestContext) -> anyhow::Result<()> { +pub fn sub_fitting(context: simplex::TestContext) -> anyhow::Result<()> { let a = rand::thread_rng().gen_range(T::ZERO..=T::MAX); let b = rand::thread_rng().gen_range(T::ZERO..=a); run( &context, T::program(), - T::witness(op(CommonOp::SafeSub), a, b, Some(a - b)), + T::witness(op(CommonOp::Sub), a, b, Some(a - b)), Expect::Ok, ) } -pub fn safe_sub_overflow(context: simplex::TestContext) -> anyhow::Result<()> { +pub fn sub_overflow(context: simplex::TestContext) -> anyhow::Result<()> { let a = rand::thread_rng().gen_range(T::ZERO..=T::MAX - T::ONE); let b = rand::thread_rng().gen_range(a + T::ONE..=T::MAX); run( &context, T::program(), - T::witness(op(CommonOp::SafeSub), a, b, None), + T::witness(op(CommonOp::Sub), a, b, None), Expect::PrunedBranch, ) } @@ -187,26 +187,26 @@ pub fn checked_mul_overflow(context: simplex::TestContext) -> anyho ) } -pub fn safe_mul_fitting(context: simplex::TestContext) -> anyhow::Result<()> { +pub fn mul_fitting(context: simplex::TestContext) -> anyhow::Result<()> { let a = rand::thread_rng().gen_range(T::ZERO..T::MUL_BOUND); let b = rand::thread_rng().gen_range(T::ZERO..T::MUL_BOUND); run( &context, T::program(), - T::witness(op(CommonOp::SafeMul), a, b, Some(a * b)), + T::witness(op(CommonOp::Mul), a, b, Some(a * b)), Expect::Ok, ) } -pub fn safe_mul_overflow(context: simplex::TestContext) -> anyhow::Result<()> { +pub fn mul_overflow(context: simplex::TestContext) -> anyhow::Result<()> { let two = T::ONE + T::ONE; let b = rand::thread_rng().gen_range(two..=T::MAX); run( &context, T::program(), - T::witness(op(CommonOp::SafeMul), T::MAX, b, None), + T::witness(op(CommonOp::Mul), T::MAX, b, None), Expect::PrunedBranch, ) } @@ -370,11 +370,11 @@ macro_rules! uint_tests { ($t:ty) => { $crate::uint_tests!(@stub $t; checked_add_fitting checked_add_overflow - safe_add_fitting safe_add_overflow + add_fitting add_overflow checked_sub_fitting checked_sub_overflow - safe_sub_fitting safe_sub_overflow + sub_fitting sub_overflow checked_mul_fitting checked_mul_overflow - safe_mul_fitting safe_mul_overflow + mul_fitting mul_overflow checked_div_fitting checked_div_by_zero div_fitting div_by_zero gt_greater gt_equal diff --git a/tests/stdlib/u128/math/primitives.rs b/tests/stdlib/u128/math/primitives.rs index f415f31..3d39661 100644 --- a/tests/stdlib/u128/math/primitives.rs +++ b/tests/stdlib/u128/math/primitives.rs @@ -12,19 +12,18 @@ use simplicityhl_std::artifacts::tests::u128::math::primitives::derived_primitiv use FunctionToTest::*; enum FunctionToTest { - Add128, - Add128_64, + Add128Carry, + Add128_64Carry, FullAdd128, - Sub128, + Sub128Borrow, FullSub128, - Mul128, - Mul128_64, + Mul128Wide, + Mul128_64Wide, CalculateNormalizerBase64, EstimateQuotientDigitBase64, DivMod128_64, DivMod128, } -// div_128 is already tested through safe_div_fitting and safe_div_by_zero in api.rs fn program() -> U128BasicMathTestProgram { U128BasicMathTestProgram::new(&U128BasicMathTestArguments {}) @@ -95,21 +94,21 @@ fn split_helper(a: U256) -> (u128, u128) { } #[simplex::test] -fn add_128_not_overflow(context: simplex::TestContext) -> anyhow::Result<()> { +fn add_128_carry_not_overflow(context: simplex::TestContext) -> anyhow::Result<()> { let a = rand::thread_rng().gen_range(0..=u128::MAX / 2); let b = rand::thread_rng().gen_range(0..=u128::MAX / 2); let result = a + b; - case(Add128).args(a, b).expect(result).run(&context) + case(Add128Carry).args(a, b).expect(result).run(&context) } #[simplex::test] -fn add_128_overflow(context: simplex::TestContext) -> anyhow::Result<()> { +fn add_128_carry_overflow(context: simplex::TestContext) -> anyhow::Result<()> { let a = u128::MAX; let b = rand::thread_rng().gen_range(1..=u128::MAX); let result = b - 1; - case(Add128) + case(Add128Carry) .args(a, b) .expect(result) .flag(true) @@ -117,21 +116,21 @@ fn add_128_overflow(context: simplex::TestContext) -> anyhow::Result<()> { } #[simplex::test] -fn add_128_64_not_overflow(context: simplex::TestContext) -> anyhow::Result<()> { +fn add_128_64_carry_not_overflow(context: simplex::TestContext) -> anyhow::Result<()> { let a = rand::thread_rng().gen_range(0..=u128::MAX / 2); let b = rand::thread_rng().gen_range(0..=u64::MAX) as u128; let result = a + b; - case(Add128_64).args(a, b).expect(result).run(&context) + case(Add128_64Carry).args(a, b).expect(result).run(&context) } #[simplex::test] -fn add_128_64_overflow(context: simplex::TestContext) -> anyhow::Result<()> { +fn add_128_64_carry_overflow(context: simplex::TestContext) -> anyhow::Result<()> { let a = u128::MAX; let b = rand::thread_rng().gen_range(1..=u64::MAX) as u128; let result = b - 1; - case(Add128_64) + case(Add128_64Carry) .args(a, b) .expect(result) .flag(true) @@ -203,23 +202,23 @@ fn full_add_128_overflow_carry_low_true(context: simplex::TestContext) -> anyhow } #[simplex::test] -fn sub_128_not_overflow(context: simplex::TestContext) -> anyhow::Result<()> { +fn sub_128_borrow_not_overflow(context: simplex::TestContext) -> anyhow::Result<()> { let a = rand::thread_rng().gen_range(0..=u128::MAX); let b = rand::thread_rng().gen_range(0..=a); let result = a - b; - case(Sub128).args(a, b).expect(result).run(&context) + case(Sub128Borrow).args(a, b).expect(result).run(&context) } #[simplex::test] -fn sub_128_a_eq_b(context: simplex::TestContext) -> anyhow::Result<()> { +fn sub_128_borrow_a_eq_b(context: simplex::TestContext) -> anyhow::Result<()> { let a = rand::thread_rng().gen_range(0..=u128::MAX); - case(Sub128).args(a, a).expect(0).run(&context) + case(Sub128Borrow).args(a, a).expect(0).run(&context) } #[simplex::test] -fn sub_128_a_low_eq_b_low(context: simplex::TestContext) -> anyhow::Result<()> { +fn sub_128_borrow_a_low_eq_b_low(context: simplex::TestContext) -> anyhow::Result<()> { let a = rand::thread_rng().gen_range(0..=u128::MAX); let b_high = rand::thread_rng().gen_range(0..=u64::MAX); @@ -229,7 +228,7 @@ fn sub_128_a_low_eq_b_low(context: simplex::TestContext) -> anyhow::Result<()> { let carry = a < b; let result = a.wrapping_sub(b); - case(Sub128) + case(Sub128Borrow) .args(a, b) .expect(result) .flag(carry) @@ -237,7 +236,7 @@ fn sub_128_a_low_eq_b_low(context: simplex::TestContext) -> anyhow::Result<()> { } #[simplex::test] -fn sub_128_diff_is_u64_max(context: simplex::TestContext) -> anyhow::Result<()> { +fn sub_128_borrow_diff_is_u64_max(context: simplex::TestContext) -> anyhow::Result<()> { let a_low: u64 = u64::MAX; let a_high = rand::thread_rng().gen_range(0..=u64::MAX); @@ -249,7 +248,7 @@ fn sub_128_diff_is_u64_max(context: simplex::TestContext) -> anyhow::Result<()> let carry = a < b; let result = a.wrapping_sub(b); - case(Sub128) + case(Sub128Borrow) .args(a, b) .expect(result) .flag(carry) @@ -257,20 +256,20 @@ fn sub_128_diff_is_u64_max(context: simplex::TestContext) -> anyhow::Result<()> } #[simplex::test] -fn sub_128_diff_is_u128_max(context: simplex::TestContext) -> anyhow::Result<()> { +fn sub_128_borrow_diff_is_u128_max(context: simplex::TestContext) -> anyhow::Result<()> { let a = u128::MAX; let b = 0; - case(Sub128).args(a, b).expect(a).run(&context) + case(Sub128Borrow).args(a, b).expect(a).run(&context) } #[simplex::test] -fn sub_128_overflow(context: simplex::TestContext) -> anyhow::Result<()> { +fn sub_128_borrow_overflow(context: simplex::TestContext) -> anyhow::Result<()> { let a = rand::thread_rng().gen_range(0..u128::MAX); let b = u128::MAX; let result = a + 1; - case(Sub128) + case(Sub128Borrow) .args(a, b) .expect(result) .flag(true) @@ -342,14 +341,14 @@ fn full_sub_128_overflow_borrow_low_true(context: simplex::TestContext) -> anyho } #[simplex::test] -fn mul_128(context: simplex::TestContext) -> anyhow::Result<()> { +fn mul_128_wide(context: simplex::TestContext) -> anyhow::Result<()> { let a = rand::thread_rng().gen_range(0..u128::MAX); let b = rand::thread_rng().gen_range(0..u128::MAX); let result = U256::from(a) * U256::from(b); let (result_high, result_low) = split_helper(result); - case(Mul128) + case(Mul128Wide) .args(a, b) .expect(result_high) .second(result_low) @@ -357,14 +356,14 @@ fn mul_128(context: simplex::TestContext) -> anyhow::Result<()> { } #[simplex::test] -fn mul_128_64(context: simplex::TestContext) -> anyhow::Result<()> { +fn mul_128_64_wide(context: simplex::TestContext) -> anyhow::Result<()> { let a = rand::thread_rng().gen_range(0..u128::MAX); let b = rand::thread_rng().gen_range(0..u64::MAX); let result = U256::from(a) * U256::from(b); let (result_high, result_low) = split_helper(result); - case(Mul128_64) + case(Mul128_64Wide) .args(a, b as u128) .expect(result_high) .second(result_low) diff --git a/tests/stdlib/u256/math/add.rs b/tests/stdlib/u256/math/add.rs index bbd813c..5af5660 100644 --- a/tests/stdlib/u256/math/add.rs +++ b/tests/stdlib/u256/math/add.rs @@ -11,8 +11,8 @@ use simplicityhl_std::artifacts::tests::u256::math::add::derived_add::{ use FunctionToTest::*; enum FunctionToTest { - Add256, - Add256_128, + Add256Carry, + Add256_128Carry, FullAdd256, } @@ -77,24 +77,24 @@ impl Case { } #[simplex::test] -fn add_256_not_overflow(context: simplex::TestContext) -> anyhow::Result<()> { +fn add_256_carry_not_overflow(context: simplex::TestContext) -> anyhow::Result<()> { let a = generate_u256(U256::zero(), U256::MAX / 2); let b = generate_u256(U256::zero(), U256::MAX / 2); let result = (a + b).to_big_endian(); - case(Add256) + case(Add256Carry) .args(a.to_big_endian(), b.to_big_endian()) .expect(result) .run(&context) } #[simplex::test] -fn add_256_overflow(context: simplex::TestContext) -> anyhow::Result<()> { +fn add_256_carry_overflow(context: simplex::TestContext) -> anyhow::Result<()> { let a = U256::MAX; let b = generate_u256(U256::one(), U256::MAX); let result = (b - 1).to_big_endian(); - case(Add256) + case(Add256Carry) .args(a.to_big_endian(), b.to_big_endian()) .expect(result) .flag(true) @@ -102,24 +102,24 @@ fn add_256_overflow(context: simplex::TestContext) -> anyhow::Result<()> { } #[simplex::test] -fn add_256_128_not_overflow(context: simplex::TestContext) -> anyhow::Result<()> { +fn add_256_128_carry_not_overflow(context: simplex::TestContext) -> anyhow::Result<()> { let a = generate_u256(U256::zero(), U256::MAX / 2); let b = generate_u256(U256::one(), U256::from(u128::MAX)); let result = (a + b).to_big_endian(); - case(Add256_128) + case(Add256_128Carry) .args(a.to_big_endian(), b.to_big_endian()) .expect(result) .run(&context) } #[simplex::test] -fn add_256_128_overflow(context: simplex::TestContext) -> anyhow::Result<()> { +fn add_256_128_carry_overflow(context: simplex::TestContext) -> anyhow::Result<()> { let a = U256::MAX; let b = generate_u256(U256::one(), U256::from(u128::MAX)); let result = (b - 1).to_big_endian(); - case(Add256_128) + case(Add256_128Carry) .args(a.to_big_endian(), b.to_big_endian()) .expect(result) .flag(true) diff --git a/tests/stdlib/u256/math/div.rs b/tests/stdlib/u256/math/div.rs index a35f63f..7b40598 100644 --- a/tests/stdlib/u256/math/div.rs +++ b/tests/stdlib/u256/math/div.rs @@ -17,7 +17,6 @@ enum FunctionToTest { DivMod256_128, DivMod256, } -// div_256 is already tested through safe_div_fitting and safe_div_by_zero in api.rs fn program() -> U256TestDivProgram { U256TestDivProgram::new(&U256TestDivArguments {}) diff --git a/tests/stdlib/u256/math/sub_mul.rs b/tests/stdlib/u256/math/sub_mul.rs index 2d5a64d..3773232 100644 --- a/tests/stdlib/u256/math/sub_mul.rs +++ b/tests/stdlib/u256/math/sub_mul.rs @@ -13,12 +13,12 @@ use simplicityhl_std::artifacts::tests::u256::math::sub_mul::derived_sub_mul::{ use FunctionToTest::*; enum FunctionToTest { - Sub256, - Mul256, - Mul256_64, + Sub256Borrow, + Mul256Wide, + Mul256_64Wide, + Mul256_128Wide, + Mul512_128Wide, Mul256_128, - Mul512_128, - SafeMul256_128, } fn program() -> U256TestSubMulProgram { @@ -103,26 +103,26 @@ fn split_u512(a: [u8; 64]) -> ([u8; 32], [u8; 32]) { } #[simplex::test] -fn sub_256_not_overflow(context: simplex::TestContext) -> anyhow::Result<()> { +fn sub_256_borrow_not_overflow(context: simplex::TestContext) -> anyhow::Result<()> { let a = generate_u256(U256::zero(), U256::MAX); let b = generate_u256(U256::zero(), a); let result = (a - b).to_big_endian(); - case(Sub256) + case(Sub256Borrow) .args(a.to_big_endian(), b.to_big_endian()) .expect(result) .run(&context) } #[simplex::test] -fn sub_256_a_eq_b(context: simplex::TestContext) -> anyhow::Result<()> { +fn sub_256_borrow_a_eq_b(context: simplex::TestContext) -> anyhow::Result<()> { let a = generate_u256(U256::zero(), U256::MAX).to_big_endian(); - case(Sub256).args(a, a).expect([0; 32]).run(&context) + case(Sub256Borrow).args(a, a).expect([0; 32]).run(&context) } #[simplex::test] -fn sub_256_a_low_eq_b_low(context: simplex::TestContext) -> anyhow::Result<()> { +fn sub_256_borrow_a_low_eq_b_low(context: simplex::TestContext) -> anyhow::Result<()> { let a = generate_u256(U256::zero(), U256::MAX); let b_high = rand::thread_rng().gen_range(0..=u128::MAX); @@ -131,7 +131,7 @@ fn sub_256_a_low_eq_b_low(context: simplex::TestContext) -> anyhow::Result<()> { let (result, carry) = a.overflowing_sub(b); - case(Sub256) + case(Sub256Borrow) .args(a.to_big_endian(), b.to_big_endian()) .expect(result.to_big_endian()) .flag(carry) @@ -139,7 +139,7 @@ fn sub_256_a_low_eq_b_low(context: simplex::TestContext) -> anyhow::Result<()> { } #[simplex::test] -fn sub_256_diff_is_u128_max(context: simplex::TestContext) -> anyhow::Result<()> { +fn sub_256_borrow_diff_is_u128_max(context: simplex::TestContext) -> anyhow::Result<()> { let a_low: u128 = u128::MAX; let a_high = rand::thread_rng().gen_range(0..=u128::MAX); @@ -150,7 +150,7 @@ fn sub_256_diff_is_u128_max(context: simplex::TestContext) -> anyhow::Result<()> let (result, carry) = a.overflowing_sub(b); - case(Sub256) + case(Sub256Borrow) .args(a.to_big_endian(), b.to_big_endian()) .expect(result.to_big_endian()) .flag(carry) @@ -158,23 +158,23 @@ fn sub_256_diff_is_u128_max(context: simplex::TestContext) -> anyhow::Result<()> } #[simplex::test] -fn sub_256_diff_is_u256_max(context: simplex::TestContext) -> anyhow::Result<()> { +fn sub_256_borrow_diff_is_u256_max(context: simplex::TestContext) -> anyhow::Result<()> { let a = U256::MAX.to_big_endian(); let b = U256::zero(); - case(Sub256) + case(Sub256Borrow) .args(a, b.to_big_endian()) .expect(a) .run(&context) } #[simplex::test] -fn sub_256_overflow(context: simplex::TestContext) -> anyhow::Result<()> { +fn sub_256_borrow_overflow(context: simplex::TestContext) -> anyhow::Result<()> { let a = generate_u256(U256::one(), U256::MAX - 1); let b = U256::MAX; let result = a + 1; - case(Sub256) + case(Sub256Borrow) .args(a.to_big_endian(), b.to_big_endian()) .expect(result.to_big_endian()) .flag(true) @@ -182,14 +182,14 @@ fn sub_256_overflow(context: simplex::TestContext) -> anyhow::Result<()> { } #[simplex::test] -fn mul_256(context: simplex::TestContext) -> anyhow::Result<()> { +fn mul_256_wide(context: simplex::TestContext) -> anyhow::Result<()> { let a = generate_u256(U256::one(), U256::MAX); let b = generate_u256(U256::one(), U256::MAX); let result = a.full_mul(b).to_big_endian(); let (result_high, result_low) = split_u512(result); - case(Mul256) + case(Mul256Wide) .args(a.to_big_endian(), b.to_big_endian()) .expect(result_high) .second(result_low) @@ -197,14 +197,14 @@ fn mul_256(context: simplex::TestContext) -> anyhow::Result<()> { } #[simplex::test] -fn mul_256_64(context: simplex::TestContext) -> anyhow::Result<()> { +fn mul_256_64_wide(context: simplex::TestContext) -> anyhow::Result<()> { let a = generate_u256(U256::one(), U256::MAX); let b = generate_u256(U256::one(), U256::from(u64::MAX)); let result = a.full_mul(b).to_big_endian(); let (result_high, result_low) = split_u512(result); - case(Mul256_64) + case(Mul256_64Wide) .args(a.to_big_endian(), b.to_big_endian()) .expect(result_high) .second(result_low) @@ -212,14 +212,14 @@ fn mul_256_64(context: simplex::TestContext) -> anyhow::Result<()> { } #[simplex::test] -fn mul_256_128(context: simplex::TestContext) -> anyhow::Result<()> { +fn mul_256_128_wide(context: simplex::TestContext) -> anyhow::Result<()> { let a = generate_u256(U256::one(), U256::MAX); let b = generate_u256(U256::one(), U256::from(u128::MAX)); let result = a.full_mul(b).to_big_endian(); let (result_high, result_low) = split_u512(result); - case(Mul256_128) + case(Mul256_128Wide) .args(a.to_big_endian(), b.to_big_endian()) .expect(result_high) .second(result_low) @@ -227,7 +227,7 @@ fn mul_256_128(context: simplex::TestContext) -> anyhow::Result<()> { } #[simplex::test] -fn mul_512_128(context: simplex::TestContext) -> anyhow::Result<()> { +fn mul_512_128_wide(context: simplex::TestContext) -> anyhow::Result<()> { let a_1 = generate_u256(U256::one(), U256::MAX); let a_0 = generate_u256(U256::one(), U256::MAX); let b = rand::thread_rng().gen_range(1..=u128::MAX); @@ -244,7 +244,7 @@ fn mul_512_128(context: simplex::TestContext) -> anyhow::Result<()> { let res_3_final = (U256::from_big_endian(&res_3_1) + U256::from_big_endian(&res_3)).to_big_endian(); - case(Mul512_128) + case(Mul512_128Wide) .args(a_1.to_big_endian(), a_0.to_big_endian()) .third_arg(b) .expect(res_3_final) @@ -254,22 +254,22 @@ fn mul_512_128(context: simplex::TestContext) -> anyhow::Result<()> { } #[simplex::test] -fn safe_mul_256_128_fitting(context: simplex::TestContext) -> anyhow::Result<()> { +fn mul_256_128_fitting(context: simplex::TestContext) -> anyhow::Result<()> { let a = generate_u256(U256::zero(), U256::from(u128::MAX)); let b = generate_u256(U256::zero(), U256::from(u128::MAX)); - case(SafeMul256_128) + case(Mul256_128) .args(a.to_big_endian(), b.to_big_endian()) .expect((a * b).to_big_endian()) .run(&context) } #[simplex::test] -fn safe_mul_256_128_overflow(context: simplex::TestContext) -> anyhow::Result<()> { +fn mul_256_128_overflow(context: simplex::TestContext) -> anyhow::Result<()> { let a = generate_u256(U256::from(u128::MAX) + 1, U256::MAX); let b = U256::from(u128::MAX); - case(SafeMul256_128) + case(Mul256_128) .args(a.to_big_endian(), b.to_big_endian()) .expect([0; 32]) .expecting(&context, Expect::AssertFailed)