Hashes.sol 1.0 KB

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