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

61 lines
1.5 KiB
Nim
Raw Normal View History

2024-05-17 00:59:00 +03:00
import std/options
import pkg/taskpools
import pkg/taskpools/flowvars
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
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 18:32:14 +03:00
without queue =? newSignalQueue[?!CircomProof](maxItems=1), err:
2024-05-17 00:30:11 +03:00
return failure(err)
2024-05-20 17:54:06 +03:00
template 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-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
##
discard
2024-05-16 21:46:34 +03:00
2024-05-16 21:49:31 +03:00
proc init*(_: type AsyncCircomCompat, params: CircomCompatParams): 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)
AsyncCircomCompat(circom)