Hashes.sol 951 B

1234567891011121314151617181920212223242526272829
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3. /**
  4. * @dev Library of standard hash functions.
  5. */
  6. library Hashes {
  7. /**
  8. * @dev Commutative Keccak256 hash of a sorted pair of bytes32. Frequently used when working with merkle proofs.
  9. *
  10. * NOTE: Equivalent to the `standardNodeHash` in our https://github.com/OpenZeppelin/merkle-tree[JavaScript library].
  11. */
  12. function commutativeKeccak256(bytes32 a, bytes32 b) internal pure returns (bytes32) {
  13. return a < b ? _efficientKeccak256(a, b) : _efficientKeccak256(b, a);
  14. }
  15. /**
  16. * @dev Implementation of keccak256(abi.encode(a, b)) that doesn't allocate or expand memory.
  17. */
  18. function _efficientKeccak256(bytes32 a, bytes32 b) private pure returns (bytes32 value) {
  19. /// @solidity memory-safe-assembly
  20. assembly {
  21. mstore(0x00, a)
  22. mstore(0x20, b)
  23. value := keccak256(0x00, 0x40)
  24. }
  25. }
  26. }