Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup bindings #35

Closed
wants to merge 28 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
1fe33b8
Try compiling rollup to JS
L-as Jun 22, 2023
e7055a6
Fix indentation
L-as Jun 22, 2023
4a84c77
fix bindings export
Trivo25 Jun 23, 2023
22e0653
Only compile transaction snark vk for now
L-as Jun 24, 2023
1bda30e
Expose transaction_snark and compile in separate mode
L-as Jun 30, 2023
d9459f4
Compile vk and step function at once
L-as Jul 4, 2023
d2086f9
Bind deploy and step mostly
L-as Jul 14, 2023
12cdc3c
Fix rebase
MartinOndejka Jul 27, 2023
cbe28f8
Fix building
L-as Aug 22, 2023
fed7598
MVP rollup bindings
L-as Aug 22, 2023
7a71936
Fix account update not being string
L-as Aug 22, 2023
1668e9f
Export Async_js.init
L-as Aug 22, 2023
7a5476d
Remove redundant comment
L-as Aug 22, 2023
054580c
tmp: add testing account to the ledger
MartinOndejka Aug 23, 2023
f406d62
Fix sparse ledger
MartinOndejka Aug 28, 2023
d3249ac
Fix supply increase
MartinOndejka Aug 28, 2023
8bc69cf
Fix concurency of ledger target hash
MartinOndejka Aug 28, 2023
d11d2ee
Fix a shebang
L-as Aug 24, 2023
fb02cd4
Make the deployment work
MartinOndejka Sep 1, 2023
4d0f9e9
Add genesis accounts
MartinOndejka Sep 1, 2023
09f2efe
Fix node worker threads
MartinOndejka Sep 25, 2023
eda7788
Split command apply and prove for concurency
MartinOndejka Sep 25, 2023
3a6eb22
Change Rollup to RollupBindings and add conversion utilities
MartinOndejka Sep 26, 2023
8db993d
Add getRoot method
MartinOndejka Sep 26, 2023
4688328
Minor refactor
MartinOndejka Sep 27, 2023
05b0ab9
Fix global slot
MartinOndejka Sep 27, 2023
3ea9983
Add genesis ledger hash
MartinOndejka Sep 28, 2023
c5a0cc5
Add random curve point to the ts ec bindings
MartinOndejka Oct 16, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions crypto/bindings/curve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
GroupAffine,
} from '../elliptic_curve.js';
import { withPrefix } from './util.js';
import { FiniteField, Fp, Fq } from '../finite_field.js';

