Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow non-pure provable types as inputs/outputs in zkprogram #1828

Open
wants to merge 139 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
139 commits
Select commit Hold shift + click to select a range
66bf44c
feat(zkprogram.ts): change compileProgram method publicInputType from…
ymekuria Sep 11, 2024
3a7c5d6
feat(zkprogram.ts): change compileProgram method publicOutputType fro…
ymekuria Sep 11, 2024
57e38d2
feat(zkprogram.ts): add createProgramState function to initialize pro…
ymekuria Sep 11, 2024
bc711c7
feat(zkprogram.ts): add auxiliaryValues map in createProgramState fun…
ymekuria Sep 11, 2024
0bec8d0
feat(zkprogram.ts): add get method to createProgramState
ymekuria Sep 12, 2024
b920736
refactor(zkprogram.ts): rename 'get' method to 'getAuxiliaryOutput' f…
ymekuria Sep 12, 2024
e7c3485
refactor(zkprogram.ts): rename auxiliaryValues variable to methodCach…
ymekuria Sep 12, 2024
55aafd5
feat(zkprogram.ts): add setAuxiliaryOutput method to program state fo…
ymekuria Sep 12, 2024
dc35f8e
feat(zkprogram.ts): get auxiliray output from cache
ymekuria Sep 16, 2024
7d9b3c0
feat(zkprogram.ts): initialize programState cache
ymekuria Sep 16, 2024
8145573
feat(zkprogram.ts): add programState as a parameter in the compile fu…
ymekuria Sep 16, 2024
e81eea3
refactor(zkprogram.ts): add optional 'state' parameter to compileProg…
ymekuria Sep 16, 2024
6999038
feat(zkprogram.ts): decompose pure values from auxiliary data type
ymekuria Sep 16, 2024
dbc9edf
refactor(zkprogram.ts): pass pure publicInput to picklesFromFunction
ymekuria Sep 16, 2024
0a6e584
feat(zkprogram.ts): decompose auxiliary data from public input
ymekuria Sep 16, 2024
c5ffd54
feat(zkprogram.ts): add setAuxilaryInput method to creatProgramState …
ymekuria Sep 17, 2024
3c56586
feat(zkprogram.ts): update method to add auxiliary input data to cache
ymekuria Sep 17, 2024
da4a7c3
refactor(zkprogram.ts): rename getAuxiliaryOutput to getAuxiliaryInpu…
ymekuria Sep 17, 2024
00f62e3
refactor(zkprogram.ts): rename publicInputAuxilary variable
ymekuria Sep 18, 2024
4eadd0c
feat(zkprogram.ts): storing of publicInput auxiliary data in cache
ymekuria Sep 18, 2024
4cb4b18
feat(zkprogram.ts): get auxiliary data from cache
ymekuria Sep 18, 2024
5d8047b
feat(zkprogram.ts): recompose the input data to include auxiliary
ymekuria Sep 18, 2024
e9d936f
feat(zkprogram.ts): recompose data type from pure values and auxiliar…
ymekuria Sep 18, 2024
de947d4
refactor(zkprogram.ts): rename methodCache to auxInputCache for bette…
ymekuria Sep 18, 2024
ef8f3ce
feat(zkprogram.ts): reset auxiliary cache
ymekuria Sep 18, 2024
16c4ea7
feat(program-with-non-pure-input.ts): add a example with nonpure input
ymekuria Sep 18, 2024
a56602b
refactor(zkprogram.ts): fix typo in function name 'setAuxilaryInput' …
ymekuria Sep 18, 2024
0130410
chore(bindings): update subproject commit reference to 59eb4c0c3 for …
ymekuria Oct 8, 2024
1063de1
Merge branch 'v2' into feature/non-pure-zkprogram-inputs
ymekuria Oct 8, 2024
b980781
chore(mina): update subproject commit reference to 24c8b2d723fb09d0d7…
ymekuria Oct 8, 2024
764c68b
feat(zkprogram.ts): update createProgramState function to handle meth…
ymekuria Oct 8, 2024
c5fa6f7
feat(zkprogram.ts): decompose auxiliary data from publicInput
ymekuria Oct 8, 2024
921aee2
feat(zkprogram.ts): update publicInputType from ProvablePure to Provable
ymekuria Oct 9, 2024
c27ab3b
feat(zkprogram.ts): change publicInputType to Provable from ProvablePure
ymekuria Oct 10, 2024
08574eb
feat(zkprogram.ts): change publicOutputType in picklesRuleFromFunctio…
ymekuria Oct 10, 2024
6077306
feat(zkprogram.ts): update picklesRuleFromFunction publicInputType an…
ymekuria Oct 14, 2024
7d601c7
feat(zkprogram.ts): get auxilaryPublicInput from programState cache
ymekuria Oct 14, 2024
3a5e476
feat(zkprogram.ts): recompose non pure data type with program State a…
ymekuria Oct 14, 2024
437286f
reset(zkprogram.ts): reset the cache
ymekuria Oct 16, 2024
8b8a9ee
feat(zkprogram.ts): add helper function toFieldAndAuxConsts.
ymekuria Oct 16, 2024
0a9a258
feat(zkprogram.ts): decompose field elements from non pure public inp…
ymekuria Oct 16, 2024
2d872e7
feat(zkprogram.ts): decompose auxilary data from non pure public inpu…
ymekuria Oct 16, 2024
ee12afc
feat(zkprogram.ts)return auxilary data and fields in toFieldAndAuxConsts
ymekuria Oct 16, 2024
eb5676f
refactor(zkprogram.ts): rename variables 'fields' and 'aux' to 'publi…
ymekuria Oct 16, 2024
6e0e1e3
refactor(zkprogram.ts): simplify the toFieldAndAuxConsts function by …
ymekuria Oct 16, 2024
37a09be
feat(zkprogram.ts): decompose non pure publicInput into pure field el…
ymekuria Oct 16, 2024
7491a80
feat(zkprogram.ts): set publicInput auxiliary data in cache
ymekuria Oct 16, 2024
6a2856a
feat(zkprogram.ts): change type of auxInputCache and methodCache to u…
ymekuria Oct 16, 2024
b77c5e1
feat(zkprogram.ts): add optional 'aux' parameter to 'fromFieldConsts'…
ymekuria Oct 16, 2024
df972be
refactor(zkprogram.ts): update function signature and parameter types…
ymekuria Oct 16, 2024
c6a8260
feat(zkprogram.ts): add fromFieldAndAuxConsts helper function
ymekuria Oct 16, 2024
efa1379
refactor(zkprogram.ts): simplify fromFieldConsts function parameters …
ymekuria Oct 16, 2024
062d313
feat(zkprogram.ts): add conditional check before storing publicInput …
ymekuria Oct 16, 2024
e9aa3eb
feat(zkprogram.ts): add conditional to recompose non pure data type i…
ymekuria Oct 16, 2024
f741f98
feat(zkprogram.ts): refactor public output generation logic to handle…
ymekuria Oct 16, 2024
ee71654
feat(zkprogram.ts): clear auxiliary cache before generating proof
ymekuria Oct 16, 2024
e65064f
refactor(zkprogram.ts): remove unused variables and unnecessary code …
ymekuria Oct 16, 2024
5347ac3
feat(zkprogram.ts): update publicInputType in picklesRuleFromFunction
ymekuria Oct 16, 2024
5e68a11
refactor(zkprogram.ts): remove unused code related to decomposing aux…
ymekuria Oct 16, 2024
b3521e9
feat(zkprogram.ts): update ProofBase class publicInputType and public…
ymekuria Oct 16, 2024
ce84a0d
feat(zkprogram.ts): update analyzeMethods publicInputType parameter t…
ymekuria Oct 16, 2024
3c8d030
feat(zkprogram.ts): update toFieldConst parameter type from ProvableP…
ymekuria Oct 16, 2024
d0885f2
feat(zkprogram.ts): update PicklesRuleFromFunction publicInputType an…
ymekuria Oct 16, 2024
84df9aa
feat(zkprogram.ts): update config publicInputType to ProvableType
ymekuria Oct 16, 2024
77146bf
feat(zkprogram.ts): update config publicOutputType to Provable
ymekuria Oct 16, 2024
e05f3c1
feat(zkprogram.ts): temp update to fromFieldConsts type
ymekuria Oct 16, 2024
cc54133
Merge branch 'v2' into feature/non-pure-zkprogram-inputs
ymekuria Oct 18, 2024
dc2b2cb
refactor(program-with-non-pure-input.ts): update publicInput property…
ymekuria Oct 18, 2024
0355f67
feat(zkprogram.ts): update publicIntype parameter in fromFieldVars fu…
ymekuria Oct 18, 2024
8b8c120
feat(program-with-non-pure-input.ts): add 'baseCase' method to example
ymekuria Oct 19, 2024
1fbe24f
feat(program-with-non-pure-input.ts): compile zkprogram
ymekuria Oct 19, 2024
36b92fc
feat(program-with-non-pure-input.ts): update baseCase method to retur…
ymekuria Oct 19, 2024
a9ab6e1
feat(program-with-non-pure-input.ts): add invoke basecase method with…
ymekuria Oct 19, 2024
9f5f39b
feat(program-with-non-pure-input.ts): add verification step after run…
ymekuria Oct 19, 2024
df1f615
feat(zkprogram.ts): add check if publicInput has aux data
ymekuria Oct 19, 2024
67854f0
feat(zkprogram.ts): check if public input has aux data before seriali…
ymekuria Oct 19, 2024
146c783
feat(zkprogram.ts): check if public input aux data exists before storing
ymekuria Oct 19, 2024
e4c3fb3
feat(zkprogram.ts): return publicInputFields when no aux data
ymekuria Oct 19, 2024
a836f0e
feature(zkprogram.ts): use fromFieldAndAuxConsts function in case whe…
ymekuria Oct 19, 2024
519c301
feat(zkprogram.ts): update type parameter in fromFieldVars to Provabl…
ymekuria Oct 20, 2024
f661d91
feat(zkprogram.ts): update fromFieldVars function to include an empty…
ymekuria Oct 20, 2024
3914a7d
refactor(zkprogram.ts): remove unnecessary if statement and add missi…
ymekuria Oct 20, 2024
4647ffb
refactor(zkprogram.ts): rename use 'publicInputAuxExists' check
ymekuria Oct 20, 2024
b5eeee5
refactor(zkprogram.ts): remove unnecessary console.log statements for…
ymekuria Oct 20, 2024
8cbe588
feat(zkprogram.ts): change the type of auxInputCache values from unkn…
ymekuria Oct 21, 2024
bb50596
feat(zkprogram.ts): change parameter type of setAuxilaryInput functio…
ymekuria Oct 21, 2024
974dfb0
feat(zkprogram.ts): check if the publicInput has aux data before gett…
ymekuria Oct 21, 2024
cb41aaf
chore(CHANGELOG-v2.md): update changelog
ymekuria Oct 21, 2024
4390daf
feat(zkprogram.ts): improve cache error handling in get by throwing a…
ymekuria Oct 21, 2024
9838622
refactor(zkprogram.ts): remove logs
ymekuria Oct 21, 2024
1ee4236
chore(CHANGELOG-v2.md): update changelog
ymekuria Oct 21, 2024
80191e4
refactor(zkprogram.ts): remove unused function fromFieldConsts to cle…
ymekuria Oct 21, 2024
f23a636
refactor(zkprogram.ts): rename auxInputCache to nonPureDataCache for …
ymekuria Oct 22, 2024
291f109
refactor(zkprogram.ts): rename getAuxilaryInput to getNonPureInput fo…
ymekuria Oct 22, 2024
b4f5dc7
refactor(zkprogram.ts): rename setAuxilaryInput to setNonPureInput fo…
ymekuria Oct 22, 2024
4c7e5fe
refactor(zkprogram.ts): rename resetAuxCache function to resetNonPure…
ymekuria Oct 22, 2024
340bbd6
feat(zkprogram.ts): update Zkprogram.proof PublicInputType from Flexi…
ymekuria Oct 22, 2024
8f9c71c
refactor(zkprogram.ts): update PublicOutputType constraint to match P…
ymekuria Oct 22, 2024
679e499
feat(zkprogram.ts): add setNonPureOutput function to store non-pure o…
ymekuria Oct 22, 2024
4b192d4
feat(zkprogram.ts): store nonPureOutput in program state cache
ymekuria Oct 22, 2024
595478a
feat(zkprogram.ts): add getNonPureOutput method to programState cache
ymekuria Oct 22, 2024
bbb52ea
feat(zkprogram.ts): add key parameter to resetNonPureDataCache method…
ymekuria Oct 23, 2024
87f1bf9
feat(zkprogram.ts): update method call to reset nonPureDataCache with…
ymekuria Oct 23, 2024
5fc295c
feat(program-with-non-pure-input.ts): update input struct label prope…
ymekuria Oct 23, 2024
29fc2d8
feat(zkprogram.ts): add non pure output to program state cache
ymekuria Oct 23, 2024
a218e97
feat(zkprogram.ts): rename publicInputAuxExists variable to nonPureIn…
ymekuria Oct 23, 2024
333efcf
feat(zkprogram.ts): set non-pure output in cash only if it exists to …
ymekuria Oct 23, 2024
b9c131f
feat(zkprogram.ts):recompose publicOutput from nonPureOutput and outp…
ymekuria Oct 23, 2024
5e5b842
feat(program-with-non-pure-input.ts): add assertion to check struct l…
ymekuria Oct 23, 2024
138045f
feat(zkprogram.ts): add `getNonPureUpdate` function to check if non p…
ymekuria Oct 24, 2024
8267677
feat(zkprogram.ts): add check for result publicOutput before adding a…
ymekuria Oct 24, 2024
fbc199d
feat(zkprogram.ts): deserialize proof public output with updates to a…
ymekuria Oct 24, 2024
4f37717
feat(provable-context.ts): add auxInputData field to SnarkContext typ…
ymekuria Oct 30, 2024
dddffad
feat(zkprogram.ts): add auxInputData to snarkContext
ymekuria Oct 30, 2024
3f65845
feat(zkprogram.ts): get auxInputData from snarkContext in `picklesRul…
ymekuria Oct 30, 2024
cd7bb42
feat(zkprogram.ts): update fromFieldVars function signature to includ…
ymekuria Oct 30, 2024
80438a9
feat(zkprogram.ts): deserilze input with auxInputData from snarkContext
ymekuria Oct 30, 2024
05ac4fe
feat(zkprogram.ts): deserialize publicOutput with nonPureOutput
ymekuria Oct 30, 2024
a143585
refactor(zkprogram.ts): remove unused code
ymekuria Oct 30, 2024
04326e8
refactor(zkprogram.ts): remove unused getNonPureUpdate function to im…
ymekuria Oct 30, 2024
b6c146e
refactor(zkprogram.ts): rename to fromFieldConsts
ymekuria Oct 30, 2024
52699f0
feat(program-with-non-pure-input.ts): remove unused 'description' fie…
ymekuria Nov 5, 2024
f67d054
result(zkprogram.ts): refactor publicOutput result conditional check …
ymekuria Nov 5, 2024
24ce22c
refactor(program-with-non-pure-input.ts): remove unnecessary console.…
ymekuria Nov 5, 2024
02b8b78
refactor(zkprogram.ts): change arrow function syntax for consistency
ymekuria Nov 11, 2024
ff9852b
refactor(zkprogram.ts): merge nonPureDataCache with methodCache to cl…
ymekuria Nov 11, 2024
5687495
feat(zkprogram.ts): use methodCache to set non pure inputs and outputs
ymekuria Nov 11, 2024
35420b1
feat(zkprogram.ts): use methodCache to get non pure inputs and outputs
ymekuria Nov 11, 2024
a03f1f5
refactor(zkprogram.ts): change return type of getNonPureInput and get…
ymekuria Nov 11, 2024
0303637
refactor(zkprogram.ts): improve error message for undefined non-pure …
ymekuria Nov 11, 2024
be54738
refactor(zkprogram.ts): merge programState reset methods
ymekuria Nov 11, 2024
4efec25
Merge branch `main` into feature/non-pure-zkprogram-inputs
ymekuria Nov 11, 2024
24b1bd7
chore(CHANGELOG): delete CHANGELOG-v2.md file as it is no longer needed
ymekuria Nov 13, 2024
4aa4b99
feat(proof.ts): update proof.ts publicInput and output file to useFle…
ymekuria Nov 13, 2024
cad292f
feature(zkprogram.ts): updated proof public input and output type to …
ymekuria Nov 13, 2024
a4725e3
chore(bindings): update subproject commit reference to acc5a7c56645ea…
ymekuria Nov 13, 2024
7948c23
style(program-with-non-pure-input.ts): update input label from 'inCir…
ymekuria Nov 13, 2024
dc2dd4f
feat(zkprogram.ts): remove setNonPureInput and getNonPureInput functi…
ymekuria Nov 13, 2024
d9277dd
refactor(zkprogram.ts): update methodCache key from 'nonPureOutput' t…
ymekuria Nov 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/bindings
39 changes: 39 additions & 0 deletions src/examples/zkprogram/program-with-non-pure-input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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',
publicInput: MyStruct,
publicOutput: MyStruct,

methods: {
baseCase: {
privateInputs: [],
async method(input: MyStruct) {
//update input in circuit
input.label = 'in-circuit';
return {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works! But right now we don't handle the case where the aux values are changed in the circuit, like this

input.label = "in circuit";
return {
   publicOutput: input
}

// after the circuit
assert(input.label == "in circuit")

so either we make this work or we explcitily tell developers not to touch the auxiliary values in the circuit; I prefer the first approach

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need them in the circuit!

publicOutput: input,
};
},
},
},
});

//

console.log('compiling MyProgram...');
await MyProgram.compile();
console.log('compile done');

let input = new MyStruct({ label: 'input', value: Field(5) });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is missing the description property


let { proof } = await MyProgram.baseCase(input);
let ok = await MyProgram.verify(proof);

assert(ok, 'proof not valid!');
assert(proof.publicOutput.label === 'in-circuit');
6 changes: 3 additions & 3 deletions src/lib/proof-system/proof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -22,8 +22,8 @@ export { dummyProof, extractProofs, extractProofTypes, type ProofValue };
type MaxProofs = 0 | 1 | 2;

class ProofBase<Input = any, Output = any> {
static publicInputType: FlexibleProvablePure<any> = undefined as any;
static publicOutputType: FlexibleProvablePure<any> = undefined as any;
static publicInputType: FlexibleProvable<any> = undefined as any;
static publicOutputType: FlexibleProvable<any> = undefined as any;
static tag: () => { name: string } = () => {
throw Error(
`You cannot use the \`Proof\` class directly. Instead, define a subclass:\n` +
Expand Down
130 changes: 104 additions & 26 deletions src/lib/proof-system/zkprogram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -89,19 +89,28 @@ const Void: ProvablePureExtended<void, void, null> = EmptyVoid<Field>();

