Skip to content

Commit 2754b99

Browse files
committed
plonk_proof_crreate wip
1 parent d6998dc commit 2754b99

File tree

3 files changed

+71
-36
lines changed

3 files changed

+71
-36
lines changed

src/bindings/crypto/bindings-napi.ts

Lines changed: 57 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
import { Buffer } from 'node:buffer';
2-
import { fieldFromRust, fieldToRust, fieldsFromRustFlat, fieldsToRustFlat } from './bindings/conversion-base.js';
3-
import { Gate, OrInfinity, PolyComm, Wire, Field } from './bindings/kimchi-types.js';
41
import { MlArray } from '../../lib/ml/base.js';
5-
import { mapTuple } from './bindings/util.js';
62
import type * as napiNamespace from '../compiled/node_bindings/plonk_wasm.cjs';
3+
import {
4+
fieldFromRust,
5+
fieldToRust,
6+
fieldsFromRustFlat,
7+
fieldsToRustFlat,
8+
} from './bindings/conversion-base.js';
9+
import { mapFromUintArray } from './bindings/conversion-core.js';
10+
import { Field, Gate, LookupTable, OrInfinity, PolyComm, Wire } from './bindings/kimchi-types.js';
11+
import { mapTuple } from './bindings/util.js';
712

813
export { bindingsNapi };
914

@@ -17,7 +22,7 @@ type NapiClasses = {
1722
CommitmentCurve: typeof napiNamespace.WasmGVesta | typeof napiNamespace.WasmGPallas;
1823
makeAffine: () => NapiAffine;
1924
PolyComm: napiNamespace.WasmFpPolyComm | napiNamespace.WasmFqPolyComm;
20-
}
25+
};
2126

