Skip to content

Commit ee21b6b

Browse files
authored
fix(avm): tag check before casting in sha (#19220)
Closes [this linear issue](https://linear.app/aztec-labs/issue/AVM-168/sim-completeness-ts-sha256compression-invalid-cast-tonumber). It only impacted Sha256C
1 parent 1a1c595 commit ee21b6b

File tree

1 file changed

+7
-3
lines changed
  • yarn-project/simulator/src/public/avm/opcodes

1 file changed

+7
-3
lines changed

yarn-project/simulator/src/public/avm/opcodes/hashing.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,17 @@ export class Sha256Compression extends Instruction {
132132
const [outputOffset, stateOffset, inputsOffset] = addressing.resolve(operands, memory);
133133

134134
// Note: size of output is same as size of state
135-
const inputs = Uint32Array.from(memory.getSlice(inputsOffset, INPUTS_SIZE).map(word => word.toNumber()));
136-
const state = Uint32Array.from(memory.getSlice(stateOffset, STATE_SIZE).map(word => word.toNumber()));
135+
const inputs = memory.getSlice(inputsOffset, INPUTS_SIZE).map(word => word.toBigInt());
136+
const state = memory.getSlice(stateOffset, STATE_SIZE).map(word => word.toBigInt());
137137

138138
memory.checkTagsRange(TypeTag.UINT32, inputsOffset, INPUTS_SIZE);
139139
memory.checkTagsRange(TypeTag.UINT32, stateOffset, STATE_SIZE);
140140

141-
const output = sha256Compression(state, inputs);
141+
// At this point both state and inputs are Uint32Array-compatible
142+
const inputsArray = new Uint32Array(inputs.map(i => Number(i)));
143+
const stateArray = new Uint32Array(state.map(i => Number(i)));
144+
145+
const output = sha256Compression(stateArray, inputsArray);
142146

143147
// Conversion required from Uint32Array to Uint32[] (can't map directly, need `...`)
144148
const res = [...output].map(word => new Uint32(word));

0 commit comments

Comments
 (0)