Skip to content

Commit

Permalink
audit: separate function for pairwise (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
JordyRo1 authored Nov 14, 2023
1 parent 67cb9f4 commit 8cfc977
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 46 deletions.
83 changes: 44 additions & 39 deletions src/operations/time_series/metrics.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use pragma::operations::time_series::structs::{TickElem, PAIRWISE_OPERATION};
use pragma::operations::time_series::structs::{TickElem};

use cubit::f128::types::fixed::{
HALF_u128, MAX_u128, ONE_u128, Fixed, FixedInto, FixedTrait, FixedAdd, FixedDiv, FixedMul,
Expand Down Expand Up @@ -86,9 +86,9 @@ fn variance(tick_arr: Span<TickElem>) -> u128 {
let mean_ = mean(tick_arr);
let tick_arr_len = tick_arr.len();
let mean_arr = fill_1d(tick_arr_len, mean_);
let diff_arr = pairwise_1D(Operations::SUBTRACTION(()), arr_len, arr_.span(), mean_arr.span());
let diff_arr = pairwise_1D_sub(arr_len, arr_.span(), mean_arr.span());

let diff_squared = pairwise_1D(Operations::MULTIPLICATION(()), arr_len, diff_arr, diff_arr);
let diff_squared = pairwise_1D_mul(arr_len, diff_arr, diff_arr);

let sum_ = sum_array(diff_squared);

Expand Down Expand Up @@ -190,47 +190,52 @@ fn twap(arr: Span<TickElem>) -> u128 {
}

/// Computes a result array given two arrays and one operation
/// e.g : [1, 2, 3] + [1, 2, 3] = [2, 4, 6]
fn pairwise_1D(operation: Operations, x_len: u32, x: Span<Fixed>, y: Span<Fixed>) -> Span<Fixed> {
/// e.g : [1, 2, 3] - [1, 2, 3] = [0,0, 0]
fn pairwise_1D_sub(x_len: u32, x: Span<Fixed>, y: Span<Fixed>) -> Span<Fixed> {
//We assume, for simplicity, that the input arrays (x & y) are arrays of positive elements
let mut cur_idx: u32 = 0;
let mut output = ArrayTrait::<Fixed>::new();
match operation {
Operations::SUBTRACTION(()) => {
loop {
if (cur_idx >= x_len) {
break ();
}
let x1 = *x.get(cur_idx).unwrap().unbox();
let y1 = *y.get(cur_idx).unwrap().unbox();
if x1 < y1 {
output.append(FixedTrait::new(mag: y1.mag - x1.mag, sign: true));
} else {
output.append(FixedTrait::new(mag: x1.mag - y1.mag, sign: false));
}

cur_idx = cur_idx + 1;
};
},
Operations::MULTIPLICATION(()) => {
loop {
if (cur_idx >= x_len) {
break ();
}
let x1 = *x.get(cur_idx).unwrap().unbox();
let y1 = *y.get(cur_idx).unwrap().unbox();
if x1.sign == y1.sign {
output.append(FixedTrait::new(mag: x1.mag * y1.mag, sign: false));
} else {
output.append(FixedTrait::new(mag: x1.mag * y1.mag, sign: true));
}
cur_idx = cur_idx + 1;
};
},
}
loop {
if (cur_idx >= x_len) {
break ();
}
let x1 = *x.get(cur_idx).unwrap().unbox();
let y1 = *y.get(cur_idx).unwrap().unbox();
if x1 < y1 {
output.append(FixedTrait::new(mag: y1.mag - x1.mag, sign: true));
} else {
output.append(FixedTrait::new(mag: x1.mag - y1.mag, sign: false));
}

cur_idx = cur_idx + 1;
};
output.span()
}

/// Computes a result array given two arrays and one operation
/// e.g : [1, 2, 3] * [1, 2, 3] = [2, 4, 9]
fn pairwise_1D_mul(x_len: u32, x: Span<Fixed>, y: Span<Fixed>) -> Span<Fixed> {
//We assume, for simplicity, that the input arrays (x & y) are arrays of positive
let mut cur_idx: u32 = 0;
let mut output = ArrayTrait::<Fixed>::new();
loop {
if (cur_idx >= x_len) {
break ();
}
let x1 = *x.get(cur_idx).unwrap().unbox();
let y1 = *y.get(cur_idx).unwrap().unbox();
if x1.sign == y1.sign {
output.append(FixedTrait::new(mag: x1.mag * y1.mag, sign: false));
} else {
output.append(FixedTrait::new(mag: x1.mag * y1.mag, sign: true));
}
cur_idx = cur_idx + 1;
};
output.span()
}


/// Fills an array with one `value`
fn fill_1d(arr_len: u32, value: u128) -> Array<Fixed> {
let mut cur_idx = 0;
Expand Down Expand Up @@ -279,7 +284,7 @@ fn test_utils() {
//pairwise_1D
let x = fill_1d(3, 1);
let y = fill_1d(3, 2);
let z = pairwise_1D(Operations::SUBTRACTION(()), 3, x.span(), y.span());
let z = pairwise_1D_sub(3, x.span(), y.span());
assert(*z.at(0).mag == 1, 'wrong value');
assert(*z.at(0).sign == true, 'wrong value');
assert(*z.at(1).mag == 1, 'wrong value');
Expand All @@ -295,7 +300,7 @@ fn test_utils() {
//pairwise_1D
let x = fill_1d(3, 3);
let y = fill_1d(3, 2);
let z = pairwise_1D(Operations::SUBTRACTION(()), 3, x.span(), y.span());
let z = pairwise_1D_sub(3, x.span(), y.span());
assert(*z.at(0).mag == 1, 'wrong value');
assert(*z.at(0).sign == false, 'wrong value');
assert(*z.at(1).mag == 1, 'wrong value');
Expand Down
7 changes: 0 additions & 7 deletions src/operations/time_series/structs.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,3 @@ struct List {
arr: Array<u128>,
}

struct PAIRWISE_OPERATION {
ADDITION: (), // DEFAULT
SUBTRACTION: (),
MULTIPLICATION: (),
DIVISION: (),
FIXED_POINT_MULTIPLICATION: (),
}

0 comments on commit 8cfc977

Please sign in to comment.