2227
function bindingsNapi(napi: any) {
2328
const fpCore = conversionCorePerField({
@@ -37,24 +42,30 @@ function bindingsNapi(napi: any) {
3742
wireToRust([, row, col]: Wire) {
3843
return { row, col };
3944
},
45+
lookupTablesToRust([, ...tables]: MlArray<LookupTable>) {
46+
return tables;
47+
},
48+
runtimeTableCfgsToRust([, ...tables]: MlArray<Uint8Array>) {
49+
return tables.map((table) => Array.from(table));
50+
},
4051
gateToRust(gate: Gate): any {
41-
const [, typ, [, ...wires], coeffs] = gate;
42-
const mapped = mapTuple(wires, (wire) => this.wireToRust(wire));
43-
const nativeWires = {
44-
w0: mapped[0],
45-
w1: mapped[1],
46-
w2: mapped[2],
47-
w3: mapped[3],
48-
w4: mapped[4],
49-
w5: mapped[5],
50-
w6: mapped[6],
51-
} as const;
52-
return {
53-
typ,
54-
wires: nativeWires,
55-
coeffs: Array.from(fieldsToRustFlat(coeffs)),
56-
};
57-
},
52+
const [, typ, [, ...wires], coeffs] = gate;
53+
const mapped = mapTuple(wires, (wire) => this.wireToRust(wire));
54+
const nativeWires = {
55+
w0: mapped[0],
56+
w1: mapped[1],
57+
w2: mapped[2],
58+
w3: mapped[3],
59+
w4: mapped[4],
60+
w5: mapped[5],
61+
w6: mapped[6],
62+
} as const;
63+
return {
64+
typ,
65+
wires: nativeWires,
66+
coeffs: Array.from(fieldsToRustFlat(coeffs)),
67+
};
68+
},
5869
};
5970

6071
return {
@@ -82,16 +93,24 @@ function conversionCorePerField({ makeAffine, PolyComm }: NapiClasses) {
8293
};
8394
const affineFromRust = (pt: NapiAffine): OrInfinity => {
8495
if (pt.infinity) return 0;
96+
console.log('pt', pt);
97+
console.log('pt.x', pt.x);
98+
console.log('pt.y', pt.y);
99+
85100
const xField = fieldFromRust(pt.x);
86101
const yField = fieldFromRust(pt.y);
87102
return [0, [0, xField, yField]];
88-
};
103+
};
89104

90105
const pointToRust = (point: OrInfinity): NapiAffine => affineToRust(point);
91106
const pointFromRust = (point: NapiAffine): OrInfinity => affineFromRust(point);
92107

93-
const pointsToRust = ([, ...points]: MlArray<OrInfinity>): NapiAffine[] => points.map(affineToRust);
94-
const pointsFromRust = (points: NapiAffine[]): MlArray<OrInfinity> => [0, ...points.map(affineFromRust)];
108+
const pointsToRust = ([, ...points]: MlArray<OrInfinity>): NapiAffine[] =>
109+
points.map(affineToRust);
110+
const pointsFromRust = (points: NapiAffine[]): MlArray<OrInfinity> => [
111+
0,
112+
...points.map(affineFromRust),
113+
];
95114

96115
const polyCommToRust = (polyComm: PolyComm): NapiPolyComm => {
97116
const [, camlElems] = polyComm;
@@ -100,10 +119,22 @@ function conversionCorePerField({ makeAffine, PolyComm }: NapiClasses) {
100119
return new PolyCommClass(unshifted as unknown, undefined);
101120
};
102121

103-
const polyCommFromRust = (polyComm: NapiPolyComm): PolyComm => {
122+
/* const polyCommFromRust = (polyComm: NapiPolyComm): PolyComm => {
123+
console.log('polyComm', polyComm);
104124
const rustUnshifted = asArrayLike<NapiAffine>(polyComm.unshifted, 'polyComm.unshifted');
125+
console.log('rustUnshifted', rustUnshifted);
105126
const mlUnshifted = rustUnshifted.map(affineFromRust);
106127
return [0, [0, ...mlUnshifted]];
128+
}; */
129+
const polyCommFromRust = (polyComm: any): any => {
130+
let rustUnshifted = polyComm.unshifted;
131+
console.log('rustUnshifted', rustUnshifted);
132+
let mlUnshifted = mapFromUintArray(rustUnshifted, (ptr) => {
133+
console.log('ptr', ptr);
134+
/* return affineFromRust(wrap(ptr, CommitmentCurve));
135+
*/
136+
});
137+
return [0, [0, ...mlUnshifted]];
107138
};
108139

109140
const polyCommsToRust = ([, ...comms]: MlArray<PolyComm>): NapiPolyComm[] =>

src/bindings/crypto/constants.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// @gen this file is generated from `bindings/ocaml/o1js_constants.ml` - don't edit it directly
22
export {
3-
prefixes,
3+
mocks,
4+
poseidonParamsKimchiFp,
5+
poseidonParamsLegacyFp,
46
prefixHashes,
57
prefixHashesLegacy,
6-
versionBytes,
8+
prefixes,
79
protocolVersions,
8-
poseidonParamsKimchiFp,
9-
poseidonParamsLegacyFp,
10-
mocks,
10+
versionBytes,
1111
};
1212

1313
let prefixes = {

src/lib/proof-system/prover-keys.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55
*
66
* The inputs are `SnarkKeyHeader` and `SnarkKey`, which are OCaml tagged enums defined in pickles_bindings.ml
77
*/
8+
import { Pickles, wasm } from '../../bindings.js';
89
import {
910
WasmPastaFpPlonkIndex,
1011
WasmPastaFqPlonkIndex,
1112
} from '../../bindings/compiled/node_bindings/plonk_wasm.cjs';
12-
import { Pickles, wasm } from '../../bindings.js';
13-
import { VerifierIndex } from '../../bindings/crypto/bindings/kimchi-types.js';
1413
import { getRustConversion } from '../../bindings/crypto/bindings.js';
14+
import { VerifierIndex } from '../../bindings/crypto/bindings/kimchi-types.js';
1515
import { MlString } from '../ml/base.js';
1616
import { CacheHeader, cacheHeaderVersion } from './cache.js';
1717
import type { MethodInterface } from './zkprogram.js';
1818

19-
export { parseHeader, encodeProverKey, decodeProverKey, SnarkKeyHeader, SnarkKey };
19+
export { SnarkKey, SnarkKeyHeader, decodeProverKey, encodeProverKey, parseHeader };
2020
export type { MlWrapVerificationKey };
2121

2222
// there are 4 types of snark keys in Pickles which we all handle at once
@@ -94,7 +94,9 @@ function encodeProverKey(value: SnarkKey): Uint8Array {
9494
switch (value[0]) {
9595
case KeyType.StepProvingKey: {
9696
let index = value[1][1];
97-
let encoded = wasm.caml_pasta_fp_plonk_index_encode(index);
97+
let encoded = wasm.caml_pasta_fp_plonk_index_encode(
98+
(wasm as any).prover_index_fp_from_bytes(index.serialize())
99+
);
98100
return encoded;
99101
}
100102
case KeyType.StepVerificationKey: {
@@ -106,7 +108,9 @@ function encodeProverKey(value: SnarkKey): Uint8Array {
106108
}
107109
case KeyType.WrapProvingKey: {
108110
let index = value[1][1];
109-
let encoded = wasm.caml_pasta_fq_plonk_index_encode(index);
111+
let encoded = wasm.caml_pasta_fq_plonk_index_encode(
112+
(wasm as any).prover_index_fq_from_bytes(index.serialize())
113+
);
110114
return encoded;
111115
}
112116
case KeyType.WrapVerificationKey: {

0 commit comments

Comments
 (0)