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

zkApps composability, pt 3: witness Party #294

Merged
merged 19 commits into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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: 1 addition & 1 deletion MINA_COMMIT
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
The mina commit used to generate the backends for node and chrome is
0aab6afdf53e59d9f2854b9a7aef7cef47480c36
122d163109df05c7295983ebe187e2ad898956a0
65 changes: 19 additions & 46 deletions src/build/jsLayoutToTypes.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,14 @@ function writeType(typeData, isJson, withTypeMap) {
let converters = {};
if (!isJson && typeData.checkedType) {
let name = typeData.checkedTypeName;
let typeString = writeType(
typeData.checkedType,
isJson,
withTypeMap
).output;
converters[name] = {
toJsonName: `convert${name}ToJson`,
toFieldsName: `convert${name}ToFields`,
typeName: name,
toJsonType: `(${name.toLowerCase()}: ${typeString}) => ${
writeType(typeData, true, true).output
}`,
toFieldsType: `(${name.toLowerCase()}: ${typeString}) => Field[]`,
type: writeType(typeData.checkedType, false, withTypeMap).output,
jsonType: writeType(typeData, true, true).output,
};
typeData = typeData.checkedType;
}
let { type, inner, layout, optionType } = typeData;
let { type, inner, entries, keys, optionType, docEntries } = typeData;
if (type === 'array') {
let {
output,
Expand Down Expand Up @@ -82,7 +73,8 @@ function writeType(typeData, isJson, withTypeMap) {
let output = '{\n';
indent += ' ';
// TODO: make docs work and use them for doccomments
for (let { key, value, docs } of layout) {
for (let key of keys) {
let value = entries[key];
let questionMark = '';
if (
!isJson &&
Expand Down Expand Up @@ -119,40 +111,30 @@ function writeTsContent(types, isJson) {
let dependencies = new Set();
let converters = {};
let exports = new Set();
for (let [key, value] of Object.entries(types)) {
for (let [Type, value] of Object.entries(types)) {
let inner = writeType(value, isJson);
exports.add(key);
exports.add(Type);
mergeSet(dependencies, inner.dependencies);
mergeObject(converters, inner.converters);
output += `type ${key} = ${inner.output};\n\n`;

output += `type ${Type} = ${inner.output};\n\n`;
if (!isJson) {
output +=
`let ${key} = {\n` +
` toJson(${key.toLowerCase()}: ${key}): Json.${key} {\n` +
` return toJson(jsLayout.${key}, ${key.toLowerCase()}, jsonConverters);\n` +
` },\n` +
` toFields(${key.toLowerCase()}: ${key}): Field[] {\n` +
` return toFields(jsLayout.${key}, ${key.toLowerCase()}, fieldsConverters);\n` +
` },\n` +
`};\n\n`;
output += `let ${Type} = asFieldsAndAux<${Type}, Json.${Type}>(jsLayout.${Type} as any, customTypes);\n\n`;
}
}

let jsonConverters_ = Object.values(converters).map((c) => c.toJsonName);
let fieldsConverters = Object.values(converters).map((c) => c.toFieldsName);
let customTypes = Object.values(converters);
let customTypeNames = Object.values(converters).map((c) => c.typeName);
let imports = new Set();
mergeSet(imports, dependencies);
mergeSet(imports, new Set(jsonConverters_));
mergeSet(imports, new Set(fieldsConverters));
mergeSet(imports, new Set(customTypeNames));

let importPath = isJson ? '../parties-leaves-json' : '../parties-leaves';
return `// @generated this file is auto-generated - don't edit it directly

import { ${[...imports].join(', ')} } from '${importPath}';
${
!isJson
? "import { toJson, toFields } from '../parties-helpers';\n" +
? "import { asFieldsAndAux, AsFieldsAndAux } from '../parties-helpers';\n" +
"import * as Json from './parties-json';\n" +
"import { jsLayout } from './js-layout';\n"
: ''
Expand All @@ -166,22 +148,13 @@ ${
}

${
!isJson
? `type JsonConverters = { ${Object.values(converters)
.map((c) => `${c.typeName}: ${c.toJsonType}`)
.join(';\n')} };
let jsonConverters: JsonConverters = { ${Object.values(converters)
.map((c) => `${c.typeName}: ${c.toJsonName}`)
.join(',\n')} };

type FieldsConverters = { ${Object.values(converters)
.map((c) => `${c.typeName}: ${c.toFieldsType}`)
.join(';\n')} };
let fieldsConverters: FieldsConverters = { ${Object.values(converters)
.map((c) => `${c.typeName}: ${c.toFieldsName}`)
.join(',\n')} };
(!isJson || '') &&
`
type CustomTypes = { ${customTypes
.map((c) => `${c.typeName}: AsFieldsAndAux<${c.type}, ${c.jsonType}>;`)
.join(' ')} }
let customTypes: CustomTypes = { ${customTypeNames.join(', ')} };
`
: ''
}

${output}`;
Expand Down
24 changes: 12 additions & 12 deletions src/chrome_bindings/plonk_wasm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2002,12 +2002,12 @@ export interface InitOutput {
readonly caml_pasta_fq_plonk_gate_vector_get: (a: number, b: number) => number;
readonly caml_pasta_fq_plonk_gate_vector_wrap: (a: number, b: number, c: number) => void;
readonly caml_pasta_fq_plonk_gate_vector_digest: (a: number, b: number) => void;
readonly wasmfqpolycomm_new: (a: number, b: number, c: number) => number;
readonly wasmfqpolycomm_unshifted: (a: number, b: number) => void;
readonly wasmfqpolycomm_set_unshifted: (a: number, b: number, c: number) => void;
readonly __wbg_wasmfqpolycomm_free: (a: number) => void;
readonly __wbg_get_wasmfqpolycomm_shifted: (a: number) => number;
readonly __wbg_set_wasmfqpolycomm_shifted: (a: number, b: number) => void;
readonly wasmfppolycomm_new: (a: number, b: number, c: number) => number;
readonly wasmfppolycomm_unshifted: (a: number, b: number) => void;
readonly wasmfppolycomm_set_unshifted: (a: number, b: number, c: number) => void;
readonly __wbg_wasmfppolycomm_free: (a: number) => void;
readonly __wbg_get_wasmfppolycomm_shifted: (a: number) => number;
readonly __wbg_set_wasmfppolycomm_shifted: (a: number, b: number) => void;
readonly __wbg_wasmfqgatevector_free: (a: number) => void;
readonly __wbg_set_wasmfqgate_wires: (a: number, b: number) => void;
readonly caml_pasta_fq_plonk_gate_vector_create: () => number;
Expand Down Expand Up @@ -2523,12 +2523,12 @@ export interface InitOutput {
readonly caml_fq_srs_read: (a: number, b: number, c: number, d: number, e: number) => void;
readonly caml_fq_srs_h: (a: number) => number;
readonly caml_fq_srs_write: (a: number, b: number, c: number, d: number, e: number) => void;
readonly wasmfppolycomm_new: (a: number, b: number, c: number) => number;
readonly wasmfppolycomm_unshifted: (a: number, b: number) => void;
readonly wasmfppolycomm_set_unshifted: (a: number, b: number, c: number) => void;
readonly __wbg_wasmfppolycomm_free: (a: number) => void;
readonly __wbg_get_wasmfppolycomm_shifted: (a: number) => number;
readonly __wbg_set_wasmfppolycomm_shifted: (a: number, b: number) => void;
readonly wasmfqpolycomm_new: (a: number, b: number, c: number) => number;
readonly wasmfqpolycomm_unshifted: (a: number, b: number) => void;
readonly wasmfqpolycomm_set_unshifted: (a: number, b: number, c: number) => void;
readonly __wbg_wasmfqpolycomm_free: (a: number) => void;
readonly __wbg_get_wasmfqpolycomm_shifted: (a: number) => number;
readonly __wbg_set_wasmfqpolycomm_shifted: (a: number, b: number) => void;
readonly __wbg_wire_free: (a: number) => void;
readonly __wbg_get_wire_row: (a: number) => number;
readonly __wbg_set_wire_row: (a: number, b: number) => void;
Expand Down
Binary file modified src/chrome_bindings/plonk_wasm_bg.wasm
Binary file not shown.
24 changes: 12 additions & 12 deletions src/chrome_bindings/plonk_wasm_bg.wasm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ export function caml_pasta_fq_plonk_gate_vector_add(a: number, b: number): void;
export function caml_pasta_fq_plonk_gate_vector_get(a: number, b: number): number;
export function caml_pasta_fq_plonk_gate_vector_wrap(a: number, b: number, c: number): void;
export function caml_pasta_fq_plonk_gate_vector_digest(a: number, b: number): void;
export function wasmfqpolycomm_new(a: number, b: number, c: number): number;
export function wasmfqpolycomm_unshifted(a: number, b: number): void;
export function wasmfqpolycomm_set_unshifted(a: number, b: number, c: number): void;
export function __wbg_wasmfqpolycomm_free(a: number): void;
export function __wbg_get_wasmfqpolycomm_shifted(a: number): number;
export function __wbg_set_wasmfqpolycomm_shifted(a: number, b: number): void;
export function wasmfppolycomm_new(a: number, b: number, c: number): number;
export function wasmfppolycomm_unshifted(a: number, b: number): void;
export function wasmfppolycomm_set_unshifted(a: number, b: number, c: number): void;
export function __wbg_wasmfppolycomm_free(a: number): void;
export function __wbg_get_wasmfppolycomm_shifted(a: number): number;
export function __wbg_set_wasmfppolycomm_shifted(a: number, b: number): void;
export function __wbg_wasmfqgatevector_free(a: number): void;
export function __wbg_set_wasmfqgate_wires(a: number, b: number): void;
export function caml_pasta_fq_plonk_gate_vector_create(): number;
Expand Down Expand Up @@ -550,12 +550,12 @@ export function caml_fq_srs_batch_accumulator_check(a: number, b: number, c: num
export function caml_fq_srs_read(a: number, b: number, c: number, d: number, e: number): void;
export function caml_fq_srs_h(a: number): number;
export function caml_fq_srs_write(a: number, b: number, c: number, d: number, e: number): void;
export function wasmfppolycomm_new(a: number, b: number, c: number): number;
export function wasmfppolycomm_unshifted(a: number, b: number): void;
export function wasmfppolycomm_set_unshifted(a: number, b: number, c: number): void;
export function __wbg_wasmfppolycomm_free(a: number): void;
export function __wbg_get_wasmfppolycomm_shifted(a: number): number;
export function __wbg_set_wasmfppolycomm_shifted(a: number, b: number): void;
export function wasmfqpolycomm_new(a: number, b: number, c: number): number;
export function wasmfqpolycomm_unshifted(a: number, b: number): void;
export function wasmfqpolycomm_set_unshifted(a: number, b: number, c: number): void;
export function __wbg_wasmfqpolycomm_free(a: number): void;
export function __wbg_get_wasmfqpolycomm_shifted(a: number): number;
export function __wbg_set_wasmfqpolycomm_shifted(a: number, b: number): void;
export function __wbg_wire_free(a: number): void;
export function __wbg_get_wire_row(a: number): number;
export function __wbg_set_wire_row(a: number, b: number): void;
Expand Down
90 changes: 45 additions & 45 deletions src/chrome_bindings/snarky_js_chrome.bc.js

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions src/examples/party-witness.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Types, Party, PrivateKey, Circuit, circuitValue } from 'snarkyjs';

let address = PrivateKey.random().toPublicKey();

let party = Party.defaultParty(address);
party.body.callDepth = 5;
party.lazyAuthorization = {
kind: 'lazy-signature',
privateKey: PrivateKey.random(),
};

let fields = Types.Party.toFields(party);
let aux = Types.Party.toAuxiliary(party);

let partyRaw = Types.Party.fromFields(fields, aux);
let json = Types.Party.toJson(partyRaw);

if (address.toBase58() !== json.body.publicKey) throw Error('fail');

let Null = circuitValue<null>(null);

Circuit.runAndCheck(() => {
let partyWitness = Party.witness(Null, () => ({ party, result: null })).party;
console.assert(partyWitness.body.callDepth === 5);
Circuit.assertEqual(Types.Party, partyWitness, party);
Circuit.assertEqual(
PrivateKey,
(partyWitness.lazyAuthorization as any).privateKey,
(party.lazyAuthorization as any).privateKey
);
});

let result = Circuit.constraintSystem(() => {
let partyWitness = Party.witness(Null, () => ({ party, result: null })).party;
console.assert(partyWitness.body.callDepth === 0);
Circuit.assertEqual(Types.Party, partyWitness, party);
});

console.log(`a party has ${Types.Party.sizeInFields()} fields`);
console.log(
`witnessing a party and comparing it to another one creates ${result.rows} rows`
);
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export {
Ledger,
isReady,
shutdown,
Types,
} from './snarky';
export type { VerificationKey, Keypair } from './snarky';
export * from './snarky/addons';
Expand All @@ -23,8 +22,9 @@ export {
circuitMain,
circuitValue,
} from './lib/circuit_value';

export * from './lib/int';
export { Types } from './snarky/types';

export * as Mina from './lib/mina';
export {
SmartContract,
Expand Down
3 changes: 2 additions & 1 deletion src/lib/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'isomorphic-fetch';
import { Bool, Field, Types, Ledger } from '../snarky';
import { Bool, Field, Ledger } from '../snarky';
import { UInt32, UInt64 } from './int';
import {
getDefaultTokenId,
Expand All @@ -9,6 +9,7 @@ import {
} from './party';
import { PublicKey } from './signature';
import { NetworkValue } from './precondition';
import { Types } from '../snarky/types';

export {
fetchAccount,
Expand Down
5 changes: 3 additions & 2 deletions src/lib/int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
UInt32,
Field,
Bool,
Sign,
} from '../../dist/server';

describe('int', () => {
Expand All @@ -27,7 +28,7 @@ describe('int', () => {
describe('Int64', () => {
describe('toString', () => {
it('should be the same as Field.zero', async () => {
const int = new Int64(UInt64.zero, Field.one);
const int = new Int64(UInt64.zero, Sign.one);
const field = Field.zero;
expect(int.toString()).toEqual(field.toString());
});
Expand All @@ -52,7 +53,7 @@ describe('int', () => {

describe('fromUnsigned', () => {
it('should be the same as UInt64.zero', async () => {
expect(new Int64(UInt64.zero, Field.one)).toEqual(
expect(new Int64(UInt64.zero, Sign.one)).toEqual(
Int64.fromUnsigned(UInt64.zero)
);
});
Expand Down
Loading