functional implementation

This commit is contained in:
benbierens 2024-03-04 09:43:35 +01:00
parent 7934d415cd
commit 983da3c24f
No known key found for this signature in database
GPG Key ID: 877D2C2E09A22F3A
1 changed files with 37 additions and 15 deletions

View File

@ -2,6 +2,7 @@ import os
import httpclient import httpclient
import zip/zipfiles import zip/zipfiles
import pkg/chronos import pkg/chronos
import pkg/chronicles
import pkg/questionable import pkg/questionable
import pkg/confutils/defs import pkg/confutils/defs
import pkg/stew/io2 import pkg/stew/io2
@ -30,19 +31,20 @@ proc initializeFromConfig(
endsWith($config.circomZkey, ".zkey"): endsWith($config.circomZkey, ".zkey"):
return failure("Circom zkey file not accessible") return failure("Circom zkey file not accessible")
trace "Initialized prover backend from cli config"
success(initializeCircomBackend( success(initializeCircomBackend(
$config.circomR1cs, $config.circomR1cs,
$config.circomWasm, $config.circomWasm,
$config.circomZkey)) $config.circomZkey))
proc r1csFilePath(config: CodexConf): string = proc r1csFilePath(config: CodexConf): string =
config.dataDir / "circuit.r1cs" config.dataDir / "proof_main.r1cs"
proc wasmFilePath(config: CodexConf): string = proc wasmFilePath(config: CodexConf): string =
config.dataDir / "circuit.wasm" config.dataDir / "proof_main.wasm"
proc zkeyFilePath(config: CodexConf): string = proc zkeyFilePath(config: CodexConf): string =
config.dataDir / "circuit.zkey" config.dataDir / "proof_main.zkey"
proc zipFilePath(config: CodexConf): string = proc zipFilePath(config: CodexConf): string =
config.dataDir / "circuit.zip" config.dataDir / "circuit.zip"
@ -51,6 +53,7 @@ proc initializeFromCeremonyFiles(config: CodexConf): ?!AnyBackend =
if fileExists(config.r1csFilePath) and if fileExists(config.r1csFilePath) and
fileExists(config.wasmFilePath) and fileExists(config.wasmFilePath) and
fileExists(config.zkeyFilePath): fileExists(config.zkeyFilePath):
trace "Initialized prover backend from local files"
return success(initializeCircomBackend( return success(initializeCircomBackend(
config.r1csFilePath, config.r1csFilePath,
config.wasmFilePath, config.wasmFilePath,
@ -58,28 +61,47 @@ proc initializeFromCeremonyFiles(config: CodexConf): ?!AnyBackend =
failure("Ceremony files not found") failure("Ceremony files not found")
proc downloadCeremonyUrl( proc downloadCeremony(
config: CodexConf, config: CodexConf,
proofCeremonyUrl: string ceremonyHash: string
) = ): ?!void =
var client = newHttpClient() # TODO:
client.downloadFile( # In the future, the zip file will be stored in the Codex network
"https://circuit.codex.storage/proving-key/" & proofCeremonyUrl, config.zipFilePath) # instead of a url + ceremonyHash, we'll get a CID from the marketplace contract.
echo "OVERRIDE!"
let hash = "1f512a1c6a089eff7eb22b810438c34fc59d4b0e7935dbc77ec77255d39ec094"
let url = "https://circuit.codex.storage/proving-key/" & hash # was ceremonyHash
trace "Downloading ceremony file", url, filepath = config.zipFilePath
try:
# Nim's default webclient does not support SSL on all platforms.
# Not without shipping additional binaries and cert-files... :(
# So we're using curl for now.
var rc = execShellCmd("curl -o " & config.zipFilePath & " " & url)
if not rc == 0:
return failure("Download failed with return code: " & $rc)
except Exception as exc:
return failure(exc.msg)
trace "Download completed."
success()
proc unzipCeremonyFile( proc unzipCeremonyFile(
config: CodexConf): ?!void = config: CodexConf): ?!void =
trace "Unzipping..."
var z: ZipArchive var z: ZipArchive
if not z.open(config.zipFilePath): if not z.open(config.zipFilePath):
return failure("Unable to open zip file: " & config.zipFilePath) return failure("Unable to open zip file: " & config.zipFilePath)
z.extractAll($config.dataDir) z.extractAll($config.dataDir)
success() success()
proc initializeFromCeremonyUrl( proc initializeFromCeremonyHash(
config: CodexConf, config: CodexConf,
proofCeremonyUrl: ?string): Future[?!AnyBackend] {.async.} = ceremonyHash: ?string): Future[?!AnyBackend] {.async.} =
if url =? proofCeremonyUrl: if hash =? ceremonyHash:
downloadCeremonyUrl(config, url) if dlErr =? downloadCeremony(config, hash).errorOption:
return failure(dlErr)
if err =? unzipCeremonyFile(config).errorOption: if err =? unzipCeremonyFile(config).errorOption:
return failure(err) return failure(err)
without backend =? initializeFromCeremonyFiles(config), err: without backend =? initializeFromCeremonyFiles(config), err:
@ -90,13 +112,13 @@ proc initializeFromCeremonyUrl(
proc initializeBackend*( proc initializeBackend*(
config: CodexConf, config: CodexConf,
proofCeremonyUrl: ?string): Future[?!AnyBackend] {.async.} = ceremonyHash: ?string): Future[?!AnyBackend] {.async.} =
without backend =? initializeFromConfig(config), cliErr: without backend =? initializeFromConfig(config), cliErr:
info "Could not initialize prover backend from CLI options...", msg = cliErr.msg info "Could not initialize prover backend from CLI options...", msg = cliErr.msg
without backend =? initializeFromCeremonyFiles(config), localErr: without backend =? initializeFromCeremonyFiles(config), localErr:
info "Could not initialize prover backend from local files...", msg = localErr.msg info "Could not initialize prover backend from local files...", msg = localErr.msg
without backend =? (await initializeFromCeremonyUrl(config, proofCeremonyUrl)), urlErr: without backend =? (await initializeFromCeremonyHash(config, ceremonyHash)), urlErr:
warn "Could not initialize prover backend from ceremony url...", msg = urlErr.msg warn "Could not initialize prover backend from ceremony url...", msg = urlErr.msg
return failure(urlErr) return failure(urlErr)
return success(backend) return success(backend)