From 3188ef661a80f8914f54b469fe14454b5ff0caa9 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Wed, 11 Nov 2020 17:34:05 +0000 Subject: [PATCH 1/3] Add header format for snark keys from RFC0039 --- src/lib/snark_keys_header/dune | 6 + .../snark_keys_header/snark_keys_header.ml | 104 ++++++++++++++++++ src/snark_keys_header.opam | 24 ++++ 3 files changed, 134 insertions(+) create mode 100644 src/lib/snark_keys_header/dune create mode 100644 src/lib/snark_keys_header/snark_keys_header.ml create mode 100644 src/snark_keys_header.opam diff --git a/src/lib/snark_keys_header/dune b/src/lib/snark_keys_header/dune new file mode 100644 index 00000000000..65fd9e4df31 --- /dev/null +++ b/src/lib/snark_keys_header/dune @@ -0,0 +1,6 @@ +(library + (name snark_keys_header) + (public_name snark_keys_header) + (libraries core_kernel runtime_config unsigned_extended) + (instrumentation (backend bisect_ppx)) + (preprocess (pps ppx_version ppx_deriving_yojson))) diff --git a/src/lib/snark_keys_header/snark_keys_header.ml b/src/lib/snark_keys_header/snark_keys_header.ml new file mode 100644 index 00000000000..a01085d6203 --- /dev/null +++ b/src/lib/snark_keys_header/snark_keys_header.ml @@ -0,0 +1,104 @@ +open Core_kernel + +(** The string that preceeds the JSON header, to identify the file kind before + attempting to parse it. +*) +let header_string = "MINA_SNARK_KEYS\n" + +module Kind = struct + (** The 'kind' of data in the file. + For example, a step proving key for the base transaction snark may have the + kind: +{[ + {type_= "step_proving_key"; identifier= "transaction_snark_base"} +|} + *) + type t = + { type_: string [@key "type"] + (** Identifies the type of data that the file contains *) + ; identifier: string + (** Identifies the specific purpose of the file's data, in a + human-readable format + *) + } + [@@deriving yojson] +end + +module Constraint_constants = struct + module Transaction_capacity = struct + (** Transaction pool capacity *) + type t = Log_2 of int | Txns_per_second_x10 of int + + let to_yojson t : Yojson.Safe.t = + match t with + | Log2 i -> + `Assoc [("two_to_the", `Int i)] + | Txns_per_second_x10 i -> + `Assoc [("txns_per_second_x10", `Int i)] + + let of_yojson (json : Yojson.Safe.t) = + match json with + | `Assoc [("two_to_the", `Int i)] -> + Ok (Log2 i) + | `Assoc [("txns_per_second_x10", `Int i)] -> + Ok (Txns_per_second_x10 i) + | `Assoc _ -> + Error + "Snark_keys_header.Constraint_constants.Transaction_capacity.of_yojson: \ + Expected a JSON object containing the field 'two_to_the' or \ + 'txns_per_second_x10'" + | _ -> + Error + "Snark_keys_header.Constraint_constants.Transaction_capacity.of_yojson: \ + Expected a JSON object" + end + + module Fork_config = struct + (** Fork data *) + type t = Runtime_config.t = + {previous_state_hash: string; previous_length: int} + [@@deriving yojson] + + let opt_to_yojson t : Yojson.Safe.t = + match t with Some t -> to_yojson t | None -> `Assoc [] + + let opt_of_yojson (json : Yojson.Safe.t) = + match json with + | `Assoc [] -> + Ok None + | _ -> + Result.map (of_yojson json) ~f:(fun t -> Some t) + end + + (** The constants used in the constraint system. *) + type constraint_constants = + { sub_windows_per_window: int + ; ledger_depth: int + ; work_delay: int + ; block_window_duration_ms: int + ; transaction_capacity: Transaction_capacity.t + ; coinbase_amount: Unsigned_extended.Uint64.t + ; supercharged_coinbase_factor: int + ; account_creation_fee: Unsigned_extended.Uint64.t + ; fork: + (Fork_config.t option[@to_yojson Fork_config.opt_to_yojson] + [@of_yojson Fork_config.opt_of_yojson]) } + [@@deriving yojson] +end + +module Commits = struct + (** Commit identifiers *) + type t = {mina: string; marlin: string} [@@deriving yojson] +end + +(** Header contents *) +type t = + { header_version: int + ; kind: Kind.t + ; constraint_constants: Constraint_constants.t + ; commits: Commits.t + ; length: int + ; commit_date: string + ; constraint_system_hash: string + ; identifying_hash: string } +[@@deriving yojson] diff --git a/src/snark_keys_header.opam b/src/snark_keys_header.opam new file mode 100644 index 00000000000..fa5836a0180 --- /dev/null +++ b/src/snark_keys_header.opam @@ -0,0 +1,24 @@ +opam-version: "2" +name: "snark_keys_header" +maintainer: "opensource@o1labs.org" +authors: ["O(1) Labs, LLC "] +homepage: "https://github.com/CodaProtocol/coda" +bug-reports: "https://github.com/CodaProtocol/coda/issues" +dev-repo: "https://github.com/CodaProtocol/coda.git" +license: "Apache" +build: [ + ["dune" "build" "-p" name "-j" jobs] +] +depends: [ + "core_kernel" + "dune" {build & >= "2.0"} + "ppx_version" + "ppx_deriving_yojson" + "unsigned_extended" +] +synopsis: "Headers for mina snark keys" +description: " +Headers for mina snark key files and related files, exposing relevant data in a +JSON-formatted object +" + From d0c1c8a33fe0c61522c3ae777b2b5779de6fcb50 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Wed, 11 Nov 2020 17:49:21 +0000 Subject: [PATCH 2/3] Use the correct constructor name for Transaction_capacity.Log_2 --- src/lib/snark_keys_header/snark_keys_header.ml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/snark_keys_header/snark_keys_header.ml b/src/lib/snark_keys_header/snark_keys_header.ml index a01085d6203..089403a9443 100644 --- a/src/lib/snark_keys_header/snark_keys_header.ml +++ b/src/lib/snark_keys_header/snark_keys_header.ml @@ -31,7 +31,7 @@ module Constraint_constants = struct let to_yojson t : Yojson.Safe.t = match t with - | Log2 i -> + | Log_2 i -> `Assoc [("two_to_the", `Int i)] | Txns_per_second_x10 i -> `Assoc [("txns_per_second_x10", `Int i)] @@ -39,7 +39,7 @@ module Constraint_constants = struct let of_yojson (json : Yojson.Safe.t) = match json with | `Assoc [("two_to_the", `Int i)] -> - Ok (Log2 i) + Ok (Log_2 i) | `Assoc [("txns_per_second_x10", `Int i)] -> Ok (Txns_per_second_x10 i) | `Assoc _ -> @@ -55,7 +55,7 @@ module Constraint_constants = struct module Fork_config = struct (** Fork data *) - type t = Runtime_config.t = + type t = Runtime_config.Fork_config.t = {previous_state_hash: string; previous_length: int} [@@deriving yojson] From 9d4dee40ffc38bf90aa8757a13cca64de6330b06 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Wed, 11 Nov 2020 17:49:54 +0000 Subject: [PATCH 3/3] Fixup compilation error --- src/lib/snark_keys_header/snark_keys_header.ml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/snark_keys_header/snark_keys_header.ml b/src/lib/snark_keys_header/snark_keys_header.ml index 089403a9443..3f8fd6bbee2 100644 --- a/src/lib/snark_keys_header/snark_keys_header.ml +++ b/src/lib/snark_keys_header/snark_keys_header.ml @@ -71,15 +71,15 @@ module Constraint_constants = struct end (** The constants used in the constraint system. *) - type constraint_constants = + type t = { sub_windows_per_window: int ; ledger_depth: int ; work_delay: int ; block_window_duration_ms: int ; transaction_capacity: Transaction_capacity.t - ; coinbase_amount: Unsigned_extended.Uint64.t + ; coinbase_amount: Unsigned_extended.UInt64.t ; supercharged_coinbase_factor: int - ; account_creation_fee: Unsigned_extended.Uint64.t + ; account_creation_fee: Unsigned_extended.UInt64.t ; fork: (Fork_config.t option[@to_yojson Fork_config.opt_to_yojson] [@of_yojson Fork_config.opt_of_yojson]) }