Skip to content

feat: add DA (version 2) #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion contracts/FluiDex.sol
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,14 @@ contract FluiDexDemo is
* @param _block_id the l2 block id
* @param _public_inputs the public inputs of this block
* @param _serialized_proof the serialized proof of this block
* @param _public_data the serialized tx data inside this block (data availability)
* @return true if the block was accepted
*/
function submitBlock(
uint256 _block_id,
uint256[] memory _public_inputs,
uint256[] memory _serialized_proof
uint256[] memory _serialized_proof,
bytes memory _public_data
) external override returns (bool) {
// _public_inputs[0] is previous_state_root
// _public_inputs[1] is new_state_root
Expand All @@ -193,6 +195,12 @@ contract FluiDexDemo is

if (_serialized_proof.length != 0) {
// TODO: hash inputs and then pass into verifier
assert(
verifyDA(
_public_inputs,
_public_data
)
);
assert(
verifier.verify_serialized_proof(
_public_inputs,
Expand Down Expand Up @@ -220,6 +228,35 @@ contract FluiDexDemo is
return true;
}

function verifyDA(
uint256[] memory _public_inputs,
bytes memory _public_data
) private view returns (bool) {
// _public_inputs[2]/[3] is the low/high 128bit of sha256 hash of _public_data respectively
require(_public_inputs.length >= 4);

bytes32 h = sha256(_public_data);

// console.logBytes(_public_data);
// console.logBytes32(h);

uint256 h_lo = 0;
for (uint256 i = 0; i < 16; i++) {
uint256 tmp = uint256(uint8(h[i + 16])) << (120 - 8 * i);
h_lo = h_lo + tmp;
}
uint256 h_hi = 0;
for (uint256 i = 0; i < 16; i++) {
uint256 tmp = uint256(uint8(h[i])) << (120 - 8 * i);
h_hi = h_hi + tmp;
}

assert(_public_inputs[2] == h_hi);
assert(_public_inputs[3] == h_lo);

return true;
}

/**
* @dev require a token is registered
* @param token the ERC20 token address
Expand Down
11 changes: 9 additions & 2 deletions contracts/FluiDexDelegate.sol
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,20 @@ contract FluiDexDelegate is AccessControl, IFluiDexDelegate, ReentrancyGuard {
* @param _block_id the l2 block id
* @param _public_inputs the public inputs of this block
* @param _serialized_proof the serialized proof of this block
* @param _public_data the serialized tx data inside this block (data availability)
* @return true if the block was accepted
*/
function submitBlock(
uint256 _block_id,
uint256[] memory _public_inputs,
uint256[] memory _serialized_proof
uint256[] memory _serialized_proof,
bytes memory _public_data
) external override returns (bool) {
return target.submitBlock(_block_id, _public_inputs, _serialized_proof);
return target.submitBlock(
_block_id,
_public_inputs,
_serialized_proof,
_public_data
);
}
}
4 changes: 3 additions & 1 deletion contracts/IFluiDex.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ interface IFluiDex {
* @param _block_id the l2 block id
* @param _public_inputs the public inputs of this block
* @param _serialized_proof the serialized proof of this block
* @param _public_data the serialized tx data inside this block (data availability)
* @return true if the block was accepted
*/
function submitBlock(
uint256 _block_id,
uint256[] memory _public_inputs,
uint256[] memory _serialized_proof
uint256[] memory _serialized_proof,
bytes memory _public_data
) external returns (bool);
}
4 changes: 3 additions & 1 deletion contracts/IFluiDexDelegate.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ interface IFluiDexDelegate {
* @param _block_id the l2 block id
* @param _public_inputs the public inputs of this block
* @param _serialized_proof the serialized proof of this block
* @param _public_data the serialized tx data inside this block (data availability)
* @return true if the block was accepted
*/
function submitBlock(
uint256 _block_id,
uint256[] memory _public_inputs,
uint256[] memory _serialized_proof
uint256[] memory _serialized_proof,
bytes memory _public_data
) external returns (bool);
}