File tree Expand file tree Collapse file tree 1 file changed +26
-5
lines changed
Expand file tree Collapse file tree 1 file changed +26
-5
lines changed Original file line number Diff line number Diff line change @@ -62,13 +62,34 @@ impl Poseidon2 {
6262 fn hash_internal <let N : u32 >(input : [Field ; N ], in_len : u32 ) -> Field {
6363 let two_pow_64 = 18446744073709551616 ;
6464 let iv : Field = (in_len as Field ) * two_pow_64 ;
65- let mut sponge = Poseidon2 ::new (iv );
66- for i in 0 ..input .len () {
67- if i < in_len {
68- sponge .absorb (input [i ]);
65+ let mut state = [0 ; 4 ];
66+ state [RATE ] = iv ;
67+
68+ if std::runtime:: is_unconstrained () {
69+ for i in 0 ..in_len {
70+ state [i % RATE ] += input [i ];
71+ if (i + 1 ) % RATE == 0 {
72+ state = crate ::poseidon2_permutation (state , 4 );
73+ }
74+ }
75+ } else {
76+ for i in 0 ..input .len () {
77+ if i < in_len {
78+ state [i % RATE ] += input [i ];
79+ if (i + 1 ) % RATE == 0 {
80+ state = crate ::poseidon2_permutation (state , 4 );
81+ }
82+ }
6983 }
7084 }
71- sponge .squeeze ()
85+
86+ // Always run final permutation unless we just completed a full chunk
87+ // still need to permute once if in_len is 0
88+ if (in_len == 0 ) | (in_len % RATE != 0 ) {
89+ state = crate ::poseidon2_permutation (state , 4 )
90+ };
91+
92+ state [0 ]
7293 }
7394}
7495
You can’t perform that action at this time.
0 commit comments