Skip to content

Commit 3fee0b7

Browse files
authored
fix: BLAKE2 test implementation (#435)
* fix: `BLAKE2` test implementation * chore: nit
1 parent 0de663e commit 3fee0b7

File tree

4 files changed

+102
-14
lines changed

4 files changed

+102
-14
lines changed

bindings/tester/LoadTester.abi

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,43 @@
11
[
2+
{
3+
"type": "function",
4+
"name": "F",
5+
"inputs": [
6+
{
7+
"name": "rounds",
8+
"type": "uint32",
9+
"internalType": "uint32"
10+
},
11+
{
12+
"name": "h",
13+
"type": "bytes32[2]",
14+
"internalType": "bytes32[2]"
15+
},
16+
{
17+
"name": "m",
18+
"type": "bytes32[4]",
19+
"internalType": "bytes32[4]"
20+
},
21+
{
22+
"name": "t",
23+
"type": "bytes8[2]",
24+
"internalType": "bytes8[2]"
25+
},
26+
{
27+
"name": "f",
28+
"type": "bool",
29+
"internalType": "bool"
30+
}
31+
],
32+
"outputs": [
33+
{
34+
"name": "",
35+
"type": "bytes32[2]",
36+
"internalType": "bytes32[2]"
37+
}
38+
],
39+
"stateMutability": "view"
40+
},
241
{
342
"type": "function",
443
"name": "dumpster",

bindings/tester/LoadTester.bin

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

bindings/tester/loadTester.go

Lines changed: 33 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

contracts/src/tester/LoadTester.sol

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -911,22 +911,40 @@ contract LoadTester {
911911
}
912912
}
913913

914+
// https://eips.ethereum.org/EIPS/eip-152#example-usage-in-solidity
914915
function testBlake2f(bytes memory inputData) public returns (bytes memory result) {
916+
bytes32[2] memory h;
917+
h[0] = hex"48c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5";
918+
h[1] = hex"d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b";
919+
920+
bytes32[4] memory m;
921+
m[0] = hex"6162630000000000000000000000000000000000000000000000000000000000";
922+
m[1] = hex"0000000000000000000000000000000000000000000000000000000000000000";
923+
m[2] = hex"0000000000000000000000000000000000000000000000000000000000000000";
924+
m[3] = hex"0000000000000000000000000000000000000000000000000000000000000000";
925+
926+
bytes8[2] memory t;
927+
t[0] = hex"0300000000000000";
928+
t[1] = hex"0000000000000000";
929+
930+
bytes32[2] memory result = F(12, h, m, t, true);
931+
bytes32[2] memory expected;
932+
expected[0] = hex"ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d1";
933+
expected[1] = hex"7d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923";
934+
require(result[0] == expected[0], "TestBlake2f - First hash doesn't match");
935+
require(result[1] == expected[1], "TestBlake2f - Second hash doesn't match");
936+
}
937+
938+
function F(uint32 rounds, bytes32[2] memory h, bytes32[4] memory m, bytes8[2] memory t, bool f) public view returns (bytes32[2] memory) {
915939
address BLAKE_2F_PRECOMPILED_CONTRACT = 0x0000000000000000000000000000000000000009;
916940

941+
bytes32[2] memory output;
942+
bytes memory args = abi.encodePacked(rounds, h[0], h[1], m[0], m[1], m[2], m[3], t[0], t[1], f);
917943
assembly {
918-
let success := call(
919-
gas(),
920-
BLAKE_2F_PRECOMPILED_CONTRACT,
921-
0, // no ether transfer
922-
add(inputData, 32), // inputData offset
923-
mload(inputData), // inputData length
924-
result, // output area
925-
64 // output area size (2 * 32 bytes)
926-
)
927-
if iszero(success) {
928-
revert(0, 0)
944+
if iszero(staticcall(not(0), BLAKE_2F_PRECOMPILED_CONTRACT, add(args, 32), 0xd5, output, 0x40)) {
945+
revert(0, 0)
929946
}
930947
}
948+
return output;
931949
}
932950
}

0 commit comments

Comments
 (0)