Hashes.sol 955 B

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