Skip to content
Merged
6 changes: 3 additions & 3 deletions bitvm/src/bigint/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ impl<const N_BITS: u32, const LIMB_SIZE: u32> BigIntImpl<N_BITS, LIMB_SIZE> {
{ 1 << LIMB_SIZE }

// Double the limb, take the result to the alt stack, and add initial carry
{ n + 1 } OP_PICK limb_double_without_carry OP_TOALTSTACK
{ n * Self::N_LIMBS + 1 } OP_PICK limb_double_without_carry OP_TOALTSTACK

for i in 0..Self::N_LIMBS - 2 {
{ n + i + 3 } OP_PICK limb_double_with_carry OP_TOALTSTACK
{ n * Self::N_LIMBS + i + 3 } OP_PICK limb_double_with_carry OP_TOALTSTACK
}

// When we got {limb} {base} {carry} on the stack, we drop the base
OP_NIP // {limb} {carry}
{ n + 9 } OP_PICK { limb_double_with_carry_allow_overflow(Self::HEAD_OFFSET) }
{ n * Self::N_LIMBS + Self::N_LIMBS } OP_PICK { limb_double_with_carry_allow_overflow(Self::HEAD_OFFSET) }

// Take all limbs from the alt stack to the main stack
for _ in 0..Self::N_LIMBS - 1 {
Expand Down
131 changes: 4 additions & 127 deletions bitvm/src/bigint/bits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,44 +45,6 @@ impl<const N_BITS: u32, const LIMB_SIZE: u32> BigIntImpl<N_BITS, LIMB_SIZE> {
{ limb_to_le_bits_toaltstack(N_BITS - LIMB_SIZE * (Self::N_LIMBS - 1)) }
}
}

pub fn limb_from_bytes() -> Script {
let bytes_per_limb = LIMB_SIZE.div_ceil(8);

assert!(LIMB_SIZE > 0, "LIMB_SIZE must not be 0");
assert!(LIMB_SIZE < 33, "LIMB_SIZE must be less than 33");

script! {
// This will be our sum on the stack
OP_0
for i in 0..bytes_per_limb {
// Check that the number is a u8
OP_SWAP
OP_DUP
{ 256 }
OP_LESSTHAN
OP_VERIFY
// lshift
for _ in 0..8*i {
OP_DUP
OP_ADD
}
OP_ADD
}
}
}

pub fn from_bytes() -> Script {
script! {
for _ in 0..Self::N_LIMBS {
{ Self::limb_from_bytes() }
OP_TOALTSTACK
}
for _ in 0..Self::N_LIMBS {
OP_FROMALTSTACK
}
}
}
}

fn limb_to_be_bits_common(num_bits: u32) -> Script {
Expand Down Expand Up @@ -188,6 +150,10 @@ pub fn limb_to_le_bits_toaltstack(num_bits: u32) -> Script {
OP_TOALTSTACK
}
}
} else if num_bits == 1 {
script! {
OP_TOALTSTACK
}
} else {
script! {}
}
Expand Down Expand Up @@ -598,93 +564,4 @@ mod test {
run(script);
}
}

