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

Reimplement snarky backend with TS bigints #1092

Merged
merged 32 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
db1f2cb
bindings
mitschabaude Aug 24, 2023
d013edb
witness generation benchmark
mitschabaude Aug 24, 2023
85a77a0
change low-level field type to bigint
mitschabaude Aug 24, 2023
56c702c
hashing benchmark
mitschabaude Aug 25, 2023
15aa0af
bump es target for FinalizationRegistry
mitschabaude Aug 28, 2023
c50020b
bindings
mitschabaude Aug 28, 2023
f0e3caa
ml option
mitschabaude Aug 29, 2023
44221d1
bindings
mitschabaude Aug 29, 2023
94f396f
bump typescript
mitschabaude Aug 30, 2023
5bcfbc4
new ml conversion utils
mitschabaude Aug 30, 2023
27bec86
add jsoo tag to low-level field type
mitschabaude Aug 30, 2023
bf688af
fix scalar type
mitschabaude Aug 30, 2023
effa569
minor integration test fixes
mitschabaude Sep 1, 2023
087cf60
bindings
mitschabaude Sep 1, 2023
3756cf4
bindings
mitschabaude Sep 5, 2023
98b5d60
upgrade esbuild and use label to make just fix work in cjs
mitschabaude Sep 5, 2023
c4dceef
make equivalence testing helpers more generic
mitschabaude Jun 14, 2023
24398e6
expose handleErrors for bindings test
mitschabaude Sep 5, 2023
185c473
expose Random.byte
mitschabaude Sep 5, 2023
551e687
expose Random.biguint
mitschabaude Sep 5, 2023
5d9686e
add more MlOption helpers
mitschabaude Sep 5, 2023
1a79e71
allow label in handleErrors
mitschabaude Sep 6, 2023
94d71b0
Merge branch 'main' into perf/bigint-backend
mitschabaude Sep 6, 2023
7726494
bindings
mitschabaude Sep 12, 2023
2fc8f1e
Merge branch 'main' into perf/bigint-backend
mitschabaude Sep 29, 2023
e97ca8a
bindings
mitschabaude Sep 29, 2023
bb19f1c
bindings (wrong commit last time)
mitschabaude Oct 2, 2023
6baf7bb
o1js rename
mitschabaude Oct 3, 2023
db33b73
bindings
mitschabaude Oct 3, 2023
4544cad
bindings
mitschabaude Oct 3, 2023
aec52d7
some build trickery to fix minimal mina ci test
mitschabaude Oct 3, 2023
99ebb12
changelog
mitschabaude Oct 3, 2023
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- To recover existing verification keys and behavior, change the order of properties in your Struct definitions to be alphabetical
- The `customObjectKeys` option is removed from `Struct`

### Changed

- Improve prover performance by ~25% https://github.com/o1-labs/o1js/pull/1092
- Change internal representation of field elements to be JS bigint instead of Uint8Array

## [0.13.0](https://github.com/o1-labs/o1js/compare/fbd4b2717...c2f392fe5)

### Breaking changes
Expand Down
2 changes: 1 addition & 1 deletion src/bindings
3 changes: 2 additions & 1 deletion src/build/buildExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ async function build(srcPath, isWeb = false) {
makeNodeModulesExternal(),
makeJsooExternal(),
],
dropLabels: ['CJS'],
});

let absPath = path.resolve('.', outfile);
Expand Down Expand Up @@ -110,7 +111,7 @@ function makeNodeModulesExternal() {
}

