From 6e9fcc632c9599679bca8788c9c4221eabdb5e21 Mon Sep 17 00:00:00 2001 From: Balazs Komuves Date: Thu, 11 Jul 2024 15:47:23 +0200 Subject: [PATCH] fix the incorrect handling of singleton Merkle trees --- circuit/codex/merkle.circom | 13 ++++++++++++- circuit/codex/sample_cells.circom | 23 ++++++++++++++++++----- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/circuit/codex/merkle.circom b/circuit/codex/merkle.circom index 6af6587..9e00bea 100644 --- a/circuit/codex/merkle.circom +++ b/circuit/codex/merkle.circom @@ -50,6 +50,17 @@ template RootFromMerklePath( maxDepth ) { signal input merklePath[ maxDepth ]; signal output recRoot; + // in case of a singleton tree, we receive maskBits = [0,0,0,...,0] + // but what we really need is [1,0,0,0,...,0] + // maybe it's the best to fix that here + // + // this is a bit of hackish, but because we always expect [1,1,...,1,0,0,...,0] anyway, + // we can just set the first entry to 1 and that should fix this issue. + // + signal maskBitsCorrected[ maxDepth + 1]; + maskBitsCorrected[0] <== 1; + for(var i=1; i<=maxDepth; i++) { maskBitsCorrected[i] <== maskBits[i]; } + // the sequence of reconstructed hashes along the path signal aux[ maxDepth+1 ]; aux[0] <== leaf; @@ -96,7 +107,7 @@ template RootFromMerklePath( maxDepth ) { var sum = 0; signal prods[maxDepth]; for(var i=0; i