Skip to content

Commit

Permalink
fix: sha256 output was not forcing conversion of u32 words to 4-bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
enitrat committed Aug 27, 2024
1 parent 4e29cc6 commit d6cc314
Showing 1 changed file with 52 additions and 1 deletion.
53 changes: 52 additions & 1 deletion crates/evm/src/precompiles/sha256.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl Sha256 of Precompile {
let mut result_bytes = array![];
for word in result_words_32
.span() {
let word_bytes = (*word).to_be_bytes();
let word_bytes = (*word).to_be_bytes_padded();
result_bytes.append_span(word_bytes);
};

Expand Down Expand Up @@ -96,6 +96,57 @@ mod tests {
assert_eq!(gas, 72);
}

#[test]
fn test_sha256_more_than_32_bytes() {
let calldata = [
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0xf3,
0x45,
0x78,
0x90,
0x7f,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00
];

let (gas, result) = Sha256::exec(calldata.span()).unwrap();

let result: u256 = result.from_be_bytes().unwrap();
let expected_result = 0x3b745a1c00d035c334f358d007a430e4cf0ae63aa0556fb05529706de546464d;

assert_eq!(result, expected_result);
assert_eq!(gas, 72);
}


// source:
// <https://www.evm.codes/playground?unit=Wei&codeType=Mnemonic&code='wFirsWplaceqparameters%20in%20memorybFFjdata~0vMSTOREvvwDoqcallZSizeZ_1XSizeb1FX_2jaddressY4%200xFFFFFFFFjgasvSTATICCALLvvwPutqresulWalonVonqstackvPOPb20vMLOAD'~Y1j//%20v%5Cnq%20thVj%20wb~0x_Offset~Zb20jretYvPUSHXjargsWt%20Ve%20%01VWXYZ_bjqvw~_>
Expand Down

0 comments on commit d6cc314

Please sign in to comment.