From aa42ce864e7e6c1531d891acc7ee54b7c9040d4b Mon Sep 17 00:00:00 2001 From: Gregor Date: Mon, 11 Mar 2024 21:20:39 +0100 Subject: [PATCH 1/3] stub out async request --- src/base/snark0.ml | 10 ++++++++++ src/base/snark_intf.ml | 4 ++++ 2 files changed, 14 insertions(+) diff --git a/src/base/snark0.ml b/src/base/snark0.ml index 473b7117a..79e8d2351 100644 --- a/src/base/snark0.ml +++ b/src/base/snark0.ml @@ -1428,6 +1428,16 @@ module Run = struct in Staged.stage finish_computation + module Async' (Promise : Base.Monad.S) = struct + let request (request : unit -> 'a Promise.t Request.t) = + let r = exists (Typ.Internal.ref ()) ~request ?compute:None in + match !r with + | Some p -> + Promise.map p ~f:(fun x -> Some x) + | None -> + Promise.return None + end + let run_unchecked x = finalize_is_running (fun () -> Perform.run_unchecked ~run:as_stateful (fun () -> mark_active ~f:x) ) diff --git a/src/base/snark_intf.ml b/src/base/snark_intf.ml index a6416ca31..1b179733c 100644 --- a/src/base/snark_intf.ml +++ b/src/base/snark_intf.ml @@ -1405,6 +1405,10 @@ module type Run_basic = sig (* Callback, low-level version of [as_prover] and [exists]. *) val as_prover_manual : int -> (field array option -> Field.t array) Staged.t + module Async' (Promise : Base.Monad.S) : sig + val request : (unit -> 'a Promise.t Request.t) -> 'a option Promise.t + end + (** Generate the public input vector for a given statement. *) val generate_public_input : ('input_var, 'input_value) Typ.t -> 'input_value -> Field.Constant.Vector.t From dd396002752f01f88fa25d917f92680cca72b0fb Mon Sep 17 00:00:00 2001 From: Gregor Date: Tue, 12 Mar 2024 10:40:55 +0100 Subject: [PATCH 2/3] proper implementation, make intf only return unit for now --- src/base/snark0.ml | 25 ++++++++++++++++++------- src/base/snark_intf.ml | 4 +++- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/base/snark0.ml b/src/base/snark0.ml index 79e8d2351..0b99d4bd7 100644 --- a/src/base/snark0.ml +++ b/src/base/snark0.ml @@ -1428,14 +1428,25 @@ module Run = struct in Staged.stage finish_computation + let request_manual (req : unit -> 'a Request.t) () : 'a = + Request.Handler.run (Run_state.handler !state) (req ()) + |> Option.value_exn ~message:"Unhandled request" + module Async' (Promise : Base.Monad.S) = struct - let request (request : unit -> 'a Promise.t Request.t) = - let r = exists (Typ.Internal.ref ()) ~request ?compute:None in - match !r with - | Some p -> - Promise.map p ~f:(fun x -> Some x) - | None -> - Promise.return None + let run_prover ~(else_ : unit -> 'a) (f : unit -> 'a Promise.t) : + 'a Promise.t = + if Run_state.has_witness !state then ( + let old = Run_state.as_prover !state in + Run_state.set_as_prover !state true ; + let%map.Promise result = f () in + Run_state.set_as_prover !state old ; + result ) + else Promise.return (else_ ()) + + let as_prover (f : unit -> unit Promise.t) : unit Promise.t = + run_prover ~else_:(fun () -> ()) f + + let unit_request req = as_prover (request_manual req) end let run_unchecked x = diff --git a/src/base/snark_intf.ml b/src/base/snark_intf.ml index 1b179733c..027a63301 100644 --- a/src/base/snark_intf.ml +++ b/src/base/snark_intf.ml @@ -1406,7 +1406,9 @@ module type Run_basic = sig val as_prover_manual : int -> (field array option -> Field.t array) Staged.t module Async' (Promise : Base.Monad.S) : sig - val request : (unit -> 'a Promise.t Request.t) -> 'a option Promise.t + val as_prover : (unit -> unit Promise.t) -> unit Promise.t + + val unit_request : (unit -> unit Promise.t Request.t) -> unit Promise.t end (** Generate the public input vector for a given statement. *) From 9123bde13dbb0b623b44cb005087f5444da18191 Mon Sep 17 00:00:00 2001 From: Gregor Date: Tue, 12 Mar 2024 10:45:00 +0100 Subject: [PATCH 3/3] more descriptive name --- src/base/snark0.ml | 2 +- src/base/snark_intf.ml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/base/snark0.ml b/src/base/snark0.ml index 0b99d4bd7..11ffde363 100644 --- a/src/base/snark0.ml +++ b/src/base/snark0.ml @@ -1432,7 +1432,7 @@ module Run = struct Request.Handler.run (Run_state.handler !state) (req ()) |> Option.value_exn ~message:"Unhandled request" - module Async' (Promise : Base.Monad.S) = struct + module Async_generic (Promise : Base.Monad.S) = struct let run_prover ~(else_ : unit -> 'a) (f : unit -> 'a Promise.t) : 'a Promise.t = if Run_state.has_witness !state then ( diff --git a/src/base/snark_intf.ml b/src/base/snark_intf.ml index 027a63301..465daf208 100644 --- a/src/base/snark_intf.ml +++ b/src/base/snark_intf.ml @@ -1405,7 +1405,7 @@ module type Run_basic = sig (* Callback, low-level version of [as_prover] and [exists]. *) val as_prover_manual : int -> (field array option -> Field.t array) Staged.t - module Async' (Promise : Base.Monad.S) : sig + module Async_generic (Promise : Base.Monad.S) : sig val as_prover : (unit -> unit Promise.t) -> unit Promise.t val unit_request : (unit -> unit Promise.t Request.t) -> unit Promise.t