Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

130 lines
3.5 KiB
Nim
Raw Normal View History

## 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/taskpools
import pkg/questionable/results
import pkg/libp2p/cid
import ../../manifest
import ../../merkletree
import ../../stores
import ../../market
import ../../utils/poseidon2digest
Rework circuit downloader (#882) * Introduces a start method to prover * Moves backend creation into start method * sets up three paths for backend initialization * Extracts backend initialization to backend-factory * Implements loading backend from cli files or previously downloaded local files * Wires up downloading and unzipping * functional implementation * Fixes testprover.nim * Sets up tests for backendfactory * includes libzip-dev * pulls in updated contracts * removes integration cli tests for r1cs, wasm, and zkey file arguments. * Fixes issue where inner-scope values are lost before returning * sets local proof verification for dist-test images * Adds two traces and bumps nim-ethers * Adds separate path for circuit files * Create circuit dir if not exists * fix: make sure requestStorage is mined * fix: correct place to plug confirm * test: fixing contracts tests * Restores gitmodules * restores nim-datastore reference * Sets up downloader exe * sets up tool skeleton * implements getting of circuit hash * Implements downloader tool * sets up test skeleton * Implements test for cirdl * includes testTools in testAll * Cleanup building.md * cleans up previous downloader implementation * cleans up testbackendfactory * moves start of prover into node.nim * Fills in arguments in example command * Initializes backend in prover constructor * Restores tests * Restores tests for cli instructions * Review comments by Dmitriy, part 1 * Quotes path in download instruction. * replaces curl with chronos http session * Moves cirdl build output to 'build' folder. * Fixes chronicles log output * Add cirdl support to the codex Dockerfile Signed-off-by: Slava <20563034+veaceslavdoina@users.noreply.github.com> * Add cirdl support to the docker entrypoint Signed-off-by: Slava <20563034+veaceslavdoina@users.noreply.github.com> * Add cirdl support to the release workflow Signed-off-by: Slava <20563034+veaceslavdoina@users.noreply.github.com> * Disable verify_circuit flag for releases Signed-off-by: Slava <20563034+veaceslavdoina@users.noreply.github.com> * Removes backendFactory placeholder type * wip * Replaces zip library with status-im/zippy library (which supports zip and tar) * Updates cirdl to not change circuitdir folder * Switches from zip to tar.gz * Review comments by Dmitriy * updates codex-contracts-eth * Adds testTools to CI * Adds check for access to config.circuitdir * Update fixture circuit zkey * Update matrix to run tools tests on Windows * Adds 'deps' dependency for cirdl * Adjust docker-entrypoint.sh to use CODEX_CIRCUIT_DIR env var * Review comments by Giuliano --------- Signed-off-by: Slava <20563034+veaceslavdoina@users.noreply.github.com> Co-authored-by: Adam Uhlíř <adam@uhlir.dev> Co-authored-by: Veaceslav Doina <20563034+veaceslavdoina@users.noreply.github.com>
2024-09-23 16:37:17 +02:00
import ../../conf
import ../builder
import ../sampler
import ./backends
import ../types
import pkg/constantine/math/arithmetic
import pkg/constantine/math/io/io_bigints
import pkg/constantine/math/io/io_fields
export backends
logScope:
topics = "codex prover"
type Prover* = ref object
case backendKind: ProverBackendCmd
of ProverBackendCmd.nimgroth16:
groth16Backend*: NimGroth16BackendRef
of ProverBackendCmd.circomcompat:
circomCompatBackend*: CircomCompatBackendRef
nSamples: int
tp: Taskpool
func toJsonDecimal*(big: BigInt[254]): string =
let s = big.toDecimal.strip(leading = true, trailing = false, chars = {'0'})
if s.len == 0: "0" else: s
func toJson*(input: ProofInputs[Poseidon2Hash]): JsonNode =
var input = input
%*{
"dataSetRoot": input.datasetRoot.toBig.toJsonDecimal,
"entropy": input.entropy.toBig.toJsonDecimal,
"nCellsPerSlot": input.nCellsPerSlot,
"nSlotsPerDataSet": input.nSlotsPerDataSet,
"slotIndex": input.slotIndex,
"slotRoot": input.slotRoot.toDecimal,
"slotProof": input.slotProof.mapIt(it.toBig.toJsonDecimal),
"cellData": input.samples.mapIt(it.cellData.mapIt(it.toBig.toJsonDecimal)),
"merklePaths": input.samples.mapIt(it.merklePaths.mapIt(it.toBig.toJsonDecimal)),
}
proc prove*[SomeSampler](
self: Prover,
sampler: SomeSampler,
manifest: Manifest,
challenge: ProofChallenge,
verify = false,
): Future[?!(Groth16Proof, ?bool)] {.async: (raises: [CancelledError]).} =
## Prove a statement using backend.
## Returns a future that resolves to a proof.
logScope:
cid = manifest.treeCid
challenge = challenge
trace "Received proof challenge"
let
proofInput = ?await sampler.getProofInput(challenge, self.nSamples)
# prove slot
2025-06-25 16:55:04 +10:00
trace "generating proof with input", input = proofInput.toJson
case self.backendKind
of ProverBackendCmd.nimgroth16:
let
proof = ?await self.groth16Backend.prove(proofInput)
verified =
if verify:
(?await self.groth16Backend.verify(proof)).some
else:
bool.none
return success (proof.toGroth16Proof, verified)
of ProverBackendCmd.circomcompat:
let
proof = ?await self.circomCompatBackend.prove(proofInput)
verified =
if verify:
(?await self.circomCompatBackend.verify(proof, proofInput)).some
else:
bool.none
return success (proof.toGroth16Proof, verified)
proc new*(
_: type Prover, backend: CircomCompatBackendRef, nSamples: int, tp: Taskpool
): Prover =
Prover(
circomCompatBackend: backend,
backendKind: ProverBackendCmd.circomcompat,
nSamples: nSamples,
tp: tp,
)
proc new*(
_: type Prover, backend: NimGroth16BackendRef, nSamples: int, tp: Taskpool
): Prover =
Prover(
groth16Backend: backend,
backendKind: ProverBackendCmd.nimgroth16,
nSamples: nSamples,
tp: tp,
)