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
…#877)

* fix: sha256 output was not forcing conversion of u32 words to 4-bytes

* fix expected gas
  • Loading branch information
enitrat authored Aug 28, 2024
1 parent 4e29cc6 commit 675b73d
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, 84); // BASE + 2 WORDS
}


// 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 675b73d

Please sign in to comment.