export {
VestaBindings,
Expand All @@ -21,10 +22,10 @@ export {
fromMlOrInfinity,
};

const VestaBindings = withPrefix('caml_vesta', createCurveBindings(Vesta));
const PallasBindings = withPrefix('caml_pallas', createCurveBindings(Pallas));
const VestaBindings = withPrefix('caml_vesta', createCurveBindings(Vesta, Fp));
const PallasBindings = withPrefix('caml_pallas', createCurveBindings(Pallas, Fq));

function createCurveBindings(Curve: ProjectiveCurve) {
function createCurveBindings(Curve: ProjectiveCurve, ScalarField: FiniteField) {
return {
one(): GroupProjective {
return Curve.one;
Expand All @@ -37,7 +38,7 @@ function createCurveBindings(Curve: ProjectiveCurve) {
return Curve.scale(g, s);
},
random(): GroupProjective {
throw Error('random not implemented');
return Curve.scale(Curve.one, ScalarField.random());
},
rng(i: number): GroupProjective {
throw Error('rng not implemented');
Expand Down
2 changes: 1 addition & 1 deletion crypto/test_vectors/dump-test-vectors.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!/bin/sh

set -e

Expand Down
2 changes: 1 addition & 1 deletion crypto/test_vectors/dune
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
(action
(progn
(run chmod -R +w ../../../../../crypto/proof-systems/ .)
(run %{script} poseidonKimchi.ts poseidonLegacy.ts))))
(run /bin/sh %{script} poseidonKimchi.ts poseidonLegacy.ts))))
10 changes: 5 additions & 5 deletions js/node/node-backend.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { isMainThread, parentPort, workerData, Worker } from 'worker_threads';
import os from 'os';
import wasm_ from '../../compiled/_node_bindings/plonk_wasm.cjs';
import { fileURLToPath } from 'url';
import { Worker, isMainThread, parentPort, workerData } from 'worker_threads';
import wasm_ from '../../compiled/_node_bindings/plonk_wasm.cjs';
let url = import.meta.url;
let filename = url !== undefined ? fileURLToPath(url) : __filename;

Expand All @@ -19,7 +19,7 @@ let workersReady;
globalThis.startWorkers = startWorkers;
globalThis.terminateWorkers = terminateWorkers;

if (!isMainThread) {
if (!isMainThread && !workerData?.isSnarkyMainThread) {
parentPort.postMessage({ type: 'wasm_bindgen_worker_ready' });
wasm.wbg_rayon_start_worker(workerData.receiver);
}
Expand Down Expand Up @@ -80,15 +80,15 @@ async function withThreadPool(run) {
}

async function initThreadPool() {
if (!isMainThread) return;
if (!isMainThread && !workerData?.isSnarkyMainThread) return;
workersReady = new Promise((resolve) => (workersReadyResolve = resolve));
await wasm.initThreadPool(getEfficientNumWorkers(), filename);
await workersReady;
workersReady = undefined;
}

async function exitThreadPool() {
if (!isMainThread) return;
if (!isMainThread && !workerData?.isSnarkyMainThread) return;
await wasm.exitThreadPool();
}

Expand Down
18 changes: 18 additions & 0 deletions js/snarky-class-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,22 @@ export default [
},
],
},
{
name: 'RollupBindings',
props: [
{
name: 'compile',
type: 'function',
},
],
},
{
name: 'Async_js',
props: [
{
name: 'init',
type: 'function',
},
],
},
];
2 changes: 2 additions & 0 deletions js/wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export { getSnarky, getWasm, withThreadPool };

let getSnarky = () => snarky;

wasm.console_error_panic_hook_set_once();

function getWasm() {
return wasm;
}
2 changes: 1 addition & 1 deletion kimchi/js/node_js/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ set -euo pipefail

