Validation icon indicating copy to clipboard operation
Validation copied to clipboard

Feature: Modernize PrimeNumber rule with optimized algorithms and large number support

Open olabie2 opened this issue 5 months ago • 1 comments

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:

  1. 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.

  2. 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.

  3. For Very Large Numbers: For numbers that exceed PHP_INT_MAX (passed as strings), the rule will delegate to the gmp_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.

olabie2 avatar Aug 06 '25 19:08 olabie2

Hi there @olabie2!

Those look like exciting ideas. I am personally passionate about prime numbers myself. However, the prime number rule is more of an example than an actual useful rule in this project. We are definitely not a mathematical library. Our users validate API response objects, form data, log structures and stuff like that, far from number-crunching realms.

Personally, if you need it inside Respect\Validation, I would recommend creating a very specialized repository with a custom rule that could be integrated (our rule system is extensible outside our repo).

Let me know if I'm missing anything here. Maybe there's a really cool use case I haven't thought of.

If you decide to go the specialized repository route and need help from our side regarding integration/extensibility, let us know!

alganet avatar Dec 06 '25 09:12 alganet

I'm going to close this. Feel free to reopen it if you believe it is worth discussing this further.

alganet avatar Dec 16 '25 16:12 alganet