Skip to content

Commit

Permalink
Refactor u4 and u32 (BitVM#201)
Browse files Browse the repository at this point in the history
* refactor u32

* refactor u4 and u32

* refactor u4_rot, fix bugs and clean up use's

* fix typo
  • Loading branch information
just-erray authored Jan 21, 2025
1 parent 8a04162 commit ca0166f
Show file tree
Hide file tree
Showing 26 changed files with 850 additions and 3,227 deletions.
58 changes: 0 additions & 58 deletions bitvm/src/bigint/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,31 +63,6 @@ impl<const N_BITS: u32, const LIMB_SIZE: u32> BigIntImpl<N_BITS, LIMB_SIZE> {
}
}

pub fn add1() -> Script {
script! {
1
{ 1 << LIMB_SIZE }

// A0 + 1
limb_add_carry OP_TOALTSTACK

// from A1 + carry_0
// to A{N-2} + carry_{N-3}
for _ in 0..Self::N_LIMBS - 2 {
OP_SWAP
limb_add_carry OP_TOALTSTACK
}

// A{N-1} + carry_{N-2}
OP_NIP
{ limb_add_nocarry(Self::HEAD_OFFSET) }

for _ in 0..Self::N_LIMBS - 1 {
OP_FROMALTSTACK
}
}
}

/// Double the BigInt on top of the stack
///
/// # Note
Expand Down Expand Up @@ -596,37 +571,4 @@ mod test {
run(script);
}
}

#[test]
fn test_1add() {
println!("U254.add1: {} bytes", U254::add1().len());
let mut prng = ChaCha20Rng::seed_from_u64(0);
for _ in 0..100 {
let a: BigUint = prng.sample(RandomBits::new(254));
let c: BigUint = (a.clone().add(BigUint::one())).rem(BigUint::one().shl(254));

let script = script! {
{ U254::push_u32_le(&a.to_u32_digits()) }
{ U254::add1() }
{ U254::push_u32_le(&c.to_u32_digits()) }
{ U254::equalverify(1, 0) }
OP_TRUE
};
run(script);
}

for _ in 0..100 {
let a: u64 = prng.gen();
let c = a.wrapping_add(1u64);

let script = script! {
{ U64::push_u64_le(&[a]) }
{ U64::add1() }
{ U64::push_u64_le(&[c]) }
{ U64::equalverify(1, 0) }
OP_TRUE
};
run(script);
}
}
}
1 change: 0 additions & 1 deletion bitvm/src/bigint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ pub mod mul;
pub mod std;
pub mod sub;
pub mod u29x9;
pub mod u32x8;

pub struct BigIntImpl<const N_BITS: u32, const LIMB_SIZE: u32> {}

Expand Down
270 changes: 0 additions & 270 deletions bitvm/src/bigint/u32x8.rs

This file was deleted.

6 changes: 2 additions & 4 deletions bitvm/src/hash/blake3_u4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ pub struct TablesVars {

impl TablesVars {
pub fn new(stack: &mut StackTracker, use_full_tables: bool) -> Self {
let depth_lookup = if !use_full_tables { u4_push_from_depth_half_lookup(stack, -18) } else { u4_push_from_depth_lookup(stack, -17) };
let xor_table = if !use_full_tables { u4_push_xor_table_stack(stack) } else { u4_push_xor_full_table_stack(stack) };
let depth_lookup = if !use_full_tables { u4_push_from_depth_half_lookup(stack, -18) } else { u4_push_from_depth_full_lookup(stack, -17) };
let xor_table = if !use_full_tables { u4_push_half_xor_table_stack(stack) } else { u4_push_full_xor_table_stack(stack) };
let shift_tables = u4_push_shift_for_blake(stack);
let modulo = u4_push_modulo_for_blake(stack);
let quotient = u4_push_quotient_for_blake(stack);
Expand Down Expand Up @@ -659,9 +659,7 @@ pub fn blake3(stack: &mut StackTracker, mut msg_len: u32, final_rounds: u8) {

#[cfg(test)]
mod tests {

use std::collections::HashMap;

pub use bitcoin_script::script;
//pub use bitcoin::ScriptBuf as Script;
use bitcoin_script_stack::{debugger::debug_script, script_util::verify_n, stack::StackTracker, optimizer::optimize};
Expand Down
Loading

0 comments on commit ca0166f

Please sign in to comment.