entropyxyz/crypto-primes

Better handling of precomputed tables

fjarri opened this issue · 1 comments

We have some precomputed reciprocals that we use in sieving (see precomputed.rs). We currently use lazy_static to handle lazy evaluation ofthose.

lazy_static has some side effects (spin_no_std feature is enabled for all other instances of lazy_static in the dependency tree - see rust-lang-nursery/lazy-static.rs#204).

once_cell is an alternative to it. It will be eventually included in the language (rust-lang/rust#74465). There is a problem though: in order to have no_std, once_cell needs the critical-section feature enabled; critical-section crate is brittle (it requires the user of the final application to be aware of it and follow some rules; see https://docs.rs/critical-section/latest/critical_section/#usage-in-libraries).

Another variant is, of course, creating the Reciprocal objects directly during compilation. It doesn't seem to be very slow, but will take about 400kb.

Ideally, whatever we choose, it should be gated by a feature. Need to also test how slow it actually is (especially relatively to the primality checks) to compute the reciprocals on the fly.

Removed reciprocal precomputation in 09a8f12 - the performance difference is negligible compared to actual primality checks, so all the brittle lazy evaluation libraries are simply not worth the trouble. This can be reverted when once_cell is a part of Rust proper.