Skip to content

Instantly share code, notes, and snippets.

@marekyggdrasil
Last active February 21, 2022 14:42
Show Gist options
  • Save marekyggdrasil/2a8fc00a86af5d05542e01667a4a2cd7 to your computer and use it in GitHub Desktop.
Save marekyggdrasil/2a8fc00a86af5d05542e01667a4a2cd7 to your computer and use it in GitHub Desktop.
I found this problem with serialization / deserialization of objects of type CircuitValue in snarky-js when arrayProp is involved, not sure if it is correct or not....

Running the version without arrayProp

[nodemon] starting `node --no-warnings --experimental-specifier-resolution=node --loader ts-node/esm try_circuit_serialization.ts`

* checking if JSONized objects match after parsing

{ a: '1', b: { value: '1' } }
{ a: '1', b: { value: '1' } }

* checking if after parsing the object retains field-based type

if hasn't crashed then ok...

and if we run with arrayProp

TypeError: o.toJSON is not a function
    at /Users/marek/Development/octa/prototype/transaction-data-repository/node_modules/snarkyjs/dist/server/index.js:3:4884
    at Array.forEach (<anonymous>)
    at Function.toJSON (/Users/marek/Development/octa/prototype/transaction-data-repository/node_modules/snarkyjs/dist/server/index.js:3:4859)
    at Bla.toJSON (/Users/marek/Development/octa/prototype/transaction-data-repository/node_modules/snarkyjs/dist/server/index.js:3:4411)
    at file:///Users/marek/Development/octa/prototype/transaction-data-repository/try_circuit_serialization_array.ts:42:44
[nodemon] app crashed - waiting for file changes before starting...
import {
Field,
UInt64,
Int64,
CircuitValue,
prop,
isReady
} from 'snarkyjs';
await isReady;
class Bla extends CircuitValue {
@prop a: Field;
@prop b: UInt64;
constructor(
a: Field,
b: UInt64
) {
super();
this.a = a;
this.b = b;
}
}
function castBla(inp: Bla | null): Bla {
if (inp === null) {
throw Error();
}
return inp;
}
console.log();
const bla = new Bla(
new Field(1),
new UInt64(new Field(1)));
const bla2: Bla = castBla(Bla.fromJSON(bla.toJSON()));
console.log("* checking if JSONized objects match after parsing")
console.log();
console.log(bla.toJSON());
console.log(bla2.toJSON());
console.log();
console.log("* checking if after parsing the object retains field-based type");
console.log();
const b1: UInt64 = bla.b;
const b2: UInt64 = bla2.b;
console.log("if hasn't crashed then ok...")
console.log();
import {
Field,
UInt64,
Int64,
CircuitValue,
prop,
arrayProp,
isReady
} from 'snarkyjs';
await isReady;
class Bla extends CircuitValue {
@prop a: Field;
@prop b: UInt64;
@arrayProp(Int64, 5) vals: Int64[];
constructor(
a: Field,
b: UInt64,
vals: Int64[]
) {
super();
this.a = a;
this.b = b;
this.vals = vals;
}
}
function castBla(inp: Bla | null): Bla {
if (inp === null) {
throw Error();
}
return inp;
}
console.log();
const bla = new Bla(
new Field(1),
new UInt64(new Field(1)),
[new Int64(new Field(1)), new Int64(new Field(2)), new Int64(new Field(3))]);
const bla2: Bla = castBla(Bla.fromJSON(bla.toJSON()));
console.log("* checking if JSONized objects match after parsing")
console.log();
console.log(bla.toJSON());
console.log(bla2.toJSON());
console.log();
console.log("* checking if after parsing the object retains field-based type");
console.log();
const b1: UInt64 = bla.b;
const b2: UInt64 = bla2.b;
console.log("if hasn't crashed then ok...")
console.log();
console.log();
console.log();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment