Skip to content

Commit

Permalink
Include Run_state in backend
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmr1993 committed Dec 29, 2024
1 parent 19176cc commit c99cd4a
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 77 deletions.
7 changes: 5 additions & 2 deletions src/base/backend_extended.ml
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,17 @@ module type S = sig
-> (Cvar.t -> Field.t)
-> bool
end

module Run_state : Run_state_intf.S
end

module Make (Backend : Backend_intf.S) :
S
with type Field.t = Backend.Field.t
and type Field.Vector.t = Backend.Field.Vector.t
and type Bigint.t = Backend.Bigint.t
and type R1CS_constraint_system.t = Backend.R1CS_constraint_system.t =
struct
and type R1CS_constraint_system.t = Backend.R1CS_constraint_system.t
and type 'field Run_state.t = 'field Backend.Run_state.t = struct
open Backend

module Bigint = struct
Expand Down Expand Up @@ -219,4 +221,5 @@ struct
end

module R1CS_constraint_system = R1CS_constraint_system
module Run_state = Run_state
end
2 changes: 2 additions & 0 deletions src/base/backend_intf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ module type S = sig
val field_size : Bigint.t

module R1CS_constraint_system : Constraint_system.S with module Field := Field

module Run_state : Run_state_intf.S
end
5 changes: 4 additions & 1 deletion src/base/checked.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ end)
(As_prover : As_prover_intf.Basic
with type field := Basic.field
with module Types := Types) :
Checked_intf.S with module Types := Types with type field = Field.t = struct
Checked_intf.S
with module Types := Types
with type field = Field.t
and type run_state = Basic.run_state = struct
include Basic

let request_witness (typ : ('var, 'value) Types.Typ.t)
Expand Down
10 changes: 7 additions & 3 deletions src/base/checked_intf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module type Basic = sig

type 'a t = 'a Types.Checked.t

type run_state

include Monad_let.S with type 'a t := 'a t

val add_constraint : (field Cvar.t, field) Constraint.t -> unit t
Expand All @@ -24,7 +26,7 @@ module type Basic = sig

val next_auxiliary : unit -> int t

val direct : (field Run_state.t -> field Run_state.t * 'a) -> 'a t
val direct : (run_state -> run_state * 'a) -> 'a t

val constraint_count :
?weight:((field Cvar.t, field) Constraint.t -> int)
Expand All @@ -38,6 +40,8 @@ module type S = sig

type field

type run_state

type 'a t = 'a Types.Checked.t

include Monad_let.S with type 'a t := 'a t
Expand Down Expand Up @@ -100,7 +104,7 @@ module type S = sig
val assert_equal :
?label:Base.string -> field Cvar.t -> field Cvar.t -> unit t

val direct : (field Run_state.t -> field Run_state.t * 'a) -> 'a t
val direct : (run_state -> run_state * 'a) -> 'a t

val constraint_count :
?weight:((field Cvar.t, field) Constraint.t -> int)
Expand All @@ -112,5 +116,5 @@ end
module type Extended = sig
include S

val run : 'a t -> field Run_state.t -> field Run_state.t * 'a
val run : 'a t -> run_state -> run_state * 'a
end
34 changes: 21 additions & 13 deletions src/base/checked_runner.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@ let eval_constraints = ref true

let eval_constraints_ref = eval_constraints

type ('a, 'f) t =
| Pure of 'a
| Function of ('f Run_state.t -> 'f Run_state.t * 'a)
module T (Backend : Backend_extended.S) = struct
type 'a t =
| Pure of 'a
| Function of
( Backend.Field.t Backend.Run_state.t
-> Backend.Field.t Backend.Run_state.t * 'a )
end

module Simple_types (Backend : Backend_extended.S) = Types.Make_types (struct
type field = Backend.Field.t

type field_var = field Cvar.t

type 'a checked = ('a, field) t
type 'a checked = 'a T(Backend).t

type 'a as_prover = (field_var -> field) -> 'a
end)
Expand All @@ -36,11 +40,15 @@ module Make_checked
with type field := Backend.Field.t
with module Types := Types) =
struct
type run_state = Backend.Field.t Run_state.t
type run_state = Backend.Field.t Backend.Run_state.t

type field = Backend.Field.t

type 'a t = 'a Types.Checked.t
type 'a t = 'a T(Backend).t =
| Pure of 'a
| Function of
( Backend.Field.t Backend.Run_state.t
-> Backend.Field.t Backend.Run_state.t * 'a )

let eval (t : 'a t) : run_state -> run_state * 'a =
match t with Pure a -> fun s -> (s, a) | Function g -> g
Expand Down Expand Up @@ -291,14 +299,14 @@ module type Run_extras = sig
type cvar
type run_state
module Types : Types.Types
val get_value : field Run_state.t -> cvar -> field
val get_value : run_state -> cvar -> field
val run_as_prover :
'a Types.As_prover.t option
-> field Run_state.t
-> field Run_state.t * 'a option
'a Types.As_prover.t option -> run_state -> run_state * 'a option
end
module Make
Expand Down Expand Up @@ -341,12 +349,14 @@ struct
Checked_intf.Basic
with module Types := Types
with type field := Checked_runner.field
and type run_state := run_state
include
Run_extras
with module Types := Types
with type field := Backend.Field.t
and type cvar := Backend.Cvar.t
and type run_state := run_state
end )
let run = Checked_runner.eval
Expand Down Expand Up @@ -403,8 +413,6 @@ module type S = sig
val clear_constraint_logger : unit -> unit
type run_state = field Run_state.t
type state = run_state
type ('a, 't) run = 't -> run_state -> run_state * 'a
Expand All @@ -426,6 +434,6 @@ module type S = sig
-> (field Cvar.t, field) Constraint.t option
-> unit )
-> unit
-> field Run_state.t
-> run_state
end
end
10 changes: 6 additions & 4 deletions src/base/runners.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ module Make
with type field = Backend.Field.t
and type field_var = Backend.Cvar.t)
(Checked : Checked_intf.Extended
with module Types := Types
with type field = Backend.Field.t
with module Types := Types)
and type run_state = Backend.Field.t Backend.Run_state.t)
(As_prover : As_prover_intf.Basic
with type field := Backend.Field.t
with module Types := Types)
Expand All @@ -16,7 +17,8 @@ module Make
with type field := Backend.Field.t
and type cvar := Backend.Cvar.t
and type constr := Backend.Constraint.t option
and type r1cs := Backend.R1CS_constraint_system.t) =
and type r1cs := Backend.R1CS_constraint_system.t
and type run_state = Backend.Field.t Backend.Run_state.t) =
struct
open Backend

Expand Down Expand Up @@ -215,7 +217,7 @@ struct
Runner.State.make ~num_inputs ~input ~next_auxiliary ~aux ~system
~with_witness:false ()
in
let id = Run_state.id state in
let id = Backend.Run_state.id state in
let state, () =
(* create constraints to validate the input (using the input [Typ]'s [check]) *)
let checked =
Expand All @@ -226,7 +228,7 @@ struct
in
let run_computation k = k var state in
let finish_computation (state, res) =
let final_id = Run_state.id state in
let final_id = Backend.Run_state.id state in
if id <> final_id then
failwith "Snarky's internal state has been clobbered." ;
let res, _ = return_typ.var_to_fields res in
Expand Down
Loading

0 comments on commit c99cd4a

Please sign in to comment.