From 558f78aea87ac8fad3f5860bb92cb3d259affe59 Mon Sep 17 00:00:00 2001 From: Jordy Romuald <87231934+JordyRo1@users.noreply.github.com> Date: Tue, 14 Nov 2023 09:50:43 +0100 Subject: [PATCH] audit: additional verifications (#78) Audit: adding more verifications to avoid undefined operations --- src/operations/time_series/convert.cairo | 2 +- src/operations/time_series/metrics.cairo | 2 ++ src/operations/time_series/scaler.cairo | 3 +++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/operations/time_series/convert.cairo b/src/operations/time_series/convert.cairo index 72da3c41..df9f8341 100644 --- a/src/operations/time_series/convert.cairo +++ b/src/operations/time_series/convert.cairo @@ -17,7 +17,7 @@ fn div_decimals(a_price: u128, b_price: u128, output_decimals: u128) -> u128 { assert(power <= MAX_POWER, 'Conversion overflow'); assert(a_price <= MAX_POWER, 'Conversion overflow'); - + assert(b_price > 0, 'Division by zero'); a_price * power / b_price } diff --git a/src/operations/time_series/metrics.cairo b/src/operations/time_series/metrics.cairo index 2fd8ecad..8cc94f09 100644 --- a/src/operations/time_series/metrics.cairo +++ b/src/operations/time_series/metrics.cairo @@ -130,8 +130,10 @@ fn _sum_volatility(arr: Span) -> Fixed { let prev_val = *arr.at(cur_idx - 1); let cur_value = cur_val.value; let prev_value = prev_val.value; + assert(prev_value.mag > 0, 'failed to compute vol'); let cur_timestamp = cur_val.tick; let prev_timestamp = prev_val.tick; + assert(cur_timestamp > prev_timestamp, 'failed to compute vol'); if (prev_timestamp > cur_timestamp) { //edge case assert(1 == 1, 'failed to compute vol'); diff --git a/src/operations/time_series/scaler.cairo b/src/operations/time_series/scaler.cairo index cf15c0d4..537d19b3 100644 --- a/src/operations/time_series/scaler.cairo +++ b/src/operations/time_series/scaler.cairo @@ -10,6 +10,7 @@ use debug::PrintTrait; use cubit::f128::types::fixed::{FixedTrait, Fixed, FixedPrint, ONE_u128}; fn calculate_slope(x1: Fixed, x2: Fixed, y1: Fixed, y2: Fixed) -> Fixed { + assert(x2 > x1, 'cannot compute slope(x2 Fixed { fn scale_data( start_tick: u64, end_tick: u64, tick_array: Span, num_intervals: u32 ) -> Array { + assert(num_intervals > 1, 'intervals must be > than 1'); + assert(start_tick <= end_tick, 'start_tick must be < end_tick'); let interval = (end_tick - start_tick) / (num_intervals.into() - 1); let mut output: Array = ArrayTrait::new();