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 131 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
131 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
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
4 changes: 3 additions & 1 deletion CHANGELOG-v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ 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

- New method `toCanonical()` in the `Provable<T>` 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
Expand Down
2 changes: 1 addition & 1 deletion src/bindings
Submodule bindings updated 0 files
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 = 'inCircuit';
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 === 'inCircuit');
140 changes: 113 additions & 27 deletions src/lib/proof-system/zkprogram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,19 +162,36 @@ const FeatureFlags = {

function createProgramState() {
let methodCache: Map<string, unknown> = 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);
},
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 @@ -234,8 +251,8 @@ async function featureFlagsfromFlatMethodIntfs(
}

class ProofBase<Input, Output> {
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 Expand Up @@ -558,8 +575,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 @@ -635,10 +652,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 @@ -778,12 +795,35 @@ 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
));

// store publicInput auxilary data in programState cache
programState.setNonPureInput(publicInputAux);
} else {
publicInputFields = toFieldConsts(publicInputType, publicInput);
}

let previousProofs = MlArray.to(
getPreviousProofsForProver(args, methodIntfs[i])
);

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 @@ -804,8 +844,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 @@ -1062,8 +1115,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 @@ -1175,7 +1228,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 @@ -1203,8 +1256,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 },
{
Expand All @@ -1220,7 +1273,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 @@ -1266,10 +1323,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(({ proofInstance, classReference }) => {
if (!(proofInstance instanceof DynamicProof)) {
return;
Expand Down Expand Up @@ -1299,7 +1368,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 @@ -1487,23 +1556,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 toFieldVars<T>(type: ProvablePure<T>, value: T) {
return MlFieldArray.to(type.toFields(value));
}

function fromFieldConsts<T>(type: ProvablePure<T>, fields: MlFieldConstArray) {
return type.fromFields(MlFieldConstArray.from(fields));
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: ProvablePure<T>, value: T) {

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 636 files