#[test]
fn test_u29_limb_from_bytes() {
assert_eq!(U254::N_LIMBS, 9);
let script = script! {
{ 0x01 }
{ 0x12 }
{ 0x13 }
{ 0x14 }
{ U254::limb_from_bytes() }
{ 0x01121314 }
OP_EQUAL
};
run(script);

let script = script! {
{ 0x00 }
{ 0x01 }
{ 0x13 }
{ 0x14 }
{ U254::limb_from_bytes() }
{ 0x00011314 }
OP_EQUAL
};
run(script);
}
#[test]
fn test_u254_from_bytes() {
assert_eq!(U254::N_LIMBS, 9);
let script = script! {
{ 0x00 }
{ 0x01 }
{ 0x13 }
{ 0x14 }

{ 0x01 }
{ 0x22 }
{ 0x23 }
{ 0x24 }

{ 0x01 }
{ 0x32 }
{ 0x33 }
{ 0x34 }

{ 0x01 }
{ 0x42 }
{ 0x43 }
{ 0x44 }

{ 0x01 }
{ 0x52 }
{ 0x53 }
{ 0x54 }

{ 0x01 }
{ 0x62 }
{ 0x63 }
{ 0x64 }

{ 0x01 }
{ 0x72 }
{ 0x73 }
{ 0x74 }

{ 0x01 }
{ 0x82 }
{ 0x83 }
{ 0x84 }

{ 0x01 }
{ 0x92 }
{ 0x93 }
{ 0x94 }

{ U254::from_bytes() }
{ 0x00011314 }
{ 0x01222324 }
{ 0x01323334 }
{ 0x01424344 }
{ 0x01525354 }
{ 0x01626364 }
{ 0x01727374 }
{ 0x01828384 }
{ 0x01929394 }
{ U254::equal(0, 1) }
};
run(script);
}
}
60 changes: 33 additions & 27 deletions bitvm/src/bigint/inv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl<const N_BITS: u32, const LIMB_SIZE: u32> BigIntImpl<N_BITS, LIMB_SIZE> {
pub fn limb_shr1_carry(num_bits: u32) -> Script {
let powers_of_2_script = if num_bits < 7 {
script! {
for i in 0..num_bits - 1 {
for i in 1..num_bits {
Comment thread
Hakkush-07 marked this conversation as resolved.
Resolved
{ 2_u32.pow(i) }
}
}
Expand Down Expand Up @@ -158,37 +158,43 @@ mod test {

#[test]
fn test_limb_shr1_carry() {
println!("limb_shr1_carry: {} bytes", limb_shr1_carry(29).len());
let mut prng = ChaCha20Rng::seed_from_u64(0);
for shift in 2..30 {
println!(
"limb_shr1_carry({:?}): {} bytes",
shift,
limb_shr1_carry(shift).len()
);
let mut prng = ChaCha20Rng::seed_from_u64(0);

for _ in 0..100 {
let mut a: u32 = prng.gen();
a %= 1 << shift;

for _ in 0..100 {
let mut a: u32 = prng.gen();
a %= 1 << 29;

let script = script! {
{ a }
{ 0 }
{ limb_shr1_carry(29) }
{ a & 1 } OP_EQUALVERIFY
{ a >> 1 } OP_EQUAL
};
let script = script! {
{ a }
{ 0 }
{ limb_shr1_carry(shift) }
{ a & 1 } OP_EQUALVERIFY
{ a >> 1 } OP_EQUAL
};

run(script);
}
run(script);
}

for _ in 0..100 {
let mut a: u32 = prng.gen();
a %= 1 << 29;
for _ in 0..100 {
let mut a: u32 = prng.gen();
a %= 1 << shift;

let script = script! {
{ a }
{ 1 }
{ limb_shr1_carry(29) }
{ a & 1 } OP_EQUALVERIFY
{ (1 << 28) | (a >> 1) } OP_EQUAL
};
let script = script! {
{ a }
{ 1 }
{ limb_shr1_carry(shift) }
{ a & 1 } OP_EQUALVERIFY
{ (1 << (shift - 1)) | (a >> 1) } OP_EQUAL
};

run(script);
run(script);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion bitvm/src/bigint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ pub mod add;
pub mod bits;
pub mod cmp;
pub mod inv;
pub mod mul;
pub mod std;
pub mod sub;

Expand All @@ -15,6 +14,7 @@ impl<const N_BITS: u32, const LIMB_SIZE: u32> BigIntImpl<N_BITS, LIMB_SIZE> {
pub const N_LIMBS: u32 = N_BITS.div_ceil(LIMB_SIZE);
pub const HEAD: u32 = N_BITS - (Self::N_LIMBS - 1) * LIMB_SIZE;
pub const HEAD_OFFSET: u32 = 1u32 << Self::HEAD;
const _ASSERTION: () = assert!(Self::N_LIMBS > 1);
}

pub type U254 = BigIntImpl<254, 29>;
Expand Down
87 changes: 0 additions & 87 deletions bitvm/src/bigint/mul.rs

This file was deleted.

17 changes: 16 additions & 1 deletion bitvm/src/bigint/std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ impl<const N_BITS: u32, const LIMB_SIZE: u32> BigIntImpl<N_BITS, LIMB_SIZE> {
bits.push((elem & (1 << i)) != 0);
}
}
// make sure most significant 1 lies inside the limits
let ms_one = if bits.len() > 0 {
let mut ms_one = bits.len() - 1;
while !bits[ms_one] {
if ms_one != 0 {
ms_one -= 1;
} else {
break;
}
}
ms_one
} else {
0
};
assert!(ms_one < Self::N_BITS as usize);
bits.resize(N_BITS as usize, false);

let mut limbs = vec![];
Expand Down Expand Up @@ -159,7 +174,7 @@ impl<const N_BITS: u32, const LIMB_SIZE: u32> BigIntImpl<N_BITS, LIMB_SIZE> {
a = (a + 1) * Self::N_LIMBS - 1;

script! {
if a < 134 {
if a < 128 {
for _ in 0..Self::N_LIMBS {
{ a } OP_PICK
}
Expand Down
2 changes: 1 addition & 1 deletion bitvm/src/bn254/fq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ macro_rules! fp_lc_mul {
for i in 2..=window {
for j in 1 << (i - 1)..1 << i {
if j % 2 == 0 {
{ T::double_allow_overflow_keep_element( (j/2 - 1) * T::N_LIMBS ) }
{ T::double_allow_overflow_keep_element(j/2 - 1) }
} else {
{ T::add_ref_with_top(j - 2) }
}
Expand Down
Loading