if [[ -z "${PLONK_WASM_NODEJS-}" ]]; then
export RUSTFLAGS="-C target-feature=+atomics,+bulk-memory,+mutable-globals -C link-arg=--no-check-features -C link-arg=--max-memory=4294967296"
rustup run nightly-2022-09-12 wasm-pack build --target nodejs --out-dir ../js/node_js ../../wasm -- -Z build-std=panic_abort,std --features nodejs
wasm-pack build --target nodejs --out-dir ../js/node_js ../../wasm -- -Z build-std=panic_abort,std --features nodejs
else
cp "$PLONK_WASM_NODEJS"/* -R .
fi
2 changes: 2 additions & 0 deletions kimchi/wasm/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
[toolchain]
channel = "nightly-2023-02-05"
targets = ["wasm32-unknown-unknown"]
components = ["rustfmt", "rust-src"]
5 changes: 5 additions & 0 deletions kimchi/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ pub fn wait_until_non_zero(ptr: *const u32) -> u32 {
unreachable!();
}

#[wasm_bindgen]
pub fn console_error_panic_hook_set_once() {
console_error_panic_hook::set_once();
}

pub mod rayon;

/// Arkworks types
Expand Down
1 change: 0 additions & 1 deletion kimchi/wasm/src/pasta_fp_plonk_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ pub fn caml_pasta_fp_plonk_index_create(
prev_challenges: i32,
srs: &WasmSrs,
) -> Result<WasmPastaFpPlonkIndex, JsError> {
console_error_panic_hook::set_once();
let index = crate::rayon::run_in_pool(|| {
// flatten the permutation information (because OCaml has a different way of keeping track of permutations)
let gates: Vec<_> = gates
Expand Down
1 change: 0 additions & 1 deletion kimchi/wasm/src/pasta_fq_plonk_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ pub fn caml_pasta_fq_plonk_index_create(
prev_challenges: i32,
srs: &WasmSrs,
) -> Result<WasmPastaFqPlonkIndex, JsError> {
console_error_panic_hook::set_once();
let index = crate::rayon::run_in_pool(|| {
// flatten the permutation information (because OCaml has a different way of keeping track of permutations)
let gates: Vec<_> = gates
Expand Down
1 change: 0 additions & 1 deletion kimchi/wasm/src/plonk_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,6 @@ macro_rules! impl_proof {
prev_challenges: WasmFlatVector<$WasmF>,
prev_sgs: WasmVector<$WasmG>,
) -> Result<WasmProverProof, JsError> {
console_error_panic_hook::set_once();
let (maybe_proof, public_input) = crate::rayon::run_in_pool(|| {
{
let ptr: &mut poly_commitment::srs::SRS<$G> =
Expand Down
7 changes: 3 additions & 4 deletions ocaml/dune
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(env
(_
(js_of_ocaml
(compilation_mode whole_program))))
(compilation_mode separate))))

(data_only_dirs test_module)

Expand All @@ -15,7 +15,7 @@
(link_flags
(-noautolink -g))
(js_of_ocaml
(flags +toplevel.js +dynlink.js --pretty --source-map)
(flags +toplevel.js +dynlink.js --pretty --source-map --debug-info)
(link_flags --source-map)
(javascript_files overrides.js))
(libraries snarky_js_bindings_lib node_backend)
Expand All @@ -24,7 +24,7 @@
../kimchi/js/node_js/plonk_wasm_bg.wasm)
(instrumentation
(backend bisect_ppx))
(forbidden_libraries async core re2 ctypes)
(forbidden_libraries re2 ctypes)
(preprocess
(pps ppx_version js_of_ocaml-ppx)))

Expand All @@ -42,7 +42,6 @@
../kimchi/js/web/plonk_wasm_bg.wasm)
(instrumentation
(backend bisect_ppx))
(forbidden_libraries async core re2 ctypes)
(preprocess
(pps ppx_version js_of_ocaml-ppx)))

Expand Down
19 changes: 19 additions & 0 deletions ocaml/lib/consistency_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,21 @@ module Transaction_hash = struct
let payment = Signed_command.sign kp payload in
(payment :> Signed_command.t)
|> Signed_command.to_yojson |> Yojson.Safe.to_string |> Js.string

let payment_of_base64 (base64 : Js.js_string Js.t) =
let command = Signed_command.of_base64 @@ Js.to_string @@ base64 in
match command with
| Ok command ->
Signed_command.to_yojson command |> Yojson.Safe.to_string |> Js.string
| Error e ->
Error.raise e

let payment_to_base64 (command : Js.js_string Js.t) =
let command : Signed_command.t =
command |> Js.to_string |> Yojson.Safe.from_string
|> Signed_command.of_yojson |> ok_exn
in
Signed_command.to_base64 command |> Js.string
end

let test =
Expand Down Expand Up @@ -397,5 +412,9 @@ let test =
method serializePaymentV1 = serialize_payment_v1

val examplePayment = example_payment

method paymentOfBase64 = payment_of_base64

method paymentToBase64 = payment_to_base64
end
end
13 changes: 12 additions & 1 deletion ocaml/lib/dune
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
(library
(name snarky_js_bindings_lib)
(libraries
;; rollup ;;
async_kernel
async_js
transaction_snark
zkapps_rollup
mina_state
genesis_ledger
consensus
staged_ledger_diff
transaction_protocol_state
;; opam libraries ;;
core_kernel
base
Expand Down Expand Up @@ -40,6 +50,7 @@
mina_signature_kind
mina_transaction
mina_transaction_logic
mina_ledger
random_oracle
random_oracle_input
sgn
Expand Down Expand Up @@ -67,4 +78,4 @@
(instrumentation
(backend bisect_ppx))
(preprocess
(pps ppx_custom_printf ppx_version js_of_ocaml-ppx)))
(pps ppx_jane ppx_custom_printf ppx_version js_of_ocaml-ppx ppx_deriving_yojson)))
Loading