From c67c2e7cd737e3f9e56f04f176d50c5e257d69a5 Mon Sep 17 00:00:00 2001 From: benbierens Date: Fri, 1 Mar 2024 14:25:48 +0100 Subject: [PATCH] Implements loading backend from cli files or previously downloaded local files --- codex/codex.nim | 22 ----------- codex/slots/proofs/backendfactory.nim | 56 +++++++++++++++++++++------ 2 files changed, 45 insertions(+), 33 deletions(-) diff --git a/codex/codex.nim b/codex/codex.nim index f0b588cd..eb2b26e4 100644 --- a/codex/codex.nim +++ b/codex/codex.nim @@ -269,28 +269,6 @@ proc new*( engine = BlockExcEngine.new(repoStore, wallet, network, blockDiscovery, peerStore, pendingBlocks) store = NetworkStore.new(engine, repoStore) prover = if config.prover: - if not fileAccessible($config.circomR1cs, {AccessFlags.Read}) and - endsWith($config.circomR1cs, ".r1cs"): - error "Circom R1CS file not accessible" - raise (ref Defect)( - msg: "r1cs file not readable, doesn't exist or wrong extension (.r1cs)") - - if not fileAccessible($config.circomWasm, {AccessFlags.Read}) and - endsWith($config.circomWasm, ".wasm"): - error "Circom wasm file not accessible" - raise (ref Defect)( - msg: "wasm file not readable, doesn't exist or wrong extension (.wasm)") - - let zkey = if not config.circomNoZkey: - if not fileAccessible($config.circomZkey, {AccessFlags.Read}) and - endsWith($config.circomZkey, ".zkey"): - error "Circom zkey file not accessible" - raise (ref Defect)( - msg: "zkey file not readable, doesn't exist or wrong extension (.zkey)") - - $config.circomZkey - else: "" - some Prover.new( store, config.numProofSamples) diff --git a/codex/slots/proofs/backendfactory.nim b/codex/slots/proofs/backendfactory.nim index f08d7867..dd048e85 100644 --- a/codex/slots/proofs/backendfactory.nim +++ b/codex/slots/proofs/backendfactory.nim @@ -1,32 +1,66 @@ +import os import pkg/chronos import pkg/questionable import pkg/confutils/defs +import pkg/stew/io2 import ../../conf import ./backends +proc initializeCircomBackend( + r1csFile: string, + wasmFile: string, + zKeyFile: string +): AnyBackend = + CircomCompat.init(r1csFile, wasmFile, zKeyFile) + proc initializeFromConfig( config: CodexConf): ?!AnyBackend = + if not fileAccessible($config.circomR1cs, {AccessFlags.Read}) and + endsWith($config.circomR1cs, ".r1cs"): + return failure("Circom R1CS file not accessible") - # check provided files exist - # initialize backend with files - # or failure + if not fileAccessible($config.circomWasm, {AccessFlags.Read}) and + endsWith($config.circomWasm, ".wasm"): + return failure("Circom wasm file not accessible") - success(CircomCompat.init($config.circomR1cs, $config.circomWasm, $config.circomZkey)) + if not fileAccessible($config.circomZkey, {AccessFlags.Read}) and + endsWith($config.circomZkey, ".zkey"): + return failure("Circom zkey file not accessible") -proc initializeFromCeremonyFiles(): ?!AnyBackend = + success(initializeCircomBackend( + $config.circomR1cs, + $config.circomWasm, + $config.circomZkey)) - # initialize from previously-downloaded files if they exist - echo "todo" - failure("todo") +proc r1csFilePath(config: CodexConf): string = + config.dataDir / "circuit.r1cs" + +proc wasmFilePath(config: CodexConf): string = + config.dataDir / "circuit.wasm" + +proc zkeyFilePath(config: CodexConf): string = + config.dataDir / "circuit.zkey" + +proc initializeFromCeremonyFiles(config: CodexConf): ?!AnyBackend = + if fileExists(config.r1csFilePath) and + fileExists(config.wasmFilePath) and + fileExists(config.zkeyFilePath): + return success(initializeCircomBackend( + config.r1csFilePath, + config.wasmFilePath, + config.zkeyFilePath)) + + failure("Ceremony files not found") proc initializeFromCeremonyUrl( + config: CodexConf, proofCeremonyUrl: ?string): Future[?!AnyBackend] {.async.} = # download the ceremony url # unzip it - without backend =? initializeFromCeremonyFiles(), err: + without backend =? initializeFromCeremonyFiles(config), err: return failure(err) return success(backend) @@ -36,9 +70,9 @@ proc initializeBackend*( without backend =? initializeFromConfig(config), cliErr: info "Could not initialize prover backend from CLI options...", msg = cliErr.msg - without backend =? initializeFromCeremonyFiles(), localErr: + without backend =? initializeFromCeremonyFiles(config), localErr: info "Could not initialize prover backend from local files...", msg = localErr.msg - without backend =? (await initializeFromCeremonyUrl(proofCeremonyUrl)), urlErr: + without backend =? (await initializeFromCeremonyUrl(config, proofCeremonyUrl)), urlErr: warn "Could not initialize prover backend from ceremony url...", msg = urlErr.msg return failure(urlErr) return success(backend)