Hashes.sol 1.0 KB

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