Skip to content

Commit

Permalink
using column-major order for weight matrices in verifier
Browse files Browse the repository at this point in the history
  • Loading branch information
akirillo committed Sep 12, 2023
1 parent 8fbaf94 commit 5cf3582
Show file tree
Hide file tree
Showing 6 changed files with 279 additions and 398 deletions.
108 changes: 21 additions & 87 deletions src/testing/test_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -61,103 +61,37 @@ fn get_test_matrix() -> SparseWeightMatrix {
// [4, 5, 6, 0],
// ]

// Matrix (sparse):
// Matrix (sparse, column-major):
// [
// [(0, 1)],
// [(0, 2), (1, 3)],
// [(0, 4), (1, 5), (2, 6)],
// [(0, 1), (1, 2), (2, 4)],
// [(1, 3), (2, 5)],
// [(2, 6)],
// ]

let mut matrix = ArrayTrait::new();

let mut row_0 = ArrayTrait::new();
row_0.append((0, 1.into()));
matrix.append(row_0);

let mut row_1 = ArrayTrait::new();
row_1.append((0, 2.into()));
row_1.append((1, 3.into()));
matrix.append(row_1);

let mut row_2 = ArrayTrait::new();
row_2.append((0, 4.into()));
row_2.append((1, 5.into()));
row_2.append((2, 6.into()));
matrix.append(row_2);
let matrix = array![
array![(0, 1.into()), (1, 2.into()), (2, 4.into())],
array![(1, 3.into()), (2, 5.into())],
array![(2, 6.into())],
];

matrix
}

