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

Conversation

mitschabaude
Copy link
Contributor

@mitschabaude mitschabaude commented Aug 24, 2023

This PR

  • reimplements the small parts of bindings.js in TS with bigints, thus giving snarky-ml a faster backend
  • picks a few other low-hanging perf improvements for witness generation

Results:

  • speeds up pure witness generation by about 5x
  • speeds up the Pickles part of proving due to the faster backend. Together with witness generation, we observe about 25% improvement in proving time.

closes #1063
closes #1066

bindings: o1-labs/o1js-bindings#127
mina: MinaProtocol/mina#13966
snarky: o1-labs/snarky#830

Note for reviewers: 90% of the changes happen in the binding repo

Benchmark 1: Pure witness generation

./run src/examples/benchmarks/mul-witness.ts --bundle

times:

  • 8.80s - initial
  • 3.76s - avoid JS errors for snarky control flow
  • 3.23s - avoid frequent recomputation of Field.{zero, one}
  • 1.74s - swap out Rust Field backend with bigint implementation
  • 1.50s - avoid errors altogether in snarky control flow

Improvement: 83%

Benchmark 2: Series of multi-contract interactions with proving (DEX)

./run src/examples/zkapps/dex/happy-path-with-actions.ts --bundle

Total time on main: 393 sec
Total time on this PR: 303 sec
Improvement: 23%

Time for the swap proof (main): 52 sec
Time for the swap proof (this PR): 39 sec
Improvement: 25%

Full timing output (main):

compile (token)... 34.219 sec
compile (dex token holder)... 9.219 sec
compile (dex main contract)... 14.346 sec
deploy & init token contracts... 29.876 sec
deploy dex contracts... 34.732 sec
transfer tokens to user... 33.664 sec
create user's lq token account... 15.867 sec
supply liquidity... 53.252 sec
redeem liquidity, step 1... 15.757 sec
redeem liquidity, step 2a (get back token X)... 50.273 sec
redeem liquidity, step 2b (get back token Y)... 49.889 sec
swap 10 X for Y... 51.719 sec

Happy path with actions... 392.933 sec

Full timing output (this PR):

compile (token)... 32.873 sec
compile (dex token holder)... 8.059 sec
compile (dex main contract)... 13.675 sec
deploy & init token contracts... 21.747 sec
deploy dex contracts... 25.119 sec
transfer tokens to user... 24.005 sec
create user's lq token account... 11.589 sec
supply liquidity... 41.279 sec
redeem liquidity, step 1... 11.546 sec
redeem liquidity, step 2a (get back token X)... 37.088 sec
redeem liquidity, step 2b (get back token Y)... 37.073 sec
swap 10 X for Y... 38.814 sec

Happy path with actions... 302.960 sec

@mitschabaude mitschabaude changed the base branch from main to berkeley August 30, 2023 12:31
@mitschabaude mitschabaude changed the base branch from berkeley to main August 30, 2023 12:32
Copy link
Member

@Trivo25 Trivo25 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an awesome improvement!

src/lib/zkapp.ts Outdated Show resolved Hide resolved
@mitschabaude mitschabaude changed the base branch from main to perf/bigint-backend-1 September 13, 2023 08:12
@mitschabaude mitschabaude changed the base branch from perf/bigint-backend-1 to main September 13, 2023 08:14
@mitschabaude mitschabaude merged commit 045faa7 into main Oct 3, 2023
13 checks passed
@mitschabaude mitschabaude deleted the perf/bigint-backend branch October 3, 2023 20:41
@mitschabaude
Copy link
Contributor Author

Final update: After measuring the timings again on main, the proof creation time improved further.
It's now about 35% better than what is used to be. I think this is mostly because of the further improvements to snarky error handling that I added in the snarky PR after measuring this. However, also other improvements from crypto eng work in mina/develop (which we recently merged into main) could be mixed in there

Happy path with actions... 
compile (token)... 28.898 sec
compile (dex token holder)... 7.049 sec
compile (dex main contract)... 12.504 sec
deploy & init token contracts... 18.444 sec
deploy dex contracts... 21.505 sec
transfer tokens to user... 20.080 sec
create user's lq token account... 9.812 sec
supply liquidity... 35.416 sec
redeem liquidity, step 1... 9.781 sec
redeem liquidity, step 2a (get back token X)... 32.392 sec
redeem liquidity, step 2b (get back token Y)... 32.395 sec
swap 10 X for Y... 33.746 sec

Happy path with actions... 262.092 sec

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Remove bottlenecks in witness generation Move snarky backend to bigint-based
2 participants