@@ -20,6 +20,16 @@ import { bindingsNapi } from './bindings-napi.js';
2020
2121export { getRustConversion , RustConversion , Wasm , createNativeRustConversion } ;
2222
23+
24+ /* TODO: Uncomment in phase 2 of conversion layer
25+ import { conversionCore as conversionCoreNative } from './native/conversion-core.js';
26+ import { fieldsFromRustFlat as fieldsFromRustFlatNative, fieldsToRustFlat as fieldsToRustFlatNative } from './native/conversion-base.js';
27+ import { proofConversion as proofConversionNative } from './native/conversion-proof.js';
28+ import { verifierIndexConversion as verifierIndexConversionNative } from './native/conversion-verifier-index.js';
29+ import { oraclesConversion as oraclesConversionNative } from './native/conversion-oracles.js';
30+
31+ export { getRustConversion, type RustConversion, type NativeConversion, type Wasm };*/
32+
2333const tsBindings = {
2434 jsEnvironment,
2535 prefixHashes,
@@ -33,6 +43,11 @@ const tsBindings = {
3343 ...FqVectorBindings ,
3444 rustConversion : createRustConversion ,
3545 nativeRustConversion : createNativeRustConversion ,
46+ /* TODO: Uncomment in phase 2 of conversion layer
47+ srs: (wasm: Wasm) => {
48+ const bundle = getConversionBundle(wasm);
49+ return bundle.srsFactory(wasm, bundle.conversion);
50+ },*/
3651 srs : ( wasm : Wasm ) => srs ( wasm , getRustConversion ( wasm ) ) ,
3752} ;
3853
@@ -41,15 +56,17 @@ const tsBindings = {
4156
4257type Wasm = typeof wasmNamespace ;
4358
44- function createNativeRustConversion ( napi : any ) {
45- return bindingsNapi ( napi ) ;
59+ type RustConversion = ReturnType < typeof buildWasmConversion > ;
60+
61+ function getRustConversion ( wasm : Wasm ) : RustConversion {
62+ return createRustConversion ( wasm ) ;
4663}
4764
4865function createRustConversion ( wasm : Wasm ) {
49- return buildConversion ( wasm ) ;
66+ return buildWasmConversion ( wasm ) ;
5067}
5168
52- function buildConversion ( wasm : Wasm ) {
69+ function buildWasmConversion ( wasm : Wasm ) {
5370 let core = conversionCore ( wasm ) ;
5471 let verifierIndex = verifierIndexConversion ( wasm , core ) ;
5572 let oracles = oraclesConversion ( wasm ) ;
@@ -65,10 +82,68 @@ function buildConversion(wasm: Wasm) {
6582 } ;
6683}
6784
85+ function createNativeRustConversion ( napi : any ) {
86+ return bindingsNapi ( napi ) ;
87+ }
88+
89+ /* TODO: Uncomment in phase 2 of conversion layer
90+
91+ function shouldUseNativeConversion(wasm: Wasm): boolean {
92+ const marker = (wasm as any).__kimchi_use_native;
93+ const globalMarker =
94+ typeof globalThis !== 'undefined' &&
95+ (globalThis as any).__kimchi_use_native;
96+ return Boolean(marker || globalMarker);
97+ }
98+
99+ function createRustConversion(wasm: Wasm): RustConversion {
100+ return shouldUseNativeConversion(wasm)
101+ ? createNativeConversion(wasm)
102+ : createWasmConversion(wasm);
103+ }
104+
105+ function createWasmConversion(wasm: Wasm) {
106+ const core = conversionCore(wasm);
107+ const verifierIndex = verifierIndexConversion(wasm, core);
108+ const oracles = oraclesConversion(wasm);
109+ const proof = proofConversion(wasm, core);
110+
111+ return {
112+ fp: { ...core.fp, ...verifierIndex.fp, ...oracles.fp, ...proof.fp },
113+ fq: { ...core.fq, ...verifierIndex.fq, ...oracles.fq, ...proof.fq },
114+ fieldsToRustFlat,
115+ fieldsFromRustFlat,
116+ wireToRust: core.wireToRust,
117+ mapMlArrayToRustVector: core.mapMlArrayToRustVector,
118+ };
119+ }
120+
68121type RustConversion = ReturnType<typeof buildConversion>;
69122
70- let rustConversion : RustConversion | undefined ;
123+ function createNativeConversion(wasm: Wasm) {
124+ const core = conversionCoreNative(wasm);
125+ const verifierIndex = verifierIndexConversionNative(wasm, core);
126+ const oracles = oraclesConversionNative(wasm);
127+ const proof = proofConversionNative(wasm, core);
128+
129+ return {
130+ fp: { ...core.fp, ...verifierIndex.fp, ...oracles.fp, ...proof.fp },
131+ fq: { ...core.fq, ...verifierIndex.fq, ...oracles.fq, ...proof.fq },
132+ fieldsToRustFlatNative,
133+ fieldsFromRustFlatNative,
134+ wireToRust: core.wireToRust,
135+ mapMlArrayToRustVector: core.mapMlArrayToRustVector,
136+ };
137+ }
138+
139+ type ConversionBundle =
140+ | { conversion: WasmConversion; srsFactory: typeof srs }
141+ | { conversion: NativeConversion; srsFactory: typeof srsNative };
71142
72- function getRustConversion ( wasm : Wasm ) {
73- return rustConversion ?? ( rustConversion = createRustConversion ( wasm ) ) ;
143+ function getConversionBundle(wasm: Wasm): ConversionBundle {
144+ if (shouldUseNativeConversion(wasm)) {
145+ return { conversion: createNativeConversion(wasm), srsFactory: srsNative };
146+ }
147+ return { conversion: createWasmConversion(wasm), srsFactory: srs };
74148}
149+ */
0 commit comments