factorize CheckInclusions loop

Signed-off-by: Csaba Kiraly <csaba.kiraly@gmail.com>
This commit is contained in:
Csaba Kiraly 2022-11-17 14:23:31 +01:00
parent d2a72613b0
commit 69fa933f10
No known key found for this signature in database
GPG Key ID: 0FE274EE8C95166E
1 changed files with 24 additions and 12 deletions

View File

@ -20,6 +20,24 @@ template HashCheck(blockSize) {
// is this enough or do we need output? // is this enough or do we need output?
} }
template CheckInclusion(nLevels) {
signal input index;
signal input chunkHash;
signal input treeSiblings[nLevels];
signal input root;
component num2Bits = Num2Bits(nLevels);
num2Bits.in <== index;
component inclusionProof = MerkleTreeInclusionProof(nLevels);
inclusionProof.leaf <== chunkHash;
for (var j = 0; j < nLevels; j++) {
inclusionProof.siblings[j] <== treeSiblings[j];
inclusionProof.pathIndices[j] <== num2Bits.out[j];
}
root === inclusionProof.root;
}
template StorageProver(blockSize, qLen, nLevels) { template StorageProver(blockSize, qLen, nLevels) {
// blockSize: size of block in bits (sha256), or in symbols (Poseidon) // blockSize: size of block in bits (sha256), or in symbols (Poseidon)
// qLen: query length, i.e. number if indices to be proven // qLen: query length, i.e. number if indices to be proven
@ -45,20 +63,14 @@ template StorageProver(blockSize, qLen, nLevels) {
// - convert indices to treePathIndices // - convert indices to treePathIndices
// - check chunkHash and treeSiblings according to treePathIndices against root // - check chunkHash and treeSiblings according to treePathIndices against root
component num2Bits[qLen]; component checkInclusion[qLen];
component inclusionProofs[qLen];
for (var i = 0; i < qLen; i++) { for (var i = 0; i < qLen; i++) {
num2Bits[i] = Num2Bits(nLevels); checkInclusion[i] = CheckInclusion(nLevels);
num2Bits[i].in <== indices[i]; checkInclusion[i].index <== indices[i];
checkInclusion[i].treeSiblings <== treeSiblings[i];
inclusionProofs[i] = MerkleTreeInclusionProof(nLevels); checkInclusion[i].chunkHash <== chunkHashes[i];
inclusionProofs[i].leaf <== chunkHashes[i]; checkInclusion[i].root <== root;
for (var j = 0; j < nLevels; j++) {
inclusionProofs[i].siblings[j] <== treeSiblings[i][j];
inclusionProofs[i].pathIndices[j] <== num2Bits[i].out[j];
}
root === inclusionProofs[i].root;
} }
} }