Skip to content

Commit 62f6cdf

Browse files
committed
Plonk_cs: add dump_extra_circuit_data
Changes are cherry-picked from openmina@798853c and an additional layer of documentation is added on top.
1 parent c281535 commit 62f6cdf

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

src/lib/crypto/kimchi_backend/pasta/constraint_system/intf.ml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,14 @@ module type Full = sig
159159
(** Serialize the constraint system to JSON format.
160160
Useful for debugging and external analysis. *)
161161
val to_json : t -> string
162+
163+
(** Dump extra circuit data to a file for use by external systems.
164+
165+
This function exports additional circuit metadata and configuration
166+
that is needed by the Mina Rust node for circuit verification and
167+
proof processing.
168+
169+
@param t The constraint system
170+
@param string The file path where the circuit data should be written *)
171+
val dump_extra_circuit_data : t -> string -> unit
162172
end

src/lib/crypto/kimchi_pasta_snarky_backend/plonk_constraint_system.ml

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,7 @@ module V = struct
777777
(** An internal variable is generated to hold an intermediate value
778778
(e.g., in reducing linear combinations to single PLONK positions).
779779
*)
780-
[@@deriving compare, hash, sexp]
780+
[@@deriving compare, hash, sexp, bin_io]
781781
end
782782

783783
include T
@@ -1003,6 +1003,8 @@ module Make
10031003
val digest : t -> Md5.t
10041004

10051005
val to_json : t -> string
1006+
1007+
val dump_extra_circuit_data : t -> string -> unit
10061008
end = struct
10071009
open Core_kernel
10081010
module Constraint = Plonk_constraint.Make (Fp)
@@ -2434,4 +2436,36 @@ end = struct
24342436
Option.try_with (fun () -> reduce_to_v values.(i)) )
24352437
in
24362438
add_row sys values kind coeffs
2439+
2440+
(* ((Fp.t * V.t) list * Fp.t option) *)
2441+
type concrete_table = ((Fp.t * V.t) list * Fp.t option) Internal_var.Table.t
2442+
[@@deriving bin_io]
2443+
2444+
(* V.t option array list *)
2445+
type concrete_rows_rev = V.t option array list [@@deriving bin_io]
2446+
2447+
let dump_extra_circuit_data (sys : t) base_path =
2448+
let rows_rev_name = base_path ^ "_rows_rev.bin" in
2449+
let internal_vars_name = base_path ^ "_internal_vars.bin" in
2450+
let gates_json_name = base_path ^ "_gates.json" in
2451+
if Sys.file_exists rows_rev_name then Sys.remove rows_rev_name ;
2452+
if Sys.file_exists internal_vars_name then Sys.remove internal_vars_name ;
2453+
if Sys.file_exists gates_json_name then Sys.remove gates_json_name ;
2454+
2455+
let table : concrete_rows_rev = sys.rows_rev in
2456+
let size = bin_size_concrete_rows_rev table in
2457+
let buf = Bigstring.create size in
2458+
ignore (bin_write_concrete_rows_rev buf ~pos:0 table : int) ;
2459+
Core_kernel.Out_channel.write_all rows_rev_name
2460+
~data:(Bigstring.to_string buf) ;
2461+
2462+
let table : concrete_table = sys.internal_vars in
2463+
let size = bin_size_concrete_table table in
2464+
let buf = Bigstring.create size in
2465+
ignore (bin_write_concrete_table buf ~pos:0 table : int) ;
2466+
Core_kernel.Out_channel.write_all internal_vars_name
2467+
~data:(Bigstring.to_string buf) ;
2468+
2469+
let gates_json = to_json sys in
2470+
Core_kernel.Out_channel.write_all gates_json_name ~data:gates_json
24372471
end

0 commit comments

Comments
 (0)