Skip to content

Commit eaab427

Browse files
Remove stack_hints and analyzer overhead (BitVM#204)
Co-authored-by: Lukas <[email protected]>
1 parent b291f58 commit eaab427

File tree

8 files changed

+70
-87
lines changed

8 files changed

+70
-87
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,4 @@ ark-r1cs-std = { git = "https://github.com/arkworks-rs/r1cs-std/" }
6868
ark-crypto-primitives = { git = "https://github.com/arkworks-rs/crypto-primitives/" }
6969
ark-relations = { git = "https://github.com/arkworks-rs/snark/" }
7070
ark-snark = { git = "https://github.com/arkworks-rs/snark/" }
71-
ark-groth16 = { git = "https://github.com/arkworks-rs/groth16" }
71+
ark-groth16 = { git = "https://github.com/arkworks-rs/groth16" }

bitvm/src/bigint/inv.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -252,13 +252,11 @@ impl<const N_BITS: u32, const LIMB_SIZE: u32> BigIntImpl<N_BITS, LIMB_SIZE> {
252252
for i in 0..=Self::N_BITS {
253253
{ Self::N_BITS - i } OP_EQUAL OP_TOALTSTACK
254254
}
255-
{ script! {
256-
for i in 0..=Self::N_BITS {
257-
OP_FROMALTSTACK OP_IF
258-
{ Self::push_u32_le(&inv_list[i as usize].to_u32_digits()) }
259-
OP_ENDIF
260-
}
261-
}.add_stack_hint(0, 9).add_altstack_hint(-(Self::N_BITS as i32) - 1, -(Self::N_BITS as i32) - 1)}
255+
for i in 0..=Self::N_BITS {
256+
OP_FROMALTSTACK OP_IF
257+
{ Self::push_u32_le(&inv_list[i as usize].to_u32_digits()) }
258+
OP_ENDIF
259+
}
262260
}
263261
}
264262
}
@@ -368,7 +366,7 @@ mod test {
368366
use crate::bigint::inv::{limb_div3_carry, limb_shr1_carry};
369367
use crate::bigint::{U254, U64};
370368
use crate::treepp::*;
371-
369+
372370
use core::ops::{Div, Shr};
373371
use num_bigint::{BigUint, RandomBits};
374372
use rand::{Rng, SeedableRng};
@@ -499,8 +497,6 @@ mod test {
499497
{ U64::equalverify(1, 0) }
500498
OP_TRUE
501499
};
502-
let stack = script.clone().analyze_stack();
503-
assert!(stack.is_valid_final_state_without_inputs());
504500
run(script);
505501
}
506502
}

bitvm/src/bigint/std.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,6 @@ impl<const N_BITS: u32, const LIMB_SIZE: u32> BigIntImpl<N_BITS, LIMB_SIZE> {
171171
OP_1SUB OP_PICK
172172
}
173173
}
174-
.add_stack_hint(-(Self::N_LIMBS as i32), Self::N_LIMBS as i32)
175174
}
176175

