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: muldiv #20

Merged
merged 1 commit into from
Feb 28, 2023
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
36 changes: 30 additions & 6 deletions src/math.cairo
Original file line number Diff line number Diff line change
@@ -1,9 +1,33 @@
// use quaireaux::math::fibonacci;
#[derive(Copy, Drop)]
enum Rounding {
Down: (), // Toward negative infinity
Up: (), // Toward infinity
Zero: (), // Toward zero
}

// Return x * y / denominator rounded down
fn mul_div_down(x: u128, y: u128, denominator: u128) -> u128 {
let max_u128 = 0xffffffffffffffffffffffffffffffff_u128;
let cond = denominator != 0_u128 & (y == 0_u128 | x <= max_u128 / y);
assert(cond, 'division underflow');
x * y / denominator
}

fn add(a: felt, b: felt) -> felt {
a + b
// Return x * y % denominator
fn mul_mod(x: u128, y: u128, denominator: u128) -> u128 {
x * y % denominator
}

// fn fib(a: felt, b: felt, n: felt) -> felt {
// fibonacci::fib(a, b, n)
// }
// Return x * y / denominator with specified rounding
fn mul_div(x: u128, y: u128, denominator: u128, rounding: Rounding) -> u128 {
let result = mul_div_down(x, y, denominator);
match rounding {
Rounding::Down(_) => result,
Rounding::Up(_) => if mul_mod(x, y, denominator) > 0_u128 {
result + 1_u128
} else {
result
},
Rounding::Zero(_) => result,
}
}
51 changes: 46 additions & 5 deletions src/test/math_test.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,48 @@
use cairo_erc4626::math;
use option::OptionTrait;
use traits::TryInto;
use traits::PartialEq;

use cairo_erc4626::math::mul_div;
use cairo_erc4626::math::Rounding;

fn t_mul_div_down(x: felt, y: felt, denominator: felt, expected: felt) {
let result = mul_div(x.try_into().unwrap(), y.try_into().unwrap(), denominator.try_into().unwrap(), Rounding::Down(()));
assert(result == expected.try_into().unwrap(), 'mul_div invalid');
}

fn t_mul_div_up(x: felt, y: felt, denominator: felt, expected: felt) {
let result = mul_div(x.try_into().unwrap(), y.try_into().unwrap(), denominator.try_into().unwrap(), Rounding::Up(()));
assert(result == expected.try_into().unwrap(), 'mul_div invalid');
}

#[test]
fn test_mul_div_down() {
let base = 1000000000000000000;
t_mul_div_down(13, 7, 5, 18);
t_mul_div_down(7, 13, 5, 18);
t_mul_div_down(13, base, 7, 1857142857142857142);
t_mul_div_down(base, 13, 7, 1857142857142857142);
t_mul_div_down(0, 7, base, 0);
}

#[test]
fn test_mul_div_up() {
let base = 1000000000000000000;
t_mul_div_up(13, 7, 5, 19);
t_mul_div_up(7, 13, 5, 19);
t_mul_div_up(13, base, 7, 1857142857142857143);
t_mul_div_up(base, 13, 7, 1857142857142857143);
t_mul_div_up(0, 7, base, 0);
}

#[test]
#[should_panic(expected = ('division underflow',))]
fn test_mul_div_down_failed() {
mul_div(1_u128, 1_u128, 0_u128, Rounding::Down(()));
}

#[test]
fn math_test() {
assert(math::add(2, 3) == 5, 'invalid');
// assert(math::fib(0, 1, 10) == 55, 'invalid');
}
#[should_panic(expected = ('division underflow',))]
fn test_mul_div_up_failed() {
mul_div(1_u128, 1_u128, 0_u128, Rounding::Up(()));
}