Skip to content

Commit 9c40fb3

Browse files
committed
refact: add tests for new get_proof slicing
1 parent 4ee3053 commit 9c40fb3

4 files changed

Lines changed: 83 additions & 3 deletions

File tree

src/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ relwithdebinfo=yes
202202
endif
203203

204204
ifeq ($(coverage),yes)
205-
OPTFLAGS+=-Og -g -fno-omit-frame-pointer -fno-dce -fno-inline
205+
OPTFLAGS+=-Og -g -fno-omit-frame-pointer -fno-inline
206206
DEFS+=-DCODE_COVERAGE
207207
else ifeq ($(relwithdebinfo),yes)
208208
OPTFLAGS+=-O2 -g
@@ -327,7 +327,7 @@ ifeq ($(coverage),yes)
327327
ifeq ($(COVERAGE_TOOLCHAIN),gcc)
328328
CC=gcc
329329
CXX=g++
330-
CXXFLAGS+=--coverage
330+
CXXFLAGS+=--coverage -fno-dce
331331
LDFLAGS+=--coverage
332332
else ifeq ($(COVERAGE_TOOLCHAIN),clang)
333333
CC=clang

tests/lua/cartesi/tests/util.lua

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,48 @@ function test_util.check_proof(proof, hash_fn)
243243
return hash == proof.root_hash
244244
end
245245

246+
function test_util.slice_proof(proof, new_log2_root_size, new_log2_target_size, hash_fn)
247+
assert(hash_fn, "hash_fn is nil")
248+
assert(new_log2_root_size <= proof.log2_root_size, "log2_root_size is too large")
249+
assert(new_log2_target_size >= proof.log2_target_size, "log2_target_size is too small")
250+
assert(new_log2_target_size <= new_log2_root_size, "log2_target_size > log2_root_size")
251+
-- Bubble up from original target to new target size
252+
local hash = proof.target_hash
253+
for log2_size = proof.log2_target_size, new_log2_target_size - 1 do
254+
local bit = (proof.target_address & (1 << log2_size)) ~= 0
255+
local first, second
256+
if bit then
257+
first, second = proof.sibling_hashes[log2_size - proof.log2_target_size + 1], hash
258+
else
259+
first, second = hash, proof.sibling_hashes[log2_size - proof.log2_target_size + 1]
260+
end
261+
hash = cartesi[hash_fn](first, second)
262+
end
263+
local sliced = {
264+
log2_root_size = new_log2_root_size,
265+
log2_target_size = new_log2_target_size,
266+
target_hash = hash,
267+
target_address = (proof.target_address >> new_log2_target_size) << new_log2_target_size,
268+
sibling_hashes = {},
269+
}
270+
-- Copy sibling hashes and continue bubbling up to compute root hash
271+
for log2_size = new_log2_target_size, new_log2_root_size - 1 do
272+
local sibling = proof.sibling_hashes[log2_size - proof.log2_target_size + 1]
273+
sliced.sibling_hashes[log2_size - new_log2_target_size + 1] = sibling
274+
local bit = (proof.target_address & (1 << log2_size)) ~= 0
275+
local first, second
276+
if bit then
277+
first, second = sibling, hash
278+
else
279+
first, second = hash, sibling
280+
end
281+
hash = cartesi[hash_fn](first, second)
282+
end
283+
sliced.root_hash = hash
284+
assert(test_util.check_proof(sliced, hash_fn), "produced invalid sliced proof")
285+
return sliced
286+
end
287+
246288
function test_util.align(v, el)
247289
return (v >> el << el)
248290
end

tests/lua/spec-hash-tree.lua

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,44 @@ describe("hash tree", function()
266266
end
267267
end)
268268

269+
it("should have consistent sliced proofs", function()
270+
local word_index = 0
271+
for _, p in ipairs(interesting_pages) do
272+
local page, range = table.unpack(p)
273+
local range_log2_size = math.ceil(math.log(bit_ceil(range.length), 2))
274+
local interesting_sizes = {
275+
LOG2_WORD_SIZE,
276+
LOG2_WORD_SIZE + 1,
277+
LOG2_PAGE_SIZE - 1,
278+
LOG2_PAGE_SIZE,
279+
LOG2_PAGE_SIZE + 1,
280+
range_log2_size - 1,
281+
range_log2_size,
282+
range_log2_size + 1,
283+
LOG2_ROOT_SIZE,
284+
}
285+
local address = get_aligned_address(page + (word_index * WORD_SIZE % PAGE_SIZE), LOG2_WORD_SIZE)
286+
word_index = word_index + 17
287+
local full_proof = machine:get_proof(address, LOG2_WORD_SIZE)
288+
for _, log2_root_size in ipairs(interesting_sizes) do
289+
for _, log2_target_size in ipairs(interesting_sizes) do
290+
if log2_target_size >= LOG2_WORD_SIZE
291+
and log2_root_size > log2_target_size
292+
and log2_root_size <= LOG2_ROOT_SIZE
293+
then
294+
local sproof = util.slice_proof(
295+
full_proof, log2_root_size, log2_target_size, hash_function
296+
)
297+
expect_consistent_proof(sproof, hash_function)
298+
local aligned = get_aligned_address(address, log2_target_size)
299+
local mproof = machine:get_proof(aligned, log2_target_size, log2_root_size)
300+
expect.equal(sproof, mproof)
301+
end
302+
end
303+
end
304+
end
305+
end)
306+
269307
it("should have consistent proofs for all words (and up) in a few pages", function()
270308
local clone_machine <close> = make_machine({ phtc_size = 1 })
271309
clone_machine:run()

tests/misc/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ all: $(BUILDDIR)/test-machine-c-api
111111
@exit 1
112112

113113
$(BUILDDIR)/test-machine-c-api: test-machine-c-api.o ../../src/libcartesi.a ../../src/libcartesi_hash_tree.a
114-
$(CXX) -o $@ $^ $(CXXFLAGS) $(LIBCARTESI_LIBS) $(OMP_LIB)
114+
$(CXX) -o $@ $^ $(CXXFLAGS) $(LDFLAGS) $(LIBCARTESI_LIBS) $(OMP_LIB)
115115

116116
%.o: %.cpp
117117
$(CXX) $(CXXFLAGS) -c -o $@ $<

0 commit comments

Comments
 (0)