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

Further coverage of UintLike #37

Closed xuganyu96 closed 7 months ago

xuganyu96 commented 7 months ago

This PR is a continuation of #36 (and thus is a halfway point to #34), though it is still WIP.

36 in its current state will not compile. There are many unimplemented!(). Some of the API's from crypto-bigint has also changed.

This PR improves on #36 by:

As of ba9d3c5633020e7b50e5569e6ddb8e831fbd3d2c the crate will compile, but not all tests will pass:

xuganyu96 commented 7 months ago

Now all tests pass.

tarcieri commented 7 months ago

Related: https://github.com/RustCrypto/crypto-bigint/pull/425

xuganyu96 commented 7 months ago

I made an attempt to implement UintLike for BoxedUint and UintModLike for BoxedResidue. There are a few missing implementations that will need to be merged into RustCrypto/crypto-bigint

xuganyu96 commented 7 months ago

There seems to be a bug with BoxedResidue::square (see https://github.com/RustCrypto/crypto-bigint/issues/441). This can be temporarily side-stepped by calling retrieving and re-instantiating the BoxedResidue, although it is certainly not the ideal solution.

At least for now, it is possible to run generate_prime and generate_safe_prime with BoxedUint. Two simple tests have been added to presets.rs to demonstrate the progress.

xuganyu96 commented 7 months ago

@fjarri @tarcieri Missing arithmetic operations for BoxedUint have been implemented: https://github.com/RustCrypto/crypto-bigint/pull/436. Prime generation for BoxedUint is tested in presets.rs.

There are a two awkward API's. T::one() and T:zero() both need to be replaced with T::one_with_precision() and T::zero_with_precision, which is a bit weird for stack-allocated Uint; also, many T::from(u32) must be immediately followed by a widen(bits_precision), which is also weird for stack-allocated Uint.

Also, the main public API generate_prime and generate_safe_prime need to accommodate the new argument bits_precision. I think with some clear explanation in the documentation, the user should be able to understand, but maybe we can do better. I am open to ideas (maybe we can make generate_prime into a macro?)

Thank you!

xuganyu96 commented 7 months ago

Closing this PR to consolidate work on #36