-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds
keccak256
primitive without memory allocation
Co-authored-by: agus <[email protected]>
- Loading branch information
1 parent
88c4a6d
commit b1edae2
Showing
4 changed files
with
113 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// SPDX-License-Identifier: Apache 2 | ||
|
||
// forge test --match-contract TestKeccak | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
import "forge-std/Test.sol"; | ||
import { BytesParsing } from "../src/libraries/BytesParsing.sol"; | ||
import { keccak256Word } from "../src/Utils.sol"; | ||
|
||
contract TestKeccak is Test { | ||
using BytesParsing for bytes; | ||
|
||
function test_bytesShouldHashTheSame(bytes memory data) public { | ||
bytes32 hash = data.keccak256Subarray(0, data.length); | ||
assertEq(hash, keccak256(abi.encodePacked(data))); | ||
} | ||
|
||
function test_bytesSubArrayEndShouldHashTheSame(bytes calldata data, uint256 indexSeed) public { | ||
vm.assume(data.length > 0); | ||
uint256 length = indexSeed % data.length; | ||
bytes32 hash = data.keccak256Subarray(0, length); | ||
assertEq(hash, keccak256(abi.encodePacked(data[0:length]))); | ||
} | ||
|
||
function test_bytesSubArrayStartShouldHashTheSame(bytes calldata data, uint256 indexSeed) public { | ||
vm.assume(data.length > 0); | ||
uint256 start = indexSeed % data.length; | ||
bytes32 hash = data.keccak256Subarray(start, data.length - start); | ||
assertEq(hash, keccak256(abi.encodePacked(data[start:data.length]))); | ||
} | ||
|
||
function test_bytesSubArrayStartEndShouldHashTheSame(bytes calldata data, uint256 startSeed, uint256 endSeed) public { | ||
vm.assume(data.length > 0); | ||
uint256 end = endSeed % data.length; | ||
vm.assume(end > 0); | ||
uint256 start = startSeed % end; | ||
bytes32 hash = data.keccak256Subarray(start, end - start); | ||
assertEq(hash, keccak256(abi.encodePacked(data[start:end]))); | ||
} | ||
|
||
function test_wordShouldHashTheSame(bytes32 data) public { | ||
bytes32 hash = keccak256Word(data); | ||
assertEq(hash, keccak256(abi.encodePacked(data))); | ||
} | ||
} |