Commit graph

107 commits

Author SHA1 Message Date
nicoo
37f717f5e3 factor: Add tests against (random) numbers with known factorisations (#1573) (#1578)
* factor::Factors: Derive implementations of Eq and PartialEq

* factors::Factors: Implement quickcheck::Arbitrary

This generates uniformly-distributed integers with known factorisations,
and will enable further testing.

* factor: Test against random numbers with known factorisations

* factor::Factors::arbitrary: Simplify method signature
2020-08-08 22:26:25 -05:00
nicoo
34a5941ee9
factor::numeric: Add more property-based tests (#1577)
* factor::numeric::gcd: Rename test against the Euclidean algorithm

* factor::numeric::gcd: Add various property-based tests

* factor::numeric::modular_inverse: Rename test

* factor::numeric::modular_inverse: Add test on random values
2020-08-03 14:00:34 +02:00
nicoo
70828329ba
factor::miller_rabin: Deduplicate parametrized_check macro (#1575)
* factor::miller_rabin::tests::first_composite: Drop useless generic

* factor::miller_rabin::tests: Reuse parametrized_check macro
2020-08-03 13:58:09 +02:00
nicoo
1eabda91cf
factor: Split numeric.rs into multiple modules (#1567)
* factor::numeric: Start refactoring into multiple submodules

No change to the module's interface, but it should make it much easier to
keep the tests right next to the code they are related to.

Moreover, build.rs' dependency is now limited to numeric::{modular_inverse,
traits}, meaning that the rest of it can use build-time generated tables etc.

* factor::numeric: Move gcd (and its test) to a submodule

* factor::numeric: Move Montgomery arithmetic to its own module

Finally hollowed-out numeric.rs

* factor: Move numeric.rs to numeric/mod.rs

* factor::numeric: Fix an erroneous lint on obsolete Rust versions
2020-08-02 20:28:00 +02:00
nicoo
645e9a24bd factor::miller_rabin: Add missing copyright header in source file 2020-07-26 14:59:30 +02:00
Roy Ivy III
a6d7379b97
Merge pull request #1563 from nbraud/factor/faster/gcd
factor/perf ~ speed up factor::numeric::gcd
2020-07-25 11:07:57 -05:00
nicoo
6bef6306c0 factor::numeric::gcd: Avoid redundant u64::trailing_zeros and shifts
7 to 10% faster, according to criterion.
2020-07-25 15:04:57 +02:00
nicoo
3e55139c13 factor::miller_rabbin::Result: Mark as #[must_use]
Ignoring a value of that type is a bug: they are only produced by
`miller_rabbin::test`, which is a pure, but expensive, function.

As such, an ignored value is always either a mistake, or an easy
optimisation opportunity (just remove the useless call to `test`).
2020-07-24 23:06:08 +02:00
nicoo
ecc3e2db24 factor::miller_rabin::test: Minor readability improvement 2020-07-24 23:06:08 +02:00
nicoo
e415b17c01 factor::miller_rabin: Remove duplicated work
The duplicate work was introduced in 2015.
2020-07-24 23:06:06 +02:00
nicoo
29d45e472d factor::numeric::gcd: Implement Stein's binary GCD algorithm
Also add a property-based test against the Euclidean implementation.

numeric::gcd got ~50-65% faster, according to criterion. The effect on the
overall system is small, but later PRs will use a lot more GCD computations.
2020-07-24 23:00:32 +02:00
nicoo
4f23767b85 factor::numeric::gcd: Add criterion-powered benchmark
The function had to be made `pub`, this is a [known limitation] of Criterion.

[known limitation]: https://bheisler.github.io/criterion.rs/book/user_guide/known_limitations.html
2020-07-24 22:58:26 +02:00
nicoo
1b593d94c9 factor: Update rand dependency to v0.7 2020-07-24 22:57:08 +02:00
nicoo
9a80ab7741 factor::numeric::DoubleInt: Document the DoubleWidth associated type 2020-07-24 11:28:26 -05:00
nicoo
17c69674eb factor::numeric::Int: Remove from_u128 method
It was unused, the debug assertions only need `to_u128`.
2020-07-24 11:28:26 -05:00
nicoo
1172af09c0 factor::numeric::DoubleInt: Clarify methods and associated types
- `DoubleInt::Double` renamed to `DoubleWidth`
- `{as,from}_double()` renamed to `{as,from}_double_width()`.

This should hopefully clarify that this is not a “double precision”
floating-point type, but an integer type with a larger range (used
for storing intermediate results, typ. from a multiplication)
2020-07-24 11:28:26 -05:00
nicoo
86a4749e3a factor::numeric: fix original "Generalise modular inverse computation" 2020-07-24 11:28:12 -05:00
nicoo
6e228d3184 Merge branches 'factor/faster/{centralise_logic, montgomery32}' 2020-07-05 00:20:16 +02:00
nicoo
4cfe754551 factor::miller_rabin::is_prime: Fix bug
Montgomery only works for odd n, so attempting to construct an instance
for an even number results in a panic!

The most obvious solution is to special-case even numbers.
2020-07-05 00:08:49 +02:00
nicoo
9b0f131135 Fix bug in factor::factor::factor (>_>")
Non-prime numbers, such as 0 or 1, shouldn't be inserted in the factorisation.
2020-07-05 00:07:38 +02:00
nicoo
e9e263ac66 factor::Factors: Derive Clone and Debug
Useful for printing out in-progress factorisations when debugging.
2020-07-05 00:07:33 +02:00
nicoo
7bdc81b882 factor: Add a first property-based test 2020-07-05 00:07:16 +02:00
nicoo
dcc22188ba factor: Fix clippy warnings 2020-07-05 00:05:45 +02:00
nicoo
f1d1cb80e4 factor::Factors: Generalise invariant check (from prime() to add()) 2020-07-05 00:05:45 +02:00
nicoo
2d2b6faab4 factor: Avoid moving data around in main factoring loop
Instead, the same `Factors` object is passed around through the execution.
~10% faster.
2020-07-05 00:05:45 +02:00
nicoo
6713d2ad62 factor: Move recursive factoring logic from rho to factor
No functional change, but prepares a coming optimisation.
2020-07-05 00:05:45 +02:00
nicoo
bd4d6fcac5 factor: Split the CLI and I/O code off the factoring logic 2020-07-05 00:05:45 +02:00
nicoo
6256750376 factor::miller_rabin: Use a macro to instantiate every test 2020-07-05 00:04:56 +02:00
nicoo
7a1b86c9c2 factor::numeric::tests: Use a macro to instantiate every test 2020-07-05 00:04:53 +02:00
nicoo
cbcc760f83 factor::miller_rabin: Squash another bug! >:3
Detected by the testsuite improvement just prior.
2020-07-05 00:04:49 +02:00
nicoo
3d6fdffe14 factor::miller_rabin: Generalise tests to 32 and 64b Montgomery 2020-07-05 00:04:29 +02:00
nicoo
9b149a759b factor::miller_rabin: Hoist edge-cases (even, <2) out of test()
test() takes a modulus that is known to not be even or <2 (otherwise the
Montgomery value could not be constructed), so those checks can be hoisted
into is_prime() and out of the critical path.
2020-07-05 00:04:26 +02:00
nicoo
4f08e28167 factor::miller_rabin: Add property-based test 2020-07-05 00:04:08 +02:00
nicoo
d2fa0fe63c factor::miller_rabin::tests: small_composites → small_semiprimes
This is more descriptive, as semiprime are the products of 2 primes;
all semiprimes are composite, but not all composite numbers are semiprime.
2020-07-05 00:01:48 +02:00
nicoo
1e4d824829 factor::miller_rabin: Add negative test over all small composites 2020-07-05 00:01:44 +02:00
nicoo
600268c6e4 factor::miller_rabin::tests: Refactor 2020-07-05 00:01:40 +02:00
nicoo
0a1200bdb8 factor::miller_rabin: Add test for the largest 64b composite numbers 2020-07-05 00:01:37 +02:00
nicoo
308290325a factor::miller_rabin::is_prime: Fix bug
Montgomery<_> only works for odd n, so attempting to construct an instance
for an even number results in a panic!

The most obvious solution is to special-case even numbers.
2020-07-05 00:01:33 +02:00
nicoo
d2b43f49f9 factor::numeric::OverflowingAdd: Generate impls with a macro 2020-07-05 00:01:21 +02:00
nicoo
b25c77c5f9 factor::numeric: Generate implementations of DoubleInt with a macro 2020-07-05 00:01:21 +02:00
nicoo
f95f977f98 factor::numeric: Generate implementations of Int with a macro 2020-07-05 00:01:21 +02:00
nicoo
53954badd7 factor::numeric: Refactor away the use of {To,From}Primitives 2020-07-05 00:01:21 +02:00
nicoo
19a8231fb2 factor::numeric::Arithmetic: Rename associated type I to ModInt 2020-07-05 00:01:21 +02:00
nicoo
caa79a1261 factor::numeric: Split Int and DoubleInt traits 2020-07-05 00:01:21 +02:00
nicoo
28244413d1 factor::numeric: Document when to remove OverflowingAdd trait 2020-07-05 00:01:21 +02:00
nicoo
3f79be0219 factor::numeric: Use debug_assert! for runtime assertions. 2020-07-05 00:01:21 +02:00
nicoo
774feb0a40 factor::numeric: Generalise tests for Arithmetic trait 2020-07-05 00:01:21 +02:00
Alex Lyon
4d28f48ad9 factor: combine Montgomery and Montgomery32 2020-07-05 00:01:21 +02:00
nicoo
a440807e6c factor::miller_rabin: Use a specialized basis for 32b integers
~3% faster
2020-07-05 00:01:21 +02:00
nicoo
e68bb192f2 factor::numeric: Add a 32b Montgomery variant [WiP]
~32% faster
2020-07-05 00:01:21 +02:00