Skip to content

Commit 32b66e5

Browse files
fix: do multiple digest writes for e1 for sha512/384
1 parent 49e1eb4 commit 32b66e5

File tree

1 file changed

+27
-12
lines changed
  • extensions/sha2/circuit/src/sha2_chip

1 file changed

+27
-12
lines changed

extensions/sha2/circuit/src/sha2_chip/mod.rs

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -115,20 +115,35 @@ impl<F: PrimeField32, C: ShaChipConfig> StepExecutorE1<F> for Sha2VmStep<C> {
115115
);
116116
}
117117
Rv32Sha2Opcode::SHA512 => {
118-
memory_write::<{ Sha512Config::WRITE_SIZE }>(
119-
state.memory,
120-
RV32_MEMORY_AS,
121-
dst,
122-
output.as_slice().try_into().unwrap(),
123-
);
118+
for i in 0..C::NUM_WRITES {
119+
memory_write::<{ Sha512Config::WRITE_SIZE }>(
120+
state.memory,
121+
RV32_MEMORY_AS,
122+
dst + (i * Sha512Config::WRITE_SIZE) as u32,
123+
output.as_slice()
124+
[i * Sha512Config::WRITE_SIZE..(i + 1) * Sha512Config::WRITE_SIZE]
125+
.try_into()
126+
.unwrap(),
127+
);
128+
}
124129
}
125130
Rv32Sha2Opcode::SHA384 => {
126-
memory_write::<{ Sha384Config::WRITE_SIZE }>(
127-
state.memory,
128-
RV32_MEMORY_AS,
129-
dst,
130-
output.as_slice().try_into().unwrap(),
131-
);
131+
// Pad the output with zeros to 64 bytes
132+
let output = output
133+
.into_iter()
134+
.chain(iter::repeat(0).take(16))
135+
.collect::<Vec<_>>();
136+
for i in 0..C::NUM_WRITES {
137+
memory_write::<{ Sha384Config::WRITE_SIZE }>(
138+
state.memory,
139+
RV32_MEMORY_AS,
140+
dst + (i * Sha384Config::WRITE_SIZE) as u32,
141+
output.as_slice()
142+
[i * Sha384Config::WRITE_SIZE..(i + 1) * Sha384Config::WRITE_SIZE]
143+
.try_into()
144+
.unwrap(),
145+
);
146+
}
132147
}
133148
}
134149

0 commit comments

Comments
 (0)