Skip to content

Commit

Permalink
refactor(sol): use Hash.sha256ToField library where required (#637)
Browse files Browse the repository at this point in the history
* refactor(sol): use Hash.sha256ToField library where required

* fmt :)

* feat: expose bytes32 hash directly

* fmt: https://i.pinimg.com/originals/81/23/a1/8123a132c007eab782d6ca9bed517eb3.jpg

---------

Co-authored-by: Maddiaa0 <[email protected]>
  • Loading branch information
Maddiaa0 and Maddiaa0 authored May 22, 2023
1 parent a613419 commit 9d3ea2c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 27 deletions.
10 changes: 4 additions & 6 deletions l1-contracts/src/core/Decoder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Copyright 2023 Aztec Labs.
pragma solidity >=0.8.18;

import {Hash} from "@aztec/core/libraries/Hash.sol";

/**
* @title Decoder
* @author Aztec Labs
Expand Down Expand Up @@ -108,10 +110,6 @@ contract Decoder {
uint256 internal constant CONTRACTS_PER_KERNEL = 1;
uint256 internal constant L1_TO_L2_MSGS_PER_ROLLUP = 16;

// Prime field order
uint256 internal constant P =
21888242871839275222246405745257275088548364400416034343698204186575808495617;

/**
* @notice Decodes the inputs and computes values to check state against
* @param _l2Block - The L2 block calldata.
Expand Down Expand Up @@ -167,7 +165,7 @@ contract Decoder {
mstore(add(temp, add(0x20, endOfTreesData)), _diffRoot)
mstore(add(temp, add(0x40, endOfTreesData)), _l1ToL2MsgsHash)
}
return bytes32(uint256(sha256(temp)) % P);
return Hash.sha256ToField(temp);
}

/**
Expand Down Expand Up @@ -504,7 +502,7 @@ contract Decoder {

// Compute current iteration's logs hash and truncate the hash to field
// See: https://discourse.aztec.network/t/proposal-forcing-the-sequencer-to-actually-submit-data-to-l1/426/2
logsHash = bytes32(uint256(sha256(temp)) % P);
logsHash = Hash.sha256ToField(temp);
}

return (logsHash, offset);
Expand Down
10 changes: 10 additions & 0 deletions l1-contracts/src/core/libraries/Hash.sol
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,14 @@ library Hash {
function sha256ToField(bytes memory _data) internal pure returns (bytes32) {
return bytes32(uint256(sha256(_data)) % Constants.P);
}

/**
* @notice Computes the sha256 hash of the provided data and converts it to a field element
* @dev Using modulo to convert the hash to a field element.
* @param _data - A bytes32 value to hash
* @return The hash of the provided data as a field element
*/
function sha256ToField(bytes32 _data) internal pure returns (bytes32) {
return sha256ToField(abi.encodePacked(_data));
}
}
29 changes: 8 additions & 21 deletions l1-contracts/test/Decoder.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pragma solidity >=0.8.18;
import {Test} from "forge-std/Test.sol";

import {Decoder} from "@aztec/core/Decoder.sol";
import {Constants} from "@aztec/core/libraries/Constants.sol";
import {Hash} from "@aztec/core/libraries/Hash.sol";
import {DecoderHelper} from "./DecoderHelper.sol";

/**
Expand Down Expand Up @@ -144,12 +144,9 @@ contract DecoderTest is Test {

// Note: First 32 bytes are 0 because those correspond to the hash of previous iteration and there was no previous
// iteration.
bytes32 referenceLogsHash = bytes32(
uint256(
sha256(
hex"0000000000000000000000000000000000000000000000000000000000000000aafdc7aa93e78a70"
)
) % Constants.P

bytes32 referenceLogsHash = Hash.sha256ToField(
hex"0000000000000000000000000000000000000000000000000000000000000000aafdc7aa93e78a70"
);

assertEq(bytesAdvanced, emptyKernelData.length, "Advanced by an incorrect number of bytes");
Expand All @@ -167,22 +164,12 @@ contract DecoderTest is Test {
hex"0000002400000008aafdc7aa93e78a700000001497aee30906a86173c86c6d3f108eefc36e7fb014";
(bytes32 logsHash, uint256 bytesAdvanced) = helper.computeKernelLogsHash(emptyKernelData);

bytes32 referenceLogsHashFromIteration1 = bytes32(
uint256(
sha256(
hex"0000000000000000000000000000000000000000000000000000000000000000aafdc7aa93e78a70"
)
) % Constants.P
bytes32 referenceLogsHashFromIteration1 = Hash.sha256ToField(
hex"0000000000000000000000000000000000000000000000000000000000000000aafdc7aa93e78a70"
);

bytes32 referenceLogsHashFromIteration2 = bytes32(
uint256(
sha256(
bytes.concat(
referenceLogsHashFromIteration1, hex"97aee30906a86173c86c6d3f108eefc36e7fb014"
)
)
) % Constants.P
bytes32 referenceLogsHashFromIteration2 = Hash.sha256ToField(
bytes.concat(referenceLogsHashFromIteration1, hex"97aee30906a86173c86c6d3f108eefc36e7fb014")
);

assertEq(bytesAdvanced, emptyKernelData.length, "Advanced by an incorrect number of bytes");
Expand Down

0 comments on commit 9d3ea2c

Please sign in to comment.