nim-codex/codex/slots/proofs/backends/asynccircoms.nim

91 lines
2.4 KiB
Nim
Raw Normal View History

2024-05-17 00:59:00 +03:00
import std/options
import pkg/taskpools
import pkg/chronicles
import pkg/chronos
import pkg/chronos/threadsync
import pkg/questionable/results
2024-05-16 21:43:35 +03:00
import ../../types
2024-05-17 00:51:57 +03:00
import ../../../utils/asyncthreads
2024-05-16 21:43:35 +03:00
2024-05-16 21:41:24 +03:00
import ./circomcompat
logScope:
topics = "codex asyncprover"
2024-05-20 17:15:51 +03:00
type AsyncCircomCompat* = object
2024-05-20 17:33:41 +03:00
circom*: CircomCompat
2024-05-20 17:15:51 +03:00
tp*: Taskpool
2024-05-17 00:30:11 +03:00
proc proveTask[H](
2024-05-20 18:13:34 +03:00
circom: CircomCompat, data: ProofInputs[H], results: SignalQueuePtr[?!CircomProof]
2024-05-20 17:15:51 +03:00
) =
2024-05-20 17:45:13 +03:00
let proof = circom.prove(data)
2024-05-20 17:31:34 +03:00
2024-05-20 17:54:06 +03:00
if (let sent = results.send(proof); sent.isErr()):
2024-05-20 17:31:34 +03:00
error "Error sending proof results", msg = sent.error().msg
2024-05-16 21:49:31 +03:00
proc prove*[H](
2024-05-16 21:49:31 +03:00
self: AsyncCircomCompat, input: ProofInputs[H]
2024-05-16 21:41:24 +03:00
): Future[?!CircomProof] {.async.} =
## Generates proof using circom-compat asynchronously
##
2024-05-20 20:54:52 +03:00
without queue =? newSignalQueue[?!CircomProof](maxItems = 1), err:
2024-05-17 00:30:11 +03:00
return failure(err)
2024-05-20 19:16:33 +03:00
proc spawnTask() =
2024-05-20 17:33:41 +03:00
self.tp.spawn proveTask(self.circom, input, queue)
2024-05-20 18:13:34 +03:00
2024-05-20 17:15:51 +03:00
spawnTask()
2024-05-16 21:41:24 +03:00
2024-05-20 18:04:05 +03:00
let taskRes = await queue.recvAsync()
if (let res = queue.release(); res.isErr):
error "Error releasing proof queue ", msg = res.error().msg
without proofRes =? taskRes, err:
2024-05-18 18:12:50 +03:00
return failure(err)
2024-05-20 18:04:05 +03:00
without proof =? proofRes, err:
2024-05-20 17:15:51 +03:00
return failure(err)
2024-05-17 00:39:07 +03:00
2024-05-20 17:15:51 +03:00
success(proof)
2024-05-18 18:12:50 +03:00
2024-05-20 18:57:42 +03:00
proc verifyTask[H](
circom: CircomCompat,
proof: CircomProof,
inputs: ProofInputs[H],
results: SignalQueuePtr[?!bool],
) =
let verified = circom.verify(proof, inputs)
if (let sent = results.send(verified); sent.isErr()):
error "Error sending verification results", msg = sent.error().msg
2024-05-16 21:41:24 +03:00
proc verify*[H](
2024-05-16 21:49:31 +03:00
self: AsyncCircomCompat, proof: CircomProof, inputs: ProofInputs[H]
2024-05-16 21:41:24 +03:00
): Future[?!bool] {.async.} =
## Verify a proof using a ctx
2024-05-20 18:57:42 +03:00
##
without queue =? newSignalQueue[?!bool](maxItems = 1), err:
return failure(err)
2024-05-20 19:16:33 +03:00
proc spawnTask() =
2024-05-20 18:57:42 +03:00
self.tp.spawn verifyTask(self.circom, proof, inputs, queue)
spawnTask()
let taskRes = await queue.recvAsync()
if (let res = queue.release(); res.isErr):
error "Error releasing proof queue ", msg = res.error().msg
without verifyRes =? taskRes, err:
return failure(err)
2024-05-20 18:58:29 +03:00
without verified =? verifyRes, err:
2024-05-20 18:57:42 +03:00
return failure(err)
success(verified)
2024-05-20 19:04:58 +03:00
proc init*(_: type AsyncCircomCompat, params: CircomCompatParams, tp: Taskpool): AsyncCircomCompat =
2024-05-16 21:46:34 +03:00
## Create a new async circom
##
2024-05-20 17:31:34 +03:00
let circom = CircomCompat.init(params)
2024-05-20 19:16:33 +03:00
AsyncCircomCompat(circom: circom, tp: tp)