-
Notifications
You must be signed in to change notification settings - Fork 321
Support 5 mod 8 case in SqrtPrecomputation #968
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Thank you for the PR! Could you please add a reference to the algorithm for this case? |
The algorithm to find x such that Because we have prime number |
So, what do you think? Is there something wrong, or do you just not need the algorithm extension? |
@@ -157,6 +157,13 @@ impl<const N: usize> BigInt<N> { | |||
(((self.0[0] << 62) >> 62) % 4) as u8 | |||
} | |||
|
|||
#[doc(hidden)] | |||
pub const fn mod_8(&self) -> u8 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, this and the above mod_4
generalize to any modulo power two and could be rewritten in a general way correct?
https://www.geeksforgeeks.org/compute-modulus-division-by-a-power-of-2-number
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly, the value a % (2^d) is equal to a & (2^d - 1) and this evaluation is more efficient, however I wrote implementation similar to the existing code for the sake of simplicity.
@@ -93,6 +93,37 @@ pub trait MontConfig<const N: usize>: 'static + Sync + Send + Sized { | |||
} | |||
}; | |||
|
|||
/// (MODULUS + 3) / 8 when MODULUS % 8 == 5. Used for square root precomputations. | |||
#[doc(hidden)] | |||
const MODULUS_PLUS_THREE_DIV_EIGHT: Option<BigInt<N>> = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very interesting! I too think it would be helpful to reference a source—perhaps from Hacker's Delight, a university lecture, or an ePrint paper—for further context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is just a partial case of Tonneli-Shanks algorithm https://en.wikipedia.org/wiki/Tonelli%E2%80%93Shanks_algorithm
For the case p=3 mod 4 the solution is straightforward and simple, for the case p=5 mod 8 we need one probing Legendre symbol evaluation and after that multiply by non-residue in appropriate power if needed. The most complicated case is p=1 mod 8 where the number of Legendre symbol evaluations can be different for different values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @serhii023, thanks for the link—this adds some context. And BTW in general, optimizations are great and appreciated.
That being said, a reasonable concern here is that proof by example is not generally sufficient for a cryptographic library (which has a higher standard than general purpose software).
What are your thoughts? Would you be interested in writing a proof for the cases you've referenced?
TBH, maybe a cool side-project in Lean etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is a proof:
Let
Consider than partial case where
Consider our case
- If
$a^{2k+1} \equiv 1 \pmod p$ than using the same technique as in$p=4k+3$ case multiplying both sides by$a$ we get$a^{2k+2} \equiv a \equiv x^2 \pmod p$ so that$x = \pm a^{k+1} \pmod p \quad \square$ - If
$a^{2k+1} \equiv -1 \pmod p$ than we could not obtain square root using the same technique, but recalling Tonneli-Shanks trick of multiplying each side by some non-residue we could obtain the square root. Firstly,$2 \in \mathbb{F}_p$ is always quadratic non-residue modullo$p$ as by Legendre symbol properties$(\frac{2}{p})=(-1)^{\frac{p^2 - 1}{8}} = (-1)^{8k^2 +10k + 3} = -1$ . So that by Euler criterion:$2^{(p-1)/2} \equiv 2^{4k+2} \equiv -1 \pmod p$ . So multiplying both sides of$a^{2k+1} \equiv -1 \pmod p$ by$2^{4k+2}$ we get the following conjugation:$a^{2k+1} 2^{4k+2} \equiv 1 \pmod p$ . Multiplying both sides by$a$ we get$a^{2k+2} 2^{4k+2} \equiv a \equiv x^2 \pmod p$ so that$x = \pm a^{k+1} 2^{2k+1} \pmod p \quad \square$
Above proof is not a proof by example, but a general proof for all primes of form
How does this compare with Atkin's algorithm, as outlined on page 10 here: https://eprint.iacr.org/2012/685.pdf? |
It seems Atkin's algorithm is a constant-time optimization of the straightforward solution proposed by @serhii023 |
@Pratyush @z-tech Hello ! Any ideas this PR could be merged ? There is a strange bug in general library Tonelli-Shanks implementation for the case 8k+5: sometimes it hangs infinitely so merging this PR could fix that issue. You could run use ark_ff::{fields::{Fp256, MontBackend, MontConfig}, BigInteger, PrimeField};
#[derive(MontConfig)]
#[modulus = "7237005577332262213973186563042994240857116359379907606001950938285454250989"]
#[generator = "5"]
pub struct FqConfig;
pub type Fq = Fp256<MontBackend<FqConfig, 4>>; I emphasize that constant-time implementation is not necessary for classic sqrt usages as most of the times it is used for elliptic curve random point generation and secret data does not take part in sqrt computation completely. |
Description
This pull request optimizes the square root method for cases where the given modulus equals 5 mod 8. This specific case now runs in constant time, significantly improving performance compared to the Tonelli-Shanks algorithm.