Skip to content

Commit 9938faf

Browse files
committed
bindings: conversion bundle for rust between wasm and native
1 parent 4bd05f9 commit 9938faf

File tree

2 files changed

+71
-18
lines changed

2 files changed

+71
-18
lines changed

src/bindings/crypto/bindings.ts

Lines changed: 60 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,15 @@ import { verifierIndexConversion } from './bindings/conversion-verifier-index.js
1616
import { oraclesConversion } from './bindings/conversion-oracles.js';
1717
import { jsEnvironment } from './bindings/env.js';
1818
import { srs } from './bindings/srs.js';
19+
// native
20+
import { conversionCore as conversionCoreNative } from './native/conversion-core.js';
21+
import { fieldsFromRustFlat as fieldsFromRustFlatNative, fieldsToRustFlat as fieldsToRustFlatNative } from './native/conversion-base.js';
22+
import { proofConversion as proofConversionNative } from './native/conversion-proof.js';
23+
import { verifierIndexConversion as verifierIndexConversionNative } from './native/conversion-verifier-index.js';
24+
import { oraclesConversion as oraclesConversionNative } from './native/conversion-oracles.js';
25+
import { srs as srsNative } from './native/srs.js';
1926

20-
export { getRustConversion, RustConversion, Wasm, createNativeRustConversion };
27+
export { getRustConversion, type RustConversion, type NativeConversion, type Wasm };
2128

2229
const tsBindings = {
2330
jsEnvironment,
@@ -31,28 +38,28 @@ const tsBindings = {
3138
...FpVectorBindings,
3239
...FqVectorBindings,
3340
rustConversion: createRustConversion,
34-
nativeRustConversion: createNativeRustConversion,
35-
srs: (wasm: Wasm) => srs(wasm, getRustConversion(wasm)),
41+
srs: (wasm: Wasm) => {
42+
const bundle = getConversionBundle(wasm);
43+
return bundle.srsFactory(wasm, bundle.conversion);
44+
},
3645
};
3746

3847
// this is put in a global variable so that mina/src/lib/crypto/kimchi_bindings/js/bindings.js finds it
3948
(globalThis as any).__snarkyTsBindings = tsBindings;
4049

4150
type Wasm = typeof wasmNamespace;
4251

43-
function createNativeRustConversion(wasm: Wasm) {
44-
return buildConversion(wasm);
52+
function createRustConversion(wasm: Wasm): RustConversion {
53+
return shouldUseNativeConversion(wasm)
54+
? createNativeConversion(wasm)
55+
: createWasmConversion(wasm);
4556
}
4657

47-
function createRustConversion(wasm: Wasm) {
48-
return buildConversion(wasm);
49-
}
50-
51-
function buildConversion(wasm: Wasm) {
52-
let core = conversionCore(wasm);
53-
let verifierIndex = verifierIndexConversion(wasm, core);
54-
let oracles = oraclesConversion(wasm);
55-
let proof = proofConversion(wasm, core);
58+
function createWasmConversion(wasm: Wasm) {
59+
const core = conversionCore(wasm);
60+
const verifierIndex = verifierIndexConversion(wasm, core);
61+
const oracles = oraclesConversion(wasm);
62+
const proof = proofConversion(wasm, core);
5663

5764
return {
5865
fp: { ...core.fp, ...verifierIndex.fp, ...oracles.fp, ...proof.fp },
@@ -64,10 +71,45 @@ function buildConversion(wasm: Wasm) {
6471
};
6572
}
6673

67-
type RustConversion = ReturnType<typeof buildConversion>;
74+
type WasmConversion = ReturnType<typeof createWasmConversion>;
75+
type NativeConversion = ReturnType<typeof createNativeConversion>;
76+
type RustConversion = WasmConversion | NativeConversion;
77+
78+
function getRustConversion(wasm: Wasm): RustConversion {
79+
return createRustConversion(wasm);
80+
}
81+
82+
function createNativeConversion(wasm: Wasm) {
83+
const core = conversionCoreNative(wasm);
84+
const verifierIndex = verifierIndexConversionNative(wasm, core);
85+
const oracles = oraclesConversionNative(wasm);
86+
const proof = proofConversionNative(wasm, core);
87+
88+
return {
89+
fp: { ...core.fp, ...verifierIndex.fp, ...oracles.fp, ...proof.fp },
90+
fq: { ...core.fq, ...verifierIndex.fq, ...oracles.fq, ...proof.fq },
91+
fieldsToRustFlatNative,
92+
fieldsFromRustFlatNative,
93+
wireToRust: core.wireToRust,
94+
mapMlArrayToRustVector: core.mapMlArrayToRustVector,
95+
};
96+
}
97+
98+
function shouldUseNativeConversion(wasm: Wasm): boolean {
99+
const marker = (wasm as any).__kimchi_use_native;
100+
const globalMarker =
101+
typeof globalThis !== 'undefined' &&
102+
(globalThis as any).__kimchi_use_native;
103+
return Boolean(marker || globalMarker);
104+
}
68105

69-
let rustConversion: RustConversion | undefined;
106+
type ConversionBundle =
107+
| { conversion: WasmConversion; srsFactory: typeof srs }
108+
| { conversion: NativeConversion; srsFactory: typeof srsNative };
70109

71-
function getRustConversion(wasm: Wasm) {
72-
return rustConversion ?? (rustConversion = createRustConversion(wasm));
110+
function getConversionBundle(wasm: Wasm): ConversionBundle {
111+
if (shouldUseNativeConversion(wasm)) {
112+
return { conversion: createNativeConversion(wasm), srsFactory: srsNative };
113+
}
114+
return { conversion: createWasmConversion(wasm), srsFactory: srs };
73115
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type * as wasmNamespace from '../../compiled/node_bindings/plonk_wasm.cjs';
2+
3+
import { conversionCore as conversionCoreWasm, ConversionCores } from '../bindings/conversion-core.js';
4+
5+
export type NativeConversionCores = ConversionCores;
6+
7+
type Wasm = typeof wasmNamespace;
8+
9+
export function conversionCore(wasm: Wasm): NativeConversionCores {
10+
return conversionCoreWasm(wasm);
11+
}

0 commit comments

Comments
 (0)