Skip to content

Commit 9b1e7d5

Browse files
authored
Merge pull request #80 from aave/mike/improve/wad-ray-math
Mike/improve/wad ray math
2 parents 6463bfb + 0f54b08 commit 9b1e7d5

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

aave-core/aave-math/sources/wad_ray_math.move

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ module aave_math::wad_ray_math {
115115
/// @return c Result of a*b, in ray, rounded down
116116
public fun ray_mul_down(a: u256, b: u256): u256 {
117117
if (a == 0 || b == 0) return 0;
118+
assert!(a <= U256_MAX / b, error_config::get_eoverflow());
118119
(a * b) / RAY
119120
}
120121

@@ -155,6 +156,10 @@ module aave_math::wad_ray_math {
155156
public fun ray_div_down(a: u256, b: u256): u256 {
156157
assert!(b > 0, error_config::get_edivision_by_zero());
157158
if (a == 0) return 0;
159+
assert!(
160+
a <= U256_MAX / RAY,
161+
error_config::get_eoverflow()
162+
);
158163
(a * RAY) / b
159164
}
160165

aave-core/aave-math/tests/wad_ray_math_tests.move

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,4 +445,22 @@ module aave_math::wad_ray_math_tests {
445445
assert!(interest_up == 1050000000000000000000000000, TEST_SUCCESS);
446446
assert!(interest_down == 1050000000000000000000000000, TEST_SUCCESS);
447447
}
448+
449+
#[test]
450+
#[expected_failure(abort_code = EOVERFLOW, location = aave_math::wad_ray_math)]
451+
fun test_ray_div_down_overflow() {
452+
// a * RAY overflows at a = floor(U256_MAX / RAY) + 1
453+
let a = get_u256_max_for_testing() / get_ray_for_testing() + 1;
454+
let b = 1;
455+
ray_div_down(a, b);
456+
}
457+
458+
#[test]
459+
#[expected_failure(abort_code = EOVERFLOW, location = aave_math::wad_ray_math)]
460+
fun test_ray_mul_down_overflow() {
461+
// a * b overflows at a = floor(U256_MAX / b) + 1 (choose b > 0)
462+
let b = 1000000000000000000000000000; // 1 RAY
463+
let a = get_u256_max_for_testing() / b + 1;
464+
ray_mul_down(a, b);
465+
}
448466
}

0 commit comments

Comments
 (0)