2024-02-09 21:40:30 +00:00
|
|
|
## 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.
|
|
|
|
##
|
|
|
|
|
|
|
|
import pkg/chronos
|
|
|
|
import pkg/chronicles
|
|
|
|
import pkg/circomcompat
|
|
|
|
import pkg/poseidon2
|
|
|
|
import pkg/questionable/results
|
|
|
|
|
|
|
|
import pkg/libp2p/cid
|
|
|
|
|
|
|
|
import ../../manifest
|
|
|
|
import ../../merkletree
|
|
|
|
import ../../stores
|
|
|
|
import ../../market
|
|
|
|
import ../../utils/poseidon2digest
|
|
|
|
|
|
|
|
import ../builder
|
|
|
|
import ../sampler
|
|
|
|
|
|
|
|
import ./backends
|
|
|
|
import ../types
|
|
|
|
|
|
|
|
export backends
|
|
|
|
|
2024-02-19 18:58:39 +00:00
|
|
|
logScope:
|
|
|
|
topics = "codex prover"
|
|
|
|
|
2024-02-09 21:40:30 +00:00
|
|
|
type
|
|
|
|
AnyBackend* = CircomCompat
|
2024-02-19 18:58:39 +00:00
|
|
|
AnyProof* = CircomProof
|
|
|
|
|
2024-02-09 21:40:30 +00:00
|
|
|
AnySampler* = Poseidon2Sampler
|
2024-02-19 18:58:39 +00:00
|
|
|
AnyBuilder* = Poseidon2Builder
|
2024-02-09 21:40:30 +00:00
|
|
|
|
2024-02-19 18:58:39 +00:00
|
|
|
AnyProofInputs* = ProofInputs[Poseidon2Hash]
|
2024-02-09 21:40:30 +00:00
|
|
|
Prover* = ref object of RootObj
|
|
|
|
backend: AnyBackend
|
|
|
|
store: BlockStore
|
2024-02-19 18:58:39 +00:00
|
|
|
nSamples: int
|
2024-02-09 21:40:30 +00:00
|
|
|
|
|
|
|
proc prove*(
|
|
|
|
self: Prover,
|
|
|
|
slotIdx: int,
|
|
|
|
manifest: Manifest,
|
2024-05-16 18:27:28 +00:00
|
|
|
challenge: ProofChallenge
|
|
|
|
): Future[?!(AnyProofInputs, AnyProof)] {.async.} =
|
2024-02-09 21:40:30 +00:00
|
|
|
## Prove a statement using backend.
|
|
|
|
## Returns a future that resolves to a proof.
|
|
|
|
|
|
|
|
logScope:
|
|
|
|
cid = manifest.treeCid
|
|
|
|
slot = slotIdx
|
|
|
|
challenge = challenge
|
|
|
|
|
|
|
|
trace "Received proof challenge"
|
|
|
|
|
|
|
|
without builder =? AnyBuilder.new(self.store, manifest), err:
|
|
|
|
error "Unable to create slots builder", err = err.msg
|
|
|
|
return failure(err)
|
|
|
|
|
|
|
|
without sampler =? AnySampler.new(slotIdx, self.store, builder), err:
|
|
|
|
error "Unable to create data sampler", err = err.msg
|
|
|
|
return failure(err)
|
|
|
|
|
2024-02-19 18:58:39 +00:00
|
|
|
without proofInput =? await sampler.getProofInput(challenge, self.nSamples), err:
|
2024-02-09 21:40:30 +00:00
|
|
|
error "Unable to get proof input for slot", err = err.msg
|
|
|
|
return failure(err)
|
|
|
|
|
|
|
|
# prove slot
|
|
|
|
without proof =? self.backend.prove(proofInput), err:
|
|
|
|
error "Unable to prove slot", err = err.msg
|
|
|
|
return failure(err)
|
|
|
|
|
2024-02-19 18:58:39 +00:00
|
|
|
success (proofInput, proof)
|
2024-02-09 21:40:30 +00:00
|
|
|
|
|
|
|
proc verify*(
|
|
|
|
self: Prover,
|
|
|
|
proof: AnyProof,
|
2024-05-16 18:27:28 +00:00
|
|
|
inputs: AnyProofInputs
|
|
|
|
): Future[?!bool] {.async.} =
|
2024-02-09 21:40:30 +00:00
|
|
|
## Prove a statement using backend.
|
|
|
|
## Returns a future that resolves to a proof.
|
|
|
|
|
2024-02-19 18:58:39 +00:00
|
|
|
self.backend.verify(proof, inputs)
|
2024-02-09 21:40:30 +00:00
|
|
|
|
|
|
|
proc new*(
|
|
|
|
_: type Prover,
|
|
|
|
store: BlockStore,
|
2024-02-19 18:58:39 +00:00
|
|
|
backend: AnyBackend,
|
2024-05-16 18:27:28 +00:00
|
|
|
nSamples: int
|
|
|
|
): Prover =
|
2024-02-09 21:40:30 +00:00
|
|
|
|
|
|
|
Prover(
|
|
|
|
backend: backend,
|
2024-02-19 18:58:39 +00:00
|
|
|
store: store,
|
|
|
|
nSamples: nSamples)
|