function makeJsooExternal() {
let isJsoo = /(bc.cjs|plonk_wasm.cjs|wrapper.js)$/;
let isJsoo = /(bc.cjs|plonk_wasm.cjs)$/;
return {
name: 'plugin-external',
setup(build) {
Expand Down
1 change: 1 addition & 0 deletions src/build/buildWeb.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ async function buildWeb({ production }) {
outfile: 'dist/web/index.js',
resolveExtensions: ['.js', '.ts'],
plugins: [wasmPlugin(), srcStringPlugin()],
dropLabels: ['CJS'],
external: ['*.bc.js'],
target,
allowOverwrite: true,
Expand Down
2 changes: 1 addition & 1 deletion src/examples/benchmarks/hash-witness.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* benchmark witness generation for an all-mul circuit
*/
import { Field, Provable, Poseidon } from 'snarkyjs';
import { Field, Provable, Poseidon } from 'o1js';
import { tic, toc } from './tic-toc.js';

// parameters
Expand Down
2 changes: 1 addition & 1 deletion src/examples/benchmarks/import.web.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
let start = performance.now();
await import('snarkyjs');
await import('o1js');
let time = performance.now() - start;

console.log(`import jsoo: ${time.toFixed(0)}ms`);
2 changes: 1 addition & 1 deletion src/examples/benchmarks/mul-witness.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* benchmark witness generation for an all-mul circuit
*/
import { Field, Provable } from 'snarkyjs';
import { Field, Provable } from 'o1js';
import { tic, toc } from './tic-toc.js';

// parameters
Expand Down
36 changes: 18 additions & 18 deletions src/lib/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,20 @@ export {
toConstantField,
};

type FieldConst = Uint8Array;
type FieldConst = [0, bigint];

function constToBigint(x: FieldConst): Fp {
return Fp.fromBytes([...x]);
return x[1];
}
function constFromBigint(x: Fp) {
return Uint8Array.from(Fp.toBytes(x));
function constFromBigint(x: Fp): FieldConst {
return [0, x];
}

const FieldConst = {
fromBigint: constFromBigint,
toBigint: constToBigint,
equal(x: FieldConst, y: FieldConst) {
for (let i = 0, n = Fp.sizeInBytes(); i < n; i++) {
if (x[i] !== y[i]) return false;
}
return true;
return x[1] === y[1];
},
[0]: constFromBigint(0n),
[1]: constFromBigint(1n),
Expand Down Expand Up @@ -145,15 +142,16 @@ class Field {
this.value = x.value;
return;
}
// FieldVar
if (Array.isArray(x)) {
this.value = x;
return;
}
// FieldConst
if (x instanceof Uint8Array) {
this.value = FieldVar.constant(x);
return;
if (typeof x[1] === 'bigint') {
// FieldConst
this.value = FieldVar.constant(x as FieldConst);
return;
} else {
// FieldVar
this.value = x as FieldVar;
return;
}
}
// TODO this should handle common values efficiently by reading from a lookup table
this.value = FieldVar.constant(Fp(x));
Expand Down Expand Up @@ -1269,12 +1267,14 @@ class Field {

const FieldBinable = defineBinable({
toBytes(t: Field) {
return [...toConstantField(t, 'toBytes').value[1]];
let t0 = toConstantField(t, 'toBytes').toBigInt();
return Fp.toBytes(t0);
},
readBytes(bytes, offset) {
let uint8array = new Uint8Array(32);
uint8array.set(bytes.slice(offset, offset + 32));
return [new Field(uint8array), offset + 32];
let x = Fp.fromBytes([...uint8array]);
return [new Field(x), offset + 32];
},
});

Expand Down
15 changes: 9 additions & 6 deletions src/lib/scalar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ export { Scalar, ScalarConst, unshift, shift };
export { constantScalarToBigint };

type BoolVar = FieldVar;
type ScalarConst = Uint8Array;
type ScalarConst = [0, bigint];

const ScalarConst = {
fromBigint: constFromBigint,
toBigint: constToBigint,
is(x: any): x is ScalarConst {
return Array.isArray(x) && x[0] === 0 && typeof x[1] === 'bigint';
},
};

let scalarShift = Fq(1n + 2n ** 255n);
Expand Down Expand Up @@ -44,9 +47,9 @@ class Scalar {
*
* If the input is too large, it is reduced modulo the scalar field size.
*/
static from(x: Scalar | Uint8Array | bigint | number | string) {
static from(x: Scalar | ScalarConst | bigint | number | string) {
if (x instanceof Scalar) return x;
if (x instanceof Uint8Array) x = ScalarConst.toBigint(x);
if (ScalarConst.is(x)) x = constToBigint(x);
let scalar = Fq(x);
let bits = toBits(scalar);
return new Scalar(bits, scalar);
Expand Down Expand Up @@ -348,10 +351,10 @@ function unshift(s: Fq): Fq {
}

function constToBigint(x: ScalarConst): Fq {
return Fq.fromBytes([...x]);
return x[1];
}
function constFromBigint(x: Fq) {
return Uint8Array.from(Fq.toBytes(x));
function constFromBigint(x: Fq): ScalarConst {
return [0, x];
}

function constantScalarToBigint(s: Scalar, name: string) {
Expand Down
2 changes: 1 addition & 1 deletion src/mina-signer/src/sign-zkapp-command.unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ function fixVerificationKey(a: AccountUpdate) {
let [, data, hash] = Pickles.dummyVerificationKey();
a.body.update.verificationKey.value = {
data,
hash: Field.fromBytes([...hash]),
hash: FieldConst.toBigint(hash),
};
} else {
a.body.update.verificationKey.value = {
Expand Down
Loading