Implements loading backend from cli files or previously downloaded local files

This commit is contained in:
benbierens 2024-03-01 14:25:48 +01:00
parent 83e1347038
commit c67c2e7cd7
No known key found for this signature in database
GPG Key ID: 877D2C2E09A22F3A
2 changed files with 45 additions and 33 deletions

View File

@ -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)

View File

@ -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)