177176
pub fn roll(mut a: u32) -> Script {

bitvm/src/bn254/fp254impl.rs

Lines changed: 58 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -201,31 +201,29 @@ pub trait Fp254Impl {
201201
OP_NIP
202202
OP_DUP
203203

204-
{ script! {
205-
// ⋯ C₈⁻ C₈⁻
206-
OP_IF
207-
OP_FROMALTSTACK
208-
OP_DROP
209-
OP_ENDIF
204+
// ⋯ C₈⁻ C₈⁻
205+
OP_IF
206+
OP_FROMALTSTACK
207+
OP_DROP
208+
OP_ENDIF
210209

210+
OP_FROMALTSTACK
211+
// ⋯ (B₈+C₇⁺)+A₈ C₈⁻ | ((B₇+C₆⁺)+A₇)-(C₆⁻+M₇)
212+
// ⋯ ((B₈+C₇⁺)+A₈)-(C₇⁻+M₈) C₈⁻ | (B₈+C₇⁺)+A₈
213+
for _ in 0..Self::N_LIMBS-1 {
214+
OP_FROMALTSTACK OP_DROP
211215
OP_FROMALTSTACK
212-
// ⋯ (B₈+C₇⁺)+A₈ C₈⁻ | ((B₇+C₆⁺)+A₇)-(C₆⁻+M₇)
213-
// ⋯ ((B₈+C₇⁺)+A₈)-(C₇⁻+M₈) C₈⁻ | (B₈+C₇⁺)+A₈
214-
for _ in 0..Self::N_LIMBS-1 {
215-
OP_FROMALTSTACK OP_DROP
216-
OP_FROMALTSTACK
217-
}
218-
// ⋯ (B₈+C₇⁺)+A₈ (B₇+C₆⁺)+A₇ ... (B₂+C₁⁺)+A₂ (B₁+C₀⁺)+A₁ A₀+B₀ C₈⁻
219-
// ⋯ ((B₈+C₇⁺)+A₈)-(C₇⁻+M₈) ... (A₀+B₀)-M₀ C₈⁻ | A₀+B₀
220-
{ Self::N_LIMBS }
221-
OP_ROLL
222-
OP_NOTIF
223-
OP_FROMALTSTACK
224-
OP_DROP
225-
OP_ENDIF
226-
// ⋯ (B₈+C₇⁺)+A₈ (B₇+C₆⁺)+A₇ ... (B₁+C₀⁺)+A₁ A₀+B₀
227-
// ⋯ ((B₈+C₇⁺)+A₈)-(C₇⁻+M₈) ... (A₀+B₀)-M₀
228-
}.add_stack_hint(-2, Self::N_LIMBS as i32 - 2).add_altstack_hint(-2 * Self::N_LIMBS as i32, -2 * Self::N_LIMBS as i32)}
216+
}
217+
// ⋯ (B₈+C₇⁺)+A₈ (B₇+C₆⁺)+A₇ ... (B₂+C₁⁺)+A₂ (B₁+C₀⁺)+A₁ A₀+B₀ C₈⁻
218+
// ⋯ ((B₈+C₇⁺)+A₈)-(C₇⁻+M₈) ... (A₀+B₀)-M₀ C₈⁻ | A₀+B₀
219+
{ Self::N_LIMBS }
220+
OP_ROLL
221+
OP_NOTIF
222+
OP_FROMALTSTACK
223+
OP_DROP
224+
OP_ENDIF
225+
// ⋯ (B₈+C₇⁺)+A₈ (B₇+C₆⁺)+A₇ ... (B₁+C₀⁺)+A₁ A₀+B₀
226+
// ⋯ ((B₈+C₇⁺)+A₈)-(C₇⁻+M₈) ... (A₀+B₀)-M₀
229227
}
230228
});
231229
script! {
@@ -368,28 +366,26 @@ pub trait Fp254Impl {
368366
// ⋯ C₈⁻ | (A₈-(B₈+C₇⁻))+(C₇⁺+M)₈ A₈-(B₈+C₇⁻)
369367
OP_DUP
370368
// ⋯ C₈⁻ C₈⁻
371-
{ script! {
372-
OP_NOTIF
373-
OP_FROMALTSTACK
374-
OP_DROP
375-
OP_ENDIF
369+
OP_NOTIF
370+
OP_FROMALTSTACK
371+
OP_DROP
372+
OP_ENDIF
376373

374+
OP_FROMALTSTACK
375+
// ⋯ C₈⁻ A₈-(B₈+C₇⁻) | (A₇-(B₇+C₆⁻))+(C₆⁺+M₇)
376+
// ⋯ C₈⁻ (A₈-(B₈+C₇⁻))+(C₇⁺+M₈) | (B₈+C₇⁻)+A₈
377+
for _ in 0..Self::N_LIMBS-1 {
378+
OP_FROMALTSTACK OP_DROP
379+
OP_FROMALTSTACK
380+
}
381+
// ⋯ C₈⁻ A₈-(B₈+C₇⁻) A₇-(B₇+C₆⁻) ... A₂-(B₂+C₁⁻) A₁-(B₁+C₀⁻) A₀+B₀
382+
// ⋯ C₈⁻ (A₈-(B₈+C₇⁻))+(C₇⁺+M₈) ... (A₀+B₀)-M₀ | A₀+B₀
383+
{ Self::N_LIMBS }
384+
OP_ROLL
385+
OP_IF
377386
OP_FROMALTSTACK
378-
// ⋯ C₈⁻ A₈-(B₈+C₇⁻) | (A₇-(B₇+C₆⁻))+(C₆⁺+M₇)
379-
// ⋯ C₈⁻ (A₈-(B₈+C₇⁻))+(C₇⁺+M₈) | (B₈+C₇⁻)+A₈
380-
for _ in 0..Self::N_LIMBS-1 {
381-
OP_FROMALTSTACK OP_DROP
382-
OP_FROMALTSTACK
383-
}
384-
// ⋯ C₈⁻ A₈-(B₈+C₇⁻) A₇-(B₇+C₆⁻) ... A₂-(B₂+C₁⁻) A₁-(B₁+C₀⁻) A₀+B₀
385-
// ⋯ C₈⁻ (A₈-(B₈+C₇⁻))+(C₇⁺+M₈) ... (A₀+B₀)-M₀ | A₀+B₀
386-
{ Self::N_LIMBS }
387-
OP_ROLL
388-
OP_IF
389-
OP_FROMALTSTACK
390-
OP_DROP
391-
OP_ENDIF
392-
}.add_stack_hint(-2, Self::N_LIMBS as i32 - 2).add_altstack_hint(-2 * Self::N_LIMBS as i32, -2 * Self::N_LIMBS as i32)}
387+
OP_DROP
388+
OP_ENDIF
393389
// ⋯ A₈-(B₈+C₇⁻) A₇-(B₇+C₆⁻) ... A₁-(B₁+C₀⁻) A₀+B₀
394390
// ⋯ (A₈-(B₈+C₇⁻))+(C₇⁺+M₈) ... (A₀-B₀)+M₀
395391
}
@@ -471,29 +467,27 @@ pub trait Fp254Impl {
471467
// ⋯ 2²⁹ C₈⁻ | (2⋅A₈+C₇⁺)-(C₇⁻+M₈)
472468
OP_NIP
473469
OP_DUP
474-
{ script! {
475-
// ⋯ C₈⁻ C₈⁻
476-
OP_IF
477-
OP_FROMALTSTACK
478-
OP_DROP
479-
OP_ENDIF
470+
// ⋯ C₈⁻ C₈⁻
471+
OP_IF
472+
OP_FROMALTSTACK
473+
OP_DROP
474+
OP_ENDIF
480475

476+
OP_FROMALTSTACK
477+
// ⋯ 2⋅A₈+C₇⁺ C₈⁻ | (2⋅A₇+C₆⁺)-(C₆⁻+M₇)
478+
// ⋯ (2⋅A₈+C₇⁺)-(C₇⁻+M₈) C₈⁻ | 2⋅A₈+C₇⁺
479+
for _ in 0..Self::N_LIMBS-1 {
480+
OP_FROMALTSTACK OP_DROP
481481
OP_FROMALTSTACK
482-
// ⋯ 2⋅A₈+C₇⁺ C₈⁻ | (2⋅A₇+C₆⁺)-(C₆⁻+M₇)
483-
// ⋯ (2⋅A₈+C₇⁺)-(C₇⁻+M₈) C₈⁻ | 2⋅A₈+C₇⁺
484-
for _ in 0..Self::N_LIMBS-1 {
485-
OP_FROMALTSTACK OP_DROP
486-
OP_FROMALTSTACK
487-
}
488-
// ⋯ 2⋅A₈+C₇⁺ 2⋅A₇+C₆⁺ ... 2⋅A₂+C₁⁺ 2⋅A₁+C₀⁺ 2⋅A₀ C₈⁻
489-
// ⋯ (2⋅A₈+C₇⁺)-(C₇⁻+M₈) ... 2⋅A₀-M₀ C₈⁻ | 2⋅A₀
490-
{ Self::N_LIMBS }
491-
OP_ROLL
492-
OP_NOTIF
493-
OP_FROMALTSTACK
494-
OP_DROP
495-
OP_ENDIF
496-
}.add_stack_hint(-2, Self::N_LIMBS as i32 - 2).add_altstack_hint(-2 * Self::N_LIMBS as i32, -2 * Self::N_LIMBS as i32)}
482+
}
483+
// ⋯ 2⋅A₈+C₇⁺ 2⋅A₇+C₆⁺ ... 2⋅A₂+C₁⁺ 2⋅A₁+C₀⁺ 2⋅A₀ C₈⁻
484+
// ⋯ (2⋅A₈+C₇⁺)-(C₇⁻+M₈) ... 2⋅A₀-M₀ C₈⁻ | 2⋅A₀
485+
{ Self::N_LIMBS }
486+
OP_ROLL
487+
OP_NOTIF
488+
OP_FROMALTSTACK
489+
OP_DROP
490+
OP_ENDIF
497491
// ⋯ 2⋅A₈+C₇⁺ 2⋅A₇+C₆⁺ ... 2⋅A₁+C₀⁺ 2⋅A₀
498492
// ⋯ (2⋅A₈+C₇⁺)-(C₇⁻+M₈) ... 2⋅A₀-M₀
499493
}

bitvm/src/hash/blake3.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ pub fn blake3_var_length(num_bytes: usize) -> Script {
326326
}
327327
};
328328

