Skip to content

Commit 6a807db

Browse files
authored
Merge pull request #18054 from MinaProtocol/leon/native/napi/proof-generation-functions
(`o1js`) Native prover type generation support
2 parents 720484a + 4d28733 commit 6a807db

File tree

13 files changed

+72
-74
lines changed

13 files changed

+72
-74
lines changed

src/lib/crypto/kimchi_bindings/js/dune

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
bindings/verifier-index.js
1818
native/native-overrides/oracles.js
1919
native/native-overrides/util.js
20-
native/native-overrides/circuit.js
21-
))
20+
native/native-overrides/circuit.js))
2221
(instrumentation
2322
(backend bisect_ppx))
2423
(preprocess
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
index.d.ts
2+
plonk_napi.node

src/lib/crypto/kimchi_bindings/js/native/build.sh

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,10 @@ set -euo pipefail
44
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
55
CURRENT_DIRECTORY="${SCRIPT_DIR}"
66
PROOF_SYSTEMS_ROOT=$(cd "${SCRIPT_DIR}/../../../proof-systems" && pwd)
7-
PLONK_NAPI_ROOT="${PROOF_SYSTEMS_ROOT}/plonk-napi"
87

9-
TARGET_ROOT="${CARGO_TARGET_DIR:-"${PROOF_SYSTEMS_ROOT}/target"}"
10-
export CARGO_TARGET_DIR="${TARGET_ROOT}"
11-
12-
cargo build \
13-
--manifest-path "${PLONK_NAPI_ROOT}/Cargo.toml" \
14-
--release
15-
16-
case "$(uname -s)" in
17-
Darwin*) LIB_NAME="libplonk_napi.dylib" ;;
18-
MINGW*|MSYS*|CYGWIN*) LIB_NAME="plonk_napi.dll" ;;
19-
*) LIB_NAME="libplonk_napi.so" ;;
20-
esac
21-
22-
ARTIFACT="${TARGET_ROOT}/release/${LIB_NAME}"
23-
24-
if [[ ! -f "${ARTIFACT}" ]]; then
25-
echo "Failed to locate plonk-napi artifact at ${ARTIFACT}" >&2
26-
exit 1
27-
fi
28-
29-
rm -f "${CURRENT_DIRECTORY}/plonk_napi.node"
30-
cp "${ARTIFACT}" "${CURRENT_DIRECTORY}/plonk_napi.node"
8+
napi build \
9+
--manifest-path $PROOF_SYSTEMS_ROOT/Cargo.toml \
10+
--package plonk-napi \
11+
--output-dir ./ \
12+
--release \
13+
--esm

src/lib/crypto/kimchi_bindings/js/native/dune

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@
22
(name native_backend)
33
(public_name bindings_js.native_backend)
44
(js_of_ocaml
5-
(flags
6-
(:include flags.sexp))
75
(javascript_files native_backend.js))
86
(instrumentation
97
(backend bisect_ppx))
108
(preprocess
119
(pps ppx_version js_of_ocaml-ppx)))
1210

1311
(rule
14-
(targets
15-
plonk_napi.node
16-
flags.sexp)
12+
(targets plonk_napi.node index.d.ts)
13+
(mode
14+
(promote
15+
(into ./artifacts)))
1716
(deps
17+
package.json
1818
build.sh
19+
header-d.ts
1920
../../dune-build-root
2021
(source_tree ../../../proof-systems))
2122
(locks /cargo-lock)
@@ -24,6 +25,4 @@
2425
(setenv
2526
CARGO_TARGET_DIR
2627
"%{read:../../dune-build-root}/cargo_kimchi_native"
27-
(run bash build.sh))
28-
(write-file flags.sexp "()"))))
29-
28+
(run bash build.sh)))))
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Header section
2+
// To edit this section, look for
3+
// ./src/lib/crypto/kimchi_bindings/js/native/header-d.ts
4+
// This file gets auto-included in the generated plonk-napi types to supplement
5+
// external pointer types.
6+
7+
type WasmPastaFpPlonkIndex = {};
8+
9+
// Header section end

