From 66bf44ca4a91cb9ad6b22ad6daffc2b4b41ee042 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Tue, 10 Sep 2024 23:21:40 -0700 Subject: [PATCH 001/136] feat(zkprogram.ts): change compileProgram method publicInputType from ProvablePure to Provable --- src/lib/proof-system/zkprogram.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 8c5f83da9b..28bda41eee 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -966,7 +966,7 @@ async function compileProgram({ forceRecompile, overrideWrapDomain, }: { - publicInputType: ProvablePure; + publicInputType: Provable; publicOutputType: ProvablePure; methodIntfs: MethodInterface[]; methods: ((...args: any) => unknown)[]; From 3a7c5d6d2a547c1354258b4f941c05d2f7fdb581 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Tue, 10 Sep 2024 23:45:51 -0700 Subject: [PATCH 002/136] feat(zkprogram.ts): change compileProgram method publicOutputType from ProvablePure to Provable --- src/lib/proof-system/zkprogram.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 28bda41eee..73dc089613 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -967,7 +967,7 @@ async function compileProgram({ overrideWrapDomain, }: { publicInputType: Provable; - publicOutputType: ProvablePure; + publicOutputType: Provable; methodIntfs: MethodInterface[]; methods: ((...args: any) => unknown)[]; gates: Gate[][]; From 57e38d258aa56606966a1e4d50b1d13c2e16af2e Mon Sep 17 00:00:00 2001 From: ymekuria Date: Tue, 10 Sep 2024 23:52:52 -0700 Subject: [PATCH 003/136] feat(zkprogram.ts): add createProgramState function to initialize program state for zk programs --- src/lib/proof-system/zkprogram.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 73dc089613..28c412a3e3 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -160,6 +160,10 @@ const FeatureFlags = { fromZkProgramList, }; +function createProgramState() { + +} + async function fromZkProgramList(programs: Array) { let flatMethodIntfs: Array>> = []; From bc711c7a83143478e0faf9e00937ccbf0ff4f540 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 11 Sep 2024 11:41:25 -0700 Subject: [PATCH 004/136] feat(zkprogram.ts): add auxiliaryValues map in createProgramState function --- src/lib/proof-system/zkprogram.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 28c412a3e3..22adc4af35 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -162,6 +162,10 @@ const FeatureFlags = { function createProgramState() { +function createProgramState() { + let auxiliaryValues: Map> = new Map(); + +} } async function fromZkProgramList(programs: Array) { From 0bec8d04388bea6535c5c78b97bae80ba6cd899a Mon Sep 17 00:00:00 2001 From: ymekuria Date: Thu, 12 Sep 2024 10:21:31 -0700 Subject: [PATCH 005/136] feat(zkprogram.ts): add get method to createProgramState --- src/lib/proof-system/zkprogram.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 22adc4af35..ef62908e10 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -160,11 +160,12 @@ const FeatureFlags = { fromZkProgramList, }; -function createProgramState() { - function createProgramState() { let auxiliaryValues: Map> = new Map(); + return { + get: (methodName: string) => { + } } } From b9207360ebc2f0d0d23ba774e73ec33f91b2587d Mon Sep 17 00:00:00 2001 From: ymekuria Date: Thu, 12 Sep 2024 10:31:30 -0700 Subject: [PATCH 006/136] refactor(zkprogram.ts): rename 'get' method to 'getAuxiliaryOutput' for clarity and consistency with its purpose --- src/lib/proof-system/zkprogram.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index ef62908e10..c86ccd95c6 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -163,7 +163,7 @@ const FeatureFlags = { function createProgramState() { let auxiliaryValues: Map> = new Map(); return { - get: (methodName: string) => { + getAuxiliaryOutput: (methodName: string) => { } } From e7c34853e9e208fac4b5aae63d6b24e6c552354c Mon Sep 17 00:00:00 2001 From: ymekuria Date: Thu, 12 Sep 2024 10:33:28 -0700 Subject: [PATCH 007/136] refactor(zkprogram.ts): rename auxiliaryValues variable to methodCache for better clarity and consistency in naming conventions --- src/lib/proof-system/zkprogram.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index c86ccd95c6..b0545b73ff 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -161,7 +161,7 @@ const FeatureFlags = { }; function createProgramState() { - let auxiliaryValues: Map> = new Map(); + let methodCache: Map> = new Map(); return { getAuxiliaryOutput: (methodName: string) => { From 55aafd5ea41bb5a177511f050d0f1aa02de69a45 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Thu, 12 Sep 2024 11:43:43 -0700 Subject: [PATCH 008/136] feat(zkprogram.ts): add setAuxiliaryOutput method to program state for setting auxiliary output values --- src/lib/proof-system/zkprogram.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index b0545b73ff..253e3fd884 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -163,6 +163,8 @@ const FeatureFlags = { function createProgramState() { let methodCache: Map> = new Map(); return { + setAuxiliaryOutput(value: Provable, methodName: string) { + }, getAuxiliaryOutput: (methodName: string) => { } From dc35f8e162cbba021f951bd9ee6c2ef73dbc5be6 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Mon, 16 Sep 2024 00:00:17 -0700 Subject: [PATCH 009/136] feat(zkprogram.ts): get auxiliray output from cache --- src/lib/proof-system/zkprogram.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 253e3fd884..7d3765fd06 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -166,7 +166,8 @@ function createProgramState() { setAuxiliaryOutput(value: Provable, methodName: string) { }, getAuxiliaryOutput: (methodName: string) => { - + let entry = methodCache.get(methodName); + return entry; } } } From 7d9b3c0d8e9ad00a2689492f49bfa536fa977fa9 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Mon, 16 Sep 2024 01:13:15 -0700 Subject: [PATCH 010/136] feat(zkprogram.ts): initialize programState cache --- src/lib/proof-system/zkprogram.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 7d3765fd06..f2a296c243 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -168,7 +168,9 @@ function createProgramState() { getAuxiliaryOutput: (methodName: string) => { let entry = methodCache.get(methodName); return entry; - } + } + + } } @@ -675,6 +677,8 @@ function ZkProgram< } | undefined; + const programState = createProgramState(); + async function compile({ cache = Cache.FileSystemDefault, forceRecompile = false, From 8145573357f1393f3fc335166bdaa7e6d47a891c Mon Sep 17 00:00:00 2001 From: ymekuria Date: Mon, 16 Sep 2024 01:16:11 -0700 Subject: [PATCH 011/136] feat(zkprogram.ts): add programState as a parameter in the compile function --- src/lib/proof-system/zkprogram.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index f2a296c243..186342421a 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -678,7 +678,7 @@ function ZkProgram< | undefined; const programState = createProgramState(); - + async function compile({ cache = Cache.FileSystemDefault, forceRecompile = false, @@ -700,6 +700,7 @@ function ZkProgram< cache, forceRecompile, overrideWrapDomain: config.overrideWrapDomain, + state: programState }); compileOutput = { provers, verify }; From e81eea3a08522616a0d7e3b3d007c3615a13a53d Mon Sep 17 00:00:00 2001 From: ymekuria Date: Mon, 16 Sep 2024 02:06:27 -0700 Subject: [PATCH 012/136] refactor(zkprogram.ts): add optional 'state' parameter to compileProgram function for future use and flexibility --- src/lib/proof-system/zkprogram.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 186342421a..08ec3ae7dc 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -982,6 +982,7 @@ async function compileProgram({ cache, forceRecompile, overrideWrapDomain, + state }: { publicInputType: Provable; publicOutputType: Provable; @@ -992,6 +993,7 @@ async function compileProgram({ cache: Cache; forceRecompile: boolean; overrideWrapDomain?: 0 | 1 | 2; + state?: any }) { await initializeBindings(); if (methodIntfs.length === 0) From 69990386592846110c4e8e95b567ad8d118313ea Mon Sep 17 00:00:00 2001 From: ymekuria Date: Mon, 16 Sep 2024 15:00:01 -0700 Subject: [PATCH 013/136] feat(zkprogram.ts): decompose pure values from auxiliary data type --- src/lib/proof-system/zkprogram.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 08ec3ae7dc..b94458dc01 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -1001,6 +1001,9 @@ async function compileProgram({ Try adding a method to your ZkProgram or SmartContract. If you are using a SmartContract, make sure you are using the @method decorator.`); + // decompose auxilary data type + let pureValues = publicInputType.toFields(publicInputType); + let rules = methodIntfs.map((methodEntry, i) => picklesRuleFromFunction( publicInputType, From dbc9edf22cf67edca1a89ec3721401e88b8bd7cc Mon Sep 17 00:00:00 2001 From: ymekuria Date: Mon, 16 Sep 2024 15:04:45 -0700 Subject: [PATCH 014/136] refactor(zkprogram.ts): pass pure publicInput to picklesFromFunction --- src/lib/proof-system/zkprogram.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index b94458dc01..1d9f840775 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -1002,11 +1002,11 @@ Try adding a method to your ZkProgram or SmartContract. If you are using a SmartContract, make sure you are using the @method decorator.`); // decompose auxilary data type - let pureValues = publicInputType.toFields(publicInputType); - + let pureValues: Field[] = publicInputType.toFields(publicInputType); + let rules = methodIntfs.map((methodEntry, i) => picklesRuleFromFunction( - publicInputType, + pureValues, publicOutputType, methods[i], proofSystemTag, From 0a6e5848d48865ee7cddc26c708d929f42e29e3f Mon Sep 17 00:00:00 2001 From: ymekuria Date: Mon, 16 Sep 2024 15:10:21 -0700 Subject: [PATCH 015/136] feat(zkprogram.ts): decompose auxiliary data from public input --- src/lib/proof-system/zkprogram.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 1d9f840775..ffa0d886ff 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -1004,6 +1004,8 @@ If you are using a SmartContract, make sure you are using the @method decorator. // decompose auxilary data type let pureValues: Field[] = publicInputType.toFields(publicInputType); + let auxilaryPublicInput = publicInputType.toAuxiliary() + let rules = methodIntfs.map((methodEntry, i) => picklesRuleFromFunction( pureValues, From c5ffd545fb35c3aea85579c9ef5e25d658346de3 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Tue, 17 Sep 2024 11:49:01 -0700 Subject: [PATCH 016/136] feat(zkprogram.ts): add setAuxilaryInput method to creatProgramState function --- src/lib/proof-system/zkprogram.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index ffa0d886ff..ba813d8d35 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -165,6 +165,8 @@ function createProgramState() { return { setAuxiliaryOutput(value: Provable, methodName: string) { }, + setAuxilaryInput(value: Provable, methodName: string){}, + getAuxiliaryOutput: (methodName: string) => { let entry = methodCache.get(methodName); return entry; From 3c56586a5e4c4cbe02929072c4be024ffe7de9a1 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Tue, 17 Sep 2024 12:03:46 -0700 Subject: [PATCH 017/136] feat(zkprogram.ts): update method to add auxiliary input data to cache --- src/lib/proof-system/zkprogram.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index ba813d8d35..5d8ed12348 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -165,15 +165,15 @@ function createProgramState() { return { setAuxiliaryOutput(value: Provable, methodName: string) { }, - setAuxilaryInput(value: Provable, methodName: string){}, - - getAuxiliaryOutput: (methodName: string) => { - let entry = methodCache.get(methodName); - return entry; + setAuxilaryInput(value: Provable, methodName: string){ + methodCache.set(methodName, value); + }, + getAuxiliaryOutput: (methodName: string) => { + let entry = methodCache.get(methodName); + return entry; } - - -} + + } } async function fromZkProgramList(programs: Array) { From da4a7c318ca398e354715a722d51a053ed863615 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Tue, 17 Sep 2024 12:08:31 -0700 Subject: [PATCH 018/136] refactor(zkprogram.ts): rename getAuxiliaryOutput to getAuxiliaryInput for consistency and clarity --- src/lib/proof-system/zkprogram.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 5d8ed12348..9de465e35c 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -168,7 +168,7 @@ function createProgramState() { setAuxilaryInput(value: Provable, methodName: string){ methodCache.set(methodName, value); }, - getAuxiliaryOutput: (methodName: string) => { + getAuxilaryInput: (methodName: string) => { let entry = methodCache.get(methodName); return entry; } From 00f62e3da7062572f1478b41e5c1d0b5a45fb5b5 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 18 Sep 2024 01:05:48 -0700 Subject: [PATCH 019/136] refactor(zkprogram.ts): rename publicInputAuxilary variable --- src/lib/proof-system/zkprogram.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 9de465e35c..ad07f77558 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -1003,14 +1003,14 @@ async function compileProgram({ Try adding a method to your ZkProgram or SmartContract. If you are using a SmartContract, make sure you are using the @method decorator.`); - // decompose auxilary data type - let pureValues: Field[] = publicInputType.toFields(publicInputType); + // decompose auxiliary data type + let purePublicInput = publicInputType.toFields(publicInputType); let auxilaryPublicInput = publicInputType.toAuxiliary() - + let rules = methodIntfs.map((methodEntry, i) => picklesRuleFromFunction( - pureValues, + purePublicInput, publicOutputType, methods[i], proofSystemTag, From 4eadd0caa275d2d20f3c304eed591494214a5b44 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 18 Sep 2024 01:20:56 -0700 Subject: [PATCH 020/136] feat(zkprogram.ts): storing of publicInput auxiliary data in cache --- src/lib/proof-system/zkprogram.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index ad07f77558..b10329de5b 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -1007,7 +1007,10 @@ If you are using a SmartContract, make sure you are using the @method decorator. let purePublicInput = publicInputType.toFields(publicInputType); let auxilaryPublicInput = publicInputType.toAuxiliary() - + + // store auxiliary data in cache + state.setAuxilaryInput(auxilaryPublicInput,'auxinput'); + let rules = methodIntfs.map((methodEntry, i) => picklesRuleFromFunction( purePublicInput, From 4cb4b18ed9c33aa8e78974791ed669d83c5ae7f3 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 18 Sep 2024 01:39:25 -0700 Subject: [PATCH 021/136] feat(zkprogram.ts): get auxiliary data from cache --- src/lib/proof-system/zkprogram.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index b10329de5b..741dfab554 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -763,6 +763,8 @@ function ZkProgram< let [publicOutputFields, proof] = MlPair.from(result); let publicOutput = fromFieldConsts(publicOutputType, publicOutputFields); + let publicInputAuxilirary = programState.getAuxilaryInput('auxinput') + return new ProgramProof({ publicInput, publicOutput, @@ -1007,7 +1009,7 @@ If you are using a SmartContract, make sure you are using the @method decorator. let purePublicInput = publicInputType.toFields(publicInputType); let auxilaryPublicInput = publicInputType.toAuxiliary() - + // store auxiliary data in cache state.setAuxilaryInput(auxilaryPublicInput,'auxinput'); From 5d8047b81c2472813f8a9f0d553fad4b6f558e4d Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 18 Sep 2024 01:44:11 -0700 Subject: [PATCH 022/136] feat(zkprogram.ts): recompose the input data to include auxiliary --- src/lib/proof-system/zkprogram.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 741dfab554..d2efc1db9a 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -764,9 +764,13 @@ function ZkProgram< let publicOutput = fromFieldConsts(publicOutputType, publicOutputFields); let publicInputAuxilirary = programState.getAuxilaryInput('auxinput') - + // recompose auxiliary data + let nonPureInput = publicInputType.fromValue({ + publicInput, + publicInputAuxilirary + } ) return new ProgramProof({ - publicInput, + nonPureInput, publicOutput, proof, maxProofsVerified, From e9d936fe1c706d283ba0a384282d86c13bd91948 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 18 Sep 2024 02:13:56 -0700 Subject: [PATCH 023/136] feat(zkprogram.ts): recompose data type from pure values and auxiliary data --- src/lib/proof-system/zkprogram.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index d2efc1db9a..d21ab994aa 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -765,7 +765,7 @@ function ZkProgram< let publicInputAuxilirary = programState.getAuxilaryInput('auxinput') // recompose auxiliary data - let nonPureInput = publicInputType.fromValue({ + let nonPureInput = publicInputType.fromFields({ publicInput, publicInputAuxilirary } ) From de947d46515b5bb9f94ac1ac267776fbcf12b054 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 18 Sep 2024 09:22:47 -0700 Subject: [PATCH 024/136] refactor(zkprogram.ts): rename methodCache to auxInputCache for better clarity and consistency --- src/lib/proof-system/zkprogram.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index d21ab994aa..51b22f81c5 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -161,15 +161,13 @@ const FeatureFlags = { }; function createProgramState() { - let methodCache: Map> = new Map(); + let auxInputCache: Map> = new Map(); return { - setAuxiliaryOutput(value: Provable, methodName: string) { + setAuxilaryInput(value: Provable){ + auxInputCache.set('auxinput', value); }, - setAuxilaryInput(value: Provable, methodName: string){ - methodCache.set(methodName, value); - }, - getAuxilaryInput: (methodName: string) => { - let entry = methodCache.get(methodName); + getAuxilaryInput: () => { + let entry = auxInputCache.get('auxinput'); return entry; } From ef8f3cec88b8ba5ffef47c8b60f79328359deb97 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 18 Sep 2024 10:14:35 -0700 Subject: [PATCH 025/136] feat(zkprogram.ts): reset auxiliary cache --- src/lib/proof-system/zkprogram.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 51b22f81c5..e2411271f8 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -169,7 +169,10 @@ function createProgramState() { getAuxilaryInput: () => { let entry = auxInputCache.get('auxinput'); return entry; - } + }, + resetAuxCache() { + auxInputCache.delete('auxinput'); + }, } } @@ -761,12 +764,17 @@ function ZkProgram< let [publicOutputFields, proof] = MlPair.from(result); let publicOutput = fromFieldConsts(publicOutputType, publicOutputFields); - let publicInputAuxilirary = programState.getAuxilaryInput('auxinput') + let publicInputAuxilirary = programState.getAuxilaryInput() + + // reset auxiliary data + programState.resetAuxCache() // recompose auxiliary data + let nonPureInput = publicInputType.fromFields({ publicInput, publicInputAuxilirary } ) + return new ProgramProof({ nonPureInput, publicOutput, @@ -1013,7 +1021,7 @@ If you are using a SmartContract, make sure you are using the @method decorator. let auxilaryPublicInput = publicInputType.toAuxiliary() // store auxiliary data in cache - state.setAuxilaryInput(auxilaryPublicInput,'auxinput'); + state.setAuxilaryInput(auxilaryPublicInput); let rules = methodIntfs.map((methodEntry, i) => picklesRuleFromFunction( From 16c4ea750624f0321b5c2ee8e7646ca4994049c8 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 18 Sep 2024 10:38:06 -0700 Subject: [PATCH 026/136] feat(program-with-non-pure-input.ts): add a example with nonpure input --- .../zkprogram/program-with-non-pure-input.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/examples/zkprogram/program-with-non-pure-input.ts diff --git a/src/examples/zkprogram/program-with-non-pure-input.ts b/src/examples/zkprogram/program-with-non-pure-input.ts new file mode 100644 index 0000000000..1f8b498a87 --- /dev/null +++ b/src/examples/zkprogram/program-with-non-pure-input.ts @@ -0,0 +1,13 @@ +import { Field, Provable, Struct, ZkProgram, assert } from 'o1js'; + +class MyStruct extends Struct({ + label: String, + value: Field, +}) {} + +let MyProgram = ZkProgram({ + name: 'example-with-non-pure-inputs', + publicOutput: Field, + publicInput: [MyStruct], + methods: {}, +}); From a56602b5efcf96c1aa3a79e91f313ee4eed7df0a Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 18 Sep 2024 10:57:01 -0700 Subject: [PATCH 027/136] refactor(zkprogram.ts): fix typo in function name 'setAuxilaryInput' to 'setAuxiliaryInput' for consistency --- src/lib/proof-system/zkprogram.ts | 43 +++++++++++++++---------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index e2411271f8..40f383b96b 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -163,18 +163,17 @@ const FeatureFlags = { function createProgramState() { let auxInputCache: Map> = new Map(); return { - setAuxilaryInput(value: Provable){ + setAuxilaryInput(value: Provable) { auxInputCache.set('auxinput', value); }, getAuxilaryInput: () => { - let entry = auxInputCache.get('auxinput'); - return entry; + let entry = auxInputCache.get('auxinput'); + return entry; }, resetAuxCache() { auxInputCache.delete('auxinput'); - }, - - } + }, + }; } async function fromZkProgramList(programs: Array) { @@ -680,7 +679,7 @@ function ZkProgram< } | undefined; - const programState = createProgramState(); + const programState = createProgramState(); async function compile({ cache = Cache.FileSystemDefault, @@ -703,7 +702,7 @@ function ZkProgram< cache, forceRecompile, overrideWrapDomain: config.overrideWrapDomain, - state: programState + state: programState, }); compileOutput = { provers, verify }; @@ -764,16 +763,16 @@ function ZkProgram< let [publicOutputFields, proof] = MlPair.from(result); let publicOutput = fromFieldConsts(publicOutputType, publicOutputFields); - let publicInputAuxilirary = programState.getAuxilaryInput() + let publicInputAuxilirary = programState.getAuxilaryInput(); - // reset auxiliary data - programState.resetAuxCache() - // recompose auxiliary data - + // recompose auxiliary data let nonPureInput = publicInputType.fromFields({ - publicInput, - publicInputAuxilirary - } ) + publicInput, + publicInputAuxilirary, + }); + + // reset auxiliary data + programState.resetAuxCache(); return new ProgramProof({ nonPureInput, @@ -996,10 +995,10 @@ async function compileProgram({ cache, forceRecompile, overrideWrapDomain, - state + state, }: { publicInputType: Provable; - publicOutputType: Provable; + publicOutputType: Provable; methodIntfs: MethodInterface[]; methods: ((...args: any) => unknown)[]; gates: Gate[][]; @@ -1007,7 +1006,7 @@ async function compileProgram({ cache: Cache; forceRecompile: boolean; overrideWrapDomain?: 0 | 1 | 2; - state?: any + state?: any; }) { await initializeBindings(); if (methodIntfs.length === 0) @@ -1018,11 +1017,11 @@ If you are using a SmartContract, make sure you are using the @method decorator. // decompose auxiliary data type let purePublicInput = publicInputType.toFields(publicInputType); - let auxilaryPublicInput = publicInputType.toAuxiliary() + let auxilaryPublicInput = publicInputType.toAuxiliary(); - // store auxiliary data in cache + // store auxiliary data in cache state.setAuxilaryInput(auxilaryPublicInput); - + let rules = methodIntfs.map((methodEntry, i) => picklesRuleFromFunction( purePublicInput, From 0130410e51276dcc7fa7cd746b5786869ef5675c Mon Sep 17 00:00:00 2001 From: ymekuria Date: Mon, 7 Oct 2024 21:03:50 -0700 Subject: [PATCH 028/136] chore(bindings): update subproject commit reference to 59eb4c0c3 for consistency --- src/bindings | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bindings b/src/bindings index 8e99acc94d..59eb4c0c3c 160000 --- a/src/bindings +++ b/src/bindings @@ -1 +1 @@ -Subproject commit 8e99acc94da892eeb090367844f85d22ecf4f93b +Subproject commit 59eb4c0c3c126819a88624e0445ea31f89c5850a From b9807813602f49c7ed1539370612e5675fa61241 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Mon, 7 Oct 2024 21:23:51 -0700 Subject: [PATCH 029/136] chore(mina): update subproject commit reference to 24c8b2d723fb09d0d7f996b6ac35373dc27084ef --- src/mina | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mina b/src/mina index 9bdaae9990..24c8b2d723 160000 --- a/src/mina +++ b/src/mina @@ -1 +1 @@ -Subproject commit 9bdaae99904c13e3e10841ec6b778e45cdacabfb +Subproject commit 24c8b2d723fb09d0d7f996b6ac35373dc27084ef From 764c68b7705a4053ec17aa3674aff9bd946573e8 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Tue, 8 Oct 2024 00:28:25 -0700 Subject: [PATCH 030/136] feat(zkprogram.ts): update createProgramState function to handle methodCache and auxInputCache --- src/lib/proof-system/zkprogram.ts | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index f79c499884..657b313347 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -162,6 +162,7 @@ const FeatureFlags = { function createProgramState() { let auxInputCache: Map> = new Map(); + let methodCache: Map = new Map(); return { setAuxilaryInput(value: Provable) { auxInputCache.set('auxinput', value); @@ -170,16 +171,7 @@ function createProgramState() { let entry = auxInputCache.get('auxinput'); return entry; }, - resetAuxCache() { - auxInputCache.delete('auxinput'); - }, - }; -} -function createProgramState() { - let methodCache: Map = new Map(); - - return { setAuxiliaryOutput(value: unknown, methodName: string) { methodCache.set(methodName, value); }, @@ -192,6 +184,9 @@ function createProgramState() { reset(methodName: string) { methodCache.delete(methodName); }, + resetAuxCache() { + auxInputCache.delete('auxinput'); + }, }; } From c5fa6f74c75f95c1f8efc8ec19f831a21217a10c Mon Sep 17 00:00:00 2001 From: ymekuria Date: Tue, 8 Oct 2024 15:02:25 -0700 Subject: [PATCH 031/136] feat(zkprogram.ts): decompose auxiliary data from publicInput --- src/lib/proof-system/zkprogram.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 657b313347..a784dd2d32 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -1091,7 +1091,7 @@ Try adding a method to your ZkProgram or SmartContract. If you are using a SmartContract, make sure you are using the @method decorator.`); // decompose auxiliary data type - let purePublicInput = publicInputType.toFields(publicInputType); + let purePublicInput = publicInputType.toFields(publicInput); let auxilaryPublicInput = publicInputType.toAuxiliary(); From 921aee21225291cbf35ea870b486423222e23447 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Tue, 8 Oct 2024 21:45:05 -0700 Subject: [PATCH 032/136] feat(zkprogram.ts): update publicInputType from ProvablePure to Provable --- src/lib/proof-system/zkprogram.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index a784dd2d32..3b8817c57d 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -646,7 +646,7 @@ function ZkProgram< let doProving = true; let methods = config.methods; - let publicInputType: ProvablePure = ProvableType.get( + let publicInputType: Provable = ProvableType.get( config.publicInput ?? Undefined ); let publicOutputType: ProvablePure = ProvableType.get( From c27ab3b68edf478d0adf2ada915df2feee9fb0a7 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Thu, 10 Oct 2024 14:47:43 -0700 Subject: [PATCH 033/136] feat(zkprogram.ts): change publicInputType to Provable from ProvablePure --- src/lib/proof-system/zkprogram.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 3b8817c57d..7c92e4aa7e 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -1222,7 +1222,7 @@ function inCircuitVkHash(inCircuitVk: unknown): Field { } function picklesRuleFromFunction( - publicInputType: ProvablePure, + publicInputType: Provable, publicOutputType: ProvablePure, func: (...args: unknown[]) => unknown, proofSystemTag: { name: string }, From 08574ebf46e84da07e8a61f5bfb2bc74192028cc Mon Sep 17 00:00:00 2001 From: ymekuria Date: Thu, 10 Oct 2024 15:04:40 -0700 Subject: [PATCH 034/136] feat(zkprogram.ts): change publicOutputType in picklesRuleFromFunction to Provable from ProvablePure --- src/lib/proof-system/zkprogram.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 7c92e4aa7e..f7b5a00b98 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -1223,7 +1223,7 @@ function inCircuitVkHash(inCircuitVk: unknown): Field { function picklesRuleFromFunction( publicInputType: Provable, - publicOutputType: ProvablePure, + publicOutputType: Provable, func: (...args: unknown[]) => unknown, proofSystemTag: { name: string }, { From 6077306b0ba3ddff6dc9a5d68cfabf20f4cada51 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Sun, 13 Oct 2024 22:40:32 -0700 Subject: [PATCH 035/136] feat(zkprogram.ts): update picklesRuleFromFunction publicInputType and publicOutputType to use ProvablePure --- src/lib/proof-system/zkprogram.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index f7b5a00b98..3b8817c57d 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -1222,8 +1222,8 @@ function inCircuitVkHash(inCircuitVk: unknown): Field { } function picklesRuleFromFunction( - publicInputType: Provable, - publicOutputType: Provable, + publicInputType: ProvablePure, + publicOutputType: ProvablePure, func: (...args: unknown[]) => unknown, proofSystemTag: { name: string }, { From 7d601c73dfe34aa0071243fc1da6feb15e416ce2 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Mon, 14 Oct 2024 00:12:32 -0700 Subject: [PATCH 036/136] feat(zkprogram.ts): get auxilaryPublicInput from programState cache --- src/lib/proof-system/zkprogram.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 3b8817c57d..1be487cc9c 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -815,6 +815,8 @@ function ZkProgram< programState.reset(methodIntfs[i].methodName); } + let auxilaryPublicInput = programState.getAuxilaryInput(); + let [publicOutputFields, proof] = MlPair.from(result); let publicOutput = fromFieldConsts(publicOutputType, publicOutputFields); From 3a5e476a6126f44e887f54b4433b64706daccf74 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Mon, 14 Oct 2024 00:27:42 -0700 Subject: [PATCH 037/136] feat(zkprogram.ts): recompose non pure data type with program State auxiliary data --- src/lib/proof-system/zkprogram.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 1be487cc9c..f168e18a42 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -817,6 +817,14 @@ function ZkProgram< let auxilaryPublicInput = programState.getAuxilaryInput(); + let nonPurePublicInput; + if (auxilaryPublicInput) { + nonPurePublicInput = publicInputType.fromFields( + publicInput, + auxilaryPublicInput + ); + } + let [publicOutputFields, proof] = MlPair.from(result); let publicOutput = fromFieldConsts(publicOutputType, publicOutputFields); From 437286f22c331bee299ad596f54c40fa6f32cb71 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Tue, 15 Oct 2024 21:59:53 -0700 Subject: [PATCH 038/136] reset(zkprogram.ts): reset the cache --- src/lib/proof-system/zkprogram.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index f168e18a42..c02932ffd1 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -823,6 +823,7 @@ function ZkProgram< publicInput, auxilaryPublicInput ); + programState.resetAuxCache(); } let [publicOutputFields, proof] = MlPair.from(result); From 8b8a9ee10dc07946f57ab99fa733e8f5fb3fe89c Mon Sep 17 00:00:00 2001 From: ymekuria Date: Tue, 15 Oct 2024 22:49:41 -0700 Subject: [PATCH 039/136] feat(zkprogram.ts): add helper function toFieldAndAuxConsts. --- src/lib/proof-system/zkprogram.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index c02932ffd1..e927e5d0ad 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -789,6 +789,8 @@ function ZkProgram< `Try calling \`await program.compile()\` first, this will cache provers in the background.\nIf you compiled your zkProgram with proofs disabled (\`proofsEnabled = false\`), you have to compile it with proofs enabled first.` ); } + + // serialize PublicInput into pure provable let publicInputFields = toFieldConsts(publicInputType, publicInput); let previousProofs = MlArray.to( getPreviousProofsForProver(args, methodIntfs[i]) @@ -1531,6 +1533,8 @@ function toFieldConsts(type: ProvablePure, value: T) { return MlFieldConstArray.to(type.toFields(value)); } +function toFieldAndAuxConsts(type: Provable, value: T) {} + ZkProgram.Proof = function < PublicInputType extends FlexibleProvablePure, PublicOutputType extends FlexibleProvablePure From 0a9a258a2ad8e33580d93dda89b63a972e93fe15 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Tue, 15 Oct 2024 22:53:34 -0700 Subject: [PATCH 040/136] feat(zkprogram.ts): decompose field elements from non pure public input in toFieldAndAuxConsts --- src/lib/proof-system/zkprogram.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index e927e5d0ad..0fc9ae4e66 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -1533,7 +1533,9 @@ function toFieldConsts(type: ProvablePure, value: T) { return MlFieldConstArray.to(type.toFields(value)); } -function toFieldAndAuxConsts(type: Provable, value: T) {} +function toFieldAndAuxConsts(type: Provable, value: T) { + const fields = MlFieldConstArray.to(type.toFields(value)); +} ZkProgram.Proof = function < PublicInputType extends FlexibleProvablePure, From 2d872e7a31f8b96b6d38c57b4a4ad89dc3ae8a81 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Tue, 15 Oct 2024 23:04:31 -0700 Subject: [PATCH 041/136] feat(zkprogram.ts): decompose auxilary data from non pure public input in toFieldAndAuxConsts --- src/lib/proof-system/zkprogram.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 0fc9ae4e66..92f8794ba3 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -1534,7 +1534,8 @@ function toFieldConsts(type: ProvablePure, value: T) { } function toFieldAndAuxConsts(type: Provable, value: T) { - const fields = MlFieldConstArray.to(type.toFields(value)); + let fields = MlFieldConstArray.to(type.toFields(value)); + let aux = type.toAuxiliary(value); } ZkProgram.Proof = function < From ee12afc1f8e5a681f915bd55e423562009238e73 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Tue, 15 Oct 2024 23:06:38 -0700 Subject: [PATCH 042/136] feat(zkprogram.ts)return auxilary data and fields in toFieldAndAuxConsts --- src/lib/proof-system/zkprogram.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 92f8794ba3..51a0f3a07c 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -1536,6 +1536,11 @@ function toFieldConsts(type: ProvablePure, value: T) { function toFieldAndAuxConsts(type: Provable, value: T) { let fields = MlFieldConstArray.to(type.toFields(value)); let aux = type.toAuxiliary(value); + + return { + aux, + fields, + }; } ZkProgram.Proof = function < From eb5676f86d56d88c1003e7999573cb7736281240 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Tue, 15 Oct 2024 23:09:19 -0700 Subject: [PATCH 043/136] refactor(zkprogram.ts): rename variables 'fields' and 'aux' to 'publicInputFields' and 'publicInputAux' for clarity and consistency --- src/lib/proof-system/zkprogram.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 51a0f3a07c..b8320eb8db 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -1534,12 +1534,12 @@ function toFieldConsts(type: ProvablePure, value: T) { } function toFieldAndAuxConsts(type: Provable, value: T) { - let fields = MlFieldConstArray.to(type.toFields(value)); - let aux = type.toAuxiliary(value); + let publicInputFields = MlFieldConstArray.to(type.toFields(value)); + let publicInputAux = type.toAuxiliary(value); return { - aux, - fields, + publicInputFields, + publicInputAux, }; } From 6e0e1e305dd413194a576847aa0b34a1e3965f0d Mon Sep 17 00:00:00 2001 From: ymekuria Date: Tue, 15 Oct 2024 23:13:17 -0700 Subject: [PATCH 044/136] refactor(zkprogram.ts): simplify the toFieldAndAuxConsts function by removing unnecessary variable declarations and directly returning object properties --- src/lib/proof-system/zkprogram.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index b8320eb8db..e0e6c42198 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -1534,12 +1534,9 @@ function toFieldConsts(type: ProvablePure, value: T) { } function toFieldAndAuxConsts(type: Provable, value: T) { - let publicInputFields = MlFieldConstArray.to(type.toFields(value)); - let publicInputAux = type.toAuxiliary(value); - return { - publicInputFields, - publicInputAux, + publicInputFields: MlFieldConstArray.to(type.toFields(value)), + publicInputAux: type.toAuxiliary(value), }; } From 37a09befec1eda0833cb8aacc19ddb09dac515da Mon Sep 17 00:00:00 2001 From: ymekuria Date: Tue, 15 Oct 2024 23:15:43 -0700 Subject: [PATCH 045/136] feat(zkprogram.ts): decompose non pure publicInput into pure field elements and auxiliary data --- src/lib/proof-system/zkprogram.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index e0e6c42198..b0c0484929 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -790,8 +790,11 @@ function ZkProgram< ); } - // serialize PublicInput into pure provable - let publicInputFields = toFieldConsts(publicInputType, publicInput); + // serialize publicInput into pure provable field elements and auxilary data + let { publicInputFields, publicInputAux } = toFieldAndAuxConsts( + publicInputType, + publicInput + ); let previousProofs = MlArray.to( getPreviousProofsForProver(args, methodIntfs[i]) ); From 7491a804f2b4980acab4c3900f3651002ae48a34 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Tue, 15 Oct 2024 23:23:47 -0700 Subject: [PATCH 046/136] feat(zkprogram.ts): set publicInput auxiliary data in cache --- src/lib/proof-system/zkprogram.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index b0c0484929..636dd32896 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -799,6 +799,7 @@ function ZkProgram< getPreviousProofsForProver(args, methodIntfs[i]) ); + programState.setAuxilaryInput(publicInputAux); let id = snarkContext.enter({ witnesses: args, inProver: true }); let result: UnwrapPromise>; try { From 6a2856aa9ec809b5a8d07c9b2229523de3c5d8c5 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Tue, 15 Oct 2024 23:50:46 -0700 Subject: [PATCH 047/136] feat(zkprogram.ts): change type of auxInputCache and methodCache to use 'unknown' for better flexibility and type safety --- src/lib/proof-system/zkprogram.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 636dd32896..7634b0bf49 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -161,10 +161,10 @@ const FeatureFlags = { }; function createProgramState() { - let auxInputCache: Map> = new Map(); + let auxInputCache: Map = new Map(); let methodCache: Map = new Map(); return { - setAuxilaryInput(value: Provable) { + setAuxilaryInput(value: unknown) { auxInputCache.set('auxinput', value); }, getAuxilaryInput: () => { @@ -799,6 +799,7 @@ function ZkProgram< getPreviousProofsForProver(args, methodIntfs[i]) ); + // store publicInput auxilary data in programState cache programState.setAuxilaryInput(publicInputAux); let id = snarkContext.enter({ witnesses: args, inProver: true }); let result: UnwrapPromise>; From b77c5e19784399d7363819f3017c140bdb5c2016 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 16 Oct 2024 00:05:06 -0700 Subject: [PATCH 048/136] feat(zkprogram.ts): add optional 'aux' parameter to 'fromFieldConsts' function for future extensibility and flexibility --- src/lib/proof-system/zkprogram.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 7634b0bf49..21a90fea00 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -1531,7 +1531,11 @@ function toFieldVars(type: ProvablePure, value: T) { return MlFieldArray.to(type.toFields(value)); } -function fromFieldConsts(type: ProvablePure, fields: MlFieldConstArray) { +function fromFieldConsts( + type: ProvablePure, + fields: MlFieldConstArray, + aux?: unknown +) { return type.fromFields(MlFieldConstArray.from(fields)); } function toFieldConsts(type: ProvablePure, value: T) { From df972be7b96644fa410575751e57c173906e7306 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 16 Oct 2024 00:20:10 -0700 Subject: [PATCH 049/136] refactor(zkprogram.ts): update function signature and parameter types for better clarity and consistency --- src/lib/proof-system/zkprogram.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 21a90fea00..08350aa8a7 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -1532,11 +1532,11 @@ function toFieldVars(type: ProvablePure, value: T) { } function fromFieldConsts( - type: ProvablePure, + type: Provable, fields: MlFieldConstArray, - aux?: unknown + aux?: any[] ) { - return type.fromFields(MlFieldConstArray.from(fields)); + return type.fromFields(MlFieldConstArray.from(fields), aux); } function toFieldConsts(type: ProvablePure, value: T) { return MlFieldConstArray.to(type.toFields(value)); From c6a8260e7a0c2e98a1de34b3faa0ffa99b2e3eb2 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 16 Oct 2024 00:42:20 -0700 Subject: [PATCH 050/136] feat(zkprogram.ts): add fromFieldAndAuxConsts helper function --- src/lib/proof-system/zkprogram.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 08350aa8a7..81dfa64515 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -1538,6 +1538,15 @@ function fromFieldConsts( ) { return type.fromFields(MlFieldConstArray.from(fields), aux); } + +function fromFieldAndAuxConsts( + type: Provable, + fields: MlFieldConstArray, + aux: any[] +) { + return type.fromFields(MlFieldConstArray.from(fields), aux); +} + function toFieldConsts(type: ProvablePure, value: T) { return MlFieldConstArray.to(type.toFields(value)); } From efa1379e4e2a55bf8d12392ad04aed5ffe7ec4ab Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 16 Oct 2024 00:47:36 -0700 Subject: [PATCH 051/136] refactor(zkprogram.ts): simplify fromFieldConsts function parameters to improve readability and maintainability --- src/lib/proof-system/zkprogram.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 81dfa64515..0038e09a4b 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -1531,12 +1531,8 @@ function toFieldVars(type: ProvablePure, value: T) { return MlFieldArray.to(type.toFields(value)); } -function fromFieldConsts( - type: Provable, - fields: MlFieldConstArray, - aux?: any[] -) { - return type.fromFields(MlFieldConstArray.from(fields), aux); +function fromFieldConsts(type: ProvablePure, fields: MlFieldConstArray) { + return type.fromFields(MlFieldConstArray.from(fields)); } function fromFieldAndAuxConsts( From 062d3137ffb0087b8f3beeca5200007f39b93195 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 16 Oct 2024 00:57:27 -0700 Subject: [PATCH 052/136] feat(zkprogram.ts): add conditional check before storing publicInput auxilary data in programState cache --- src/lib/proof-system/zkprogram.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 0038e09a4b..3f45bac832 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -799,8 +799,10 @@ function ZkProgram< getPreviousProofsForProver(args, methodIntfs[i]) ); - // store publicInput auxilary data in programState cache - programState.setAuxilaryInput(publicInputAux); + if (publicInputAux) { + // store publicInput auxilary data in programState cache + programState.setAuxilaryInput(publicInputAux); + } let id = snarkContext.enter({ witnesses: args, inProver: true }); let result: UnwrapPromise>; try { From e9aa3eb641b54a1f3128450efc29e49ce9f4fe88 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 16 Oct 2024 01:18:08 -0700 Subject: [PATCH 053/136] feat(zkprogram.ts): add conditional to recompose non pure data type if there is auxilary input --- src/lib/proof-system/zkprogram.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 3f45bac832..08ce2f3171 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -834,8 +834,14 @@ function ZkProgram< ); programState.resetAuxCache(); } - let [publicOutputFields, proof] = MlPair.from(result); + if (publicInputAux) { + fromFieldAndAuxConsts( + publicOutputType, + publicOutputFields, + publicInputAux + ); + } let publicOutput = fromFieldConsts(publicOutputType, publicOutputFields); return { From f741f981d35cb9275c64258303a3cdc9820e1da9 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 16 Oct 2024 01:24:45 -0700 Subject: [PATCH 054/136] feat(zkprogram.ts): refactor public output generation logic to handle both public input auxiliary constants and regular public output fields --- src/lib/proof-system/zkprogram.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 08ce2f3171..1c81b3d88c 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -834,15 +834,18 @@ function ZkProgram< ); programState.resetAuxCache(); } + + let publicOutput; let [publicOutputFields, proof] = MlPair.from(result); if (publicInputAux) { - fromFieldAndAuxConsts( + publicOutput = fromFieldAndAuxConsts( publicOutputType, publicOutputFields, publicInputAux ); + } else { + publicOutput = fromFieldConsts(publicOutputType, publicOutputFields); } - let publicOutput = fromFieldConsts(publicOutputType, publicOutputFields); return { proof: new ProgramProof({ From ee7165457e0e3f6d4a5cff5f3f21c50d2c2fff0a Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 16 Oct 2024 01:26:56 -0700 Subject: [PATCH 055/136] feat(zkprogram.ts): clear auxiliary cache before generating proof --- src/lib/proof-system/zkprogram.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 1c81b3d88c..ac466f3edc 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -843,6 +843,7 @@ function ZkProgram< publicOutputFields, publicInputAux ); + programState.resetAuxCache(); } else { publicOutput = fromFieldConsts(publicOutputType, publicOutputFields); } From e65064f1d1d069aa5f5ad958f100ac25a441781a Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 16 Oct 2024 01:27:51 -0700 Subject: [PATCH 056/136] refactor(zkprogram.ts): remove unused variables and unnecessary code for better code readability and maintainability --- src/lib/proof-system/zkprogram.ts | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index ac466f3edc..bc5381c581 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -824,17 +824,6 @@ function ZkProgram< programState.reset(methodIntfs[i].methodName); } - let auxilaryPublicInput = programState.getAuxilaryInput(); - - let nonPurePublicInput; - if (auxilaryPublicInput) { - nonPurePublicInput = publicInputType.fromFields( - publicInput, - auxilaryPublicInput - ); - programState.resetAuxCache(); - } - let publicOutput; let [publicOutputFields, proof] = MlPair.from(result); if (publicInputAux) { From 5347ac3b4419a961bf41f1115000784a2dfedb01 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 16 Oct 2024 01:31:21 -0700 Subject: [PATCH 057/136] feat(zkprogram.ts): update publicInputType in picklesRuleFromFunction --- src/lib/proof-system/zkprogram.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index bc5381c581..173fc203bc 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -1119,7 +1119,7 @@ If you are using a SmartContract, make sure you are using the @method decorator. let rules = methodIntfs.map((methodEntry, i) => picklesRuleFromFunction( - purePublicInput, + publicInputType, publicOutputType, methods[i], proofSystemTag, From 5e68a1173481c5d75976b48c9e9f429867f950c7 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 16 Oct 2024 01:33:29 -0700 Subject: [PATCH 058/136] refactor(zkprogram.ts): remove unused code related to decomposing auxiliary data type and storing it in cache --- src/lib/proof-system/zkprogram.ts | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 173fc203bc..bf20ead481 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -1109,14 +1109,6 @@ async function compileProgram({ Try adding a method to your ZkProgram or SmartContract. If you are using a SmartContract, make sure you are using the @method decorator.`); - // decompose auxiliary data type - let purePublicInput = publicInputType.toFields(publicInput); - - let auxilaryPublicInput = publicInputType.toAuxiliary(); - - // store auxiliary data in cache - state.setAuxilaryInput(auxilaryPublicInput); - let rules = methodIntfs.map((methodEntry, i) => picklesRuleFromFunction( publicInputType, From b3521e9f08c78e787c18c792a71e3af638e3f198 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 16 Oct 2024 01:38:20 -0700 Subject: [PATCH 059/136] feat(zkprogram.ts): update ProofBase class publicInputType and publicOutputType to use FlexibleProvable type for better type safety and clarity --- src/lib/proof-system/zkprogram.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index bf20ead481..8b4c3b8b8c 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -245,8 +245,8 @@ async function featureFlagsfromFlatMethodIntfs( } class ProofBase { - static publicInputType: FlexibleProvablePure = undefined as any; - static publicOutputType: FlexibleProvablePure = undefined as any; + static publicInputType: FlexibleProvable = undefined as any; + static publicOutputType: FlexibleProvable = undefined as any; static tag: () => { name: string } = () => { throw Error( `You cannot use the \`Proof\` class directly. Instead, define a subclass:\n` + From ce84a0d9e50d9c6fdb09a748a1b7cd6e72561794 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 16 Oct 2024 01:39:56 -0700 Subject: [PATCH 060/136] feat(zkprogram.ts): update analyzeMethods publicInputType parameter type from ProvablePure to Provable for consistency and clarity --- src/lib/proof-system/zkprogram.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 8b4c3b8b8c..21fe6a2972 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -1205,7 +1205,7 @@ If you are using a SmartContract, make sure you are using the @method decorator. } function analyzeMethod( - publicInputType: ProvablePure, + publicInputType: Provable, methodIntf: MethodInterface, method: (...args: any) => unknown ) { From 3c8d0304ebaad16749332c035113c7f3267e0e61 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 16 Oct 2024 01:41:49 -0700 Subject: [PATCH 061/136] feat(zkprogram.ts): update toFieldConst parameter type from ProvablePure to Provable for better clarity and consistency --- src/lib/proof-system/zkprogram.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 21fe6a2972..7994395556 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -1536,7 +1536,7 @@ function fromFieldAndAuxConsts( return type.fromFields(MlFieldConstArray.from(fields), aux); } -function toFieldConsts(type: ProvablePure, value: T) { +function toFieldConsts(type: Provable, value: T) { return MlFieldConstArray.to(type.toFields(value)); } From d0885f218b43edb670d625fdc1e405709393054b Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 16 Oct 2024 01:44:36 -0700 Subject: [PATCH 062/136] feat(zkprogram.ts): update PicklesRuleFromFunction publicInputType and publicOutputType parameters to ProvablePure to Provable for --- src/lib/proof-system/zkprogram.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 7994395556..669899a96b 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -1233,8 +1233,8 @@ function inCircuitVkHash(inCircuitVk: unknown): Field { } function picklesRuleFromFunction( - publicInputType: ProvablePure, - publicOutputType: ProvablePure, + publicInputType: Provable, + publicOutputType: Provable, func: (...args: unknown[]) => unknown, proofSystemTag: { name: string }, { From 84df9aad0a57ea6de399dded322b0d6ccdb41d8f Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 16 Oct 2024 08:56:41 -0700 Subject: [PATCH 063/136] feat(zkprogram.ts): update config publicInputType to ProvableType --- src/lib/proof-system/zkprogram.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 669899a96b..6e2a3f81da 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -569,8 +569,8 @@ let SideloadedTag = { function ZkProgram< Config extends { - publicInput?: ProvableTypePure; - publicOutput?: ProvableTypePure; + publicInput?: ProvableType; + publicOutput?: ProvableType; methods: { [I in string]: { privateInputs: Tuple; From 77146bfb4b74a89ee79fd02f006fcf0acb604d46 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 16 Oct 2024 09:28:02 -0700 Subject: [PATCH 064/136] feat(zkprogram.ts): update config publicOutputType to Provable --- src/lib/proof-system/zkprogram.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 6e2a3f81da..8391ff4799 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -649,7 +649,7 @@ function ZkProgram< let publicInputType: Provable = ProvableType.get( config.publicInput ?? Undefined ); - let publicOutputType: ProvablePure = ProvableType.get( + let publicOutputType: Provable = ProvableType.get( config.publicOutput ?? Void ); From e05f3c17985d648d52b9dd7f03a6ace36cadd558 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 16 Oct 2024 09:53:17 -0700 Subject: [PATCH 065/136] feat(zkprogram.ts): temp update to fromFieldConsts type --- src/lib/proof-system/zkprogram.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 8391ff4799..e274308fe9 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -1524,7 +1524,7 @@ function toFieldVars(type: ProvablePure, value: T) { return MlFieldArray.to(type.toFields(value)); } -function fromFieldConsts(type: ProvablePure, fields: MlFieldConstArray) { +function fromFieldConsts(type: any, fields: MlFieldConstArray) { return type.fromFields(MlFieldConstArray.from(fields)); } From dc2b2cbeb017152d7577bb04d1d6567b65433204 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Fri, 18 Oct 2024 17:32:16 -0600 Subject: [PATCH 066/136] refactor(program-with-non-pure-input.ts): update publicInput property in to accept a non pure MyStruct instance instead of an array of MyStruct instances --- src/examples/zkprogram/program-with-non-pure-input.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/examples/zkprogram/program-with-non-pure-input.ts b/src/examples/zkprogram/program-with-non-pure-input.ts index 1f8b498a87..81b494f9e4 100644 --- a/src/examples/zkprogram/program-with-non-pure-input.ts +++ b/src/examples/zkprogram/program-with-non-pure-input.ts @@ -8,6 +8,6 @@ class MyStruct extends Struct({ let MyProgram = ZkProgram({ name: 'example-with-non-pure-inputs', publicOutput: Field, - publicInput: [MyStruct], + publicInput: MyStruct, methods: {}, }); From 0355f674c134e63a1ef4243ec04f9d1a4b5f3a87 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Fri, 18 Oct 2024 17:41:34 -0600 Subject: [PATCH 067/136] feat(zkprogram.ts): update publicIntype parameter in fromFieldVars function to any for flexibility and compatibility with different types of inputs --- src/lib/proof-system/zkprogram.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index e274308fe9..e2ac02d8a1 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -1517,7 +1517,7 @@ function getMaxProofsVerified(methodIntfs: MethodInterface[]) { ) as any as 0 | 1 | 2; } -function fromFieldVars(type: ProvablePure, fields: MlFieldArray) { +function fromFieldVars(type: any, fields: MlFieldArray) { return type.fromFields(MlFieldArray.from(fields)); } function toFieldVars(type: ProvablePure, value: T) { From 8b8c1206c6a079540f8497681edcf4c43cf2afca Mon Sep 17 00:00:00 2001 From: ymekuria Date: Fri, 18 Oct 2024 23:35:59 -0600 Subject: [PATCH 068/136] feat(program-with-non-pure-input.ts): add 'baseCase' method to example --- src/examples/zkprogram/program-with-non-pure-input.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/examples/zkprogram/program-with-non-pure-input.ts b/src/examples/zkprogram/program-with-non-pure-input.ts index 81b494f9e4..6094434c62 100644 --- a/src/examples/zkprogram/program-with-non-pure-input.ts +++ b/src/examples/zkprogram/program-with-non-pure-input.ts @@ -7,7 +7,12 @@ class MyStruct extends Struct({ let MyProgram = ZkProgram({ name: 'example-with-non-pure-inputs', - publicOutput: Field, publicInput: MyStruct, - methods: {}, + + methods: { + baseCase: { + privateInputs: [], + async method(input: MyStruct) {}, + }, + }, }); From 1fbe24f8dc1d2d33535811da4a597bb79ab341ab Mon Sep 17 00:00:00 2001 From: ymekuria Date: Fri, 18 Oct 2024 23:42:23 -0600 Subject: [PATCH 069/136] feat(program-with-non-pure-input.ts): compile zkprogram --- src/examples/zkprogram/program-with-non-pure-input.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/examples/zkprogram/program-with-non-pure-input.ts b/src/examples/zkprogram/program-with-non-pure-input.ts index 6094434c62..c3e473a1c7 100644 --- a/src/examples/zkprogram/program-with-non-pure-input.ts +++ b/src/examples/zkprogram/program-with-non-pure-input.ts @@ -16,3 +16,7 @@ let MyProgram = ZkProgram({ }, }, }); + +console.log('compiling MyProgram...'); +await MyProgram.compile(); +console.log('compile done'); From 36b92fc941a469296012888ad9b6049eca647f0c Mon Sep 17 00:00:00 2001 From: ymekuria Date: Fri, 18 Oct 2024 23:58:47 -0600 Subject: [PATCH 070/136] feat(program-with-non-pure-input.ts): update baseCase method to return non pure publicOutput --- src/examples/zkprogram/program-with-non-pure-input.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/examples/zkprogram/program-with-non-pure-input.ts b/src/examples/zkprogram/program-with-non-pure-input.ts index c3e473a1c7..f215f894d4 100644 --- a/src/examples/zkprogram/program-with-non-pure-input.ts +++ b/src/examples/zkprogram/program-with-non-pure-input.ts @@ -8,11 +8,16 @@ class MyStruct extends Struct({ let MyProgram = ZkProgram({ name: 'example-with-non-pure-inputs', publicInput: MyStruct, + publicOutput: MyStruct, methods: { baseCase: { privateInputs: [], - async method(input: MyStruct) {}, + async method(input: MyStruct) { + return { + publicOutput: input, + }; + }, }, }, }); From a9ab6e1a2890809588c7efac0d635bccacee1948 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Sat, 19 Oct 2024 00:11:59 -0600 Subject: [PATCH 071/136] feat(program-with-non-pure-input.ts): add invoke basecase method with non pure input in example --- src/examples/zkprogram/program-with-non-pure-input.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/examples/zkprogram/program-with-non-pure-input.ts b/src/examples/zkprogram/program-with-non-pure-input.ts index f215f894d4..d972f64896 100644 --- a/src/examples/zkprogram/program-with-non-pure-input.ts +++ b/src/examples/zkprogram/program-with-non-pure-input.ts @@ -25,3 +25,6 @@ let MyProgram = ZkProgram({ console.log('compiling MyProgram...'); await MyProgram.compile(); console.log('compile done'); + +let input = new MyStruct({ label: 'input', value: Field(5) }); +let result = await MyProgram.baseCase(input); From 9f5f39b6a6251a12103a47c3a255a359ee686c28 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Sat, 19 Oct 2024 00:23:36 -0600 Subject: [PATCH 072/136] feat(program-with-non-pure-input.ts): add verification step after running MyProgram to ensure proof validity --- src/examples/zkprogram/program-with-non-pure-input.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/examples/zkprogram/program-with-non-pure-input.ts b/src/examples/zkprogram/program-with-non-pure-input.ts index d972f64896..0a1224113c 100644 --- a/src/examples/zkprogram/program-with-non-pure-input.ts +++ b/src/examples/zkprogram/program-with-non-pure-input.ts @@ -28,3 +28,7 @@ console.log('compile done'); let input = new MyStruct({ label: 'input', value: Field(5) }); let result = await MyProgram.baseCase(input); +let ok = await MyProgram.verify(result.proof); + +console.log('ok', ok); +assert(ok, 'proof not valid!'); From df1f6156726ce72a7b3e32c73168f86a77f7c845 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Sat, 19 Oct 2024 01:09:30 -0600 Subject: [PATCH 073/136] feat(zkprogram.ts): add check if publicInput has aux data --- src/lib/proof-system/zkprogram.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index e2ac02d8a1..40280d1bcc 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -165,10 +165,12 @@ function createProgramState() { let methodCache: Map = new Map(); return { setAuxilaryInput(value: unknown) { + console.log('setAuxilaryInput with', value); auxInputCache.set('auxinput', value); }, getAuxilaryInput: () => { let entry = auxInputCache.get('auxinput'); + console.log('getAuxilaryInput', entry); return entry; }, @@ -790,11 +792,19 @@ function ZkProgram< ); } + let publicInputAuxExists = + publicInputType.toAuxiliary(publicInput).length !== 0; + // serialize publicInput into pure provable field elements and auxilary data let { publicInputFields, publicInputAux } = toFieldAndAuxConsts( publicInputType, publicInput ); + + console.log( + 'publicInputAux after calling toFieldAndAuxConsts', + publicInputAux + ); let previousProofs = MlArray.to( getPreviousProofsForProver(args, methodIntfs[i]) ); @@ -827,13 +837,17 @@ function ZkProgram< let publicOutput; let [publicOutputFields, proof] = MlPair.from(result); if (publicInputAux) { + console.log('if publicInputAux', publicInputAux); publicOutput = fromFieldAndAuxConsts( publicOutputType, publicOutputFields, publicInputAux ); + + console.log('publicOutput after fromFieldAndAuxConsts', publicOutput); programState.resetAuxCache(); } else { + // create public output when public input is pure publicOutput = fromFieldConsts(publicOutputType, publicOutputFields); } From 67854f0a48eef9d1855e1e784defd5cd656d3deb Mon Sep 17 00:00:00 2001 From: ymekuria Date: Sat, 19 Oct 2024 02:41:36 -0600 Subject: [PATCH 074/136] feat(zkprogram.ts): check if public input has aux data before serialization --- src/lib/proof-system/zkprogram.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 40280d1bcc..42b2ded8a5 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -795,11 +795,14 @@ function ZkProgram< let publicInputAuxExists = publicInputType.toAuxiliary(publicInput).length !== 0; + let publicInputFields, publicInputAux; + if (publicInputAuxExists) { + ({ publicInputFields, publicInputAux } = toFieldAndAuxConsts( + publicInputType, + publicInput + )); + } // serialize publicInput into pure provable field elements and auxilary data - let { publicInputFields, publicInputAux } = toFieldAndAuxConsts( - publicInputType, - publicInput - ); console.log( 'publicInputAux after calling toFieldAndAuxConsts', From 146c783ad577665bbc9da11ed3ddb6a0fee49f17 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Sat, 19 Oct 2024 02:50:27 -0600 Subject: [PATCH 075/136] feat(zkprogram.ts): check if public input aux data exists before storing --- src/lib/proof-system/zkprogram.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 42b2ded8a5..6e19e1c7c2 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -797,12 +797,15 @@ function ZkProgram< let publicInputFields, publicInputAux; if (publicInputAuxExists) { + // serialize publicInput into pure provable field elements and auxilary data ({ publicInputFields, publicInputAux } = toFieldAndAuxConsts( publicInputType, publicInput )); + + // store publicInput auxilary data in programState cache + programState.setAuxilaryInput(publicInputAux); } - // serialize publicInput into pure provable field elements and auxilary data console.log( 'publicInputAux after calling toFieldAndAuxConsts', @@ -813,8 +816,6 @@ function ZkProgram< ); if (publicInputAux) { - // store publicInput auxilary data in programState cache - programState.setAuxilaryInput(publicInputAux); } let id = snarkContext.enter({ witnesses: args, inProver: true }); let result: UnwrapPromise>; From e4c3fb3776a33873660fbe9654c0c02befb3c502 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Sat, 19 Oct 2024 02:55:36 -0600 Subject: [PATCH 076/136] feat(zkprogram.ts): return publicInputFields when no aux data --- src/lib/proof-system/zkprogram.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 6e19e1c7c2..819a78e501 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -805,6 +805,8 @@ function ZkProgram< // store publicInput auxilary data in programState cache programState.setAuxilaryInput(publicInputAux); + } else { + publicInputFields = toFieldConsts(publicInputType, publicInput); } console.log( From a836f0e95e599f4580d95c6dafcdf217c76bc620 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Sat, 19 Oct 2024 15:09:49 -0600 Subject: [PATCH 077/136] feature(zkprogram.ts): use fromFieldAndAuxConsts function in case when no aux data --- src/lib/proof-system/zkprogram.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 819a78e501..1f59cb30ec 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -854,7 +854,10 @@ function ZkProgram< programState.resetAuxCache(); } else { // create public output when public input is pure - publicOutput = fromFieldConsts(publicOutputType, publicOutputFields); + publicOutput = fromFieldAndAuxConsts( + publicOutputType, + publicOutputFields + ); } return { @@ -1551,7 +1554,7 @@ function fromFieldConsts(type: any, fields: MlFieldConstArray) { function fromFieldAndAuxConsts( type: Provable, fields: MlFieldConstArray, - aux: any[] + aux: any[] = [] ) { return type.fromFields(MlFieldConstArray.from(fields), aux); } From 519c301980b7fdda44688118881bca36bcac4e26 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Sun, 20 Oct 2024 00:39:12 -0600 Subject: [PATCH 078/136] feat(zkprogram.ts): update type parameter in fromFieldVars to Provable type --- src/lib/proof-system/zkprogram.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 1f59cb30ec..430c5d5f20 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -1540,7 +1540,7 @@ function getMaxProofsVerified(methodIntfs: MethodInterface[]) { ) as any as 0 | 1 | 2; } -function fromFieldVars(type: any, fields: MlFieldArray) { +function fromFieldVars(type: Provable, fields: MlFieldArray) { return type.fromFields(MlFieldArray.from(fields)); } function toFieldVars(type: ProvablePure, value: T) { From f661d9168a3604b8c4a65661eada0ff22ac1ee8e Mon Sep 17 00:00:00 2001 From: ymekuria Date: Sun, 20 Oct 2024 00:56:14 -0600 Subject: [PATCH 079/136] feat(zkprogram.ts): update fromFieldVars function to include an empty aux value as second argument to fromFields --- src/lib/proof-system/zkprogram.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 430c5d5f20..caecf5905f 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -1541,7 +1541,7 @@ function getMaxProofsVerified(methodIntfs: MethodInterface[]) { } function fromFieldVars(type: Provable, fields: MlFieldArray) { - return type.fromFields(MlFieldArray.from(fields)); + return type.fromFields(MlFieldArray.from(fields), []); } function toFieldVars(type: ProvablePure, value: T) { return MlFieldArray.to(type.toFields(value)); From 3914a7d4899a9c13e310999e8831ec198006d6da Mon Sep 17 00:00:00 2001 From: ymekuria Date: Sun, 20 Oct 2024 01:44:21 -0600 Subject: [PATCH 080/136] refactor(zkprogram.ts): remove unnecessary if statement and add missing newline before function declaration --- src/lib/proof-system/zkprogram.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index caecf5905f..75f4bcb94d 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -817,8 +817,6 @@ function ZkProgram< getPreviousProofsForProver(args, methodIntfs[i]) ); - if (publicInputAux) { - } let id = snarkContext.enter({ witnesses: args, inProver: true }); let result: UnwrapPromise>; try { @@ -1543,6 +1541,7 @@ function getMaxProofsVerified(methodIntfs: MethodInterface[]) { function fromFieldVars(type: Provable, fields: MlFieldArray) { return type.fromFields(MlFieldArray.from(fields), []); } + function toFieldVars(type: ProvablePure, value: T) { return MlFieldArray.to(type.toFields(value)); } From 4647ffbe21be13d9f69421b859b7f852bab6baa3 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Sun, 20 Oct 2024 01:47:14 -0600 Subject: [PATCH 081/136] refactor(zkprogram.ts): rename use 'publicInputAuxExists' check --- src/lib/proof-system/zkprogram.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 75f4bcb94d..b443b922e2 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -840,7 +840,7 @@ function ZkProgram< let publicOutput; let [publicOutputFields, proof] = MlPair.from(result); - if (publicInputAux) { + if (publicInputAuxExists) { console.log('if publicInputAux', publicInputAux); publicOutput = fromFieldAndAuxConsts( publicOutputType, From b5eeee5f7cd9b4fcc1a1860e6b1097d5b9c8145c Mon Sep 17 00:00:00 2001 From: ymekuria Date: Sun, 20 Oct 2024 14:28:09 -0600 Subject: [PATCH 082/136] refactor(zkprogram.ts): remove unnecessary console.log statements for better code cleanliness and readability --- src/lib/proof-system/zkprogram.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index b443b922e2..8d991cd949 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -809,10 +809,6 @@ function ZkProgram< publicInputFields = toFieldConsts(publicInputType, publicInput); } - console.log( - 'publicInputAux after calling toFieldAndAuxConsts', - publicInputAux - ); let previousProofs = MlArray.to( getPreviousProofsForProver(args, methodIntfs[i]) ); @@ -840,8 +836,8 @@ function ZkProgram< let publicOutput; let [publicOutputFields, proof] = MlPair.from(result); - if (publicInputAuxExists) { - console.log('if publicInputAux', publicInputAux); + if (publicInputAux) { + publicInputAux = programState.getAuxilaryInput(); publicOutput = fromFieldAndAuxConsts( publicOutputType, publicOutputFields, From 8cbe588df4debc15aa553777a9cd6f2c0d44b636 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Sun, 20 Oct 2024 20:53:48 -0600 Subject: [PATCH 083/136] feat(zkprogram.ts): change the type of auxInputCache values from unknown to any[] for better type safety and clarity --- src/lib/proof-system/zkprogram.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 8d991cd949..3e2795c78a 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -161,7 +161,7 @@ const FeatureFlags = { }; function createProgramState() { - let auxInputCache: Map = new Map(); + let auxInputCache: Map = new Map(); let methodCache: Map = new Map(); return { setAuxilaryInput(value: unknown) { From bb505961094b9b57845def60a1bdd74c01de9bac Mon Sep 17 00:00:00 2001 From: ymekuria Date: Sun, 20 Oct 2024 20:54:36 -0600 Subject: [PATCH 084/136] feat(zkprogram.ts): change parameter type of setAuxilaryInput function in programState cache from unknown to any[] for better type safety and clarity --- src/lib/proof-system/zkprogram.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 3e2795c78a..fe3c62fbf1 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -164,7 +164,7 @@ function createProgramState() { let auxInputCache: Map = new Map(); let methodCache: Map = new Map(); return { - setAuxilaryInput(value: unknown) { + setAuxilaryInput(value: any[]) { console.log('setAuxilaryInput with', value); auxInputCache.set('auxinput', value); }, From 974dfb01a2ee9a121f0ce0d0c283d130b76d4ee7 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Sun, 20 Oct 2024 21:22:42 -0600 Subject: [PATCH 085/136] feat(zkprogram.ts): check if the publicInput has aux data before getting it from programState --- src/lib/proof-system/zkprogram.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index fe3c62fbf1..4d20f90122 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -836,7 +836,7 @@ function ZkProgram< let publicOutput; let [publicOutputFields, proof] = MlPair.from(result); - if (publicInputAux) { + if (publicInputAuxExists) { publicInputAux = programState.getAuxilaryInput(); publicOutput = fromFieldAndAuxConsts( publicOutputType, From cb41aaf2cfd81dba379eada6c4212efb6c193f74 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Mon, 21 Oct 2024 00:29:23 -0600 Subject: [PATCH 086/136] chore(CHANGELOG-v2.md): update changelog --- CHANGELOG-v2.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG-v2.md b/CHANGELOG-v2.md index 43e845c424..36efaddd12 100644 --- a/CHANGELOG-v2.md +++ b/CHANGELOG-v2.md @@ -34,6 +34,8 @@ Showing all changes since the last release (v.1.5.0) ### Added +-`zkProgram` to support non-pure provable types as inputs and outputs https://github.com/o1-labs/o1js/pull/1828 + - New method `toCanonical()` in the `Provable` interface to protect against incompleteness of certain operations on malicious witness inputs https://github.com/o1-labs/o1js/pull/1759 - `divMod64()` division modulo 2^64 that returns the remainder and quotient of the operation - `addMod64()` addition modulo 2^64 From 4390dafb66087b9b334e78704798a50e15c3bc3d Mon Sep 17 00:00:00 2001 From: ymekuria Date: Mon, 21 Oct 2024 00:39:21 -0600 Subject: [PATCH 087/136] feat(zkprogram.ts): improve cache error handling in get by throwing an error when auxiliary input is not defined --- src/lib/proof-system/zkprogram.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 4d20f90122..b04ffe3d23 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -171,6 +171,7 @@ function createProgramState() { getAuxilaryInput: () => { let entry = auxInputCache.get('auxinput'); console.log('getAuxilaryInput', entry); + if (entry === undefined) throw Error(`Auxiliary input not defined`); return entry; }, @@ -1346,7 +1347,7 @@ function picklesRuleFromFunction( Pickles.sideLoaded.inCircuit(computedTag, circuitVk); }); - // if the public output is empty, we don't evaluate `toFields(result)` to allow the function to return something else in that case + // if the output is empty, we don't evaluate `toFields(result)` to allow the function to return something else in that case let hasPublicOutput = publicOutputType.sizeInFields() !== 0; let publicOutput = hasPublicOutput ? publicOutputType.toFields(result.publicOutput) From 98386220edab427b783821ad6f3761e52f4c1806 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Mon, 21 Oct 2024 00:45:32 -0600 Subject: [PATCH 088/136] refactor(zkprogram.ts): remove logs --- src/lib/proof-system/zkprogram.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index b04ffe3d23..962ce227b0 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -165,12 +165,10 @@ function createProgramState() { let methodCache: Map = new Map(); return { setAuxilaryInput(value: any[]) { - console.log('setAuxilaryInput with', value); auxInputCache.set('auxinput', value); }, getAuxilaryInput: () => { let entry = auxInputCache.get('auxinput'); - console.log('getAuxilaryInput', entry); if (entry === undefined) throw Error(`Auxiliary input not defined`); return entry; }, @@ -178,6 +176,7 @@ function createProgramState() { setAuxiliaryOutput(value: unknown, methodName: string) { methodCache.set(methodName, value); }, + getAuxiliaryOutput(methodName: string): unknown { let entry = methodCache.get(methodName); if (entry === undefined) @@ -845,7 +844,6 @@ function ZkProgram< publicInputAux ); - console.log('publicOutput after fromFieldAndAuxConsts', publicOutput); programState.resetAuxCache(); } else { // create public output when public input is pure From 1ee42364177b751d5f9499d66ab4ecbac993770f Mon Sep 17 00:00:00 2001 From: ymekuria Date: Mon, 21 Oct 2024 00:58:52 -0600 Subject: [PATCH 089/136] chore(CHANGELOG-v2.md): update changelog --- CHANGELOG-v2.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG-v2.md b/CHANGELOG-v2.md index 36efaddd12..adf2199c1a 100644 --- a/CHANGELOG-v2.md +++ b/CHANGELOG-v2.md @@ -30,11 +30,11 @@ Showing all changes since the last release (v.1.5.0) - Removed implicit version `Provable.equal(x, y)` where you didn't have to pass in the type -- `zkProgram` to support `auxiliaryOutput` +- `ZkProgram` to support `auxiliaryOutput` ### Added --`zkProgram` to support non-pure provable types as inputs and outputs https://github.com/o1-labs/o1js/pull/1828 +-`ZkProgram` to support non-pure provable types as inputs and outputs https://github.com/o1-labs/o1js/pull/1828 - New method `toCanonical()` in the `Provable` interface to protect against incompleteness of certain operations on malicious witness inputs https://github.com/o1-labs/o1js/pull/1759 - `divMod64()` division modulo 2^64 that returns the remainder and quotient of the operation From 80191e4a5a1a0a4ade8ddc7474aa913a42c5e838 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Mon, 21 Oct 2024 02:34:40 -0600 Subject: [PATCH 090/136] refactor(zkprogram.ts): remove unused function fromFieldConsts to clean up the code and improve maintainability --- src/lib/proof-system/zkprogram.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 962ce227b0..1828f8f4ca 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -1541,10 +1541,6 @@ function toFieldVars(type: ProvablePure, value: T) { return MlFieldArray.to(type.toFields(value)); } -function fromFieldConsts(type: any, fields: MlFieldConstArray) { - return type.fromFields(MlFieldConstArray.from(fields)); -} - function fromFieldAndAuxConsts( type: Provable, fields: MlFieldConstArray, From f23a636202f4c5fadd8b1e5883788e0680756459 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Mon, 21 Oct 2024 22:42:56 -0600 Subject: [PATCH 091/136] refactor(zkprogram.ts): rename auxInputCache to nonPureDataCache for clarity and consistency --- src/lib/proof-system/zkprogram.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 1828f8f4ca..d81a349724 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -161,14 +161,14 @@ const FeatureFlags = { }; function createProgramState() { - let auxInputCache: Map = new Map(); + let nonPureDataCache: Map = new Map(); let methodCache: Map = new Map(); return { setAuxilaryInput(value: any[]) { - auxInputCache.set('auxinput', value); + nonPureDataCache.set('auxinput', value); }, getAuxilaryInput: () => { - let entry = auxInputCache.get('auxinput'); + let entry = nonPureDataCache.get('auxinput'); if (entry === undefined) throw Error(`Auxiliary input not defined`); return entry; }, @@ -187,7 +187,7 @@ function createProgramState() { methodCache.delete(methodName); }, resetAuxCache() { - auxInputCache.delete('auxinput'); + nonPureDataCache.delete('auxinput'); }, }; } From 291f109f55b97e1a2d5299437c50342af11628e4 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Mon, 21 Oct 2024 22:47:22 -0600 Subject: [PATCH 092/136] refactor(zkprogram.ts): rename getAuxilaryInput to getNonPureInput for better clarity and consistency --- src/lib/proof-system/zkprogram.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index d81a349724..08d9ec61ea 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -167,7 +167,7 @@ function createProgramState() { setAuxilaryInput(value: any[]) { nonPureDataCache.set('auxinput', value); }, - getAuxilaryInput: () => { + getNonPureInput: () => { let entry = nonPureDataCache.get('auxinput'); if (entry === undefined) throw Error(`Auxiliary input not defined`); return entry; @@ -837,7 +837,7 @@ function ZkProgram< let publicOutput; let [publicOutputFields, proof] = MlPair.from(result); if (publicInputAuxExists) { - publicInputAux = programState.getAuxilaryInput(); + publicInputAux = programState.getNonPureInput(); publicOutput = fromFieldAndAuxConsts( publicOutputType, publicOutputFields, From b4f5dc7c99103796952570e45d1c07b0bd047257 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Mon, 21 Oct 2024 22:56:52 -0600 Subject: [PATCH 093/136] refactor(zkprogram.ts): rename setAuxilaryInput to setNonPureInput for clarity and consistency --- src/lib/proof-system/zkprogram.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 08d9ec61ea..aeb3afb8a7 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -164,7 +164,7 @@ function createProgramState() { let nonPureDataCache: Map = new Map(); let methodCache: Map = new Map(); return { - setAuxilaryInput(value: any[]) { + setNonPureInput(value: any[]) { nonPureDataCache.set('auxinput', value); }, getNonPureInput: () => { @@ -804,7 +804,7 @@ function ZkProgram< )); // store publicInput auxilary data in programState cache - programState.setAuxilaryInput(publicInputAux); + programState.setNonPureInput(publicInputAux); } else { publicInputFields = toFieldConsts(publicInputType, publicInput); } From 4c7e5fec2f0c65df912bee7f421d41daa847e96d Mon Sep 17 00:00:00 2001 From: ymekuria Date: Mon, 21 Oct 2024 23:10:19 -0600 Subject: [PATCH 094/136] refactor(zkprogram.ts): rename resetAuxCache function to resetNonPureDataCache for clarity and consistency with its purpose. --- src/lib/proof-system/zkprogram.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index aeb3afb8a7..ec21c0b495 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -186,7 +186,7 @@ function createProgramState() { reset(methodName: string) { methodCache.delete(methodName); }, - resetAuxCache() { + resetNonPureDataCache() { nonPureDataCache.delete('auxinput'); }, }; @@ -844,7 +844,7 @@ function ZkProgram< publicInputAux ); - programState.resetAuxCache(); + programState.resetNonPureDataCache(); } else { // create public output when public input is pure publicOutput = fromFieldAndAuxConsts( From 340bbd6432feb0691382790fd7f0d53bbb17d753 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Mon, 21 Oct 2024 23:21:40 -0600 Subject: [PATCH 095/136] feat(zkprogram.ts): update Zkprogram.proof PublicInputType from FlexibleProvablePure to FlexibleProvable --- src/lib/proof-system/zkprogram.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index ec21c0b495..b507906e77 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -1561,7 +1561,7 @@ function toFieldAndAuxConsts(type: Provable, value: T) { } ZkProgram.Proof = function < - PublicInputType extends FlexibleProvablePure, + PublicInputType extends FlexibleProvable, PublicOutputType extends FlexibleProvablePure >(program: { name: string; From 8f9c71c1562e7a666bb05851102e915503e1db71 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Mon, 21 Oct 2024 23:25:34 -0600 Subject: [PATCH 096/136] refactor(zkprogram.ts): update PublicOutputType constraint to match PublicInputType for consistency and clarity --- src/lib/proof-system/zkprogram.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index b507906e77..8e6c3afc38 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -1562,7 +1562,7 @@ function toFieldAndAuxConsts(type: Provable, value: T) { ZkProgram.Proof = function < PublicInputType extends FlexibleProvable, - PublicOutputType extends FlexibleProvablePure + PublicOutputType extends FlexibleProvable >(program: { name: string; publicInputType: PublicInputType; From 679e4998b979a9642c45a882210f4afb7272b128 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Tue, 22 Oct 2024 10:19:01 -0600 Subject: [PATCH 097/136] feat(zkprogram.ts): add setNonPureOutput function to store non-pure output data in cache for later use --- src/lib/proof-system/zkprogram.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 8e6c3afc38..7ca45dc64c 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -172,6 +172,9 @@ function createProgramState() { if (entry === undefined) throw Error(`Auxiliary input not defined`); return entry; }, + setNonPureOutput(value: any[]) { + nonPureDataCache.set('nonPureOutput', value); + }, setAuxiliaryOutput(value: unknown, methodName: string) { methodCache.set(methodName, value); From 4b192d469ee3002537c5d51f82d0911338baabe5 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Tue, 22 Oct 2024 10:24:13 -0600 Subject: [PATCH 098/136] feat(zkprogram.ts): store nonPureOutput in program state cache --- src/lib/proof-system/zkprogram.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 7ca45dc64c..a0fd192cb2 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -1319,6 +1319,8 @@ function picklesRuleFromFunction( result = (await func(input, ...finalArgs)) as any; } + let nonPureOutput = publicOutputType.toAuxiliary(result.publicOutput); + proofs.forEach(({ proofInstance, classReference }) => { if (!(proofInstance instanceof DynamicProof)) { return; From 595478a5fc635466d454bcdf157371274b647c40 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Tue, 22 Oct 2024 10:28:03 -0600 Subject: [PATCH 099/136] feat(zkprogram.ts): add getNonPureOutput method to programState cache --- src/lib/proof-system/zkprogram.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index a0fd192cb2..005230852d 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -175,7 +175,11 @@ function createProgramState() { setNonPureOutput(value: any[]) { nonPureDataCache.set('nonPureOutput', value); }, - + getNonPureOutput: () => { + let entry = nonPureDataCache.get('nonPureOutput'); + if (entry === undefined) throw Error(`Auxiliary output not defined`); + return entry; + }, setAuxiliaryOutput(value: unknown, methodName: string) { methodCache.set(methodName, value); }, From bbb52ea486a2cddf58900c6ac67b57bc3fc11f43 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Tue, 22 Oct 2024 23:17:51 -0600 Subject: [PATCH 100/136] feat(zkprogram.ts): add key parameter to resetNonPureDataCache method for flexibility --- src/lib/proof-system/zkprogram.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 005230852d..ceff4013ae 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -180,6 +180,7 @@ function createProgramState() { if (entry === undefined) throw Error(`Auxiliary output not defined`); return entry; }, + setAuxiliaryOutput(value: unknown, methodName: string) { methodCache.set(methodName, value); }, @@ -193,8 +194,8 @@ function createProgramState() { reset(methodName: string) { methodCache.delete(methodName); }, - resetNonPureDataCache() { - nonPureDataCache.delete('auxinput'); + resetNonPureDataCache(key: string) { + nonPureDataCache.delete(key); }, }; } @@ -845,6 +846,7 @@ function ZkProgram< let [publicOutputFields, proof] = MlPair.from(result); if (publicInputAuxExists) { publicInputAux = programState.getNonPureInput(); + publicOutput = fromFieldAndAuxConsts( publicOutputType, publicOutputFields, From 87f1bf957f25d281ad8990acff58a4d0c90e26a9 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Tue, 22 Oct 2024 23:49:48 -0600 Subject: [PATCH 101/136] feat(zkprogram.ts): update method call to reset nonPureDataCache with 'nonPureInput' key for accuracy --- src/lib/proof-system/zkprogram.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index ceff4013ae..d75b7d26c6 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -165,10 +165,10 @@ function createProgramState() { let methodCache: Map = new Map(); return { setNonPureInput(value: any[]) { - nonPureDataCache.set('auxinput', value); + nonPureDataCache.set('nonPureInput', value); }, getNonPureInput: () => { - let entry = nonPureDataCache.get('auxinput'); + let entry = nonPureDataCache.get('nonPureInput'); if (entry === undefined) throw Error(`Auxiliary input not defined`); return entry; }, @@ -853,7 +853,7 @@ function ZkProgram< publicInputAux ); - programState.resetNonPureDataCache(); + programState.resetNonPureDataCache('nonPureInput'); } else { // create public output when public input is pure publicOutput = fromFieldAndAuxConsts( From 5fc295c9de7da8427758d7a48871bfb7f22791b0 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 23 Oct 2024 00:06:01 -0600 Subject: [PATCH 102/136] feat(program-with-non-pure-input.ts): update input struct label property in zkprogram example --- src/examples/zkprogram/program-with-non-pure-input.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/examples/zkprogram/program-with-non-pure-input.ts b/src/examples/zkprogram/program-with-non-pure-input.ts index 0a1224113c..4013a29dbc 100644 --- a/src/examples/zkprogram/program-with-non-pure-input.ts +++ b/src/examples/zkprogram/program-with-non-pure-input.ts @@ -14,6 +14,8 @@ let MyProgram = ZkProgram({ baseCase: { privateInputs: [], async method(input: MyStruct) { + //update input in circuit + input.label = 'inCircuit'; return { publicOutput: input, }; @@ -22,13 +24,15 @@ let MyProgram = ZkProgram({ }, }); +// + console.log('compiling MyProgram...'); await MyProgram.compile(); console.log('compile done'); let input = new MyStruct({ label: 'input', value: Field(5) }); + let result = await MyProgram.baseCase(input); let ok = await MyProgram.verify(result.proof); -console.log('ok', ok); assert(ok, 'proof not valid!'); From 29fc2d8b10f1ed87be814d1355e9bd27dd954272 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 23 Oct 2024 00:29:12 -0600 Subject: [PATCH 103/136] feat(zkprogram.ts): add non pure output to program state cache --- src/lib/proof-system/zkprogram.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index d75b7d26c6..06d7f61d78 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -1326,6 +1326,7 @@ function picklesRuleFromFunction( } let nonPureOutput = publicOutputType.toAuxiliary(result.publicOutput); + state?.setNonPureOutput(nonPureOutput); proofs.forEach(({ proofInstance, classReference }) => { if (!(proofInstance instanceof DynamicProof)) { From a218e97cfdc97919aa4bacc671c13f41ece131ec Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 23 Oct 2024 01:12:04 -0600 Subject: [PATCH 104/136] feat(zkprogram.ts): rename publicInputAuxExists variable to nonPureInputExists for clarity --- src/lib/proof-system/zkprogram.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 06d7f61d78..1f7037b7ae 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -800,11 +800,11 @@ function ZkProgram< ); } - let publicInputAuxExists = + let nonPureInputExists = publicInputType.toAuxiliary(publicInput).length !== 0; let publicInputFields, publicInputAux; - if (publicInputAuxExists) { + if (nonPureInputExists) { // serialize publicInput into pure provable field elements and auxilary data ({ publicInputFields, publicInputAux } = toFieldAndAuxConsts( publicInputType, @@ -844,7 +844,7 @@ function ZkProgram< let publicOutput; let [publicOutputFields, proof] = MlPair.from(result); - if (publicInputAuxExists) { + if (nonPureInputExists) { publicInputAux = programState.getNonPureInput(); publicOutput = fromFieldAndAuxConsts( @@ -855,7 +855,6 @@ function ZkProgram< programState.resetNonPureDataCache('nonPureInput'); } else { - // create public output when public input is pure publicOutput = fromFieldAndAuxConsts( publicOutputType, publicOutputFields From 333efcff3c6a91e8689a067e966d1c598cd9f5da Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 23 Oct 2024 01:32:44 -0600 Subject: [PATCH 105/136] feat(zkprogram.ts): set non-pure output in cash only if it exists to avoid unnecessary operations --- src/lib/proof-system/zkprogram.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 1f7037b7ae..76a31a67e3 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -845,7 +845,8 @@ function ZkProgram< let publicOutput; let [publicOutputFields, proof] = MlPair.from(result); if (nonPureInputExists) { - publicInputAux = programState.getNonPureInput(); + publicInputAux = programState.getNonPureOutput(); + console.log('result of get utput', publicInputAux); publicOutput = fromFieldAndAuxConsts( publicOutputType, @@ -1325,7 +1326,11 @@ function picklesRuleFromFunction( } let nonPureOutput = publicOutputType.toAuxiliary(result.publicOutput); - state?.setNonPureOutput(nonPureOutput); + let nonPureOutputExists = nonPureOutput.length !== 0; + + if (state !== undefined && nonPureOutputExists) { + state?.setNonPureOutput(nonPureOutput); + } proofs.forEach(({ proofInstance, classReference }) => { if (!(proofInstance instanceof DynamicProof)) { From b9c131ff8afc419338b3ed1a0cd3c14d13830b9a Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 23 Oct 2024 02:10:32 -0600 Subject: [PATCH 106/136] feat(zkprogram.ts):recompose publicOutput from nonPureOutput and outputFields --- src/lib/proof-system/zkprogram.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 76a31a67e3..3fb4b1526a 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -802,7 +802,7 @@ function ZkProgram< let nonPureInputExists = publicInputType.toAuxiliary(publicInput).length !== 0; - + console.log('nonPure Input Exists', nonPureInputExists); let publicInputFields, publicInputAux; if (nonPureInputExists) { // serialize publicInput into pure provable field elements and auxilary data @@ -845,16 +845,21 @@ function ZkProgram< let publicOutput; let [publicOutputFields, proof] = MlPair.from(result); if (nonPureInputExists) { - publicInputAux = programState.getNonPureOutput(); - console.log('result of get utput', publicInputAux); + let nonPureOutput = programState.getNonPureOutput(); + let nonPureOutputExists = nonPureOutput.length !== 0; + + let nonPureData = nonPureOutputExists + ? nonPureOutput + : programState.getNonPureInput(); publicOutput = fromFieldAndAuxConsts( publicOutputType, publicOutputFields, - publicInputAux + nonPureData ); programState.resetNonPureDataCache('nonPureInput'); + programState.resetNonPureDataCache('nonPureOutPut'); } else { publicOutput = fromFieldAndAuxConsts( publicOutputType, @@ -1326,10 +1331,10 @@ function picklesRuleFromFunction( } let nonPureOutput = publicOutputType.toAuxiliary(result.publicOutput); - let nonPureOutputExists = nonPureOutput.length !== 0; + let nonPureOutputExists = nonPureOutput[0].length !== 0; if (state !== undefined && nonPureOutputExists) { - state?.setNonPureOutput(nonPureOutput); + state.setNonPureOutput(nonPureOutput); } proofs.forEach(({ proofInstance, classReference }) => { From 5e5b8423b5a7ff5e1005551cc2aaea182b9f2a9c Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 23 Oct 2024 02:13:27 -0600 Subject: [PATCH 107/136] feat(program-with-non-pure-input.ts): add assertion to check struct label was updated --- src/examples/zkprogram/program-with-non-pure-input.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/examples/zkprogram/program-with-non-pure-input.ts b/src/examples/zkprogram/program-with-non-pure-input.ts index 4013a29dbc..123ec7603a 100644 --- a/src/examples/zkprogram/program-with-non-pure-input.ts +++ b/src/examples/zkprogram/program-with-non-pure-input.ts @@ -32,7 +32,8 @@ console.log('compile done'); let input = new MyStruct({ label: 'input', value: Field(5) }); -let result = await MyProgram.baseCase(input); -let ok = await MyProgram.verify(result.proof); +let { proof } = await MyProgram.baseCase(input); +let ok = await MyProgram.verify(proof); assert(ok, 'proof not valid!'); +assert(proof.publicOutput.label === 'inCircuit'); From 138045fa44bb60a50cc8a493d9265c4abd76658c Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 23 Oct 2024 19:33:32 -0600 Subject: [PATCH 108/136] feat(zkprogram.ts): add `getNonPureUpdate` function to check if non pure auxilary data was updated in-circuit --- src/lib/proof-system/zkprogram.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 3fb4b1526a..dceaae8d89 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -1802,3 +1802,19 @@ type UnwrapPromise

= P extends Promise ? T : never; type Get = T extends { [K in Key]: infer Value } ? Value : undefined; + +function getNonPureUpdate(nonPureInput: any[], nonPureOutput: any[]): any[] { + var result = []; + for (var i = 0; i < nonPureOutput.length; i++) { + if (Array.isArray(nonPureOutput[i])) { + result[i] = getNonPureUpdate(nonPureInput[i], nonPureOutput[i]); + } else { + if (nonPureOutput[i] !== '') { + result[i] = nonPureOutput[i]; + } else { + result[i] = nonPureInput[i]; + } + } + } + return result; +} From 82676777dd9f4ef71b97af015b7e6f9f2eaafe37 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 23 Oct 2024 19:49:56 -0600 Subject: [PATCH 109/136] feat(zkprogram.ts): add check for result publicOutput before adding aux data to cache --- src/lib/proof-system/zkprogram.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index dceaae8d89..99fef08ff6 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -1330,11 +1330,14 @@ function picklesRuleFromFunction( result = (await func(input, ...finalArgs)) as any; } - let nonPureOutput = publicOutputType.toAuxiliary(result.publicOutput); - let nonPureOutputExists = nonPureOutput[0].length !== 0; + if (result.publicOutput) { + // store the nonPure auxiliary data in program state cache if it exists + let nonPureOutput = publicOutputType.toAuxiliary(result.publicOutput); + let nonPureOutputExists = nonPureOutput.length !== 0; - if (state !== undefined && nonPureOutputExists) { - state.setNonPureOutput(nonPureOutput); + if (state !== undefined && nonPureOutputExists) { + state.setNonPureOutput(nonPureOutput); + } } proofs.forEach(({ proofInstance, classReference }) => { From fbc199daad0aa091e0279a795e9c1158eb2c9bcd Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 23 Oct 2024 20:01:40 -0600 Subject: [PATCH 110/136] feat(zkprogram.ts): deserialize proof public output with updates to aux data made in circuit --- src/lib/proof-system/zkprogram.ts | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 99fef08ff6..d094e4b2d7 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -846,20 +846,19 @@ function ZkProgram< let [publicOutputFields, proof] = MlPair.from(result); if (nonPureInputExists) { let nonPureOutput = programState.getNonPureOutput(); - let nonPureOutputExists = nonPureOutput.length !== 0; - - let nonPureData = nonPureOutputExists - ? nonPureOutput - : programState.getNonPureInput(); + let nonPureInput = programState.getNonPureInput(); + // get updates that were made to non-pure auxiliary data in-circuit + let nonPureUpdate = getNonPureUpdate(nonPureInput, nonPureOutput); + console.log('nonPureUpdate', nonPureUpdate); publicOutput = fromFieldAndAuxConsts( publicOutputType, publicOutputFields, - nonPureData + nonPureUpdate ); programState.resetNonPureDataCache('nonPureInput'); - programState.resetNonPureDataCache('nonPureOutPut'); + programState.resetNonPureDataCache('nonPureOutput'); } else { publicOutput = fromFieldAndAuxConsts( publicOutputType, From 4f377178404b85c2f0ae4f33d9907a1901303e13 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Tue, 29 Oct 2024 17:17:57 -0700 Subject: [PATCH 111/136] feat(provable-context.ts): add auxInputData field to SnarkContext type for additional input data support --- src/lib/provable/core/provable-context.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib/provable/core/provable-context.ts b/src/lib/provable/core/provable-context.ts index a697c5d00d..ee44ab9c5e 100644 --- a/src/lib/provable/core/provable-context.ts +++ b/src/lib/provable/core/provable-context.ts @@ -40,6 +40,7 @@ type SnarkContext = { inCheckedComputation?: boolean; inAnalyze?: boolean; inWitnessBlock?: boolean; + auxInputData?: any[]; }; let snarkContext = Context.create({ default: {} }); From dddffadaaceadb5ce7123bb15a26bc7018346a4b Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 30 Oct 2024 14:20:28 -0700 Subject: [PATCH 112/136] feat(zkprogram.ts): add auxInputData to snarkContext --- src/lib/proof-system/zkprogram.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index d094e4b2d7..f235c03375 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -821,7 +821,11 @@ function ZkProgram< getPreviousProofsForProver(args, methodIntfs[i]) ); - let id = snarkContext.enter({ witnesses: args, inProver: true }); + let id = snarkContext.enter({ + witnesses: args, + inProver: true, + auxInputData: publicInputAux, + }); let result: UnwrapPromise>; try { result = await picklesProver(publicInputFields, previousProofs); @@ -848,7 +852,7 @@ function ZkProgram< let nonPureOutput = programState.getNonPureOutput(); let nonPureInput = programState.getNonPureInput(); - // get updates that were made to non-pure auxiliary data in-circuit + // get update made to non-pure auxiliary data in circuit let nonPureUpdate = getNonPureUpdate(nonPureInput, nonPureOutput); console.log('nonPureUpdate', nonPureUpdate); publicOutput = fromFieldAndAuxConsts( From 3f6584583a4b3dc4daa15d9f493d09660105768a Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 30 Oct 2024 14:25:10 -0700 Subject: [PATCH 113/136] feat(zkprogram.ts): get auxInputData from snarkContext in `picklesRuleFromFunction` --- src/lib/proof-system/zkprogram.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index f235c03375..8d26676c1f 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -826,6 +826,7 @@ function ZkProgram< inProver: true, auxInputData: publicInputAux, }); + let result: UnwrapPromise>; try { result = await picklesProver(publicInputFields, previousProofs); @@ -1283,7 +1284,11 @@ function picklesRuleFromFunction( async function main( publicInput: MlFieldArray ): ReturnType { - let { witnesses: argsWithoutPublicInput, inProver } = snarkContext.get(); + let { + witnesses: argsWithoutPublicInput, + inProver, + auxInputData, + } = snarkContext.get(); assert(!(inProver && argsWithoutPublicInput === undefined)); let finalArgs = []; let proofs: { From cd7bb4204fca5847e6a0f9b05a48428b04ba5e21 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 30 Oct 2024 14:44:20 -0700 Subject: [PATCH 114/136] feat(zkprogram.ts): update fromFieldVars function signature to include auxData parameter for additional flexibility and reusability --- src/lib/proof-system/zkprogram.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 8d26676c1f..8a5a82f818 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -821,6 +821,7 @@ function ZkProgram< getPreviousProofsForProver(args, methodIntfs[i]) ); + console.log('auxdata before entering snarkContext ', publicInputAux); let id = snarkContext.enter({ witnesses: args, inProver: true, @@ -1565,8 +1566,12 @@ function getMaxProofsVerified(methodIntfs: MethodInterface[]) { ) as any as 0 | 1 | 2; } -function fromFieldVars(type: Provable, fields: MlFieldArray) { - return type.fromFields(MlFieldArray.from(fields), []); +function fromFieldVars( + type: Provable, + fields: MlFieldArray, + auxData: any[] = [] +) { + return type.fromFields(MlFieldArray.from(fields), auxData); } function toFieldVars(type: ProvablePure, value: T) { From 80438a9d8b84e32f2b635145d7b272a490d6096a Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 30 Oct 2024 14:47:32 -0700 Subject: [PATCH 115/136] feat(zkprogram.ts): deserilze input with auxInputData from snarkContext --- src/lib/proof-system/zkprogram.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 8a5a82f818..89bcc1b551 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -1335,10 +1335,12 @@ function picklesRuleFromFunction( if (publicInputType === Undefined || publicInputType === Void) { result = (await func(...finalArgs)) as any; } else { - let input = fromFieldVars(publicInputType, publicInput); + console.log('auxData before input', auxInputData); + let input = fromFieldVars(publicInputType, publicInput, auxInputData); result = (await func(input, ...finalArgs)) as any; } + console.log('result input', result); if (result.publicOutput) { // store the nonPure auxiliary data in program state cache if it exists let nonPureOutput = publicOutputType.toAuxiliary(result.publicOutput); From 05ac4feed6a44efcce964710eab5aa8dfcbac534 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 30 Oct 2024 14:53:59 -0700 Subject: [PATCH 116/136] feat(zkprogram.ts): deserialize publicOutput with nonPureOutput --- src/lib/proof-system/zkprogram.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 89bcc1b551..6fa032171b 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -860,7 +860,7 @@ function ZkProgram< publicOutput = fromFieldAndAuxConsts( publicOutputType, publicOutputFields, - nonPureUpdate + nonPureOutput ); programState.resetNonPureDataCache('nonPureInput'); From a143585512635afb329130ad305e07d1b5b341cf Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 30 Oct 2024 15:07:36 -0700 Subject: [PATCH 117/136] refactor(zkprogram.ts): remove unused code --- src/examples/zkprogram/program-with-non-pure-input.ts | 5 ++++- src/lib/proof-system/zkprogram.ts | 5 ----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/examples/zkprogram/program-with-non-pure-input.ts b/src/examples/zkprogram/program-with-non-pure-input.ts index 123ec7603a..4b34ea3132 100644 --- a/src/examples/zkprogram/program-with-non-pure-input.ts +++ b/src/examples/zkprogram/program-with-non-pure-input.ts @@ -2,6 +2,7 @@ import { Field, Provable, Struct, ZkProgram, assert } from 'o1js'; class MyStruct extends Struct({ label: String, + description: String, value: Field, }) {} @@ -35,5 +36,7 @@ let input = new MyStruct({ label: 'input', value: Field(5) }); let { proof } = await MyProgram.baseCase(input); let ok = await MyProgram.verify(proof); +console.log('proof', proof); + assert(ok, 'proof not valid!'); -assert(proof.publicOutput.label === 'inCircuit'); +// assert(proof.publicOutput.label === 'inCircuit'); diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 6fa032171b..30d9e7909b 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -852,18 +852,13 @@ function ZkProgram< let [publicOutputFields, proof] = MlPair.from(result); if (nonPureInputExists) { let nonPureOutput = programState.getNonPureOutput(); - let nonPureInput = programState.getNonPureInput(); - // get update made to non-pure auxiliary data in circuit - let nonPureUpdate = getNonPureUpdate(nonPureInput, nonPureOutput); - console.log('nonPureUpdate', nonPureUpdate); publicOutput = fromFieldAndAuxConsts( publicOutputType, publicOutputFields, nonPureOutput ); - programState.resetNonPureDataCache('nonPureInput'); programState.resetNonPureDataCache('nonPureOutput'); } else { publicOutput = fromFieldAndAuxConsts( From 04326e851d60e3416109cc35b73ae11857724ede Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 30 Oct 2024 15:08:42 -0700 Subject: [PATCH 118/136] refactor(zkprogram.ts): remove unused getNonPureUpdate function to improve code readability and maintainability --- src/lib/proof-system/zkprogram.ts | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 30d9e7909b..5ff12f57e8 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -1815,19 +1815,3 @@ type UnwrapPromise

= P extends Promise ? T : never; type Get = T extends { [K in Key]: infer Value } ? Value : undefined; - -function getNonPureUpdate(nonPureInput: any[], nonPureOutput: any[]): any[] { - var result = []; - for (var i = 0; i < nonPureOutput.length; i++) { - if (Array.isArray(nonPureOutput[i])) { - result[i] = getNonPureUpdate(nonPureInput[i], nonPureOutput[i]); - } else { - if (nonPureOutput[i] !== '') { - result[i] = nonPureOutput[i]; - } else { - result[i] = nonPureInput[i]; - } - } - } - return result; -} From b6c146e0a8ec4dfc31499aa596a0d1ecd2d06c93 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 30 Oct 2024 15:12:49 -0700 Subject: [PATCH 119/136] refactor(zkprogram.ts): rename to fromFieldConsts --- src/lib/proof-system/zkprogram.ts | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 5ff12f57e8..698092075b 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -853,7 +853,7 @@ function ZkProgram< if (nonPureInputExists) { let nonPureOutput = programState.getNonPureOutput(); - publicOutput = fromFieldAndAuxConsts( + publicOutput = fromFieldConsts( publicOutputType, publicOutputFields, nonPureOutput @@ -861,10 +861,7 @@ function ZkProgram< programState.resetNonPureDataCache('nonPureOutput'); } else { - publicOutput = fromFieldAndAuxConsts( - publicOutputType, - publicOutputFields - ); + publicOutput = fromFieldConsts(publicOutputType, publicOutputFields); } return { @@ -1575,7 +1572,7 @@ function toFieldVars(type: ProvablePure, value: T) { return MlFieldArray.to(type.toFields(value)); } -function fromFieldAndAuxConsts( +function fromFieldConsts( type: Provable, fields: MlFieldConstArray, aux: any[] = [] From 52699f0cea62c6b0a08e4a94010dd559e891a38b Mon Sep 17 00:00:00 2001 From: ymekuria Date: Tue, 5 Nov 2024 10:39:32 +0900 Subject: [PATCH 120/136] feat(program-with-non-pure-input.ts): remove unused 'description' field from MyStruct and add assertion to check for 'label' value in proof.publicOutput --- src/examples/zkprogram/program-with-non-pure-input.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/examples/zkprogram/program-with-non-pure-input.ts b/src/examples/zkprogram/program-with-non-pure-input.ts index 4b34ea3132..c5d87017f3 100644 --- a/src/examples/zkprogram/program-with-non-pure-input.ts +++ b/src/examples/zkprogram/program-with-non-pure-input.ts @@ -2,7 +2,6 @@ import { Field, Provable, Struct, ZkProgram, assert } from 'o1js'; class MyStruct extends Struct({ label: String, - description: String, value: Field, }) {} @@ -39,4 +38,4 @@ let ok = await MyProgram.verify(proof); console.log('proof', proof); assert(ok, 'proof not valid!'); -// assert(proof.publicOutput.label === 'inCircuit'); +assert(proof.publicOutput.label === 'inCircuit'); From f67d05478be2de423d3a761143b942b76a060e18 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Tue, 5 Nov 2024 10:46:52 +0900 Subject: [PATCH 121/136] result(zkprogram.ts): refactor publicOutput result conditional check to use optional chaining for improved readability and null safety --- src/lib/proof-system/zkprogram.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 698092075b..7fa1d44f57 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -1333,7 +1333,7 @@ function picklesRuleFromFunction( } console.log('result input', result); - if (result.publicOutput) { + if (result?.publicOutput) { // store the nonPure auxiliary data in program state cache if it exists let nonPureOutput = publicOutputType.toAuxiliary(result.publicOutput); let nonPureOutputExists = nonPureOutput.length !== 0; From 24ce22c94b84d3f87f12a1a2ba8aba8b03c236bf Mon Sep 17 00:00:00 2001 From: ymekuria Date: Tue, 5 Nov 2024 11:02:30 +0900 Subject: [PATCH 122/136] refactor(program-with-non-pure-input.ts): remove unnecessary console.log statement for 'proof' variable --- src/examples/zkprogram/program-with-non-pure-input.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/examples/zkprogram/program-with-non-pure-input.ts b/src/examples/zkprogram/program-with-non-pure-input.ts index c5d87017f3..123ec7603a 100644 --- a/src/examples/zkprogram/program-with-non-pure-input.ts +++ b/src/examples/zkprogram/program-with-non-pure-input.ts @@ -35,7 +35,5 @@ let input = new MyStruct({ label: 'input', value: Field(5) }); let { proof } = await MyProgram.baseCase(input); let ok = await MyProgram.verify(proof); -console.log('proof', proof); - assert(ok, 'proof not valid!'); assert(proof.publicOutput.label === 'inCircuit'); From 02b8b787eecadbb2c0a4f01102030e4b79b33c5c Mon Sep 17 00:00:00 2001 From: ymekuria Date: Tue, 12 Nov 2024 00:16:45 +0700 Subject: [PATCH 123/136] refactor(zkprogram.ts): change arrow function syntax for consistency --- src/lib/proof-system/zkprogram.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 7fa1d44f57..fb3a718188 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -167,7 +167,7 @@ function createProgramState() { setNonPureInput(value: any[]) { nonPureDataCache.set('nonPureInput', value); }, - getNonPureInput: () => { + getNonPureInput() { let entry = nonPureDataCache.get('nonPureInput'); if (entry === undefined) throw Error(`Auxiliary input not defined`); return entry; @@ -175,7 +175,7 @@ function createProgramState() { setNonPureOutput(value: any[]) { nonPureDataCache.set('nonPureOutput', value); }, - getNonPureOutput: () => { + getNonPureOutput(): any { let entry = nonPureDataCache.get('nonPureOutput'); if (entry === undefined) throw Error(`Auxiliary output not defined`); return entry; From ff9852be3df1faa5c83de3845919fb42d6b79942 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Tue, 12 Nov 2024 00:25:33 +0700 Subject: [PATCH 124/136] refactor(zkprogram.ts): merge nonPureDataCache with methodCache to clean up code and improve readability --- src/lib/proof-system/zkprogram.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index fb3a718188..0d3c546b0b 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -161,7 +161,6 @@ const FeatureFlags = { }; function createProgramState() { - let nonPureDataCache: Map = new Map(); let methodCache: Map = new Map(); return { setNonPureInput(value: any[]) { From 5687495118ca125e5f4f57b75a20c8170fc6572f Mon Sep 17 00:00:00 2001 From: ymekuria Date: Tue, 12 Nov 2024 00:32:22 +0700 Subject: [PATCH 125/136] feat(zkprogram.ts): use methodCache to set non pure inputs and outputs --- src/lib/proof-system/zkprogram.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 0d3c546b0b..d371ec0e92 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -164,7 +164,7 @@ function createProgramState() { let methodCache: Map = new Map(); return { setNonPureInput(value: any[]) { - nonPureDataCache.set('nonPureInput', value); + methodCache.set('nonPureInput', value); }, getNonPureInput() { let entry = nonPureDataCache.get('nonPureInput'); @@ -172,7 +172,7 @@ function createProgramState() { return entry; }, setNonPureOutput(value: any[]) { - nonPureDataCache.set('nonPureOutput', value); + methodCache.set('nonPureOutput', value); }, getNonPureOutput(): any { let entry = nonPureDataCache.get('nonPureOutput'); From 35420b1e372b1260f8e00bf1803a2f5cc016e64a Mon Sep 17 00:00:00 2001 From: ymekuria Date: Tue, 12 Nov 2024 00:36:07 +0700 Subject: [PATCH 126/136] feat(zkprogram.ts): use methodCache to get non pure inputs and outputs --- src/lib/proof-system/zkprogram.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index d371ec0e92..c89dae7666 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -167,7 +167,7 @@ function createProgramState() { methodCache.set('nonPureInput', value); }, getNonPureInput() { - let entry = nonPureDataCache.get('nonPureInput'); + let entry = methodCache.get('nonPureInput'); if (entry === undefined) throw Error(`Auxiliary input not defined`); return entry; }, @@ -175,7 +175,7 @@ function createProgramState() { methodCache.set('nonPureOutput', value); }, getNonPureOutput(): any { - let entry = nonPureDataCache.get('nonPureOutput'); + let entry = methodCache.get('nonPureOutput'); if (entry === undefined) throw Error(`Auxiliary output not defined`); return entry; }, @@ -194,7 +194,7 @@ function createProgramState() { methodCache.delete(methodName); }, resetNonPureDataCache(key: string) { - nonPureDataCache.delete(key); + methodCache.delete(key); }, }; } From a03f1f57718b7a8003ea823ecded158b1eebdd7b Mon Sep 17 00:00:00 2001 From: ymekuria Date: Tue, 12 Nov 2024 00:39:43 +0700 Subject: [PATCH 127/136] refactor(zkprogram.ts): change return type of getNonPureInput and getNonPureOutput to improve code clarity and type safety --- src/lib/proof-system/zkprogram.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index c89dae7666..30927b3fd2 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -166,7 +166,7 @@ function createProgramState() { setNonPureInput(value: any[]) { methodCache.set('nonPureInput', value); }, - getNonPureInput() { + getNonPureInput(): any[] { let entry = methodCache.get('nonPureInput'); if (entry === undefined) throw Error(`Auxiliary input not defined`); return entry; @@ -174,7 +174,7 @@ function createProgramState() { setNonPureOutput(value: any[]) { methodCache.set('nonPureOutput', value); }, - getNonPureOutput(): any { + getNonPureOutput(): any[] { let entry = methodCache.get('nonPureOutput'); if (entry === undefined) throw Error(`Auxiliary output not defined`); return entry; From 0303637ed6ffbbdd6993cc319a64477da7126a95 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Tue, 12 Nov 2024 00:52:08 +0700 Subject: [PATCH 128/136] refactor(zkprogram.ts): improve error message for undefined non-pure input and output variables to be more descriptive and accurate --- src/lib/proof-system/zkprogram.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 30927b3fd2..c52945bc9a 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -168,16 +168,16 @@ function createProgramState() { }, getNonPureInput(): any[] { let entry = methodCache.get('nonPureInput'); - if (entry === undefined) throw Error(`Auxiliary input not defined`); - return entry; + if (entry === undefined) throw Error(`Non-pure input not defined`); + return entry as any[]; }, setNonPureOutput(value: any[]) { methodCache.set('nonPureOutput', value); }, getNonPureOutput(): any[] { let entry = methodCache.get('nonPureOutput'); - if (entry === undefined) throw Error(`Auxiliary output not defined`); - return entry; + if (entry === undefined) throw Error(`Non-pure output not defined`); + return entry as any[]; }, setAuxiliaryOutput(value: unknown, methodName: string) { From be54738eea31f9747635c4eb0917b77d49039116 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Tue, 12 Nov 2024 00:59:22 +0700 Subject: [PATCH 129/136] refactor(zkprogram.ts): merge programState reset methods --- src/lib/proof-system/zkprogram.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index c52945bc9a..ed99ecfb8f 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -190,10 +190,7 @@ function createProgramState() { throw Error(`Auxiliary value for method ${methodName} not defined`); return entry; }, - reset(methodName: string) { - methodCache.delete(methodName); - }, - resetNonPureDataCache(key: string) { + reset(key: string) { methodCache.delete(key); }, }; @@ -858,7 +855,7 @@ function ZkProgram< nonPureOutput ); - programState.resetNonPureDataCache('nonPureOutput'); + programState.reset('nonPureOutput'); } else { publicOutput = fromFieldConsts(publicOutputType, publicOutputFields); } From 24b1bd72e0d7d2de571bcb3117a54dc4c88a3c50 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 13 Nov 2024 12:59:31 +0700 Subject: [PATCH 130/136] chore(CHANGELOG): delete CHANGELOG-v2.md file as it is no longer needed --- CHANGELOG-v2.md | 43 ------------------------------------------- CHANGELOG.md | 2 ++ 2 files changed, 2 insertions(+), 43 deletions(-) delete mode 100644 CHANGELOG-v2.md diff --git a/CHANGELOG-v2.md b/CHANGELOG-v2.md deleted file mode 100644 index bbf2309efb..0000000000 --- a/CHANGELOG-v2.md +++ /dev/null @@ -1,43 +0,0 @@ -# Changelog - -All notable changes to this project are documented in this file. - -The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). -This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). - - - - - -Showing all changes since the last release (v.1.5.0) ---> - -## [Unreleased](https://github.com/o1-labs/o1js/compare/1c736add...v2) - 2024-07-12 - -### Breaking Changes - -- The `divMod32()` gadget was modified to accept `nBits` instead of `quotientBits`, and assert it is in the range [0, 2\*\*255) to address an issue previoulsy where the bound on `quotientBits` was too low https://github.com/o1-labs/o1js/pull/1763. - -- `Provable.equal()` now turns both types into canonical form before comparing them https://github.com/o1-labs/o1js/pull/1759 - - - Removed implicit version `Provable.equal(x, y)` where you didn't have to pass in the type - -- `ZkProgram` to support `auxiliaryOutput` - -### Added - --`ZkProgram` to support non-pure provable types as inputs and outputs https://github.com/o1-labs/o1js/pull/1828 - -- New method `toCanonical()` in the `Provable` interface to protect against incompleteness of certain operations on malicious witness inputs https://github.com/o1-labs/o1js/pull/1759 -- `divMod64()` division modulo 2^64 that returns the remainder and quotient of the operation -- `addMod64()` addition modulo 2^64 -- Bitwise OR via `{UInt32, UInt64}.or()` -- **BLAKE2B hash function** gadget [#1285](https://github.com/o1-labs/o1js/pull/1285) diff --git a/CHANGELOG.md b/CHANGELOG.md index b0cd869cc7..400815b4c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,8 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ### Added +-`ZkProgram` to support non-pure provable types as inputs and outputs https://github.com/o1-labs/o1js/pull/1828 + - Support secp256r1 in elliptic curve and ECDSA gadgets https://github.com/o1-labs/o1js/pull/1885 ### Fixed From 4aa4b99ad830db2870789480f9b26f05ba0373be Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 13 Nov 2024 13:03:00 +0700 Subject: [PATCH 131/136] feat(proof.ts): update proof.ts publicInput and output file to useFlexibleProvablePure to FlexibleProvable --- src/lib/proof-system/proof.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/proof-system/proof.ts b/src/lib/proof-system/proof.ts index 9111482cf9..f19402fc8d 100644 --- a/src/lib/proof-system/proof.ts +++ b/src/lib/proof-system/proof.ts @@ -2,7 +2,7 @@ import { initializeBindings, withThreadPool } from '../../snarky.js'; import { Pickles } from '../../snarky.js'; import { Field, Bool } from '../provable/wrapped.js'; import type { - FlexibleProvablePure, + FlexibleProvable, InferProvable, } from '../provable/types/struct.js'; import { FeatureFlags } from './feature-flags.js'; @@ -22,8 +22,8 @@ export { dummyProof, extractProofs, extractProofTypes, type ProofValue }; type MaxProofs = 0 | 1 | 2; class ProofBase { - static publicInputType: FlexibleProvablePure = undefined as any; - static publicOutputType: FlexibleProvablePure = undefined as any; + static publicInputType: FlexibleProvable = undefined as any; + static publicOutputType: FlexibleProvable = undefined as any; static tag: () => { name: string } = () => { throw Error( `You cannot use the \`Proof\` class directly. Instead, define a subclass:\n` + From cad292fcbcc8e68d408036bdce50bf1c43403c70 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 13 Nov 2024 13:07:46 +0700 Subject: [PATCH 132/136] feature(zkprogram.ts): updated proof public input and output type to FlexibleProvable --- src/lib/proof-system/zkprogram.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index f0e3437a6e..8f818f139a 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -3,7 +3,7 @@ import { Snarky, initializeBindings, withThreadPool } from '../../snarky.js'; import { Pickles, Gate } from '../../snarky.js'; import { Field } from '../provable/wrapped.js'; import { - FlexibleProvablePure, + FlexibleProvable, InferProvable, ProvablePureExtended, Struct, From a4725e3d711d1a1f85abfa2a12fcad906f19e75a Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 13 Nov 2024 22:46:17 +0700 Subject: [PATCH 133/136] chore(bindings): update subproject commit reference to acc5a7c56645ea46f58e698bca75a011407d45b0 --- src/bindings | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bindings b/src/bindings index 59eb4c0c3c..acc5a7c566 160000 --- a/src/bindings +++ b/src/bindings @@ -1 +1 @@ -Subproject commit 59eb4c0c3c126819a88624e0445ea31f89c5850a +Subproject commit acc5a7c56645ea46f58e698bca75a011407d45b0 From 7948c23dcf1611522365b130b6c68200da2cd0ec Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 13 Nov 2024 23:25:34 +0700 Subject: [PATCH 134/136] style(program-with-non-pure-input.ts): update input label from 'inCircuit' to 'in-circuit' for consistency and readability --- src/examples/zkprogram/program-with-non-pure-input.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/examples/zkprogram/program-with-non-pure-input.ts b/src/examples/zkprogram/program-with-non-pure-input.ts index 123ec7603a..3566584f06 100644 --- a/src/examples/zkprogram/program-with-non-pure-input.ts +++ b/src/examples/zkprogram/program-with-non-pure-input.ts @@ -15,7 +15,7 @@ let MyProgram = ZkProgram({ privateInputs: [], async method(input: MyStruct) { //update input in circuit - input.label = 'inCircuit'; + input.label = 'in-circuit'; return { publicOutput: input, }; @@ -36,4 +36,4 @@ let { proof } = await MyProgram.baseCase(input); let ok = await MyProgram.verify(proof); assert(ok, 'proof not valid!'); -assert(proof.publicOutput.label === 'inCircuit'); +assert(proof.publicOutput.label === 'in-circuit'); From dc2dd4f376b5b1160d18f258368cdd2a2f77af50 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 13 Nov 2024 23:26:07 +0700 Subject: [PATCH 135/136] feat(zkprogram.ts): remove setNonPureInput and getNonPureInput functions from cache in favor of snarkContext --- src/lib/proof-system/zkprogram.ts | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 8f818f139a..0e769b9439 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -90,14 +90,6 @@ const Void: ProvablePureExtended = EmptyVoid(); function createProgramState() { let methodCache: Map = new Map(); return { - setNonPureInput(value: any[]) { - methodCache.set('nonPureInput', value); - }, - getNonPureInput(): any[] { - let entry = methodCache.get('nonPureInput'); - if (entry === undefined) throw Error(`Non-pure input not defined`); - return entry as any[]; - }, setNonPureOutput(value: any[]) { methodCache.set('nonPureOutput', value); }, @@ -419,9 +411,6 @@ function ZkProgram< publicInputType, publicInput )); - - // store publicInput auxilary data in programState cache - programState.setNonPureInput(publicInputAux); } else { publicInputFields = toFieldConsts(publicInputType, publicInput); } From d9277ddb3a9ea17ab389b5e47ed100e126ed880b Mon Sep 17 00:00:00 2001 From: ymekuria Date: Thu, 14 Nov 2024 00:01:41 +0700 Subject: [PATCH 136/136] refactor(zkprogram.ts): update methodCache key from 'nonPureOutput' to '__nonPureOutput__' to avoid collisions --- src/lib/proof-system/zkprogram.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 0e769b9439..9efb896edb 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -91,10 +91,10 @@ function createProgramState() { let methodCache: Map = new Map(); return { setNonPureOutput(value: any[]) { - methodCache.set('nonPureOutput', value); + methodCache.set('__nonPureOutput__', value); }, getNonPureOutput(): any[] { - let entry = methodCache.get('nonPureOutput'); + let entry = methodCache.get('__nonPureOutput__'); if (entry === undefined) throw Error(`Non-pure output not defined`); return entry as any[]; },