Skip to content

Commit

Permalink
🚧 wip, adding u32 stack split
Browse files Browse the repository at this point in the history
  • Loading branch information
ZamDimon committed Oct 9, 2024
1 parent b4e04cd commit c3ef167
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 5 deletions.
27 changes: 23 additions & 4 deletions bitcoin-splitter/src/split/intermediate_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,36 @@ impl IntermediateStateAsBytes {
pub fn altstack_as_u32(&self) -> Vec<u32> {
bytes_to_u32_array(&self.altstack)
}

/// Injects the stack and altstack into the script
pub fn inject_script(&self) -> Script {
script! {
// Inject the stack
for stack_element in self.stack_as_u32() {
{ stack_element }
}

// Inject the altstack
for altstack_element in self.altstack_as_u32() {
{ altstack_element }
}
for i in (0..self.altstack_as_u32().len()).rev() {
{ i } OP_ROLL
OP_TOALTSTACK
}
}
}
}

/// Converts a slice of bytes to a vector of u32 values.
pub(super) fn bytes_to_u32_array(bytes: &[u8]) -> Vec<u32> {
let mut u32_array = Vec::with_capacity((bytes.len() + 3) / 4); // Ceiling division to account for partial chunks.

for (i, chunk) in bytes.chunks(4).enumerate() {
if i % 2 == 1 {
// Skip every second chunk, as it represents the altstack
continue;
}
// if i % 2 == 1 {
// // Skip every second chunk, as it represents the altstack
// continue;
// }

// Handle chunks with fewer than 4 bytes
let padded_chunk = match chunk.len() {
Expand Down
57 changes: 57 additions & 0 deletions bitcoin-splitter/src/test_scripts/int_mul_windowed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,63 @@ mod tests {
}
}

#[test]
fn test_split_to_u32() {
// First, we generate the pair of input and output scripts
let IOPair { input, output: _ } = U254MulScript::generate_valid_io_pair();

// Splitting the script into shards
let split_result = U254MulScript::default_split(input.clone(), SplitType::ByInstructions);

for i in 0..split_result.len() {
// Forming first two inputs. Note that the first input is the input script itself
// while the second input is the output of the previous shard
let mut first_input = input.clone();
if i > 0 {
first_input = split_result.intermediate_states[i - 1].to_bytes().inject_script();
}

let second_input = split_result.intermediate_states[i].to_bytes().inject_script();

println!("Expected: {:?}", split_result.intermediate_states[i].inject_script().len());
println!("Actual: {:?}", second_input.to_asm_string().len());

println!("Expected: {:?}", split_result.intermediate_states[i].inject_script());
println!("Actual: {:?}", second_input.to_asm_string());

// Forming the function
let function = split_result.shards[i].clone();

let verification_script = script! {
{ second_input }
{ first_input }
{ function }

// Verifying that the output in mainstack is correct
for i in (0..split_result.intermediate_states[i].stack.len()).rev() {
{ i+1 } OP_ROLL OP_EQUALVERIFY
}

// Verifying that the output in altstack is correct
// Pushing elements to the mainstack
for _ in 0..2*split_result.intermediate_states[i].altstack.len() {
OP_FROMALTSTACK
}

// Verifying that altstack elements are correct
for i in (0..split_result.intermediate_states[i].altstack.len()).rev() {
{ i+1 } OP_ROLL OP_EQUALVERIFY
}

OP_TRUE
};

let result = execute_script(verification_script);

assert!(result.success, "verification has failed");
}
}

#[test]
#[ignore = "too-large computation, run separately"]
fn test_fuzzy_split() {
Expand Down
2 changes: 1 addition & 1 deletion bitcoin-splitter/src/test_scripts/sha256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ mod tests {
#[test]
fn test_sha256_verify() {
const TEST_BYTES_NUM: usize = 80;
assert!(SHA256Script::<TEST_BYTES_NUM>::verify_random());
assert!(SHA256Script::<TEST_BYTES_NUM>::verify_random(), "Random verification failed");
}

#[test]
Expand Down

0 comments on commit c3ef167

Please sign in to comment.