@izi-noir/sdk 0.1.10 → 0.1.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -67,7 +67,9 @@ var init_Barretenberg = __esm({
67
67
  import_noir_js = require("@noir-lang/noir_js");
68
68
  import_bb = require("@aztec/bb.js");
69
69
  Barretenberg = class {
70
- async compile(noirCode) {
70
+ // Note: options parameter is accepted but not used - Barretenberg uses Noir's ACIR directly
71
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
72
+ async compile(noirCode, _options) {
71
73
  const { basePath, cleanup } = await createTempDir();
72
74
  const fm = (0, import_noir_wasm.createFileManager)(basePath);
73
75
  const nargoToml = `[package]
@@ -125,6 +127,377 @@ authors = [""]
125
127
  }
126
128
  });
127
129
 
130
+ // src/infra/provingSystems/R1csBuilder.ts
131
+ var R1csBuilder;
132
+ var init_R1csBuilder = __esm({
133
+ "src/infra/provingSystems/R1csBuilder.ts"() {
134
+ "use strict";
135
+ R1csBuilder = class {
136
+ constructor(parsedCircuit) {
137
+ this.parsedCircuit = parsedCircuit;
138
+ }
139
+ constraints = [];
140
+ witnessMap = /* @__PURE__ */ new Map();
141
+ nextWitnessIdx = 1;
142
+ // w_0 = 1 is reserved
143
+ publicIndices = [];
144
+ privateIndices = [];
145
+ // BN254 scalar field modulus - 1 (for representing -1)
146
+ // Fr modulus = 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001
147
+ // -1 mod Fr = Fr - 1
148
+ NEG_ONE = "0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000000";
149
+ /**
150
+ * Build R1CS definition from the parsed circuit
151
+ */
152
+ build() {
153
+ this.registerInputs();
154
+ for (const stmt of this.parsedCircuit.statements) {
155
+ this.processStatement(stmt);
156
+ }
157
+ return {
158
+ num_witnesses: this.nextWitnessIdx,
159
+ public_inputs: this.publicIndices,
160
+ private_inputs: this.privateIndices,
161
+ constraints: this.constraints
162
+ };
163
+ }
164
+ /**
165
+ * Get the witness index for an input parameter name
166
+ */
167
+ getWitnessIndex(name) {
168
+ return this.witnessMap.get(name);
169
+ }
170
+ /**
171
+ * Register all circuit inputs as witnesses
172
+ * Public inputs come first, then private inputs
173
+ */
174
+ registerInputs() {
175
+ for (const param of this.parsedCircuit.publicParams) {
176
+ const idx = this.nextWitnessIdx++;
177
+ this.witnessMap.set(param.name, idx);
178
+ this.publicIndices.push(idx);
179
+ }
180
+ for (const param of this.parsedCircuit.privateParams) {
181
+ const idx = this.nextWitnessIdx++;
182
+ this.witnessMap.set(param.name, idx);
183
+ this.privateIndices.push(idx);
184
+ }
185
+ }
186
+ /**
187
+ * Process a single statement and generate constraints
188
+ */
189
+ processStatement(stmt) {
190
+ switch (stmt.kind) {
191
+ case "assert":
192
+ this.processAssert(stmt.condition);
193
+ break;
194
+ case "variable_declaration":
195
+ this.processVariableDecl(stmt.name, stmt.initializer);
196
+ break;
197
+ case "assignment":
198
+ this.processAssignment(stmt.target, stmt.value);
199
+ break;
200
+ case "if_statement":
201
+ for (const s of stmt.consequent) {
202
+ this.processStatement(s);
203
+ }
204
+ if (stmt.alternate) {
205
+ for (const s of stmt.alternate) {
206
+ this.processStatement(s);
207
+ }
208
+ }
209
+ break;
210
+ case "for_statement":
211
+ throw new Error(
212
+ "For loops are not yet supported in R1CS generation. Use the Barretenberg backend for complex circuits."
213
+ );
214
+ }
215
+ }
216
+ /**
217
+ * Process an assert statement
218
+ * The condition must evaluate to true for a valid proof
219
+ */
220
+ processAssert(condition) {
221
+ if (condition.kind === "binary") {
222
+ this.processBinaryAssert(condition);
223
+ } else if (condition.kind === "identifier") {
224
+ const idx = this.getOrCreateWitness(condition);
225
+ this.constraints.push({
226
+ a: [["0x1", idx]],
227
+ b: [["0x1", 0]],
228
+ // * 1
229
+ c: [["0x1", 0]]
230
+ // = 1 (w_0)
231
+ });
232
+ } else {
233
+ throw new Error(`Unsupported assert condition kind: ${condition.kind}`);
234
+ }
235
+ }
236
+ /**
237
+ * Process a binary expression in an assert
238
+ */
239
+ processBinaryAssert(expr) {
240
+ const { left, operator, right } = expr;
241
+ switch (operator) {
242
+ case "==":
243
+ this.processEquality(left, right);
244
+ break;
245
+ case "!=":
246
+ throw new Error(
247
+ "Inequality (!=) is not yet supported in R1CS. Use equality (==) instead."
248
+ );
249
+ case "+":
250
+ case "-":
251
+ case "*":
252
+ case "/":
253
+ case "%":
254
+ throw new Error(
255
+ `Arithmetic operator ${operator} must be part of an equality assertion. Use: assert(a ${operator} b == c)`
256
+ );
257
+ case "<":
258
+ case ">":
259
+ case "<=":
260
+ case ">=":
261
+ throw new Error(
262
+ `Comparison operators (${operator}) require range proofs which are not yet supported. Use the Barretenberg backend for comparison operations.`
263
+ );
264
+ case "&":
265
+ case "|":
266
+ throw new Error(
267
+ `Logical operator ${operator} is not yet supported in R1CS.`
268
+ );
269
+ default:
270
+ throw new Error(`Unsupported operator in assert: ${operator}`);
271
+ }
272
+ }
273
+ /**
274
+ * Process an equality assertion: assert(left == right)
275
+ */
276
+ processEquality(left, right) {
277
+ if (left.kind === "binary" && left.operator === "*") {
278
+ this.processMultiplicationEquality(left.left, left.right, right);
279
+ return;
280
+ }
281
+ if (right.kind === "binary" && right.operator === "*") {
282
+ this.processMultiplicationEquality(right.left, right.right, left);
283
+ return;
284
+ }
285
+ if (left.kind === "binary" && left.operator === "+") {
286
+ this.processAdditionEquality(left.left, left.right, right);
287
+ return;
288
+ }
289
+ if (right.kind === "binary" && right.operator === "+") {
290
+ this.processAdditionEquality(right.left, right.right, left);
291
+ return;
292
+ }
293
+ if (left.kind === "binary" && left.operator === "-") {
294
+ this.processSubtractionEquality(left.left, left.right, right);
295
+ return;
296
+ }
297
+ if (right.kind === "binary" && right.operator === "-") {
298
+ this.processSubtractionEquality(right.left, right.right, left);
299
+ return;
300
+ }
301
+ const leftIdx = this.getOrCreateWitness(left);
302
+ const rightIdx = this.getOrCreateWitness(right);
303
+ this.constraints.push({
304
+ a: [
305
+ ["0x1", leftIdx],
306
+ [this.NEG_ONE, rightIdx]
307
+ ],
308
+ // a - b
309
+ b: [["0x1", 0]],
310
+ // * 1 (w_0 = 1)
311
+ c: []
312
+ // = 0
313
+ });
314
+ }
315
+ /**
316
+ * Process multiplication equality: assert(a * b == c)
317
+ * R1CS: a * b = c
318
+ */
319
+ processMultiplicationEquality(left, right, result) {
320
+ const leftIdx = this.getOrCreateWitness(left);
321
+ const rightIdx = this.getOrCreateWitness(right);
322
+ const resultIdx = this.getOrCreateWitness(result);
323
+ this.constraints.push({
324
+ a: [["0x1", leftIdx]],
325
+ b: [["0x1", rightIdx]],
326
+ c: [["0x1", resultIdx]]
327
+ });
328
+ }
329
+ /**
330
+ * Process addition equality: assert(a + b == c)
331
+ * R1CS: (a + b - c) * 1 = 0
332
+ * Which is: (a + b) * 1 = c
333
+ */
334
+ processAdditionEquality(left, right, result) {
335
+ const leftIdx = this.getOrCreateWitness(left);
336
+ const rightIdx = this.getOrCreateWitness(right);
337
+ const resultIdx = this.getOrCreateWitness(result);
338
+ this.constraints.push({
339
+ a: [
340
+ ["0x1", leftIdx],
341
+ ["0x1", rightIdx]
342
+ ],
343
+ // a + b
344
+ b: [["0x1", 0]],
345
+ // * 1
346
+ c: [["0x1", resultIdx]]
347
+ // = c
348
+ });
349
+ }
350
+ /**
351
+ * Process subtraction equality: assert(a - b == c)
352
+ * R1CS: (a - b) * 1 = c
353
+ */
354
+ processSubtractionEquality(left, right, result) {
355
+ const leftIdx = this.getOrCreateWitness(left);
356
+ const rightIdx = this.getOrCreateWitness(right);
357
+ const resultIdx = this.getOrCreateWitness(result);
358
+ this.constraints.push({
359
+ a: [
360
+ ["0x1", leftIdx],
361
+ [this.NEG_ONE, rightIdx]
362
+ ],
363
+ // a - b
364
+ b: [["0x1", 0]],
365
+ // * 1
366
+ c: [["0x1", resultIdx]]
367
+ // = c
368
+ });
369
+ }
370
+ /**
371
+ * Process a variable declaration: let x = expr
372
+ * Creates a new witness for x and adds constraint if needed
373
+ */
374
+ processVariableDecl(name, initializer) {
375
+ const varIdx = this.nextWitnessIdx++;
376
+ this.witnessMap.set(name, varIdx);
377
+ if (initializer.kind === "identifier") {
378
+ const initIdx = this.getOrCreateWitness(initializer);
379
+ this.constraints.push({
380
+ a: [
381
+ ["0x1", varIdx],
382
+ [this.NEG_ONE, initIdx]
383
+ ],
384
+ b: [["0x1", 0]],
385
+ c: []
386
+ });
387
+ } else if (initializer.kind === "literal") {
388
+ } else if (initializer.kind === "binary") {
389
+ this.processVariableInitBinary(varIdx, initializer);
390
+ } else {
391
+ throw new Error(
392
+ `Unsupported initializer kind for variable declaration: ${initializer.kind}`
393
+ );
394
+ }
395
+ }
396
+ /**
397
+ * Process a binary expression as variable initializer
398
+ */
399
+ processVariableInitBinary(varIdx, expr) {
400
+ const { left, operator, right } = expr;
401
+ const leftIdx = this.getOrCreateWitness(left);
402
+ const rightIdx = this.getOrCreateWitness(right);
403
+ switch (operator) {
404
+ case "*":
405
+ this.constraints.push({
406
+ a: [["0x1", leftIdx]],
407
+ b: [["0x1", rightIdx]],
408
+ c: [["0x1", varIdx]]
409
+ });
410
+ break;
411
+ case "+":
412
+ this.constraints.push({
413
+ a: [
414
+ ["0x1", leftIdx],
415
+ ["0x1", rightIdx]
416
+ ],
417
+ b: [["0x1", 0]],
418
+ c: [["0x1", varIdx]]
419
+ });
420
+ break;
421
+ case "-":
422
+ this.constraints.push({
423
+ a: [
424
+ ["0x1", leftIdx],
425
+ [this.NEG_ONE, rightIdx]
426
+ ],
427
+ b: [["0x1", 0]],
428
+ c: [["0x1", varIdx]]
429
+ });
430
+ break;
431
+ default:
432
+ throw new Error(
433
+ `Unsupported operator in variable initializer: ${operator}`
434
+ );
435
+ }
436
+ }
437
+ /**
438
+ * Process an assignment: x = expr
439
+ * Updates the witness mapping
440
+ */
441
+ processAssignment(target, value) {
442
+ const existingIdx = this.witnessMap.get(target);
443
+ if (existingIdx === void 0) {
444
+ throw new Error(`Assignment to undeclared variable: ${target}`);
445
+ }
446
+ if (value.kind === "identifier") {
447
+ const valueIdx = this.getOrCreateWitness(value);
448
+ this.constraints.push({
449
+ a: [
450
+ ["0x1", existingIdx],
451
+ [this.NEG_ONE, valueIdx]
452
+ ],
453
+ b: [["0x1", 0]],
454
+ c: []
455
+ });
456
+ } else if (value.kind === "binary") {
457
+ this.processVariableInitBinary(existingIdx, value);
458
+ }
459
+ }
460
+ /**
461
+ * Get or create a witness index for an expression
462
+ */
463
+ getOrCreateWitness(expr) {
464
+ if (expr.kind === "identifier") {
465
+ const existing = this.witnessMap.get(expr.name);
466
+ if (existing !== void 0) {
467
+ return existing;
468
+ }
469
+ const idx = this.nextWitnessIdx++;
470
+ this.witnessMap.set(expr.name, idx);
471
+ return idx;
472
+ }
473
+ if (expr.kind === "literal") {
474
+ const idx = this.nextWitnessIdx++;
475
+ return idx;
476
+ }
477
+ if (expr.kind === "binary") {
478
+ const idx = this.nextWitnessIdx++;
479
+ this.processVariableInitBinary(idx, expr);
480
+ return idx;
481
+ }
482
+ if (expr.kind === "unary") {
483
+ const operandIdx = this.getOrCreateWitness(expr.operand);
484
+ if (expr.operator === "-") {
485
+ const idx = this.nextWitnessIdx++;
486
+ this.constraints.push({
487
+ a: [[this.NEG_ONE, operandIdx]],
488
+ b: [["0x1", 0]],
489
+ c: [["0x1", idx]]
490
+ });
491
+ return idx;
492
+ }
493
+ throw new Error(`Unsupported unary operator: ${expr.operator}`);
494
+ }
495
+ throw new Error(`Unsupported expression kind: ${expr.kind}`);
496
+ }
497
+ };
498
+ }
499
+ });
500
+
128
501
  // src/infra/provingSystems/ArkworksWasm.ts
129
502
  var ArkworksWasm_exports = {};
130
503
  __export(ArkworksWasm_exports, {
@@ -258,6 +631,7 @@ var init_ArkworksWasm = __esm({
258
631
  import_noir_wasm2 = require("@noir-lang/noir_wasm");
259
632
  import_noir_js2 = require("@noir-lang/noir_js");
260
633
  import_acvm_js = require("@noir-lang/acvm_js");
634
+ init_R1csBuilder();
261
635
  import_meta = {};
262
636
  wasmModule = null;
263
637
  wasmInitPromise2 = null;
@@ -272,8 +646,11 @@ var init_ArkworksWasm = __esm({
272
646
  }
273
647
  /**
274
648
  * Compile Noir code to a circuit with ACIR for Groth16 proving
649
+ *
650
+ * @param noirCode - The Noir source code to compile
651
+ * @param options - Optional compilation options including ParsedCircuit for dynamic R1CS
275
652
  */
276
- async compile(noirCode) {
653
+ async compile(noirCode, options) {
277
654
  const wasm = await initWasm();
278
655
  const { basePath, cleanup } = await createTempDir2();
279
656
  const fm = (0, import_noir_wasm2.createFileManager)(basePath);
@@ -298,36 +675,52 @@ authors = [""]
298
675
  throw new Error("Compilation failed: no bytecode generated");
299
676
  }
300
677
  const parameters = compiled.abi.parameters;
301
- const publicR1csIndices = [];
302
- const privateR1csIndices = [];
678
+ let r1cs;
303
679
  const witnessIndexMapping = /* @__PURE__ */ new Map();
304
- parameters.forEach((p, noirIndex) => {
305
- const r1csIndex = noirIndex + 1;
306
- witnessIndexMapping.set(noirIndex, r1csIndex);
307
- if (p.visibility === "public") {
308
- publicR1csIndices.push(r1csIndex);
309
- } else if (p.visibility === "private") {
310
- privateR1csIndices.push(r1csIndex);
311
- }
312
- });
313
- const r1cs = {
314
- num_witnesses: parameters.length + 1,
315
- // +1 for w_0
316
- public_inputs: publicR1csIndices,
317
- private_inputs: privateR1csIndices,
318
- constraints: []
319
- };
320
- if (privateR1csIndices.length === 1 && publicR1csIndices.length === 1) {
321
- const privateIdx = privateR1csIndices[0];
322
- const publicIdx = publicR1csIndices[0];
323
- r1cs.constraints.push({
324
- a: [["0x1", privateIdx]],
325
- // secret
326
- b: [["0x1", privateIdx]],
327
- // secret
328
- c: [["0x1", publicIdx]]
329
- // expected
680
+ if (options?.parsedCircuit) {
681
+ const builder = new R1csBuilder(options.parsedCircuit);
682
+ r1cs = builder.build();
683
+ parameters.forEach((p, noirIndex) => {
684
+ const r1csIndex = noirIndex + 1;
685
+ witnessIndexMapping.set(noirIndex, r1csIndex);
686
+ });
687
+ console.log("=== R1CS BUILDER DEBUG ===");
688
+ console.log("ParsedCircuit publicParams:", options.parsedCircuit.publicParams);
689
+ console.log("ParsedCircuit privateParams:", options.parsedCircuit.privateParams);
690
+ console.log("ParsedCircuit statements:", JSON.stringify(options.parsedCircuit.statements, null, 2));
691
+ console.log("Generated R1CS:", JSON.stringify(r1cs, null, 2));
692
+ console.log("==========================");
693
+ } else {
694
+ const publicR1csIndices = [];
695
+ const privateR1csIndices = [];
696
+ parameters.forEach((p, noirIndex) => {
697
+ const r1csIndex = noirIndex + 1;
698
+ witnessIndexMapping.set(noirIndex, r1csIndex);
699
+ if (p.visibility === "public") {
700
+ publicR1csIndices.push(r1csIndex);
701
+ } else if (p.visibility === "private") {
702
+ privateR1csIndices.push(r1csIndex);
703
+ }
330
704
  });
705
+ r1cs = {
706
+ num_witnesses: parameters.length + 1,
707
+ public_inputs: publicR1csIndices,
708
+ private_inputs: privateR1csIndices,
709
+ constraints: []
710
+ };
711
+ if (privateR1csIndices.length === 1 && publicR1csIndices.length === 1) {
712
+ const privateIdx = privateR1csIndices[0];
713
+ const publicIdx = publicR1csIndices[0];
714
+ r1cs.constraints.push({
715
+ a: [["0x1", privateIdx]],
716
+ b: [["0x1", privateIdx]],
717
+ c: [["0x1", publicIdx]]
718
+ });
719
+ }
720
+ console.log("=== R1CS HARDCODED DEBUG ===");
721
+ console.log("Using hardcoded R1CS pattern (no ParsedCircuit provided)");
722
+ console.log("R1CS:", JSON.stringify(r1cs, null, 2));
723
+ console.log("============================");
331
724
  }
332
725
  const r1csJson = JSON.stringify(r1cs);
333
726
  let provingKey;
@@ -344,7 +737,7 @@ authors = [""]
344
737
  }
345
738
  }
346
739
  const acirJson = JSON.stringify({
347
- functions: [{ current_witness_index: parameters.length, opcodes: [], private_parameters: privateR1csIndices, public_parameters: { witnesses: publicR1csIndices }, return_values: { witnesses: [] } }]
740
+ functions: [{ current_witness_index: parameters.length, opcodes: [], private_parameters: r1cs.private_inputs, public_parameters: { witnesses: r1cs.public_inputs }, return_values: { witnesses: [] } }]
348
741
  });
349
742
  const arkworksCircuit = {
350
743
  ...compiled,
@@ -1070,16 +1463,20 @@ var IziNoir = class _IziNoir {
1070
1463
  * After compile(), the verifying key is available via `this.vk`.
1071
1464
  *
1072
1465
  * @param noirCode - The Noir source code to compile
1466
+ * @param options - Optional compilation options including ParsedCircuit for dynamic R1CS
1073
1467
  * @returns CompileResult with circuit and verifying key
1074
1468
  *
1075
1469
  * @example
1076
1470
  * ```typescript
1471
+ * // Basic usage
1077
1472
  * const { circuit, verifyingKey } = await izi.compile(noirCode);
1078
- * console.log('VK:', izi.vk); // Available immediately after compile
1473
+ *
1474
+ * // With ParsedCircuit for dynamic R1CS generation
1475
+ * const { circuit } = await izi.compile(noirCode, { parsedCircuit });
1079
1476
  * ```
1080
1477
  */
1081
- async compile(noirCode) {
1082
- this.compiledCircuit = await this.provingSystem.compile(noirCode);
1478
+ async compile(noirCode, options) {
1479
+ this.compiledCircuit = await this.provingSystem.compile(noirCode, options);
1083
1480
  const vk = await this.extractVerifyingKey(this.compiledCircuit);
1084
1481
  if (vk) {
1085
1482
  this._verifyingKey = vk;
@@ -1,16 +1,16 @@
1
- import { I as IProvingSystem } from '../IProvingSystem-BpI0rmve.cjs';
2
- export { C as CircuitPaths, a as IziNoirConfig, P as Provider } from '../IProvingSystem-BpI0rmve.cjs';
1
+ import { I as IProvingSystem, f as CompileOptions } from '../IProvingSystem-SfzgcbqH.cjs';
2
+ export { C as CircuitPaths, a as IziNoirConfig, P as Provider } from '../IProvingSystem-SfzgcbqH.cjs';
3
3
  import { P as ProofData } from '../types-CxkI04bP.cjs';
4
4
  import { CompiledCircuit, InputMap } from '@noir-lang/types';
5
5
  export { CompiledCircuit, InputMap } from '@noir-lang/types';
6
- export { a as IziNoir, i as initNoirWasm, b as isWasmInitialized } from '../wasmInit-D615cpte.cjs';
6
+ export { a as IziNoir, i as initNoirWasm, b as isWasmInitialized } from '../wasmInit-Wyynuk6r.cjs';
7
7
 
8
8
  /**
9
9
  * Barretenberg proving system using WASM.
10
10
  * Browser compatible, produces UltraHonk proofs (~16KB).
11
11
  */
12
12
  declare class Barretenberg implements IProvingSystem {
13
- compile(noirCode: string): Promise<CompiledCircuit>;
13
+ compile(noirCode: string, _options?: CompileOptions): Promise<CompiledCircuit>;
14
14
  generateProof(circuit: CompiledCircuit, inputs: InputMap): Promise<ProofData>;
15
15
  verifyProof(circuit: CompiledCircuit, proof: Uint8Array, publicInputs: string[]): Promise<boolean>;
16
16
  }
@@ -1,16 +1,16 @@
1
- import { I as IProvingSystem } from '../IProvingSystem-D0X9Rp3W.js';
2
- export { C as CircuitPaths, a as IziNoirConfig, P as Provider } from '../IProvingSystem-D0X9Rp3W.js';
1
+ import { I as IProvingSystem, f as CompileOptions } from '../IProvingSystem-BKgFRl27.js';
2
+ export { C as CircuitPaths, a as IziNoirConfig, P as Provider } from '../IProvingSystem-BKgFRl27.js';
3
3
  import { P as ProofData } from '../types-CxkI04bP.js';
4
4
  import { CompiledCircuit, InputMap } from '@noir-lang/types';
5
5
  export { CompiledCircuit, InputMap } from '@noir-lang/types';
6
- export { a as IziNoir, i as initNoirWasm, b as isWasmInitialized } from '../wasmInit-oOZwkgo_.js';
6
+ export { a as IziNoir, i as initNoirWasm, b as isWasmInitialized } from '../wasmInit-D3RyRKIC.js';
7
7
 
8
8
  /**
9
9
  * Barretenberg proving system using WASM.
10
10
  * Browser compatible, produces UltraHonk proofs (~16KB).
11
11
  */
12
12
  declare class Barretenberg implements IProvingSystem {
13
- compile(noirCode: string): Promise<CompiledCircuit>;
13
+ compile(noirCode: string, _options?: CompileOptions): Promise<CompiledCircuit>;
14
14
  generateProof(circuit: CompiledCircuit, inputs: InputMap): Promise<ProofData>;
15
15
  verifyProof(circuit: CompiledCircuit, proof: Uint8Array, publicInputs: string[]): Promise<boolean>;
16
16
  }