From 4fd07eb1512e960bf2ae40c2ee19e6ee8db1629e Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Mon, 22 Jan 2024 14:50:17 -0600 Subject: [PATCH] moving prover around --- codex/proofs/backend.nim | 57 ---------- codex/proofs/backends/circomcompat.nim | 45 -------- codex/slots/proofs/backend.nim | 44 ++++++++ codex/slots/proofs/backends/circomcompat.nim | 106 +++++++++++++++++++ codex/{ => slots}/proofs/prover.nim | 0 5 files changed, 150 insertions(+), 102 deletions(-) delete mode 100644 codex/proofs/backend.nim delete mode 100644 codex/proofs/backends/circomcompat.nim create mode 100644 codex/slots/proofs/backend.nim create mode 100644 codex/slots/proofs/backends/circomcompat.nim rename codex/{ => slots}/proofs/prover.nim (100%) diff --git a/codex/proofs/backend.nim b/codex/proofs/backend.nim deleted file mode 100644 index 1edc18c5..00000000 --- a/codex/proofs/backend.nim +++ /dev/null @@ -1,57 +0,0 @@ -## Nim-Codex -## Copyright (c) 2022 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. - -{.push raises: [].} - -import ../stores - -type - ProverBackend* = ref object of RootObj - maxDepth : int ## maximum depth of the slot Merkle tree (so max `2^maxDepth` cells in a slot) - maxLog2NSlots : int ## maximum depth of the dataset-level Merkle tree (so max 2^8 slots per dataset) - blockTreeDepth : int ## depth of the "network block tree" (= log2(64k / 2k)) - nFieldElemsPerCell : int ## number of field elements per cell - nSamples: : int ## number of samples - - ProofBackend* = ref object of Backend - VerifyBackend* = ref object of Backend - -method release*(self: ProverBackend) {.base.} = - ## release the backend - ## - - raiseAssert("not implemented!") - -method prove*( - self: ProofBackend, - entropy: seq[byte], ## public input - dataSetRoot: seq[byte]; ## public input - slotIndex: int, ## must be public, otherwise we could prove a different slot - slotRoot: seq[byte] ## can be private input - nCellsPerSlot: int, ## can be private input (Merkle tree is safe) - nSlotsPerDataSet: int, ## can be private input (Merkle tree is safe) - slotProof: seq[byte], ## path from the slot root the the dataset root (private input) - cellData: seq[seq[byte]], ## data for the cells (private input) - merklePaths[nSamples][maxDepth]) ## Merkle paths for the cells (private input) - : Future[?!seq[byte]] {.async.} = - ## encode buffers using a backend - ## - - raiseAssert("not implemented!") - -method verify*( - self: VerifyBackend, - buffers, - parity, - recovered: var openArray[seq[byte]] -): Result[void, cstring] {.base.} = - ## decode buffers using a backend - ## - - raiseAssert("not implemented!") diff --git a/codex/proofs/backends/circomcompat.nim b/codex/proofs/backends/circomcompat.nim deleted file mode 100644 index 57043926..00000000 --- a/codex/proofs/backends/circomcompat.nim +++ /dev/null @@ -1,45 +0,0 @@ -## Nim-Codex -## Copyright (c) 2022 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. - -{.push raises: [].} - -import ../stores - -type - ProverBackend* = ref object of RootObj - - ProofBackend* = ref object of Backend - VerifyBackend* = ref object of Backend - -method release*(self: ProverBackend) {.base.} = - ## release the backend - ## - - raiseAssert("not implemented!") - -method prove*( - self: ProofBackend, - indicies, - payload: var openArray[seq[byte]] -): Result[void, cstring] {.base.} = - ## encode buffers using a backend - ## - - raiseAssert("not implemented!") - -method verify*( - self: VerifyBackend, - buffers, - parity, - recovered: var openArray[seq[byte]] -): Result[void, cstring] {.base.} = - ## decode buffers using a backend - ## - - raiseAssert("not implemented!") diff --git a/codex/slots/proofs/backend.nim b/codex/slots/proofs/backend.nim new file mode 100644 index 00000000..7b070146 --- /dev/null +++ b/codex/slots/proofs/backend.nim @@ -0,0 +1,44 @@ +## Nim-Codex +## Copyright (c) 2022 Status Research & Development GmbH +## Licensed under either of +## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +## * MIT license ([LICENSE-MIT](LICENSE-MIT)) +## at your option. +## This file may not be copied, modified, or distributed except according to +## those terms. + +{.push raises: [].} + +import pkg/chronos +import pkg/questionable/results + +import ../../stores +import ../types + +type + ProverBackend*[H, P] = ref object of RootObj + + ProofBackend*[H, P] = ref object of ProverBackend[H, P] + VerifyBackend*[H, P] = ref object of ProverBackend[H, P] + +method release*[H, P](self: ProverBackend[H, P]) {.base.} = + ## release the backend + ## + + raiseAssert("not implemented!") + +method prove*[H, P]( + self: ProofBackend[H, P], + input: ProofInput[H, P]): Future[?!seq[byte]] {.base, async.} = + ## prove the input using a backend + ## + + raiseAssert("not implemented!") + +method verify*[H, P]( + self: VerifyBackend[H, P], + proof: sink seq[seq[byte]]): Future[?!bool] {.base, async.} = + ## verify the proof using a backend + ## + + raiseAssert("not implemented!") diff --git a/codex/slots/proofs/backends/circomcompat.nim b/codex/slots/proofs/backends/circomcompat.nim new file mode 100644 index 00000000..e1734411 --- /dev/null +++ b/codex/slots/proofs/backends/circomcompat.nim @@ -0,0 +1,106 @@ +## Nim-Codex +## Copyright (c) 2024 Status Research & Development GmbH +## Licensed under either of +## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +## * MIT license ([LICENSE-MIT](LICENSE-MIT)) +## at your option. +## This file may not be copied, modified, or distributed except according to +## those terms. + +{.push raises: [].} + +import pkg/chronos +import pkg/questionable/results +import pkg/circomcompat + +import ../../../stores +import ../../types + +const + DefaultMaxDepth = 32 ## maximum depth of the slot Merkle tree (so max `2^maxDepth` cells in a slot) + DefaultMaxLog2NSlots = 8 ## maximum depth of the dataset-level Merkle tree (so max 2^8 slots per dataset) + DefaultBlockTreeDepth = 5 ## depth of the "network block tree" (= log2(64k / 2k)) + DefaultNFieldElemsPerCell = 67 ## number of field elements per cell + DefaultNSamples = 5 ## number of samples + +type + CircomCompat*[H, P] = ref object of RootObj + maxDepth : int ## maximum depth of the slot Merkle tree (so max `2^maxDepth` cells in a slot) + maxLog2NSlots : int ## maximum depth of the dataset-level Merkle tree (so max 2^8 slots per dataset) + blockTreeDepth : int ## depth of the "network block tree" (= log2(64k / 2k)) + nFieldElemsPerCell : int ## number of field elements per cell + nSamples : int ## number of samples + backend : ptr CircomCompatCtx + +func maxDepth*[H, P](self: CircomCompat[H, P]) = + ## maximum depth of the slot Merkle tree (so max `2^maxDepth` cells in a slot) + ## + + self.maxDepth + +func maxLog2NSlots*[H, P](self: CircomCompat[H, P]) = + ## maximum depth of the dataset-level Merkle tree (so max 2^8 slots per dataset) + ## + + self.maxLog2NSlots + +func blockTreeDepth*[H, P](self: CircomCompat[H, P]) = + ## depth of the "network block tree" (= log2(64k / 2k)) + ## + + self.blockTreeDepth + +func nFieldElemsPerCell*[H, P](self: CircomCompat[H, P]) = + ## number of field elements per cell + ## + + self.nFieldElemsPerCell + +func nSamples*[H, P](self: CircomCompat[H, P]) = + ## number of samples + ## + + self.nSamples + +method release*[H, P](self: CircomCompat[H, P]) {.base.} = + ## release the backend + ## + + release_circom_compat(self.backend.addr) + +method prove*[H, P]( + self: CircomCompat[H, P], + input: ProofInput[H, P]): Future[?!seq[byte]] {.base, async.} = + ## encode buffers using a backend + ## + + raiseAssert("not implemented!") + +proc new*[H, P]( + _: type CircomCompat[H, P], + r1csPath: string, + wasmPath: string, + zKeyPath: string, + maxDepth = DefaultMaxDepth, + maxLog2NSlots = DefaultMaxLog2NSlots, + blockTreeDepth = DefaultBlockTreeDepth, + nFieldElemsPerCell = DefaultNFieldElemsPerCell, + nSamples = DefaultNSamples): CircomCompat[H, P] = + ## Create a new backend + ## + + var backend: ptr CircomCompatCtx + if initCircomCompat( + r1csPath.cstring, + wasmPath.cstring, + zKeyPath.cstring, + addr backend) != ERR_OK or backend == nil: + raiseAssert("failed to initialize CircomCompat backend") + + CircomCompat[H, P]( + maxDepth: maxDepth, + maxLog2NSlots: maxLog2NSlots, + blockTreeDepth: blockTreeDepth, + nFieldElemsPerCell: nFieldElemsPerCell, + nSamples: nSamples, + backend: backend) diff --git a/codex/proofs/prover.nim b/codex/slots/proofs/prover.nim similarity index 100% rename from codex/proofs/prover.nim rename to codex/slots/proofs/prover.nim