fn get_dummy_circuit_weights() -> (
SparseWeightMatrix, SparseWeightMatrix, SparseWeightMatrix, SparseWeightMatrix, SparseWeightVec,
) {
let mut W_L = ArrayTrait::new();
let mut W_L_0 = ArrayTrait::new();
W_L_0.append((0_usize, -(1.into())));
W_L.append(W_L_0);
W_L.append(ArrayTrait::new());
let mut W_L_2 = ArrayTrait::new();
W_L_2.append((1_usize, -(1.into())));
W_L.append(W_L_2);
W_L.append(ArrayTrait::new());
let mut W_L_4 = ArrayTrait::new();
W_L_4.append((2_usize, -(1.into())));
W_L.append(W_L_4);
W_L.append(ArrayTrait::new());
W_L.append(ArrayTrait::new());
W_L.append(ArrayTrait::new());

let mut W_R = ArrayTrait::new();
W_R.append(ArrayTrait::new());
let mut W_R_1 = ArrayTrait::new();
W_R_1.append((0_usize, -(1.into())));
W_R.append(W_R_1);
W_R.append(ArrayTrait::new());
let mut W_R_3 = ArrayTrait::new();
W_R_3.append((1_usize, -(1.into())));
W_R.append(W_R_3);
W_R.append(ArrayTrait::new());
let mut W_R_5 = ArrayTrait::new();
W_R_5.append((2_usize, -(1.into())));
W_R.append(W_R_5);
W_R.append(ArrayTrait::new());
W_R.append(ArrayTrait::new());

let mut W_O = ArrayTrait::new();
W_O.append(ArrayTrait::new());
W_O.append(ArrayTrait::new());
W_O.append(ArrayTrait::new());
W_O.append(ArrayTrait::new());
let mut W_O_4 = ArrayTrait::new();
W_O_4.append((0_usize, 1.into()));
W_O.append(W_O_4);
let mut W_O_5 = ArrayTrait::new();
W_O_5.append((1_usize, 1.into()));
W_O.append(W_O_5);
W_O.append(ArrayTrait::new());
let mut W_O_7 = ArrayTrait::new();
W_O_7.append((2_usize, 1.into()));
W_O.append(W_O_7);

let mut W_V = ArrayTrait::new();
let mut W_V_0 = ArrayTrait::new();
W_V_0.append((0_usize, -(1.into())));
W_V.append(W_V_0);
let mut W_V_1 = ArrayTrait::new();
W_V_1.append((1_usize, -(1.into())));
W_V.append(W_V_1);
let mut W_V_2 = ArrayTrait::new();
W_V_2.append((2_usize, -(1.into())));
W_V.append(W_V_2);
let mut W_V_3 = ArrayTrait::new();
W_V_3.append((3_usize, -(1.into())));
W_V.append(W_V_3);
W_V.append(ArrayTrait::new());
W_V.append(ArrayTrait::new());
let mut W_V_6 = ArrayTrait::new();
W_V_6.append((0_usize, -(1.into())));
W_V.append(W_V_6);
W_V.append(ArrayTrait::new());
let W_L = array![array![(0, -1.into())], array![(2, -1.into())], array![(4, -1.into())]];

let W_R = array![array![(1, -1.into())], array![(3, -1.into())], array![(5, -1.into())]];

let W_O = array![array![(4, 1.into())], array![(5, 1.into())], array![(7, 1.into())]];

let W_V = array![
array![(0, -1.into()), (6, -1.into())],
array![(1, -1.into())],
array![(2, -1.into())],
array![(3, -1.into())],
];

let mut c = ArrayTrait::new();
c.append((6_usize, 69.into()));
Expand Down
52 changes: 1 addition & 51 deletions src/testing/tests/utils_tests.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use array::ArrayTrait;

use renegade_contracts::{
utils::{math::{get_consecutive_powers, elt_wise_mul, binary_exp}, eq::ArrayTPartialEq},
verifier::{scalar::Scalar, types::{SparseWeightMatrixTrait, SparseWeightVecTrait}},
verifier::{scalar::Scalar, types::SparseWeightVecTrait},
};

use super::super::{
Expand Down Expand Up @@ -58,30 +58,6 @@ fn test_binary_exp_basic() {
// | VERIFIER UTILS TESTS |
// ------------------------

#[test]
#[available_gas(100000000)]
fn test_flatten_sparse_weight_matrix_basic() {
let matrix = get_test_matrix();

let z = 2.into();
let width = 4;

// For z := [2, 2^2, 2^4, ...] we have expected := zW
// ("flattening" W matrix via left-multiplication by z)
let mut expected = ArrayTrait::new();
// 2*1 + 4*2 + 8*4 = 42
expected.append(42.into());
// 4*3 + 8*5 = 52
expected.append(52.into());
// 8*6 = 48
expected.append(48.into());
expected.append(0.into());

let flattened = matrix.flatten(z, width);

assert(flattened == expected, 'wrong flattened matrix');
}

#[test]
#[available_gas(100000000)]
fn test_flatten_column_basic() {
Expand All @@ -98,32 +74,6 @@ fn test_flatten_column_basic() {
assert(flattened == 114.into(), 'wrong flattened column');
}

#[test]
#[available_gas(100000000)]
fn test_get_sparse_weight_column_basic() {
let matrix = get_test_matrix();

let col_0 = matrix.get_sparse_weight_column(0);
let col_1 = matrix.get_sparse_weight_column(1);
let col_2 = matrix.get_sparse_weight_column(2);
let col_3 = matrix.get_sparse_weight_column(3);

let mut expected_col_0 = ArrayTrait::new();
expected_col_0.append((0, 1.into()));
expected_col_0.append((1, 2.into()));
expected_col_0.append((2, 4.into()));
let mut expected_col_1 = ArrayTrait::new();
expected_col_1.append((1, 3.into()));
expected_col_1.append((2, 5.into()));
let mut expected_col_2 = ArrayTrait::new();
expected_col_2.append((2, 6.into()));
let expected_col_3 = ArrayTrait::new();

assert(col_0 == expected_col_0, 'wrong column 0');
assert(col_1 == expected_col_1, 'wrong column 1');
assert(col_2 == expected_col_2, 'wrong column 1');
assert(col_3 == expected_col_3, 'wrong column 1');
}
// -----------------------
// | STORAGE UTILS TESTS |
// -----------------------
Expand Down
Loading

0 comments on commit 5cf3582

Please sign in to comment.