function createProgramState() {
let methodCache: Map<string, unknown> = new Map();

return {
setNonPureOutput(value: any[]) {
methodCache.set('__nonPureOutput__', value);
},
getNonPureOutput(): any[] {
let entry = methodCache.get('__nonPureOutput__');
if (entry === undefined) throw Error(`Non-pure output not defined`);
return entry as any[];
},

setAuxiliaryOutput(value: unknown, methodName: string) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see here! already done

methodCache.set(methodName, value);
},

getAuxiliaryOutput(methodName: string): unknown {
let entry = methodCache.get(methodName);
if (entry === undefined)
throw Error(`Auxiliary value for method ${methodName} not defined`);
return entry;
},
reset(methodName: string) {
methodCache.delete(methodName);
reset(key: string) {
methodCache.delete(key);
},
};
}
Expand Down Expand Up @@ -173,8 +182,8 @@ let SideloadedTag = {

function ZkProgram<
Config extends {
publicInput?: ProvableTypePure;
publicOutput?: ProvableTypePure;
publicInput?: ProvableType;
publicOutput?: ProvableType;
methods: {
[I in string]: {
privateInputs: Tuple<PrivateInput>;
Expand Down Expand Up @@ -250,10 +259,10 @@ function ZkProgram<
let doProving = true;

let methods = config.methods;
let publicInputType: ProvablePure<any> = ProvableType.get(
let publicInputType: Provable<any> = ProvableType.get(
config.publicInput ?? Undefined
);
let publicOutputType: ProvablePure<any> = ProvableType.get(
let publicOutputType: Provable<any> = ProvableType.get(
config.publicOutput ?? Void
);

Expand Down Expand Up @@ -391,10 +400,30 @@ 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.`
);
}
let publicInputFields = toFieldConsts(publicInputType, publicInput);

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
({ publicInputFields, publicInputAux } = toFieldAndAuxConsts(
publicInputType,
publicInput
));
} else {
publicInputFields = toFieldConsts(publicInputType, publicInput);
}

let previousProofs = MlArray.to(getPreviousProofsForProver(args));

let id = snarkContext.enter({ witnesses: args, inProver: true });
console.log('auxdata before entering snarkContext ', publicInputAux);
let id = snarkContext.enter({
witnesses: args,
inProver: true,
auxInputData: publicInputAux,
});

let result: UnwrapPromise<ReturnType<typeof picklesProver>>;
try {
result = await picklesProver(publicInputFields, previousProofs);
Expand All @@ -415,8 +444,21 @@ function ZkProgram<
programState.reset(methodIntfs[i].methodName);
}

let publicOutput;
let [publicOutputFields, proof] = MlPair.from(result);
let publicOutput = fromFieldConsts(publicOutputType, publicOutputFields);
if (nonPureInputExists) {
let nonPureOutput = programState.getNonPureOutput();

publicOutput = fromFieldConsts(
publicOutputType,
publicOutputFields,
nonPureOutput
);

programState.reset('nonPureOutput');
} else {
publicOutput = fromFieldConsts(publicOutputType, publicOutputFields);
}

return {
proof: new ProgramProof({
Expand Down Expand Up @@ -649,8 +691,8 @@ async function compileProgram({
overrideWrapDomain,
state,
}: {
publicInputType: ProvablePure<any>;
publicOutputType: ProvablePure<any>;
publicInputType: Provable<any>;
publicOutputType: Provable<any>;
methodIntfs: MethodInterface[];
methods: ((...args: any) => unknown)[];
gates: Gate[][];
Expand Down Expand Up @@ -762,7 +804,7 @@ If you are using a SmartContract, make sure you are using the @method decorator.
}

function analyzeMethod(
publicInputType: ProvablePure<any>,
publicInputType: Provable<any>,
methodIntf: MethodInterface,
method: (...args: any) => unknown
) {
Expand Down Expand Up @@ -790,8 +832,8 @@ function inCircuitVkHash(inCircuitVk: unknown): Field {
}

function picklesRuleFromFunction(
publicInputType: ProvablePure<unknown>,
publicOutputType: ProvablePure<unknown>,
publicInputType: Provable<unknown>,
publicOutputType: Provable<unknown>,
func: (...args: unknown[]) => unknown,
proofSystemTag: { name: string },
{ methodName, args, auxiliaryType }: MethodInterface,
Expand All @@ -801,7 +843,11 @@ function picklesRuleFromFunction(
async function main(
publicInput: MlFieldArray
): ReturnType<Pickles.Rule['main']> {
let { witnesses: argsWithoutPublicInput, inProver } = snarkContext.get();
let {
witnesses: argsWithoutPublicInput,
inProver,
auxInputData,
} = snarkContext.get();
assert(!(inProver && argsWithoutPublicInput === undefined));
let finalArgs = [];
let proofs: {
Expand Down Expand Up @@ -837,10 +883,22 @@ 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);
let nonPureOutputExists = nonPureOutput.length !== 0;

if (state !== undefined && nonPureOutputExists) {
state.setNonPureOutput(nonPureOutput);
}
}

proofs.forEach(({ Proof, proof }) => {
if (!(proof instanceof DynamicProof)) return;

Expand Down Expand Up @@ -869,7 +927,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)
Expand Down Expand Up @@ -957,20 +1015,40 @@ function getMaxProofsVerified(methodIntfs: MethodInterface[]) {
) as any as 0 | 1 | 2;
}

function fromFieldVars<T>(type: ProvablePure<T>, fields: MlFieldArray) {
return type.fromFields(MlFieldArray.from(fields));
function fromFieldVars<T>(
type: Provable<T>,
fields: MlFieldArray,
auxData: any[] = []
) {
return type.fromFields(MlFieldArray.from(fields), auxData);
}

function fromFieldConsts<T>(type: ProvablePure<T>, fields: MlFieldConstArray) {
return type.fromFields(MlFieldConstArray.from(fields));
function toFieldVars<T>(type: ProvablePure<T>, value: T) {
return MlFieldArray.to(type.toFields(value));
}
function toFieldConsts<T>(type: ProvablePure<T>, value: T) {

function fromFieldConsts<T>(
type: Provable<T>,
fields: MlFieldConstArray,
aux: any[] = []
Trivo25 marked this conversation as resolved.
Show resolved Hide resolved
) {
return type.fromFields(MlFieldConstArray.from(fields), aux);
}

function toFieldConsts<T>(type: Provable<T>, value: T) {
return MlFieldConstArray.to(type.toFields(value));
}

function toFieldAndAuxConsts<T>(type: Provable<T>, value: T) {
return {
publicInputFields: MlFieldConstArray.to(type.toFields(value)),
publicInputAux: type.toAuxiliary(value),
};
}

ZkProgram.Proof = function <
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be adjusted too! We need to "derive" the proof class from our zkProgram correctly via const MyProof = ZkProgram.Proof(MyProgram); which should not work on the type level right now

PublicInputType extends FlexibleProvablePure<any>,
PublicOutputType extends FlexibleProvablePure<any>
PublicInputType extends FlexibleProvable<any>,
PublicOutputType extends FlexibleProvable<any>
>(program: {
name: string;
publicInputType: PublicInputType;
Expand Down
1 change: 1 addition & 0 deletions src/lib/provable/core/provable-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type SnarkContext = {
inCheckedComputation?: boolean;
inAnalyze?: boolean;
inWitnessBlock?: boolean;
auxInputData?: any[];
};
let snarkContext = Context.create<SnarkContext>({ default: {} });

Expand Down
2 changes: 1 addition & 1 deletion src/mina
Submodule mina updated 736 files
Loading