1
- use triton_vm:: {
2
- program:: NonDeterminism ,
3
- triton_asm,
4
- twenty_first:: util_types:: mmr:: {
5
- mmr_accumulator:: MmrAccumulator , mmr_successor_proof:: MmrSuccessorProof ,
6
- } ,
7
- } ;
8
-
9
- use crate :: {
10
- arithmetic:: u64:: {
11
- add_u64:: AddU64 , log_2_floor_u64:: Log2FloorU64 , lt_u64:: LtU64ConsumeArgs ,
12
- popcount_u64:: PopCountU64 , shift_right_u64:: ShiftRightU64 , sub_u64:: SubU64 ,
13
- } ,
14
- data_type:: DataType ,
15
- field,
16
- hashing:: merkle_step_u64_index:: MerkleStepU64Index ,
17
- mmr:: {
18
- bag_peaks:: BagPeaks ,
19
- leaf_index_to_mt_index_and_peak_index:: MmrLeafIndexToMtIndexAndPeakIndex ,
20
- } ,
21
- prelude:: BasicSnippet ,
22
- Digest ,
23
- } ;
1
+ use triton_vm:: program:: NonDeterminism ;
2
+ use triton_vm:: triton_asm;
3
+ use triton_vm:: twenty_first:: util_types:: mmr:: mmr_accumulator:: MmrAccumulator ;
4
+ use triton_vm:: twenty_first:: util_types:: mmr:: mmr_successor_proof:: MmrSuccessorProof ;
5
+
6
+ use crate :: arithmetic:: u64:: add_u64:: AddU64 ;
7
+ use crate :: arithmetic:: u64:: log_2_floor_u64:: Log2FloorU64 ;
8
+ use crate :: arithmetic:: u64:: lt_u64:: LtU64ConsumeArgs ;
9
+ use crate :: arithmetic:: u64:: popcount_u64:: PopCountU64 ;
10
+ use crate :: arithmetic:: u64:: shift_right_u64:: ShiftRightU64 ;
11
+ use crate :: arithmetic:: u64:: sub_u64:: SubU64 ;
12
+ use crate :: data_type:: DataType ;
13
+ use crate :: field;
14
+ use crate :: hashing:: merkle_step_u64_index:: MerkleStepU64Index ;
15
+ use crate :: mmr:: bag_peaks:: BagPeaks ;
16
+ use crate :: mmr:: leaf_index_to_mt_index_and_peak_index:: MmrLeafIndexToMtIndexAndPeakIndex ;
17
+ use crate :: prelude:: BasicSnippet ;
18
+ use crate :: Digest ;
24
19
25
20
/// Verify that one MMR is a successor to another.
26
21
///
27
- /// Verify a the scucessorship relation between two MMRs. A `MmrSuccessorProof`
22
+ /// Verify a the successorship relation between two MMRs. A `MmrSuccessorProof`
28
23
/// is necessary to demonstrate this relation, but it is not a *stack* argument
29
24
/// because this algorithm obtains the relevant info (authentication paths) from
30
25
/// nondeterministic digests. Accordingly, nondeterminism must be initialized
@@ -369,38 +364,34 @@ impl VerifyMmrSuccessor {
369
364
#[ cfg( test) ]
370
365
mod test {
371
366
use std:: collections:: HashMap ;
367
+ use std:: collections:: VecDeque ;
372
368
373
369
use itertools:: Itertools ;
374
370
use rand:: prelude:: StdRng ;
375
371
use rand:: Rng ;
376
372
use rand:: RngCore ;
377
373
use rand:: SeedableRng ;
378
374
use triton_vm:: error:: InstructionError ;
379
- use triton_vm:: {
380
- prelude:: BFieldElement ,
381
- program:: NonDeterminism ,
382
- twenty_first:: {
383
- prelude:: Mmr ,
384
- util_types:: mmr:: {
385
- mmr_accumulator:: MmrAccumulator , mmr_successor_proof:: MmrSuccessorProof ,
386
- shared_advanced:: get_peak_heights,
387
- shared_basic:: leaf_index_to_mt_index_and_peak_index,
388
- } ,
389
- } ,
390
- } ;
375
+ use triton_vm:: prelude:: BFieldElement ;
376
+ use triton_vm:: prelude:: Tip5 ;
377
+ use triton_vm:: program:: NonDeterminism ;
378
+ use triton_vm:: twenty_first:: prelude:: Mmr ;
379
+ use triton_vm:: twenty_first:: util_types:: mmr:: mmr_accumulator:: MmrAccumulator ;
380
+ use triton_vm:: twenty_first:: util_types:: mmr:: mmr_successor_proof:: MmrSuccessorProof ;
381
+ use triton_vm:: twenty_first:: util_types:: mmr:: shared_advanced:: get_peak_heights;
382
+ use triton_vm:: twenty_first:: util_types:: mmr:: shared_basic:: leaf_index_to_mt_index_and_peak_index;
391
383
392
384
use crate :: empty_stack;
393
385
use crate :: memory:: encode_to_memory;
394
386
use crate :: memory:: FIRST_NON_DETERMINISTICALLY_INITIALIZED_MEMORY_ADDRESS ;
387
+ use crate :: prelude:: TasmObject ;
388
+ use crate :: snippet_bencher:: BenchmarkCase ;
395
389
use crate :: test_helpers:: negative_test;
396
- use crate :: traits:: algorithm:: ShadowedAlgorithm ;
390
+ use crate :: traits:: mem_preserver:: MemPreserver ;
391
+ use crate :: traits:: mem_preserver:: MemPreserverInitialState ;
392
+ use crate :: traits:: mem_preserver:: ShadowedMemPreserver ;
397
393
use crate :: traits:: rust_shadow:: RustShadow ;
398
- use crate :: {
399
- prelude:: TasmObject ,
400
- snippet_bencher:: BenchmarkCase ,
401
- traits:: algorithm:: { Algorithm , AlgorithmInitialState } ,
402
- Digest ,
403
- } ;
394
+ use crate :: Digest ;
404
395
use rand:: thread_rng;
405
396
406
397
use super :: VerifyMmrSuccessor ;
@@ -434,7 +425,7 @@ mod test {
434
425
old_mmr : & MmrAccumulator ,
435
426
new_mmr : & MmrAccumulator ,
436
427
mmr_successor_proof : & MmrSuccessorProof ,
437
- ) -> AlgorithmInitialState {
428
+ ) -> MemPreserverInitialState {
438
429
let mut nondeterminism = NonDeterminism :: new ( vec ! [ ] ) ;
439
430
VerifyMmrSuccessor :: update_nondeterminism ( & mut nondeterminism, mmr_successor_proof) ;
440
431
let old_mmr_address = FIRST_NON_DETERMINISTICALLY_INITIALIZED_MEMORY_ADDRESS ;
@@ -443,13 +434,14 @@ mod test {
443
434
let mut stack = empty_stack ( ) ;
444
435
stack. push ( old_mmr_address) ;
445
436
stack. push ( new_mmr_address) ;
446
- AlgorithmInitialState {
437
+ MemPreserverInitialState {
447
438
stack,
448
439
nondeterminism,
440
+ ..Default :: default ( )
449
441
}
450
442
}
451
443
452
- fn failing_initial_states ( ) -> Vec < AlgorithmInitialState > {
444
+ fn failing_initial_states ( ) -> Vec < MemPreserverInitialState > {
453
445
let mut rng = thread_rng ( ) ;
454
446
let mut initial_states = vec ! [ ] ;
455
447
for old_num_leafs in [ 1u64 , 8 ] {
@@ -539,13 +531,15 @@ mod test {
539
531
initial_states
540
532
}
541
533
542
- impl Algorithm for VerifyMmrSuccessor {
534
+ impl MemPreserver for VerifyMmrSuccessor {
543
535
fn rust_shadow (
544
536
& self ,
545
537
stack : & mut Vec < BFieldElement > ,
546
- memory : & mut HashMap < BFieldElement , BFieldElement > ,
547
- nondeterminism : & NonDeterminism ,
548
- ) {
538
+ memory : & HashMap < BFieldElement , BFieldElement > ,
539
+ _nd_tokens : VecDeque < BFieldElement > ,
540
+ nd_digests : VecDeque < Digest > ,
541
+ _sponge : & mut Option < Tip5 > ,
542
+ ) -> Vec < BFieldElement > {
549
543
let new_mmr_pointer = stack. pop ( ) . unwrap ( ) ;
550
544
let old_mmr_pointer = stack. pop ( ) . unwrap ( ) ;
551
545
@@ -554,17 +548,19 @@ mod test {
554
548
555
549
let num_digests = num_digests_to_read ( & old_mmr, & new_mmr) ;
556
550
557
- let digests = nondeterminism . digests [ 0 ..num_digests] . to_vec ( ) ;
551
+ let digests = ( 0 ..num_digests) . map ( |i| nd_digests [ i ] ) . collect_vec ( ) ;
558
552
let mmr_successor_proof = MmrSuccessorProof { paths : digests } ;
559
553
560
554
assert ! ( mmr_successor_proof. verify( & old_mmr, & new_mmr) ) ;
555
+
556
+ vec ! [ ]
561
557
}
562
558
563
559
fn pseudorandom_initial_state (
564
560
& self ,
565
561
seed : [ u8 ; 32 ] ,
566
562
bench_case : Option < BenchmarkCase > ,
567
- ) -> AlgorithmInitialState {
563
+ ) -> MemPreserverInitialState {
568
564
let mut rng: StdRng = SeedableRng :: from_seed ( seed) ;
569
565
let old_num_leafs = match bench_case {
570
566
Some ( BenchmarkCase :: WorstCase ) => u64:: MAX >> 2 ,
@@ -594,7 +590,7 @@ mod test {
594
590
initial_state_from_mmr_tuple ( & old_mmr, & new_mmr, & mmr_successor_proof)
595
591
}
596
592
597
- fn corner_case_initial_states ( & self ) -> Vec < AlgorithmInitialState > {
593
+ fn corner_case_initial_states ( & self ) -> Vec < MemPreserverInitialState > {
598
594
let mut rng = thread_rng ( ) ;
599
595
let mut initial_states = vec ! [ ] ;
600
596
for old_num_leafs in [ 0u64 , 1 , 8 ] {
@@ -629,15 +625,15 @@ mod test {
629
625
630
626
#[ test]
631
627
fn verify_mmr_successor_simple_test ( ) {
632
- ShadowedAlgorithm :: new ( VerifyMmrSuccessor ) . test ( ) ;
628
+ ShadowedMemPreserver :: new ( VerifyMmrSuccessor ) . test ( ) ;
633
629
}
634
630
635
631
#[ test]
636
632
fn verify_mmr_successor_negative_test ( ) {
637
633
for ( i, init_state) in failing_initial_states ( ) . into_iter ( ) . enumerate ( ) {
638
634
println ! ( "Trying failing initial state {i}." ) ;
639
635
negative_test (
640
- & ShadowedAlgorithm :: new ( VerifyMmrSuccessor ) ,
636
+ & ShadowedMemPreserver :: new ( VerifyMmrSuccessor ) ,
641
637
init_state. into ( ) ,
642
638
& [
643
639
InstructionError :: AssertionFailed ,
@@ -677,13 +673,13 @@ mod test {
677
673
678
674
#[ cfg( test) ]
679
675
mod bench {
680
- use crate :: traits:: algorithm :: ShadowedAlgorithm ;
676
+ use crate :: traits:: mem_preserver :: ShadowedMemPreserver ;
681
677
use crate :: traits:: rust_shadow:: RustShadow ;
682
678
683
679
use super :: * ;
684
680
685
681
#[ test]
686
682
fn verify_mmr_successor_benchmark ( ) {
687
- ShadowedAlgorithm :: new ( VerifyMmrSuccessor ) . bench ( ) ;
683
+ ShadowedMemPreserver :: new ( VerifyMmrSuccessor ) . bench ( ) ;
688
684
}
689
685
}
0 commit comments