Skip to content

Commit 996ebb3

Browse files
authored
Merge pull request #1928 from o1-labs/feature/expose-proof-base64
Expose proof base64 conversion
2 parents 5fe68ef + 686c085 commit 996ebb3

File tree

6 files changed

+38
-1
lines changed

6 files changed

+38
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
2424
- Add `enforceTransactionLimits` parameter on Network https://github.com/o1-labs/o1js/issues/1910
2525
- Method for optional types to assert none https://github.com/o1-labs/o1js/pull/1922
2626
- Increased maximum supported amount of methods in a `SmartContract` or `ZkProgram` to 30. https://github.com/o1-labs/o1js/pull/1918
27+
- Expose low-level conversion methods `Proof.{_proofToBase64,_proofFromBase64}` https://github.com/o1-labs/o1js/pull/1928
2728

2829
### Fixed
2930

src/lib/proof-system/proof.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { initializeBindings, withThreadPool } from '../../snarky.js';
1+
import {
2+
areBindingsInitialized,
3+
initializeBindings,
4+
withThreadPool,
5+
} from '../../snarky.js';
26
import { Pickles } from '../../snarky.js';
37
import { Field, Bool } from '../provable/wrapped.js';
48
import type {
@@ -92,6 +96,15 @@ class ProofBase<Input = any, Output = any> {
9296
publicFields() {
9397
return (this.constructor as typeof ProofBase).publicFields(this);
9498
}
99+
100+
static _proofFromBase64(proofString: string, maxProofsVerified: 0 | 1 | 2) {
101+
assertBindingsInitialized();
102+
return Pickles.proofOfBase64(proofString, maxProofsVerified)[1];
103+
}
104+
static _proofToBase64(proof: Pickles.Proof, maxProofsVerified: 0 | 1 | 2) {
105+
assertBindingsInitialized();
106+
return Pickles.proofToBase64([maxProofsVerified, proof]);
107+
}
95108
}
96109

97110
class Proof<Input, Output> extends ProofBase<Input, Output> {
@@ -428,3 +441,10 @@ function extractProofTypes(type: ProvableType) {
428441
let proofValues = extractProofs(value);
429442
return proofValues.map((proof) => proof.constructor as typeof ProofBase);
430443
}
444+
445+
function assertBindingsInitialized() {
446+
assert(
447+
areBindingsInitialized,
448+
'Bindings are not initialized. Try calling `await initializeBindings()` first.'
449+
);
450+
}

src/lib/proof-system/proof.unit-test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,19 @@ test('Proof provable', async () => {
1313

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

16+
// can't call this before bindings are initialized
17+
expect(() => MyProof._proofFromBase64('', 0)).toThrow(
18+
'Bindings are not initialized'
19+
);
20+
1621
let proof = await MyProof.dummy(Field(1n), undefined, 0);
1722

23+
// now bindings are initialized and we can call this
24+
let proofBase64 = MyProof._proofToBase64(proof.proof, 0);
25+
let proofRecovered = MyProof._proofFromBase64(proofBase64, 0);
26+
let proofBase64_2 = MyProof._proofToBase64(proofRecovered, 0);
27+
expect(proofBase64).toEqual(proofBase64_2);
28+
1829
expect(MyProof.provable.toFields(proof)).toEqual([Field(1n)]);
1930
expect(MyProof.provable.toAuxiliary(proof)).toEqual([
2031
[],

src/snarky.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,11 @@ export {
4747
MlPublicKey,
4848
MlPublicKeyVar,
4949
MlFeatureFlags,
50+
areBindingsInitialized,
5051
};
5152

53+
declare let areBindingsInitialized: boolean;
54+
5255
type WasmModule = typeof wasm;
5356

5457
type MlGroup = MlPair<FieldVar, FieldVar>;

src/snarky.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ export {
3333
withThreadPool,
3434
wasm,
3535
initializeBindings,
36+
isInitialized as areBindingsInitialized,
3637
};

src/snarky.web.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@ export {
2929
withThreadPool,
3030
wasm,
3131
initializeBindings,
32+
isInitialized as areBindingsInitialized,
3233
};

0 commit comments

Comments
 (0)