privacy-scaling-explorations / halo2

https://privacy-scaling-explorations.github.io/halo2/
Other
207 stars 129 forks source link

memo: improvement list #15

Closed ashWhiteHat closed 4 months ago

ashWhiteHat commented 2 years ago

I noted improvement list in order not to forget.

Thank you Onur for giving ideas!

barryWhiteHat commented 2 years ago

Can we move this to halo2 repo ?

ashWhiteHat commented 2 years ago

Yes!

lispc commented 2 years ago

@NoCtrlZ i posted some cpu profiling graph here. https://github.com/zcash/halo2/issues/418#issuecomment-989568648. It seems Domain::rotate_extended costs lots of time. Maybe another big issue we need to solve later.

ashWhiteHat commented 2 years ago

@lispc Thank you for nice feed back! I added it to description.

Brechtpd commented 2 years ago

Some of my initial ideas after looking at the code a bit and things I plan on looking into further and implementing. Will have to implement some of these things to know how big of different it makes to know if they are worth it or not.

Most time spent in evaluation the expressions, FFT, and multiexp. Shameless plug to my post about my libsnark optimizations but many of what is said there should also be applicable here.

FFT

Multi exp

Evaluations

Will have to test against actual transactions to see if some of these assumptions hold or not, currently most values are 0 because the bench circuit isn't filled with actual transactions so don't want to base too much on the current bench.

Current status: Biggest gains are from reusing intermediate results across expressions. This also impacts memory use a lot because of temporary buffers so optimizing both at the same time. Very easy to reduce memory by 50%-60% with practically no changes (without counting the memory savings by not using rotate_extended for the evaluations which is also potentially very high). Optimizations across expressions up to 5x faster than just hardcoding the expressions (which was already 3x faster). Currently generating the h(x) poly largely on the fly, fast and memory efficient! (but there is even more room when when trading in some performance for even further memory use reduction. How big this impact I don't know yet.

Some stuff I quickly tested a bit to have more of a feeling how this behaves:

Misc

ashWhiteHat commented 2 years ago

@Brechtpd Thank you! Let me check and I am going to work on that 😎

Brechtpd commented 2 years ago

Two important areas to optimize on the circuits side are

  1. The max degree, which greatly impacts the prover performance because a lot of calculations need to be done in the extended domain. How big the extended domain is depends on the max degree (currently the extended domain is x16 bigger).
  2. The number of lookups, which are heavier on the prover than the custom gates.

We currently have a good idea how to greatly reduce 2) by making use of the intermediate rows (the rows between the step height) by having each row do a limited number of lookups and in the custom gates use those cells to verify the data we would normally lookup.

Because lookups add 3 degrees to the input expression, having this indirection with a lookup always having a simple table cell as input also means we have a lower degree requirement (because the input degree is always 1).

For the custom gates it should always be possible to get the degree as low as needed by making use of intermediate cells (which I guess could even be done automatically), so the theoretical minimum degree we can achieve is strictly the one imposed by the lookups.

The current minimum degree for lookups (between advice cells) is 4. This would put the lower limit on the extended domain to 4x the normal size. However I think it may be possible to lower this to 3 by removing the extra terms required to have zero knowledge (which for zkEVM we don't need anyway):

Without zero knowledge I believe we can drop the (l_last + l_blind) term which lowers the required degree by 1. Can anyone help confirm if this is correct or not? I would think this wouldn't have any impact on anything else but I don't know much about the math so don't know for sure.

If this is true the theoretical lowest degree needed is just 3 and so the extended domain would only be 2x the size of the normal domain, otherwise it's x4 which is still pretty good I guess. :) Adding a selector to the lookups also increases the degree, so if we need the degree to be at 3 we would need dedicated cells that do the lookups on each row without exception (which shouldn't be a problem).

han0110 commented 2 years ago

Without zero knowledge I believe we can drop the (l_last + l_blind) term which lowers the required degree by 1. Can anyone help confirm if this is correct or not?

I think it won't break anything, but would like to hear @therealyingtong's thought.

Adding a selector to the lookups also increases the degree, so if we need the degree to be at 3 we would need dedicated cells that do the lookups on each row without exception (which shouldn't be a problem).

Ideally we don't need to always shuffle the input and table in the same grand product IIUC, we can shuffle input and table separately with their own grand products to take down the degree to 2. If we really need selector on input to make layout easier, then I think this is a possible solution.

lispc commented 2 years ago

upstream halo2 introduced AST for ploy and performance get improved. (details : https://github.com/zcash/halo2/pull/447#issuecomment-1016947456) We may need to merge the upstream later.

ashWhiteHat commented 2 years ago

Introducing assembly was done on https://github.com/appliedzkp/pairing/pull/5. It improved prove and setup slightly, and CPU dramatically as following. https://github.com/appliedzkp/pairing/pull/5#issue-1109232627

ashWhiteHat commented 2 years ago

Assembly Optimization In The Future

goff implementation will be hint.

ashWhiteHat commented 2 years ago

upstream halo2 introduced AST for ploy and performance get improved. (details : https://github.com/zcash/halo2/pull/447#issuecomment-1016947456) We may need to merge the upstream later.

Rebase and introduce assembly reduce prover time 72.02%. https://github.com/appliedzkp/zkevm-circuits/pull/302#issue-830046561

ashWhiteHat commented 2 years ago

Optimize poly evaluation and fft reduce prover time 75.6%. https://github.com/appliedzkp/zkevm-circuits/pull/396#issuecomment-1096423824

ashWhiteHat commented 2 years ago

Category

Memo

davidnevadoc commented 4 months ago

Most of these improvements now apply to primitives that are implemented in halo2curves so the issue is no longer relevant.