Skip to content

Commit

Permalink
Merge pull request #1928 from o1-labs/feature/expose-proof-base64
Browse files Browse the repository at this point in the history
Expose proof base64 conversion
  • Loading branch information
mitschabaude authored Dec 4, 2024
2 parents 5fe68ef + 686c085 commit 996ebb3
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Add `enforceTransactionLimits` parameter on Network https://github.com/o1-labs/o1js/issues/1910
- Method for optional types to assert none https://github.com/o1-labs/o1js/pull/1922
- Increased maximum supported amount of methods in a `SmartContract` or `ZkProgram` to 30. https://github.com/o1-labs/o1js/pull/1918
- Expose low-level conversion methods `Proof.{_proofToBase64,_proofFromBase64}` https://github.com/o1-labs/o1js/pull/1928

### Fixed

Expand Down
22 changes: 21 additions & 1 deletion src/lib/proof-system/proof.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { initializeBindings, withThreadPool } from '../../snarky.js';
import {
areBindingsInitialized,
initializeBindings,
withThreadPool,
} from '../../snarky.js';
import { Pickles } from '../../snarky.js';
import { Field, Bool } from '../provable/wrapped.js';
import type {
Expand Down Expand Up @@ -92,6 +96,15 @@ class ProofBase<Input = any, Output = any> {
publicFields() {
return (this.constructor as typeof ProofBase).publicFields(this);
}

static _proofFromBase64(proofString: string, maxProofsVerified: 0 | 1 | 2) {
assertBindingsInitialized();
return Pickles.proofOfBase64(proofString, maxProofsVerified)[1];
}
static _proofToBase64(proof: Pickles.Proof, maxProofsVerified: 0 | 1 | 2) {
assertBindingsInitialized();
return Pickles.proofToBase64([maxProofsVerified, proof]);
}
}

class Proof<Input, Output> extends ProofBase<Input, Output> {
Expand Down Expand Up @@ -428,3 +441,10 @@ function extractProofTypes(type: ProvableType) {
let proofValues = extractProofs(value);
return proofValues.map((proof) => proof.constructor as typeof ProofBase);
}

function assertBindingsInitialized() {
assert(
areBindingsInitialized,
'Bindings are not initialized. Try calling `await initializeBindings()` first.'
);
}
11 changes: 11 additions & 0 deletions src/lib/proof-system/proof.unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,19 @@ test('Proof provable', async () => {

expect(MyProof.provable.sizeInFields()).toEqual(1);

// can't call this before bindings are initialized
expect(() => MyProof._proofFromBase64('', 0)).toThrow(
'Bindings are not initialized'
);

let proof = await MyProof.dummy(Field(1n), undefined, 0);

// now bindings are initialized and we can call this
let proofBase64 = MyProof._proofToBase64(proof.proof, 0);
let proofRecovered = MyProof._proofFromBase64(proofBase64, 0);
let proofBase64_2 = MyProof._proofToBase64(proofRecovered, 0);
expect(proofBase64).toEqual(proofBase64_2);

expect(MyProof.provable.toFields(proof)).toEqual([Field(1n)]);
expect(MyProof.provable.toAuxiliary(proof)).toEqual([
[],
Expand Down
3 changes: 3 additions & 0 deletions src/snarky.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@ export {
MlPublicKey,
MlPublicKeyVar,
MlFeatureFlags,
areBindingsInitialized,
};

declare let areBindingsInitialized: boolean;

type WasmModule = typeof wasm;

type MlGroup = MlPair<FieldVar, FieldVar>;
Expand Down
1 change: 1 addition & 0 deletions src/snarky.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ export {
withThreadPool,
wasm,
initializeBindings,
isInitialized as areBindingsInitialized,
};
1 change: 1 addition & 0 deletions src/snarky.web.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ export {
withThreadPool,
wasm,
initializeBindings,
isInitialized as areBindingsInitialized,
};

0 comments on commit 996ebb3

Please sign in to comment.