Skip to content
This repository has been archived by the owner on Mar 12, 2024. It is now read-only.

Commit

Permalink
feat(arbitration): add merkle proof verification
Browse files Browse the repository at this point in the history
  • Loading branch information
GCdePaula committed Aug 18, 2023
1 parent 487e68d commit 0c3dcb8
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@ local consts = require "constants"
local CommitmentBuilder = {}
CommitmentBuilder.__index = CommitmentBuilder

function CommitmentBuilder:new(initial_hash)
local c = { initial_hash = initial_hash }
function CommitmentBuilder:new(initial_hash, second_state)
local c = { initial_hash = initial_hash, second_state = second_state }
setmetatable(c, self)
return c
end

function CommitmentBuilder:build(_, level)
local builder = MerkleBuilder:new()
builder:add(Hash.zero, 1 << consts.heights[consts.levels - level + 1])
if consts.log2step[consts.levels - level + 1] == 0 and self.second_state then
builder:add(self.second_state)
builder:add(Hash.zero, (1 << consts.heights[consts.levels - level + 1]) - 1)
else
builder:add(Hash.zero, 1 << consts.heights[consts.levels - level + 1])
end
-- local commitment = Hash.zero:iterated_merkle(consts.heights[level])
return builder:build(self.initial_hash)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,15 @@ end

local function generate_proof(proof, root, height, include_index)
if height == 0 then
if include_index == 0 then
proof.leaf = root
return
else
table.insert(proof, root)
end
proof.leaf = root
return
end

local new_height = height - 1
local ok, left, right = root:children()
assert(ok)

if (include_index >> new_height) == 0 then
if (include_index >> new_height) & 1 == 0 then
generate_proof(proof, left, new_height, include_index)
table.insert(proof, right)
else
Expand All @@ -66,6 +62,8 @@ function MerkleTree:prove_leaf(index)
height = self.log2size
end

print(index, height, "P")

assert((index >> height) == 0)
local proof = {}
generate_proof(proof, self.root_hash, height, index)
Expand Down Expand Up @@ -94,4 +92,20 @@ function MerkleTree:last()
return old_right, array_reverse(proof)
end

-- local Hash = require "cryptography.hash"
-- local MerkleBuilder = require "cryptography.merkle_builder"
-- local builder = MerkleBuilder:new()
-- builder:add(Hash.zero, 1 << 8)
-- local mt = builder:build()

-- local i, p = mt:last((1 << 8) - 1)
-- local r = assert(i)
-- print(i)
-- for _, v in ipairs(p) do
-- print(v)
-- r = v:join(r)
-- end

-- print("FINAL", r, mt.root_hash)

return MerkleTree
9 changes: 8 additions & 1 deletion onchain/permissionless-arbitration/offchain/entrypoint.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ print "Hello, world!"
os.execute "cd offchain/program && ./gen_machine_simple.sh"
local machine_path = "offchain/program/simple-program"


local Player = require "player"
local Client = require "blockchain.client"

local Machine = require "computation.machine"
local initial_hash = Machine:new_from_path(machine_path):state().root_hash
local m = Machine:new_from_path(machine_path)
local initial_hash = m:state().root_hash

local Blockchain = require "blockchain.node"
local blockchain = Blockchain:new()
Expand All @@ -29,6 +31,11 @@ end
local p2
do
local FakeCommitmentBuilder = require "computation.fake_commitment"

-- m:run(m.start_cycle + 1)
-- local second_hash = m:state().root_hash
-- local builder = FakeCommitmentBuilder:new(initial_hash, second_hash)

local builder = FakeCommitmentBuilder:new(initial_hash)
local client = Client:new(blockchain)
p2 = Player:new(contract, client, builder)
Expand Down
17 changes: 8 additions & 9 deletions onchain/permissionless-arbitration/src/Commitment.sol
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ library Commitment {
bytes32[] calldata hashProof
) internal pure {
uint64 treeHeight = ArbitrationConstants.height(level);
// Tree.Node expectedCommitment = getRootForLastLeaf(
// treeHeight,
// Machine.Hash.unwrap(finalState),
// hashProof
// );
Tree.Node expectedCommitment = getRootForLastLeaf(
treeHeight,
Machine.Hash.unwrap(finalState),
hashProof
);

// require(commitment.eq(expectedCommitment), "commitment last state doesn't match");
require(commitment.eq(expectedCommitment), "commitment last state doesn't match");
}


Expand All @@ -81,10 +81,9 @@ library Commitment {
bytes32 leaf,
bytes32[] calldata siblings
) internal pure returns (Tree.Node) {
uint nodesCount = treeHeight - 1;
assert(nodesCount == siblings.length);
assert(treeHeight == siblings.length);

for (uint i = 0; i < nodesCount; i++) {
for (uint i = 0; i < treeHeight; i++) {
leaf = keccak256(abi.encodePacked(siblings[i], leaf));
}

Expand Down

0 comments on commit 0c3dcb8

Please sign in to comment.