more expers

This commit is contained in:
Jaremy Creechley 2024-05-23 21:39:07 +03:00
parent 02542076f2
commit d774d588c4
No known key found for this signature in database
GPG Key ID: 4E66FB67B21D3300
5 changed files with 66 additions and 25 deletions

View File

@ -1,4 +1,5 @@
import std/options
import std/hashes
import pkg/taskpools
import pkg/chronicles
@ -6,6 +7,8 @@ import pkg/chronos
import pkg/chronos/threadsync
import pkg/questionable/results
import pkg/codex/merkletree
import ../../types
import ../../../utils/asyncthreads
@ -15,16 +18,46 @@ logScope:
topics = "codex asyncprover"
type AsyncCircomCompat* = object
params*: CircomCompatParams
circom*: CircomCompat
tp*: Taskpool
proc proveTask[H](
circom: CircomCompat, data: ProofInputs[H], results: SignalQueuePtr[?!CircomProof]
) =
let proof = circom.prove(data)
var localCircom {.threadvar.}: Option[CircomCompat]
if (let sent = results.send(proof); sent.isErr()):
proc proveTask(
params: CircomCompatParams, data: ProofInputs[Poseidon2Hash], results: SignalQueuePtr[?!CircomProof]
) =
var data = data.deepCopy()
var params = params.deepCopy()
try:
echo "TASK: task: "
echo "TASK: task: params: ", params.r1csPath.cstring.pointer.repr
echo "TASK: task: params: ", params
echo "TASK: task: ", data.hash
if localCircom.isNone:
localCircom = some CircomCompat.init(params)
# echo "TASK: task: ", data
let proof = localCircom.get().prove(data)
# echo "TASK: task: proof: ", proof.get.hash
echo "TASK: task: proof: ", proof
echo "TASK: task: params POST: ", params
let fake = CircomProof.failure("failed")
if (let sent = results.send(fake); sent.isErr()):
error "Error sending proof results", msg = sent.error().msg
except Exception:
echo "PROVER DIED"
except Defect:
echo "PROVER DIED"
except:
echo "PROVER DIED"
proc spawnProveTask(
tp: TaskPool,
params: CircomCompatParams, input: ProofInputs[Poseidon2Hash], results: SignalQueuePtr[?!CircomProof]
) =
tp.spawn proveTask(params, input, results)
proc prove*[H](
self: AsyncCircomCompat, input: ProofInputs[H]
@ -34,12 +67,11 @@ proc prove*[H](
without queue =? newSignalQueue[?!CircomProof](maxItems = 1), qerr:
return failure(qerr)
proc spawnTask() =
self.tp.spawn proveTask(self.circom, input, queue)
spawnTask()
echo "TASK: task spawn: params: ", self.params.r1csPath.cstring.pointer.repr
self.tp.spawnProveTask(self.params, input, queue)
let taskRes = await queue.recvAsync()
if (let res = queue.release(); res.isErr):
error "Error releasing proof queue ", msg = res.error().msg
without proofRes =? taskRes, perr:
@ -57,6 +89,7 @@ proc verifyTask[H](
) =
let verified = circom.verify(proof, inputs)
echo "VERIFY: task: result: ", verified
if (let sent = results.send(verified); sent.isErr()):
error "Error sending verification results", msg = sent.error().msg
@ -86,5 +119,5 @@ proc verify*[H](
proc init*(_: type AsyncCircomCompat, params: CircomCompatParams, tp: Taskpool): AsyncCircomCompat =
## Create a new async circom
##
let circom = CircomCompat.init(params)
AsyncCircomCompat(circom: circom, tp: tp)
# let circom = CircomCompat.init(params)
AsyncCircomCompat(params: params, tp: Taskpool.new(2))

View File

@ -49,8 +49,8 @@ proc release*(self: CircomCompat) =
if not isNil(self.backendCfg):
self.backendCfg.unsafeAddr.releaseCfg()
if not isNil(self.vkp):
self.vkp.unsafeAddr.release_key()
# if not isNil(self.vkp):
# self.vkp.unsafeAddr.release_key()
proc prove*[H](
self: CircomCompat,
@ -148,6 +148,7 @@ proc prove*[H](
data.len.uint) != ERR_OK:
return failure("Failed to push cell data")
var
proofPtr: ptr Proof = nil

View File

@ -5,6 +5,8 @@ import pkg/chronos
import pkg/chronos/threadsync
import pkg/questionable/results
import ../errors
const
CompletionRetryDelay* = 10.millis
CompletionTimeout* = 1.seconds
@ -43,7 +45,7 @@ proc send*[T](queue: SignalQueuePtr[T], msg: T): ?!void {.raises: [].} =
except Exception as exc:
return failure(exc.msg)
without wasSent =? queue[].signal.fireSync(InfiniteDuration).mapFailure, sigErr:
without wasSent =? queue[].signal.fireSync(InfiniteDuration).mapFailure(), sigErr:
return failure(sigErr)
if wasSent:
return ok()

View File

@ -68,30 +68,35 @@ suite "Test Prover":
challenge = 1234567.toF.toBytes.toArray32
(inputs, proof) = (await prover.prove(1, verifiable, challenge)).tryGet
check:
(await prover.verify(proof, inputs)).tryGet == true
echo "TEST PROOF: state: ", params
# check:
# (await prover.verify(proof, inputs)).tryGet == true
test "Should sample and prove many slot":
let
r1cs = "tests/circuits/fixtures/proof_main.r1cs"
wasm = "tests/circuits/fixtures/proof_main.wasm"
taskpool = Taskpool.new(num_threads = 6)
taskpool = Taskpool.new(num_threads = 8)
params = CircomCompatParams.init(r1cs, wasm)
circomBackend = AsyncCircomCompat.init(params, taskpool)
prover = Prover.new(store, circomBackend, samples)
var proofs = newSeq[Future[?!(AnyProofInputs, AnyProof)]]()
for i in 1..10:
for i in 1..50:
echo "PROVE: ", i
let
challenge = (1234567+i).toF.toBytes.toArray32
challenge = (1234567).toF.toBytes.toArray32
proofs.add(prover.prove(1, verifiable, challenge))
await allFutures(proofs)
echo "done"
for pf in proofs:
let (inputs, proof) = (await pf).tryGet
check:
(await prover.verify(proof, inputs)).tryGet == true
# for pf in proofs:
# let (inputs, proof) = (await pf).tryGet
# check:
# (await prover.verify(proof, inputs)).tryGet == true
echo "done done"

@ -1 +1 @@
Subproject commit e710e4c333f367353aaa1ee82a55a47326b63a65
Subproject commit 2e694d1a705c16d76891f075aed2305db4f3aac1