@@ -543,75 +543,6 @@ impl<'a> Poseidon2<'a> {
543
543
}
544
544
}
545
545
546
- /// Performs a poseidon hash with a sponge construction equivalent to the one in poseidon2.nr
547
- pub fn poseidon_hash ( inputs : & [ FieldElement ] ) -> Result < FieldElement , BlackBoxResolutionError > {
548
- let two_pow_64 = 18446744073709551616_u128 . into ( ) ;
549
- let iv = FieldElement :: from ( inputs. len ( ) ) * two_pow_64;
550
- let mut sponge = Poseidon2Sponge :: new ( iv, 3 ) ;
551
- for input in inputs. iter ( ) {
552
- sponge. absorb ( * input) ?;
553
- }
554
- sponge. squeeze ( )
555
- }
556
-
557
- pub struct Poseidon2Sponge < ' a > {
558
- rate : usize ,
559
- poseidon : Poseidon2 < ' a > ,
560
- squeezed : bool ,
561
- cache : Vec < FieldElement > ,
562
- state : Vec < FieldElement > ,
563
- }
564
-
565
- impl < ' a > Poseidon2Sponge < ' a > {
566
- pub fn new ( iv : FieldElement , rate : usize ) -> Poseidon2Sponge < ' a > {
567
- let mut result = Poseidon2Sponge {
568
- cache : Vec :: with_capacity ( rate) ,
569
- state : vec ! [ FieldElement :: zero( ) ; rate + 1 ] ,
570
- squeezed : false ,
571
- rate,
572
- poseidon : Poseidon2 :: new ( ) ,
573
- } ;
574
- result. state [ rate] = iv;
575
- result
576
- }
577
-
578
- fn perform_duplex ( & mut self ) -> Result < ( ) , BlackBoxResolutionError > {
579
- // zero-pad the cache
580
- for _ in self . cache . len ( ) ..self . rate {
581
- self . cache . push ( FieldElement :: zero ( ) ) ;
582
- }
583
- // add the cache into sponge state
584
- for i in 0 ..self . rate {
585
- self . state [ i] += self . cache [ i] ;
586
- }
587
- self . state = self . poseidon . permutation ( & self . state , 4 ) ?;
588
- Ok ( ( ) )
589
- }
590
-
591
- pub fn absorb ( & mut self , input : FieldElement ) -> Result < ( ) , BlackBoxResolutionError > {
592
- assert ! ( !self . squeezed) ;
593
- if self . cache . len ( ) == self . rate {
594
- // If we're absorbing, and the cache is full, apply the sponge permutation to compress the cache
595
- self . perform_duplex ( ) ?;
596
- self . cache = vec ! [ input] ;
597
- } else {
598
- // If we're absorbing, and the cache is not full, add the input into the cache
599
- self . cache . push ( input) ;
600
- }
601
- Ok ( ( ) )
602
- }
603
-
604
- pub fn squeeze ( & mut self ) -> Result < FieldElement , BlackBoxResolutionError > {
605
- assert ! ( !self . squeezed) ;
606
- // If we're in absorb mode, apply sponge permutation to compress the cache.
607
- self . perform_duplex ( ) ?;
608
- self . squeezed = true ;
609
-
610
- // Pop one item off the top of the permutation and return it.
611
- Ok ( self . state [ 0 ] )
612
- }
613
- }
614
-
615
546
#[ cfg( test) ]
616
547
mod test {
617
548
use acir:: AcirField ;
@@ -631,19 +562,4 @@ mod test {
631
562
] ;
632
563
assert_eq ! ( result, expected_result) ;
633
564
}
634
-
635
- #[ test]
636
- fn hash_smoke_test ( ) {
637
- let fields = [
638
- FieldElement :: from ( 1u128 ) ,
639
- FieldElement :: from ( 2u128 ) ,
640
- FieldElement :: from ( 3u128 ) ,
641
- FieldElement :: from ( 4u128 ) ,
642
- ] ;
643
- let result = super :: poseidon_hash ( & fields) . expect ( "should hash successfully" ) ;
644
- assert_eq ! (
645
- result,
646
- field_from_hex( "130bf204a32cac1f0ace56c78b731aa3809f06df2731ebcf6b3464a15788b1b9" ) ,
647
- ) ;
648
- }
649
565
}
0 commit comments