@@ -20,15 +20,19 @@ pub struct KeccakPermutationState {
2020
2121impl Default for KeccakPermutationState {
2222 fn default ( ) -> Self {
23- Self :: new ( )
23+ Self :: new ( [ 0u8 ; 32 ] )
2424 }
2525}
2626
2727impl KeccakPermutationState {
28- pub fn new ( ) -> Self {
28+ pub fn new ( iv : [ u8 ; 32 ] ) -> Self {
29+ let rate = 136 ;
30+ let mut state = [ 0u8 ; N ] ;
31+ state[ rate..rate + 32 ] . copy_from_slice ( & iv) ;
32+
2933 KeccakPermutationState {
30- state : [ 0u8 ; 200 ] ,
31- rate : 136 ,
34+ state,
35+ rate,
3236 capacity : 64 ,
3337 }
3438 }
@@ -70,15 +74,16 @@ pub struct KeccakDuplexSponge {
7074impl KeccakDuplexSponge {
7175 pub fn new ( iv : & [ u8 ] ) -> Self {
7276 assert_eq ! ( iv. len( ) , 32 ) ;
73- let state = KeccakPermutationState :: new ( ) ;
77+
78+ let state = KeccakPermutationState :: new ( iv. try_into ( ) . unwrap ( ) ) ;
7479 let rate = R ;
7580 let capacity = N - R ;
7681 KeccakDuplexSponge {
7782 state,
7883 rate,
7984 capacity,
8085 absorb_index : 0 ,
81- squeeze_index : 0 ,
86+ squeeze_index : rate ,
8287 }
8388 }
8489}
@@ -92,16 +97,14 @@ impl DuplexSpongeInterface for KeccakDuplexSponge {
9297 self . squeeze_index = self . rate ;
9398
9499 while !input. is_empty ( ) {
95- if self . absorb_index == self . rate {
100+ if self . absorb_index < self . rate {
101+ self . state . state [ self . absorb_index ] = input[ 0 ] ;
102+ self . absorb_index += 1 ;
103+ input = & input[ 1 ..] ;
104+ } else {
96105 self . state . permute ( ) ;
97106 self . absorb_index = 0 ;
98107 }
99-
100- let chunk_size = usize:: min ( self . rate - self . absorb_index , input. len ( ) ) ;
101- let dest = & mut self . state . state [ self . absorb_index ..self . absorb_index + chunk_size] ;
102- dest. copy_from_slice ( & input[ ..chunk_size] ) ;
103- self . absorb_index += chunk_size;
104- input = & input[ chunk_size..] ;
105108 }
106109 }
107110
@@ -116,11 +119,11 @@ impl DuplexSpongeInterface for KeccakDuplexSponge {
116119 }
117120
118121 let chunk_size = usize:: min ( self . rate - self . squeeze_index , length) ;
119- self . squeeze_index += chunk_size;
120- length -= chunk_size;
121122 output. extend_from_slice (
122123 & self . state . state [ self . squeeze_index ..self . squeeze_index + chunk_size] ,
123124 ) ;
125+ self . squeeze_index += chunk_size;
126+ length -= chunk_size;
124127 }
125128
126129 output
0 commit comments