src/lib/crypto/kimchi_bindings/js/native/native-overrides/oracles.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
// Provides: caml_pasta_fq_poseidon_block_cipher
55
// Requires: plonk_wasm, tsRustConversionNative
66
function caml_pasta_fq_poseidon_block_cipher(_fake_params, fq_vector) {
7-
8-
console.log("overriding the old wasm caml_pasta_fq_poseidon_block_cipher, now using the native one")
9-
107
// 1. get permuted field vector from rust
118
var wasm_flat_vector = plonk_wasm.caml_pasta_fq_poseidon_block_cipher(
129
tsRustConversionNative.fq.vectorToRust(fq_vector)
@@ -22,10 +19,6 @@ function caml_pasta_fq_poseidon_block_cipher(_fake_params, fq_vector) {
2219
// Provides: caml_pasta_fp_poseidon_block_cipher
2320
// Requires: plonk_wasm, tsRustConversionNative
2421
function caml_pasta_fp_poseidon_block_cipher(_fake_params, fp_vector) {
25-
26-
console.log("overriding the old wasm caml_pasta_fp_poseidon_block_cipher, now using the native one")
27-
28-
2922
// 1. get permuted field vector from rust
3023
var wasm_flat_vector = plonk_wasm.caml_pasta_fp_poseidon_block_cipher(
3124
tsRustConversionNative.fp.vectorToRust(fp_vector)
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
// Provides: plonk_napi
2-
var plonk_napi = require('./plonk_napi.node');
3-
4-
module.exports = plonk_napi;
2+
var plonk_napi = require('@o1js/native-' + process.platform + '-' + process.arch)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"napi": {
3+
"binaryName": "plonk_napi",
4+
"dtsHeaderFile": "./header-d.ts"
5+
}
6+
}
Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,35 @@
11
// Provides: plonk_wasm
2-
var plonk_wasm = require('./plonk_wasm.js');
3-
var native = null;
4-
try {
5-
native = require('../native/plonk_napi.node');
6-
} catch (e) {
7-
// native not available, keep WASM
8-
}
2+
var plonk_wasm = (function() {
3+
var wasm = require('./plonk_wasm.js');
94

10-
function snakeToCamel(name) {
11-
return name.replace(/_([a-z])/g, function (_match, ch) {
12-
return ch.toUpperCase();
13-
});
14-
}
5+
try {
6+
var native = require('@o1js/native-' + process.platform + '-' + process.arch)
157

16-
function override(functionName) {
17-
if (!native) return;
18-
var camel = snakeToCamel(functionName);
19-
var impl = native[functionName] || native[camel];
20-
if (typeof impl === 'function') {
21-
plonk_wasm[functionName] = impl;
8+
// THIS IS A RUNTIME OVERRIDE
9+
// YOU HAVE TO RUN IT TO SEE IF IT BREAKS
10+
// IT WON'T CRASH UNLESS O1JS_REQUIRE_NATIVE_BINDINGS
11+
// IS SET
12+
var overrides = [
13+
"prover_to_json",
14+
"prover_index_from_bytes",
15+
"prover_index_to_bytes",
16+
"caml_pasta_fp_poseidon_block_cipher",
17+
"caml_pasta_fq_poseidon_block_cipher",
18+
"caml_pasta_fp_plonk_proof_create",
19+
]
20+
21+
overrides.forEach(function (override) {
22+
wasm[override] = native[override]
23+
})
24+
25+
wasm.native = true;
26+
} catch (e) {
27+
if (process.env.O1JS_REQUIRE_NATIVE_BINDINGS) {
28+
console.error(e)
29+
console.log("native didn't load")
30+
process.exit(1);
31+
}
2232
}
23-
}
2433

25-
// Overwrite only the functions that are already available in native
26-
override('caml_pasta_fp_poseidon_block_cipher');
27-
override('caml_pasta_fq_poseidon_block_cipher');
28-
override('prover_to_json');
29-
override('prover_index_from_bytes');
34+
return wasm
35+
})()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
// Provides: plonk_wasm
22
var plonk_wasm = globalThis.plonk_wasm;
3+
4+
// Provides: plonk_napi
5+
var plonk_napi = globalThis.plonk_wasm

0 commit comments

Comments
 (0)