Skip to content

Commit 53a6174

Browse files
authored
Merge pull request #18079 from MinaProtocol/florian/native-proof
[WIP] Proof functions + native prover demo
2 parents c53b209 + 857e2a8 commit 53a6174

File tree

11 files changed

+123
-39
lines changed

11 files changed

+123
-39
lines changed

src/lib/crypto/kimchi_bindings/js/bindings/verifier-index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,13 @@ var caml_opt_to_rust = function (caml_optional_value, to_rust) {
2424
// Provides: caml_pasta_fp_plonk_verifier_index_create
2525
// Requires: plonk_wasm, tsRustConversion
2626
var caml_pasta_fp_plonk_verifier_index_create = function (x) {
27-
console.log("caml_pasta_fp_plonk_verifier_index_create")
28-
var vk = plonk_wasm.caml_pasta_fp_plonk_verifier_index_create(x);
27+
console.log("caml_pasta_fp_plonk_verifier_index_create", x)
28+
var bytes = plonk_wasm.prover_index_fp_to_bytes(x);
29+
console.log("bytes", bytes)
30+
var index = plonk_wasm.WasmPastaFpPlonkIndex.deserialize(bytes)
31+
console.log("index", index)
32+
var vk = plonk_wasm.caml_pasta_fp_plonk_verifier_index_create(index);
33+
console.log("vk", vk)
2934
return tsRustConversion.fp.verifierIndexFromRust(vk);
3035
};
3136

src/lib/crypto/kimchi_bindings/js/dune

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
native/native-overrides/oracles.js
2121
native/native-overrides/prover-index.js
2222
native/native-overrides/srs.js
23-
native/native-overrides/util.js))
23+
native/native-overrides/util.js
24+
native/native-overrides/proof.js
25+
native/native-overrides/verifier-index.js))
2426
(instrumentation
2527
(backend bisect_ppx))
2628
(preprocess

src/lib/crypto/kimchi_bindings/js/native/header-d.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@
44
// This file gets auto-included in the generated plonk-napi types to supplement
55
// external pointer types.
66

7-
type WasmPastaFpPlonkIndex = {};
8-
type NapiVector<T> = {};
97
type NapiGVesta = {};
108
type NapiGPallas = {};
119
type NapiPastaFpPlonkIndex = {};
1210
type NapiPastaFqPlonkIndex = {};
13-
type NapiLookupInfo = {};
1411
type NapiPastaFp = {};
1512
type NapiPastaFq = {};
13+
type NapiLookupInfo = {};
14+
type WasmPastaFpPlonkIndex = {};
15+
type WasmPastaFqPlonkIndex = {};
16+
type Proof = {}
17+
type NapiVector<T> = {};
18+
type NapiFlatVector<T> = {};
19+
type WasmVecVecFp = {};
20+
type WasmVecVecFq = {};
21+
type Self = {};
1622

1723
// Header section end
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* global plonk_wasm, tsRustConversion
2+
*/
3+
4+
5+
// Provides: caml_pasta_fp_plonk_proof_create
6+
// Requires: plonk_wasm, tsRustConversion
7+
var caml_pasta_fp_plonk_proof_create = function (
8+
index,
9+
witness_cols,
10+
caml_runtime_tables,
11+
prev_challenges,
12+
prev_sgs
13+
) {
14+
var w = new plonk_wasm.WasmVecVecFp(witness_cols.length - 1);
15+
for (var i = 1; i < witness_cols.length; i++) {
16+
w.push(tsRustConversion.fp.vectorToRust(witness_cols[i]));
17+
}
18+
witness_cols = w;
19+
prev_challenges = tsRustConversion.fp.vectorToRust(prev_challenges);
20+
var wasm_runtime_tables =
21+
tsRustConversion.fp.runtimeTablesToRust(caml_runtime_tables);
22+
prev_sgs = tsRustConversion.fp.pointsToRust(prev_sgs);
23+
var proof = plonk_wasm.caml_pasta_fp_plonk_proof_create(
24+
index,
25+
witness_cols,
26+
wasm_runtime_tables,
27+
prev_challenges,
28+
prev_sgs
29+
);
30+
return tsRustConversion.fp.proofFromRust(proof);
31+
};

src/lib/crypto/kimchi_bindings/js/native/native-overrides/prover-index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ var caml_pasta_fp_plonk_index_create = function (
123123
);
124124
console.time("conversion")
125125
var gate_vec = plonk_wasm.caml_pasta_fp_plonk_gate_vector_from_bytes(gates.serialize());
126-
var urs_ser = plonk_wasm.caml_fp_srs_from_bytes(urs.serialize())
126+
var urs_ser = plonk_wasm.caml_fp_srs_from_bytes_external(urs.serialize())
127127
console.timeEnd("conversion")
128128

129129
console.time("index_create")
@@ -216,7 +216,7 @@ var caml_pasta_fq_plonk_index_create = function (
216216

217217
console.time("conversion")
218218
var gate_vec = plonk_wasm.caml_pasta_fq_plonk_gate_vector_from_bytes(gates.serialize());
219-
var urs_ser = plonk_wasm.caml_fq_srs_from_bytes(urs.serialize())
219+
var urs_ser = plonk_wasm.caml_fq_srs_from_bytes_external(urs.serialize())
220220
console.timeEnd("conversion")
221221

222222
console.time("index_create")

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

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
/* global plonk_wasm, caml_jsstring_of_string,
2-
tsBindings, tsRustConversion
2+
tsBindings, tsRustConversionNative
33
*/
44

55
// Provides: tsSrs
66
// Requires: tsBindings, plonk_wasm
7-
var tsSrs = tsBindings.srs(plonk_wasm);
7+
var tsSrs = tsBindings.srsNative(plonk_wasm);
88

99
// srs
1010

1111
// Provides: caml_fp_srs_create
1212
// Requires: tsSrs
13-
var caml_fp_srs_create = tsSrs.fp.create;
13+
var caml_fp_srs_create = function (log_size) {
14+
console.log("native caml_fp_srs_create");
15+
return tsSrs.fp.create(log_size);
16+
}
1417

1518
// Provides: caml_fp_srs_write
1619
// Requires: plonk_wasm, caml_jsstring_of_string
@@ -43,17 +46,25 @@ var caml_fp_srs_read = function (offset, path) {
4346

4447
// Provides: caml_fp_srs_lagrange_commitments_whole_domain
4548
// Requires: tsSrs
46-
var caml_fp_srs_lagrange_commitments_whole_domain =
47-
tsSrs.fp.lagrangeCommitmentsWholeDomain;
49+
var caml_fp_srs_lagrange_commitments_whole_domain = function (srs, domain_size) {
50+
console.log("native caml_fp_srs_lagrange_commitments_whole_domain");
51+
return tsSrs.fp.lagrangeCommitmentsWholeDomain(srs, domain_size);
52+
}
4853

4954
// Provides: caml_fq_srs_lagrange_commitments_whole_domain
5055
// Requires: tsSrs
5156
var caml_fq_srs_lagrange_commitments_whole_domain =
52-
tsSrs.fq.lagrangeCommitmentsWholeDomain;
57+
function (srs, domain_size) {
58+
console.log("native caml_fq_srs_lagrange_commitments_whole_domain");
59+
return tsSrs.fq.lagrangeCommitmentsWholeDomain(srs, domain_size);
60+
}
5361

5462
// Provides: caml_fp_srs_lagrange_commitment
5563
// Requires: tsSrs
56-
var caml_fp_srs_lagrange_commitment = tsSrs.fp.lagrangeCommitment;
64+
var caml_fp_srs_lagrange_commitment = function (srs, i) {
65+
console.log("native caml_fp_srs_lagrange_commitment");
66+
return tsSrs.fp.lagrangeCommitment(srs, i);
67+
}
5768

5869
// Provides: caml_fp_srs_commit_evaluations
5970
// Requires: plonk_wasm, tsRustConversionNative
@@ -114,11 +125,17 @@ var caml_fp_srs_h = function (t) {
114125

115126
// Provides: caml_fp_srs_add_lagrange_basis
116127
// Requires: tsSrs
117-
var caml_fp_srs_add_lagrange_basis = tsSrs.fp.addLagrangeBasis;
128+
var caml_fp_srs_add_lagrange_basis = function (srs, domain_size) {
129+
console.log("native caml_fp_srs_add_lagrange_basis");
130+
return tsSrs.fp.addLagrangeBasis(srs, domain_size);
131+
};
118132

119133
// Provides: caml_fq_srs_create
120134
// Requires: tsSrs
121-
var caml_fq_srs_create = tsSrs.fq.create;
135+
var caml_fq_srs_create = function (log_size) {
136+
console.log("native caml_fq_srs_create");
137+
return tsSrs.fq.create(log_size);
138+
}
122139

123140
// Provides: caml_fq_srs_write
124141
// Requires: plonk_wasm, caml_jsstring_of_string
@@ -151,7 +168,10 @@ var caml_fq_srs_read = function (offset, path) {
151168

152169
// Provides: caml_fq_srs_lagrange_commitment
153170
// Requires: tsSrs
154-
var caml_fq_srs_lagrange_commitment = tsSrs.fq.lagrangeCommitment;
171+
var caml_fq_srs_lagrange_commitment = function (srs, i) {
172+
console.log("native caml_fq_srs_lagrange_commitment");
173+
return tsSrs.fq.lagrangeCommitment(srs, i);
174+
}
155175

156176
// Provides: caml_fq_srs_commit_evaluations
157177
// Requires: plonk_wasm, tsRustConversionNative
@@ -169,10 +189,14 @@ var caml_fq_srs_commit_evaluations = function (t, domain_size, fqs) {
169189
// Requires: plonk_wasm, tsRustConversionNative
170190
var caml_fq_srs_b_poly_commitment = function (srs, chals) {
171191
console.log("native caml_fq_srs_b_poly_commitment");
192+
console.log("srs", srs);
193+
console.log("chals", chals);
194+
console.log("conv", tsRustConversionNative.fq.vectorToRust(chals))
172195
var res = plonk_wasm.caml_fq_srs_b_poly_commitment(
173196
srs,
174197
tsRustConversionNative.fq.vectorToRust(chals)
175198
);
199+
console.log("res", res);
176200
return tsRustConversionNative.fq.polyCommFromRust(res);
177201
};
178202

@@ -212,4 +236,7 @@ var caml_fq_srs_h = function (t) {
212236

213237
// Provides: caml_fq_srs_add_lagrange_basis
214238
// Requires: tsSrs
215-
var caml_fq_srs_add_lagrange_basis = tsSrs.fq.addLagrangeBasis;
239+
var caml_fq_srs_add_lagrange_basis = function (srs, domain_size) {
240+
console.log("native caml_fq_srs_add_lagrange_basis");
241+
return tsSrs.fq.addLagrangeBasis(srs, domain_size);
242+
};

src/lib/crypto/kimchi_bindings/js/native/native-overrides/verifier-index.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,20 @@
44
// Provides: caml_pasta_fq_plonk_verifier_index_shifts
55
// Requires: plonk_wasm, tsRustConversionNative
66
var caml_pasta_fq_plonk_verifier_index_shifts = function (log2_size) {
7-
return tsRustConversionNative.fq.shiftsFromRust(
8-
plonk_wasm.caml_pasta_fq_plonk_verifier_index_shifts(log2_size)
9-
);
7+
console.log("log2_size", log2_size);
8+
try {
9+
var shifts = plonk_wasm.caml_pasta_fq_plonk_verifier_index_shifts(log2_size);
10+
console.log("shifts", shifts);
11+
console.log("shiftsFromRust", tsRustConversionNative.fq.shiftsFromRust(
12+
shifts
13+
))
14+
return tsRustConversionNative.fq.shiftsFromRust(
15+
shifts
16+
);
17+
} catch (e) {
18+
console.error("Error calling caml_pasta_fq_plonk_verifier_index_shifts:", e);
19+
throw e;
20+
}
1021
};
1122

1223
// Provides: caml_pasta_fp_plonk_verifier_index_shifts

src/lib/crypto/kimchi_bindings/js/node_js/node_backend.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@ var plonk_wasm = (function() {
1717
"prover_index_fq_to_bytes",
1818
"caml_pasta_fp_poseidon_block_cipher",
1919
"caml_pasta_fq_poseidon_block_cipher",
20+
"caml_pasta_fp_plonk_proof_create",
2021
"caml_pasta_fp_plonk_verifier_index_shifts",
2122
"caml_pasta_fq_plonk_verifier_index_shifts",
22-
"WasmFpPolyComm",
23-
"WasmFqPolyComm",
2423
"caml_pasta_fp_plonk_gate_vector_create",
2524
"caml_pasta_fq_plonk_gate_vector_create",
2625
"caml_pasta_fp_plonk_gate_vector_add",
@@ -60,8 +59,8 @@ var plonk_wasm = (function() {
6059
"WasmFpSrs",
6160
"caml_fp_srs_create",
6261
"caml_fp_srs_create_parallel",
63-
"caml_fq_srs_get",
64-
"caml_fq_srs_set",
62+
"caml_fp_srs_get",
63+
"caml_fp_srs_set",
6564
"caml_fp_srs_write",
6665
"caml_fp_srs_read",
6766
"caml_fp_srs_add_lagrange_basis",
@@ -83,16 +82,19 @@ var plonk_wasm = (function() {
8382
"caml_fq_srs_batch_accumulator_check",
8483
"caml_fq_srs_batch_accumulator_generate",
8584
"caml_fq_srs_h",
86-
"WasmFpPolyComm",
87-
"WasmFqPolyComm",
88-
"WasmGPallas",
89-
"WasmGVesta",
85+
/* "WasmFpPolyComm",
86+
"WasmFqPolyComm", */
87+
/* "WasmGPallas",
88+
"WasmGVesta", */
9089
"WasmPastaFp",
9190
"WasmPastaFq",
9291
];
9392

9493
overrides.forEach(function (override) {
95-
wasm[override] = native[override]
94+
wasm[override] = function(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12) {
95+
console.log("calling native override:", override);
96+
return native[override](x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12);
97+
}
9698
})
9799
wasm.native = true;
98100
} catch (e) {
@@ -101,4 +103,4 @@ var plonk_wasm = (function() {
101103
}
102104
}
103105
return wasm
104-
})()
106+
})()

src/lib/crypto/kimchi_bindings/stubs/kimchi_bindings.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@ module Protocol = struct
215215
-> SRS.Fp.t
216216
-> bool
217217
-> t
218-
= "caml_pasta_fp_plonk_index_create_bytecode" "caml_pasta_fp_plonk_index_create"
218+
= "caml_pasta_fp_plonk_index_create_bytecode"
219+
"caml_pasta_fp_plonk_index_create"
219220

220221
external max_degree : t -> int = "caml_pasta_fp_plonk_index_max_degree"
221222

@@ -250,7 +251,8 @@ module Protocol = struct
250251
-> SRS.Fq.t
251252
-> bool
252253
-> t
253-
= "caml_pasta_fq_plonk_index_create_bytecode" "caml_pasta_fq_plonk_index_create"
254+
= "caml_pasta_fq_plonk_index_create_bytecode"
255+
"caml_pasta_fq_plonk_index_create"
254256

255257
external max_degree : t -> int = "caml_pasta_fq_plonk_index_max_degree"
256258

0 commit comments

Comments
 (0)