Skip to content

Commit b7a2229

Browse files
authored
add BigInt::double_prevent_overflow_keep_element and use it in tmul init_table (#380)
1 parent 5ee432c commit b7a2229

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

‎bitvm/src/bigint/add.rs‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,25 @@ impl<const N_BITS: u32, const LIMB_SIZE: u32> BigIntImpl<N_BITS, LIMB_SIZE> {
144144
}
145145
}
146146

147+
/// Double the referenced BigInt but keep the original element in its position
148+
/// This function prevents overflow of the underlying integer types during
149+
/// doubling operation.
150+
pub fn double_prevent_overflow_keep_element(n: u32) -> Script {
151+
script! {
152+
{ 1 << LIMB_SIZE }
153+
{ n + 1 } OP_PICK limb_double_without_carry OP_TOALTSTACK
154+
for i in 0..Self::N_LIMBS - 2 {
155+
{ n + i + 3 } OP_PICK limb_double_with_carry OP_TOALTSTACK
156+
}
157+
OP_NIP
158+
{ n + Self::N_LIMBS } OP_PICK OP_SWAP
159+
{ limb_double_with_carry_prevent_overflow(Self::HEAD_OFFSET) }
160+
for _ in 0..Self::N_LIMBS - 1 {
161+
OP_FROMALTSTACK
162+
}
163+
}
164+
}
165+
147166
/// Left shift the BigInt on top of the stack by `bits`
148167
///
149168
/// # Note

‎bitvm/src/bn254/fq.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,9 @@ macro_rules! fp_lc_mul {
187187
for i in 2..=window {
188188
for j in 1 << (i - 1)..1 << i {
189189
if j % 2 == 0 {
190-
{ T::double_allow_overflow_keep_element( (j/2 - 1) * T::N_LIMBS ) }
190+
// { T::copy(j/2 - 1) }
191+
// { T::double_prevent_overflow() }
192+
{ T::double_prevent_overflow_keep_element((j/2 - 1) * T::N_LIMBS) }
191193
} else {
192194
{ T::add_ref_with_top(j - 2) }
193195
}

0 commit comments

Comments
 (0)