Skip to content

Commit

Permalink
fix: BLAKE2 test implementation (#435)
Browse files Browse the repository at this point in the history
* fix: `BLAKE2` test implementation

* chore: nit
  • Loading branch information
leovct authored Nov 13, 2024
1 parent 0de663e commit 3fee0b7
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 14 deletions.
39 changes: 39 additions & 0 deletions bindings/tester/LoadTester.abi
Original file line number Diff line number Diff line change
@@ -1,4 +1,43 @@
[
{
"type": "function",
"name": "F",
"inputs": [
{
"name": "rounds",
"type": "uint32",
"internalType": "uint32"
},
{
"name": "h",
"type": "bytes32[2]",
"internalType": "bytes32[2]"
},
{
"name": "m",
"type": "bytes32[4]",
"internalType": "bytes32[4]"
},
{
"name": "t",
"type": "bytes8[2]",
"internalType": "bytes8[2]"
},
{
"name": "f",
"type": "bool",
"internalType": "bool"
}
],
"outputs": [
{
"name": "",
"type": "bytes32[2]",
"internalType": "bytes32[2]"
}
],
"stateMutability": "view"
},
{
"type": "function",
"name": "dumpster",
Expand Down
2 changes: 1 addition & 1 deletion bindings/tester/LoadTester.bin

Large diffs are not rendered by default.

35 changes: 33 additions & 2 deletions bindings/tester/loadTester.go

Large diffs are not rendered by default.

40 changes: 29 additions & 11 deletions contracts/src/tester/LoadTester.sol
Original file line number Diff line number Diff line change
Expand Up @@ -911,22 +911,40 @@ contract LoadTester {
}
}

// https://eips.ethereum.org/EIPS/eip-152#example-usage-in-solidity
function testBlake2f(bytes memory inputData) public returns (bytes memory result) {
bytes32[2] memory h;
h[0] = hex"48c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5";
h[1] = hex"d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b";

bytes32[4] memory m;
m[0] = hex"6162630000000000000000000000000000000000000000000000000000000000";
m[1] = hex"0000000000000000000000000000000000000000000000000000000000000000";
m[2] = hex"0000000000000000000000000000000000000000000000000000000000000000";
m[3] = hex"0000000000000000000000000000000000000000000000000000000000000000";

bytes8[2] memory t;
t[0] = hex"0300000000000000";
t[1] = hex"0000000000000000";

bytes32[2] memory result = F(12, h, m, t, true);
bytes32[2] memory expected;
expected[0] = hex"ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d1";
expected[1] = hex"7d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923";
require(result[0] == expected[0], "TestBlake2f - First hash doesn't match");
require(result[1] == expected[1], "TestBlake2f - Second hash doesn't match");
}

function F(uint32 rounds, bytes32[2] memory h, bytes32[4] memory m, bytes8[2] memory t, bool f) public view returns (bytes32[2] memory) {
address BLAKE_2F_PRECOMPILED_CONTRACT = 0x0000000000000000000000000000000000000009;

bytes32[2] memory output;
bytes memory args = abi.encodePacked(rounds, h[0], h[1], m[0], m[1], m[2], m[3], t[0], t[1], f);
assembly {
let success := call(
gas(),
BLAKE_2F_PRECOMPILED_CONTRACT,
0, // no ether transfer
add(inputData, 32), // inputData offset
mload(inputData), // inputData length
result, // output area
64 // output area size (2 * 32 bytes)
)
if iszero(success) {
revert(0, 0)
if iszero(staticcall(not(0), BLAKE_2F_PRECOMPILED_CONTRACT, add(args, 32), 0xd5, output, 0x40)) {
revert(0, 0)
}
}
return output;
}
}

0 comments on commit 3fee0b7

Please sign in to comment.