|
| 1 | +import { expect } from 'expect'; |
| 2 | +import { createRequire } from 'node:module'; |
| 3 | +import { bindingsNapi } from '../bindings-napi.js'; |
| 4 | +import type { Field, Gate, Wire } from './kimchi-types.js'; |
| 5 | + |
| 6 | +const require = createRequire(import.meta.url); |
| 7 | + |
| 8 | +function loadNative() { |
| 9 | + const candidates = [ |
| 10 | + '../../compiled/_node_bindings/plonk_napi.node', |
| 11 | + '../../compiled/node_bindings/plonk_napi.node', |
| 12 | + ]; |
| 13 | + for (const path of candidates) { |
| 14 | + try { |
| 15 | + // eslint-disable-next-line @typescript-eslint/no-var-requires |
| 16 | + return require(path); |
| 17 | + } catch (err) { |
| 18 | + if ((err as any).code !== 'MODULE_NOT_FOUND') throw err; |
| 19 | + } |
| 20 | + } |
| 21 | + throw new Error('plonk_napi.node not found in compiled bindings'); |
| 22 | +} |
| 23 | + |
| 24 | +const native: any = loadNative(); |
| 25 | + |
| 26 | +const { fp } = bindingsNapi(native); |
| 27 | + |
| 28 | +const zeroField: Field = [0, 0n]; |
| 29 | +const mlWire = (row: number, col: number): Wire => [0, row, col]; |
| 30 | + |
| 31 | +const sampleGate: Gate = [ |
| 32 | + 0, |
| 33 | + 1, |
| 34 | + [ |
| 35 | + 0, |
| 36 | + mlWire(0, 0), |
| 37 | + mlWire(0, 1), |
| 38 | + mlWire(0, 2), |
| 39 | + mlWire(0, 3), |
| 40 | + mlWire(0, 4), |
| 41 | + mlWire(0, 5), |
| 42 | + mlWire(0, 6), |
| 43 | + ], |
| 44 | + [0, zeroField, zeroField, zeroField, zeroField, zeroField, zeroField, zeroField], |
| 45 | +]; |
| 46 | + |
| 47 | +const vector = native.camlPastaFpPlonkGateVectorCreate(); |
| 48 | +expect(native.camlPastaFpPlonkGateVectorLen(vector)).toBe(0); |
| 49 | + |
| 50 | +native.camlPastaFpPlonkGateVectorAdd(vector, fp.gateToRust(sampleGate)); |
| 51 | +expect(native.camlPastaFpPlonkGateVectorLen(vector)).toBe(1); |
| 52 | + |
| 53 | +const gate0 = native.camlPastaFpPlonkGateVectorGet(vector, 0); |
| 54 | +expect(gate0.typ).toBe(sampleGate[1]); |
| 55 | + |
| 56 | +const rustTarget = fp.wireToRust(mlWire(0, 0)); |
| 57 | +const rustHead = fp.wireToRust(mlWire(1, 2)); |
| 58 | +native.camlPastaFpPlonkGateVectorWrap(vector, rustTarget, rustHead); |
| 59 | +const wrapped = native.camlPastaFpPlonkGateVectorGet(vector, 0); |
| 60 | +expect(wrapped.wires.w0).toEqual({ row: 1, col: 2 }); |
| 61 | + |
| 62 | +native.camlPastaFpPlonkGateVectorDigest(0, vector); |
| 63 | +native.camlPastaFpPlonkCircuitSerialize(0, vector); |
| 64 | + |
| 65 | +console.log('{}', native.camlPastaFpPlonkGateVectorDigest(0, vector)); |
| 66 | + |
| 67 | +console.log('gate vector napi bindings (fp) are working ✔️'); |
0 commit comments