Skip to content

Commit

Permalink
use log2 from math.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
ftheirs committed Dec 31, 2024
1 parent 1126016 commit e84933c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
15 changes: 6 additions & 9 deletions crates/starknet-os/src/hints/compression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ use cairo_vm::vm::errors::hint_errors::HintError;
use cairo_vm::vm::vm_core::VirtualMachine;
use cairo_vm::Felt252;
use indoc::indoc;
use num_bigint::BigUint;

use crate::hints::math::log2_ceil;
use crate::hints::vars;
use crate::utils::get_constant;

const COMPRESSION_VERSION: u8 = 0;
const MAX_N_BITS: usize = 251;
const N_UNIQUE_VALUE_BUCKETS: usize = 6;
const TOTAL_N_BUCKETS: usize = N_UNIQUE_VALUE_BUCKETS + 1;

use super::constants::TOTAL_N_BUCKETS;

#[derive(Debug, Clone)]
struct UniqueValueBucket {
Expand Down Expand Up @@ -167,11 +169,6 @@ fn get_bucket_offsets(bucket_lengths: Vec<usize>) -> Vec<usize> {
offsets
}

fn log2_ceil(x: usize) -> usize {
assert!(x > 0);
(x - 1).count_ones() as usize
}

fn get_n_elms_per_felt(elm_bound: usize) -> usize {
if elm_bound <= 1 {
return MAX_N_BITS;
Expand All @@ -183,7 +180,7 @@ fn get_n_elms_per_felt(elm_bound: usize) -> usize {
return 1;
}

let log2_result = log2_ceil(elm_bound);
let log2_result = log2_ceil(&BigUint::from(elm_bound)) as usize;
assert!(log2_result > 0, "log2_ceil(elm_bound) returned 0, which would cause division by zero.");

MAX_N_BITS / log2_result
Expand Down Expand Up @@ -225,7 +222,7 @@ fn compression(
let packed_repeating_value_pointers =
pack_in_felts(compression_set.get_repeating_value_pointers(), n_unique_values);

let packed_bucket_index_per_elm = pack_in_felts(bucket_index_per_elm, TOTAL_N_BUCKETS);
let packed_bucket_index_per_elm = pack_in_felts(bucket_index_per_elm, TOTAL_N_BUCKETS as usize);

let compressed_data = packed_header
.into_iter()
Expand Down
4 changes: 2 additions & 2 deletions crates/starknet-os/src/hints/constants.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Compression constants
pub const N_UNIQUE_VALUE_BUCKETS: u64 = 6;
pub const TOTAL_N_BUCKETS: u64 = N_UNIQUE_VALUE_BUCKETS + 1;
pub const N_UNIQUE_VALUE_BUCKETS: usize = 6;
pub const TOTAL_N_BUCKETS: usize = N_UNIQUE_VALUE_BUCKETS + 1;

0 comments on commit e84933c

Please sign in to comment.