329-
let script = script! {
329+
script! {
330330
// Add the padding
331331
{ push_to_stack(0, num_padding_bytes) }
332332

@@ -396,9 +396,7 @@ pub fn blake3_var_length(num_bytes: usize) -> Script {
396396
for _ in 0..8 {
397397
u32_fromaltstack
398398
}
399-
};
400-
401-
script.add_stack_hint(-(num_bytes as i32), 32i32 - num_bytes as i32)
399+
}
402400
}
403401

404402
/// Blake3 taking a 40-byte message and returning a 20-byte digest
@@ -439,7 +437,6 @@ pub fn blake3_160() -> Script {
439437
{u32_fromaltstack()}
440438
}
441439
}
442-
.add_stack_hint(-40, -20)
443440
}
444441

445442
pub fn blake3_160_var_length(num_bytes: usize) -> Script {
@@ -450,7 +447,6 @@ pub fn blake3_160_var_length(num_bytes: usize) -> Script {
450447
OP_2DROP
451448
}
452449
}
453-
.add_stack_hint(-(num_bytes as i32), 20i32 - num_bytes as i32)
454450
}
455451

456452
pub fn push_bytes_hex(hex: &str) -> Script {
@@ -553,8 +549,6 @@ mod tests {
553549
{blake3_hash_equalverify()}
554550
OP_TRUE
555551
};
556-
let stack = script.clone().analyze_stack();
557-
println!("stack: {:?}", stack);
558552
run(script);
559553
}
560554

bitvm/src/u32/u32_and.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub fn u8_and(i: u32) -> Script {
6262
OP_OVER
6363
OP_ADD
6464
OP_ADD
65-
}.add_stack_hint(-(i as i32 + 256), -1)
65+
}
6666
}
6767

6868
/// Bitwise AND of a-th and b-th u32 elements from the top, keeps a-th element in the stack

bitvm/src/u32/u32_rrot.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ pub fn u32_rrot(rot_num: usize) -> Script {
213213
OP_FROMALTSTACK
214214
OP_FROMALTSTACK
215215
{byte_reorder(offset)}
216-
}.add_stack_hint(-4, 0)
216+
}
217217
}
218218

219219
#[cfg(test)]

bitvm/src/u32/u32_xor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub fn u8_xor(i: u32) -> Script {
7171
OP_OVER
7272
OP_ADD
7373
OP_ADD
74-
}.add_stack_hint(-(i as i32 + 256), -1)
74+
}
7575
}
7676

7777
/// Bitwise XOR of a-th and b-th u32 elements from the top, keeps a-th element in the stack

0 commit comments

Comments
 (0)