entropyxyz / crypto-primes

Random prime generation and primality testing library based on `crypto-bigint`.
https://docs.rs/crypto-primes
Apache License 2.0
17 stars 4 forks source link

Better handling of precomputed tables #7

Closed fjarri closed 1 year ago

fjarri commented 1 year ago

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 https://github.com/rust-lang-nursery/lazy-static.rs/issues/204).

once_cell is an alternative to it. It will be eventually included in the language (https://github.com/rust-lang/rust/issues/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.

fjarri commented 1 year ago

Removed reciprocal precomputation in 09a8f1275f637226d8ea42b19825dadccd8f04e7 - 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.