snarkVM icon indicating copy to clipboard operation
snarkVM copied to clipboard

Faster `sqrt` impl

Open jules opened this issue 2 years ago • 4 comments

jules avatar Jul 23 '22 22:07 jules

Screen Shot 2022-07-28 at 4 38 33 PM ^This is the pseudocode algorithm for reference.

howardwu avatar Jul 28 '22 23:07 howardwu

@ljedrz can you check the correctness of the Rust syntax and API usage (i.e. we use .floor() and .sqrt() among many operators) for edge cases that we have not have contemplated?

howardwu avatar Jul 28 '22 23:07 howardwu

Left some suggestions; as long as subtractions and pows don't overflow, I don't see any obvious footguns here. As with any arithmetic implementations, I'd recommend using something like quickcheck to verify that the results match the expectations.

ljedrz avatar Jul 29 '22 07:07 ljedrz

@ljedrz I've managed to squeeze out a lot of optimization, and managed around a 4-10% decrease in time taken to compute square roots (depending on field size, with Fr having the best decrease). Maybe you might find some more optimizations I can apply to shave it down even further.

More generally, the algorithm will become even faster if lookups are added. Since the theoretical improvement applies to both TS and Sarkar, we will see a more significant improvement by applying it here, so I think it may be worthwhile to continue working on this with the intention of merging it in.

jules avatar Aug 01 '22 00:08 jules

For now we've decided to forgo the lookup variant of the Sarkar algorithm as the speedup may not be worth it.

jules avatar Sep 02 '22 01:09 jules