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

View File

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

View File

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

View File

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

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