Skip to content

Commit

Permalink
Merge pull request #47 from HerodotusDev/feat/queries_to_points
Browse files Browse the repository at this point in the history
feat/queries_to_points
  • Loading branch information
Okm165 authored Jan 10, 2024
2 parents 475ab6f + 5069932 commit 06bd38b
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/common.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod array_extend;
mod array_split;
mod consts;
mod merge_sort;
mod bit_reverse;
mod powers_array;

#[cfg(test)]
Expand Down
20 changes: 20 additions & 0 deletions src/common/bit_reverse.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#[generate_trait]
impl BitReverseU64 of BitReverseTrait {
fn bit_reverse(self: u64) -> u64 {
let mut num = self;
let mut reversed: u64 = 0;
let mut i = 0;
loop {
if i == 64 {
break;
};

let (n, bit) = DivRem::div_rem(num, 2_u64.try_into().unwrap());
num = n;
reversed = reversed * 2 + bit;

i += 1;
};
reversed
}
}
1 change: 1 addition & 0 deletions src/common/tests.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod test_from_span;
mod test_horner_eval;
mod test_array_append;
mod test_math;
mod test_bit_reverse;
mod test_merge_sort;
mod test_powers_array;
// notice: this test takes a lot of time and computation
Expand Down
29 changes: 29 additions & 0 deletions src/common/tests/test_bit_reverse.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use cairo_verifier::common::bit_reverse::BitReverseTrait;

#[test]
#[available_gas(9999999999)]
fn test_bit_reverse_0() {
let num: u64 = 0x0000000000000001;
assert(num.bit_reverse().bit_reverse() == 0x0000000000000001, 'Invalid value')
}

#[test]
#[available_gas(9999999999)]
fn test_bit_reverse_1() {
let num: u64 = 0xf045000ca9ff9261;
assert(num.bit_reverse() == 0x8649ff953000a20f, 'Invalid value')
}

#[test]
#[available_gas(9999999999)]
fn test_bit_reverse_2() {
let num: u64 = 0xabf8194012840122;
assert(num.bit_reverse() == 0x4480214802981fd5, 'Invalid value')
}

#[test]
#[available_gas(9999999999)]
fn test_bit_reverse_3() {
let num: u64 = 0xacc8194ff2340f22;
assert(num.bit_reverse().bit_reverse() == 0xacc8194ff2340f22, 'Invalid value')
}
34 changes: 33 additions & 1 deletion src/queries/queries.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use cairo_verifier::{channel::channel::{Channel, ChannelTrait}, common::merge_sort::merge_sort};
use cairo_verifier::{
channel::channel::{Channel, ChannelTrait},
common::{
merge_sort::merge_sort, math::pow, consts::FIELD_GENERATOR, bit_reverse::BitReverseTrait
}
};

// 2^64 = 18446744073709551616
const U128maxU64: u128 = 18446744073709551616;
Expand Down Expand Up @@ -80,3 +85,30 @@ fn usort(input: Array<u64>) -> Array<felt252> {

result
}

fn queries_to_points(
queries: Span<felt252>, log_eval_domain_size: u8, eval_generator: felt252
) -> Array<felt252> {
let mut points = ArrayTrait::<felt252>::new();

// Evaluation domains of size greater than 2**64 are not supported
assert(log_eval_domain_size <= 64, 'Eval domain too big');

// A 'log_eval_domain_size' bits index can be bit reversed using bit_reverse_u64 if it is
// multiplied by 2**(64 - log_eval_domain_size) first.
let shift = pow(2, 64 - log_eval_domain_size.into());

let mut i: u32 = 0;
loop {
if i == queries.len() {
break;
}

let index: u64 = (*queries.at(i) * shift).try_into().unwrap();

// Compute the x value of the query in the evaluation domain coset:
// FIELD_GENERATOR * eval_generator ^ reversed_index.
points.append(FIELD_GENERATOR * pow(eval_generator, index.bit_reverse().into()));
};
points
}

0 comments on commit 06bd38b

Please sign in to comment.