-
Notifications
You must be signed in to change notification settings - Fork 777
Description
Hello Respect/Validation Team,
The Current Situation
The current Respect\Validation\Rules\PrimeNumber
validator is built on a basic trial division algorithm that iterates through odd numbers. While functional, this implementation faces several modern challenges:
- Sub-optimal Performance: The existing trial division method is not the most efficient implementation, leading to slower validation times even for moderately sized numbers.
- Lack of Large Number Support: The rule cannot safely or accurately handle numbers that exceed
PHP_INT_MAX
, which limits its application for use cases involving arbitrary-precision integers (e.g., in scientific computing, cryptography, or fintech).
The Proposal
I propose a comprehensive modernization of the PrimeNumber
rule. This refactoring introduces an adaptive strategy that not only adds support for large numbers but also significantly optimizes the base algorithm for smaller ones.
The new implementation would be structured as follows:
-
For Small Numbers: The existing trial division will be replaced with the highly efficient
6k ± 1
optimization. This immediately reduces the number of divisors to check by two-thirds compared to the current method, providing a solid performance boost for all integer-based validations. -
For Medium Numbers: For integers that are too large for trial division to be efficient, the rule will utilize the Miller-Rabin primality test via the
bcmath
extension. This is a robust, probabilistic test ideal for numbers within this range. -
For Very Large Numbers: For numbers that exceed
PHP_INT_MAX
(passed as strings), the rule will delegate to thegmp_prob_prime
function from the GMP (GNU Multiple Precision) extension. This is the gold standard for performance and reliability when handling arbitrary-precision integers.
The rule intelligently detects which extensions (gmp
, bcmath
) are available and falls back gracefully, ensuring it works correctly in any environment while leveraging the best available tool.
Key Advantages of This Upgrade
- Optimized Base Algorithm: Provides a faster experience even for common, smaller integer checks.
- Massive Performance Gains: The Miller-Rabin and GMP layers offer an exponential speed increase for medium-to-large numbers.
- Arbitrary-Precision Support: Unlocks the ability to validate numbers of any size, making the rule far more powerful and versatile.
- Increased Robustness: Adopts modern, industry-standard practices for primality testing.
- Seamless & Automatic: The validator automatically selects the optimal algorithm without requiring any user configuration.
I have already implemented these changes for the rule and have written the corresponding unit tests to validate all algorithmic paths. I am ready to open a pull request.
Please let me know if you would find this contribution valuable. Thank you for your consideration and for maintaining this excellent library.