Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Batch deletions from Utreexo accumulator #253

Merged
merged 5 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ version = "0.1.0"
[[package]]
name = "utreexo"
version = "0.1.0"
dependencies = [
"utils",
]
37 changes: 37 additions & 0 deletions packages/utils/src/bits.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use crate::bit_shifts::shr;

pub fn bit_len(n: u64) -> u64 {
m-kus marked this conversation as resolved.
Show resolved Hide resolved
let mut x = n | shr(n, 1_u64);
x = x | shr(x, 2_u64);
x = x | shr(x, 4_u64);
x = x | shr(x, 8_u64);
x = x | shr(x, 16_u64);
x = x | shr(x, 32_u64);
x -= shr(x, 1_u64) & 0x5555555555555555_u64;
x = (shr(x, 2_u64) & 0x3333333333333333_u64) + (x & 0x3333333333333333_u64);
x = (shr(x, 4_u64) + x) & 0x0f0f0f0f0f0f0f0f_u64;
x += shr(x, 8_u64);
x += shr(x, 16_u64);
x += shr(x, 32_u64);
x & 0x7f_u64
}

#[cfg(test)]
mod tests {
use super::bit_len;

#[test]
fn test_bit_len() {
assert_eq!(bit_len(0), 0);
assert_eq!(bit_len(1), 1);
assert_eq!(bit_len(2), 2);
assert_eq!(bit_len(3), 2);
assert_eq!(bit_len(4), 3);
assert_eq!(bit_len(7), 3);
assert_eq!(bit_len(8), 4);
assert_eq!(bit_len(9), 4);
assert_eq!(bit_len(0x4000000000000000), 63);
assert_eq!(bit_len(0x8000000000000000), 64);
assert_eq!(bit_len(0xffffffffffffffff), 64);
}
}
2 changes: 2 additions & 0 deletions packages/utils/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
pub mod bit_shifts;
pub mod bits;
pub mod bytearray;
pub mod double_sha256;
pub mod hash;
pub mod merkle_tree;
pub mod numeric;
pub mod sha256;
pub mod sort;

#[cfg(target: 'test')]
pub mod hex;
39 changes: 39 additions & 0 deletions packages/utils/src/sort.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/// Bubble sort from
/// https://github.com/keep-starknet-strange/alexandria/blob/main/packages/sorting/src/bubble_sort.cairo
pub fn bubble_sort<T, +Copy<T>, +Drop<T>, +PartialOrd<T>>(mut array: Span<T>) -> Array<T> {
if array.len() == 0 {
return array![];
}
if array.len() == 1 {
return array![*array[0]];
}
let mut idx1 = 0;
let mut idx2 = 1;
let mut sorted_iteration = true;
let mut sorted_array = array![];

loop {
if idx2 == array.len() {
sorted_array.append(*array[idx1]);
if sorted_iteration {
break;
}
array = sorted_array.span();
sorted_array = array![];
idx1 = 0;
idx2 = 1;
sorted_iteration = true;
} else {
if *array[idx1] <= *array[idx2] {
sorted_array.append(*array[idx1]);
idx1 = idx2;
idx2 += 1;
} else {
sorted_array.append(*array[idx2]);
idx2 += 1;
sorted_iteration = false;
}
};
};
sorted_array
}
3 changes: 3 additions & 0 deletions packages/utreexo/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ name = "utreexo"
version = "0.1.0"
edition = "2024_07"

[dependencies]
utils = { path = "../utils" }

[dev-dependencies]
cairo_test.workspace = true

Expand Down
Loading
Loading