Skip to content

Commit

Permalink
Merge pull request #74 from HerodotusDev/test/generate_queries
Browse files Browse the repository at this point in the history
test/generate_queries
  • Loading branch information
Okm165 authored Jan 16, 2024
2 parents 3d1663f + a0ecdfb commit 3637634
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 54 deletions.
26 changes: 15 additions & 11 deletions src/common/merge_sort.cairo
Original file line number Diff line number Diff line change
@@ -1,47 +1,51 @@
use cairo_verifier::common::array_extend::ArrayExtendTrait;
use cairo_verifier::common::array_split::ArraySplitTrait;
use core::debug::PrintTrait;
use cairo_verifier::common::array_print::{ArrayPrintTrait, SpanPrintTrait};

// Merge Sort
/// # Arguments
/// * `arr` - Array to sort
/// * `arr` - Span to sort
/// # Returns
/// * `Array<T>` - Sorted array
fn merge_sort<T, +Copy<T>, +Drop<T>, +PartialOrd<T>>(mut arr: Array<T>) -> Array<T> {
fn merge_sort<T, +Copy<T>, +Drop<T>, +PartialOrd<T>>(mut arr: Span<T>) -> Array<T> {
let mut ret: Array<T> = ArrayTrait::new();
ret.extend(arr);

let mut chunk = 1;
loop {
if chunk >= arr.len() {
break;
}
let mut start = 0;
let mut new_arr: Array<T> = ArrayTrait::new();
let arr_span = arr.span();
loop {
let start2 = start + chunk;
let size2 = if start + 2 * chunk >= arr_span.len() {
arr_span.len() - start - chunk
let size2 = if start + 2 * chunk >= arr.len() {
arr.len() - start - chunk
} else {
chunk
};

merge_arrays(arr_span.slice(start, chunk), arr_span.slice(start2, size2), ref new_arr);
merge_arrays(arr.slice(start, chunk), arr.slice(start2, size2), ref new_arr);

start += 2 * chunk;
if start + chunk >= arr_span.len() {
if start + chunk >= arr.len() {
break;
};
};
loop {
if start >= arr_span.len() {
if start >= arr.len() {
break;
};
new_arr.append(*arr_span.at(start));
new_arr.append(*arr.at(start));
start += 1;
};
arr = new_arr;
arr = new_arr.span();
ret = new_arr;
chunk *= 2;
};
arr
ret
}

fn merge_arrays<T, +Copy<T>, +Drop<T>, +PartialOrd<T>>(
Expand Down
28 changes: 14 additions & 14 deletions src/common/tests/test_merge_sort.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,100 +4,100 @@ use cairo_verifier::common::merge_sort::merge_sort;
#[test]
#[available_gas(9999999999)]
fn test_merge_sort_0() {
assert(merge_sort(ArrayTrait::<u32>::new()) == ArrayTrait::<u32>::new(), 'merge_sort_0');
assert(merge_sort(ArrayTrait::<u32>::new().span()) == ArrayTrait::<u32>::new(), 'merge_sort_0');
}

#[test]
#[available_gas(9999999999)]
fn test_merge_sort_1() {
let input: Array<u32> = array![1237];
assert(merge_sort(input) == array![1237], 'merge_sort_1');
assert(merge_sort(input.span()) == array![1237], 'merge_sort_1');
}

#[test]
#[available_gas(9999999999)]
fn test_merge_sort_2_sorted() {
let input: Array<u32> = array![1237, 82139];
assert(merge_sort(input) == array![1237, 82139], 'merge_sort_2_sorted');
assert(merge_sort(input.span()) == array![1237, 82139], 'merge_sort_2_sorted');
}

#[test]
#[available_gas(9999999999)]
fn test_merge_sort_2_unsorted() {
let input: Array<u32> = array![82139, 1237];
assert(merge_sort(input) == array![1237, 82139], 'merge_sort_2_unsorted');
assert(merge_sort(input.span()) == array![1237, 82139], 'merge_sort_2_unsorted');
}

#[test]
#[available_gas(9999999999)]
fn test_merge_sort_3_reversed() {
let input: Array<u32> = array![3, 2, 1];
let output: Array<u32> = array![1, 2, 3];
assert(merge_sort(input) == output, 'merge_sort_3_reversed');
assert(merge_sort(input.span()) == output, 'merge_sort_3_reversed');
}

#[test]
#[available_gas(9999999999)]
fn test_merge_sort_4_reversed() {
let input: Array<u32> = array![4, 3, 2, 1];
let output: Array<u32> = array![1, 2, 3, 4];
assert(merge_sort(input) == output, 'merge_sort_4_reversed');
assert(merge_sort(input.span()) == output, 'merge_sort_4_reversed');
}

#[test]
#[available_gas(9999999999)]
fn test_merge_sort_5_reversed() {
let input: Array<u32> = array![5, 4, 3, 2, 1];
let output: Array<u32> = array![1, 2, 3, 4, 5];
assert(merge_sort(input) == output, 'merge_sort_5_reversed');
assert(merge_sort(input.span()) == output, 'merge_sort_5_reversed');
}

#[test]
#[available_gas(9999999999)]
fn test_merge_sort_6_reversed() {
let input: Array<u32> = array![6, 5, 4, 3, 2, 1];
let output: Array<u32> = array![1, 2, 3, 4, 5, 6];
assert(merge_sort(input) == output, 'merge_sort_6_reversed');
assert(merge_sort(input.span()) == output, 'merge_sort_6_reversed');
}

#[test]
#[available_gas(9999999999)]
fn test_merge_sort_7_reversed() {
let input: Array<u32> = array![7, 6, 5, 4, 3, 2, 1];
let output: Array<u32> = array![1, 2, 3, 4, 5, 6, 7];
assert(merge_sort(input) == output, 'merge_sort_7_reversed');
assert(merge_sort(input.span()) == output, 'merge_sort_7_reversed');
}

#[test]
#[available_gas(9999999999)]
fn test_merge_sort_8_reversed() {
let input: Array<u32> = array![8, 7, 6, 5, 4, 3, 2, 1];
let output: Array<u32> = array![1, 2, 3, 4, 5, 6, 7, 8];
assert(merge_sort(input) == output, 'merge_sort_8_reversed');
assert(merge_sort(input.span()) == output, 'merge_sort_8_reversed');
}

#[test]
#[available_gas(9999999999)]
fn test_merge_sort_9_reversed() {
let input: Array<u32> = array![9, 8, 7, 6, 5, 4, 3, 2, 1];
let output: Array<u32> = array![1, 2, 3, 4, 5, 6, 7, 8, 9];
assert(merge_sort(input) == output, 'merge_sort_9_reversed');
assert(merge_sort(input.span()) == output, 'merge_sort_9_reversed');
}

#[test]
#[available_gas(9999999999)]
fn test_merge_sort_10_reversed() {
let input: Array<u32> = array![10, 9, 8, 7, 6, 5, 4, 3, 2, 1];
let output: Array<u32> = array![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
assert(merge_sort(input) == output, 'merge_sort_10_reversed');
assert(merge_sort(input.span()) == output, 'merge_sort_10_reversed');
}

#[test]
#[available_gas(9999999999)]
fn test_merge_sort_10_random() {
let input: Array<u32> = array![4, 2, 1, 5, 9, 10, 3, 6, 7, 8];
let output: Array<u32> = array![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
assert(merge_sort(input) == output, 'merge_sort_10_random');
assert(merge_sort(input.span()) == output, 'merge_sort_10_random');
}

#[test]
Expand Down Expand Up @@ -165,5 +165,5 @@ fn test_merge_sort_27_random() {
4228656862,
4267084068
];
assert(merge_sort(input) == output, 'merge_sort_27_random');
assert(merge_sort(input.span()) == output, 'merge_sort_27_random');
}
8 changes: 5 additions & 3 deletions src/queries/queries.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use cairo_verifier::{
common::{
merge_sort::merge_sort, math::pow, consts::FIELD_GENERATOR, bit_reverse::BitReverseTrait
},
domains::StarkDomains
domains::StarkDomains, common::array_print::SpanPrintTrait
};

// 2^64 = 18446744073709551616
Expand All @@ -13,7 +13,7 @@ fn generate_queries(
ref channel: Channel, n_samples: u32, query_upper_bound: u64
) -> Array<felt252> {
let samples = sample_random_queries(ref channel, n_samples, query_upper_bound);
usort(samples)
usort(samples.span().slice(0, n_samples))
}

fn sample_random_queries(
Expand Down Expand Up @@ -57,7 +57,7 @@ fn sample_random_queries(

// Sorts an array of field elements and removes duplicates.
// Returns the sorted array.
fn usort(input: Array<u64>) -> Array<felt252> {
fn usort(input: Span<u64>) -> Array<felt252> {
let mut result = ArrayTrait::<felt252>::new();

if input.len() == 0 {
Expand Down Expand Up @@ -111,6 +111,8 @@ fn queries_to_points(queries: Span<felt252>, stark_domains: @StarkDomains) -> Ar
.append(
FIELD_GENERATOR * pow(*stark_domains.eval_generator, index.bit_reverse().into())
);

i += 1;
};
points
}
1 change: 1 addition & 0 deletions src/queries/tests.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod test_usort;
mod test_generate_queries;
mod test_sample_random_queries;
mod test_queries_to_points;
80 changes: 60 additions & 20 deletions src/queries/tests/test_generate_queries.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ fn test_generate_queries_0() {
let mut channel = ChannelTrait::new(
u256 { low: 0xf7685ebd40e852b164633a4acbd3244c, high: 0xe8e77626586f73b955364c7b4bbf0bb7 }
);
let queries = generate_queries(ref channel, 1, 12389012333);
assert(queries.len() == 4, 'Invalid value');
assert(*queries.at(0) == 0xc53fdd1e, 'Invalid value');
assert(*queries.at(1) == 0x166d56d3d, 'Invalid value');
assert(*queries.at(2) == 0x1e563d10b, 'Invalid value');
assert(*queries.at(3) == 0x2d9a2434f, 'Invalid value');
assert(
generate_queries(
ref channel, 4, 12389012333
) == array![0xc53fdd1e, 0x166d56d3d, 0x1e563d10b, 0x2d9a2434f],
'Invalid value'
);
assert(channel.counter == 1, 'Invalid value');
}

#[test]
Expand All @@ -20,18 +21,57 @@ fn test_generate_queries_1() {
let mut channel = ChannelTrait::new(
u256 { low: 0xf7685ebd40e852b164633a4acbd3244c, high: 0xe8e77626586f73b955364c7b4bbf0bb7 }
);
let queries = generate_queries(ref channel, 10, 99809818624);
assert(queries.len() == 12, 'Invalid value');
assert(*queries.at(0) == 0x3247d4098, 'Invalid value');
assert(*queries.at(1) == 0x52d896136, 'Invalid value');
assert(*queries.at(2) == 0x557cce2e5, 'Invalid value');
assert(*queries.at(3) == 0x6188b67d1, 'Invalid value');
assert(*queries.at(4) == 0x8cf7e0675, 'Invalid value');
assert(*queries.at(5) == 0x982d6fc79, 'Invalid value');
assert(*queries.at(6) == 0xa733f8ed8, 'Invalid value');
assert(*queries.at(7) == 0xbf23e4bf7, 'Invalid value');
assert(*queries.at(8) == 0xc2321969b, 'Invalid value');
assert(*queries.at(9) == 0xca83fb21d, 'Invalid value');
assert(*queries.at(10) == 0xe4da8dce0, 'Invalid value');
assert(*queries.at(11) == 0x1405a07e8c, 'Invalid value');
assert(
generate_queries(
ref channel, 10, 99809818624
) == array![
0x3247d4098,
0x52d896136,
0x557cce2e5,
0x6188b67d1,
0x982d6fc79,
0xa733f8ed8,
0xbf23e4bf7,
0xc2321969b,
0xca83fb21d,
0x1405a07e8c
],
'Invalid value'
);
assert(channel.counter == 3, 'Invalid value');
}

// test generated based on cairo0-verifier run on fib proof from stone-prover
#[test]
#[available_gas(9999999999)]
fn test_generate_queries_2() {
let mut channel = ChannelTrait::new(
u256 { low: 0x2c31f04a6b9c83c2464b2f1688fc719e, high: 0xe631d91ef56f7e4cc7fe09cff2cc4e94 }
);
assert(
generate_queries(
ref channel, 18, 0x400000
) == array![
0x4c3e3,
0x53e5f,
0x5e7ae,
0x6f76e,
0xde621,
0xe0f5a,
0xf5b8c,
0x13d133,
0x180758,
0x1eeb19,
0x20d785,
0x21f804,
0x245054,
0x3883ce,
0x3970d6,
0x3a8f8e,
0x3b9258,
0x3c7016,
],
'Invalid value'
);
assert(channel.counter == 5, 'Invalid value');
}
63 changes: 63 additions & 0 deletions src/queries/tests/test_queries_to_points.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use cairo_verifier::queries::queries::queries_to_points;
use cairo_verifier::domains::StarkDomains;

// test generated based on cairo0-verifier run on fib proof from stone-prover
#[test]
#[available_gas(9999999999)]
fn test_queries_to_points_0() {
let stark_domains = StarkDomains {
log_eval_domain_size: 0x16,
eval_domain_size: 0,
eval_generator: 0x3e4383531eeac7c9822fb108d24a344d841544dd6482f17ead331453e3a2f4b,
log_trace_domain_size: 0,
trace_domain_size: 0,
trace_generator: 0,
};

let queries = array![
0x4c3e3,
0x53e5f,
0x5e7ae,
0x6f76e,
0xde621,
0xe0f5a,
0xf5b8c,
0x13d133,
0x180758,
0x1eeb19,
0x20d785,
0x21f804,
0x245054,
0x3883ce,
0x3970d6,
0x3a8f8e,
0x3b9258,
0x3c7016,
];

assert(
queries_to_points(
queries.span(), @stark_domains
) == array![
0x2db7913d585ab151bc2b66c288d0bc9b8c791083d1e4a347f418d499696385a,
0x13de0c034702b2f857d20f1c4392d7308adf523675a6917ef9b24ffe481b3f3,
0x79a18f6903e3f59a2fa62f2009dd31281ee4edf937ca2d9db9d53aa4136f44,
0x7101334fb9526f5fd852697914640ed5a1fe8df743eb057b22621942911d330,
0x7c6b8bb6c7d3e482aadaaa96b6899d8302963e19039896b77d14093d404ed4d,
0x3b79da623a55f681ff504b9e358c66e4195d0ad90c1bd052f7b84f00023f628,
0x2dd308f8d684d300811809255d1210ba974487fc02b9f5ae3a63c073bdfe9a8,
0x23823ef38993551b68657d4607d1617ba0f4e323052cc14b3a5b18dc0bf5875,
0x164e55f8fda082b5db06c0caedc2dd5a4c39ae8939cc61f7ee2f8255b5849c5,
0x6638bb689428e37f36e1580ef4c46395758e96562940002b6f37ef33d10ac04,
0x3abdf6cb714ba6c308c6eef1cc4d63e06928c17129867606802cfc44c961e80,
0x4e2ad9b9530b45c3dacf36b44a8836844d301661dd9d54685606a917729716d,
0x2b81aced85615991fd89e616e0ea8fb2f6841436a2565aeb19cbd785ecfca2a,
0xe6b5ebff913e45d5a57ec627c03acd209d564935994b1058a5d828bfaeee3a,
0x20d399aac1db0f8e99a1dd7e009f4d165ca411086bef0002edebf3395ef31be,
0x5c0086656cb6c7208f87148b5d36a14b626ace2c9d4c67f5ae27106c3bde97,
0x4ee93f199c1a2b9ed46dd9d0c1f51fd453a37698dd8074fe1eb197d4b42541b,
0x27b6342139b5ab8f1ff29fea7f5602d3a438163f5547d2d341a5418ea90abe4,
],
'Invalid value'
);
}
Loading

0 comments on commit 3637634

Please sign in to comment.