2018-05-02 19:43:17 +00:00
|
|
|
#include "tommath_private.h"
|
2019-10-19 14:24:39 +00:00
|
|
|
#ifdef MP_SQRTMOD_PRIME_C
|
2019-04-07 13:29:11 +00:00
|
|
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
|
|
|
|
/* SPDX-License-Identifier: Unlicense */
|
2015-04-17 20:46:11 +00:00
|
|
|
|
|
|
|
/* Tonelli-Shanks algorithm
|
|
|
|
* https://en.wikipedia.org/wiki/Tonelli%E2%80%93Shanks_algorithm
|
|
|
|
* https://gmplib.org/list-archives/gmp-discuss/2013-April/005300.html
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2019-05-12 22:22:18 +00:00
|
|
|
mp_err mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret)
|
2015-04-17 20:46:11 +00:00
|
|
|
{
|
2019-05-19 15:16:13 +00:00
|
|
|
mp_err err;
|
2019-05-12 22:22:18 +00:00
|
|
|
int legendre;
|
2017-08-30 17:13:53 +00:00
|
|
|
mp_int t1, C, Q, S, Z, M, T, R, two;
|
|
|
|
mp_digit i;
|
2015-04-17 20:46:11 +00:00
|
|
|
|
2017-08-30 17:13:53 +00:00
|
|
|
/* first handle the simple cases */
|
2017-10-15 14:11:09 +00:00
|
|
|
if (mp_cmp_d(n, 0uL) == MP_EQ) {
|
2017-08-30 17:13:53 +00:00
|
|
|
mp_zero(ret);
|
|
|
|
return MP_OKAY;
|
|
|
|
}
|
2017-10-15 14:11:09 +00:00
|
|
|
if (mp_cmp_d(prime, 2uL) == MP_EQ) return MP_VAL; /* prime must be odd */
|
2019-05-19 15:16:13 +00:00
|
|
|
if ((err = mp_kronecker(n, prime, &legendre)) != MP_OKAY) return err;
|
2017-08-30 17:13:53 +00:00
|
|
|
if (legendre == -1) return MP_VAL; /* quadratic non-residue mod prime */
|
2015-04-17 20:46:11 +00:00
|
|
|
|
2019-05-19 15:16:13 +00:00
|
|
|
if ((err = mp_init_multi(&t1, &C, &Q, &S, &Z, &M, &T, &R, &two, NULL)) != MP_OKAY) {
|
|
|
|
return err;
|
2017-08-30 17:13:53 +00:00
|
|
|
}
|
2015-04-17 20:46:11 +00:00
|
|
|
|
2017-08-30 17:13:53 +00:00
|
|
|
/* SPECIAL CASE: if prime mod 4 == 3
|
2019-05-19 15:16:13 +00:00
|
|
|
* compute directly: err = n^(prime+1)/4 mod prime
|
2017-08-30 17:13:53 +00:00
|
|
|
* Handbook of Applied Cryptography algorithm 3.36
|
|
|
|
*/
|
2019-10-29 17:41:25 +00:00
|
|
|
if ((err = mp_mod_d(prime, 4uL, &i)) != MP_OKAY) goto LBL_END;
|
2017-10-15 17:57:12 +00:00
|
|
|
if (i == 3u) {
|
2019-10-29 17:41:25 +00:00
|
|
|
if ((err = mp_add_d(prime, 1uL, &t1)) != MP_OKAY) goto LBL_END;
|
|
|
|
if ((err = mp_div_2(&t1, &t1)) != MP_OKAY) goto LBL_END;
|
|
|
|
if ((err = mp_div_2(&t1, &t1)) != MP_OKAY) goto LBL_END;
|
|
|
|
if ((err = mp_exptmod(n, &t1, prime, ret)) != MP_OKAY) goto LBL_END;
|
2019-05-19 15:16:13 +00:00
|
|
|
err = MP_OKAY;
|
2019-10-29 17:41:25 +00:00
|
|
|
goto LBL_END;
|
2017-08-30 17:13:53 +00:00
|
|
|
}
|
2015-04-17 20:46:11 +00:00
|
|
|
|
2017-08-30 17:13:53 +00:00
|
|
|
/* NOW: Tonelli-Shanks algorithm */
|
2015-04-17 20:46:11 +00:00
|
|
|
|
2017-08-30 17:13:53 +00:00
|
|
|
/* factor out powers of 2 from prime-1, defining Q and S as: prime-1 = Q*2^S */
|
2019-10-29 17:41:25 +00:00
|
|
|
if ((err = mp_copy(prime, &Q)) != MP_OKAY) goto LBL_END;
|
|
|
|
if ((err = mp_sub_d(&Q, 1uL, &Q)) != MP_OKAY) goto LBL_END;
|
2017-08-30 17:13:53 +00:00
|
|
|
/* Q = prime - 1 */
|
|
|
|
mp_zero(&S);
|
|
|
|
/* S = 0 */
|
2019-10-24 15:43:31 +00:00
|
|
|
while (mp_iseven(&Q)) {
|
2019-10-29 17:41:25 +00:00
|
|
|
if ((err = mp_div_2(&Q, &Q)) != MP_OKAY) goto LBL_END;
|
2017-08-30 17:13:53 +00:00
|
|
|
/* Q = Q / 2 */
|
2019-10-29 17:41:25 +00:00
|
|
|
if ((err = mp_add_d(&S, 1uL, &S)) != MP_OKAY) goto LBL_END;
|
2017-08-30 17:13:53 +00:00
|
|
|
/* S = S + 1 */
|
|
|
|
}
|
2015-04-17 20:46:11 +00:00
|
|
|
|
2017-08-30 17:13:53 +00:00
|
|
|
/* find a Z such that the Legendre symbol (Z|prime) == -1 */
|
2019-10-20 15:28:13 +00:00
|
|
|
mp_set(&Z, 2uL);
|
2017-08-30 17:13:53 +00:00
|
|
|
/* Z = 2 */
|
2018-07-09 18:46:22 +00:00
|
|
|
for (;;) {
|
2019-10-29 17:41:25 +00:00
|
|
|
if ((err = mp_kronecker(&Z, prime, &legendre)) != MP_OKAY) goto LBL_END;
|
2017-08-30 17:13:53 +00:00
|
|
|
if (legendre == -1) break;
|
2019-10-29 17:41:25 +00:00
|
|
|
if ((err = mp_add_d(&Z, 1uL, &Z)) != MP_OKAY) goto LBL_END;
|
2017-08-30 17:13:53 +00:00
|
|
|
/* Z = Z + 1 */
|
|
|
|
}
|
2015-04-17 20:46:11 +00:00
|
|
|
|
2019-10-29 17:41:25 +00:00
|
|
|
if ((err = mp_exptmod(&Z, &Q, prime, &C)) != MP_OKAY) goto LBL_END;
|
2017-08-30 17:13:53 +00:00
|
|
|
/* C = Z ^ Q mod prime */
|
2019-10-29 17:41:25 +00:00
|
|
|
if ((err = mp_add_d(&Q, 1uL, &t1)) != MP_OKAY) goto LBL_END;
|
|
|
|
if ((err = mp_div_2(&t1, &t1)) != MP_OKAY) goto LBL_END;
|
2017-08-30 17:13:53 +00:00
|
|
|
/* t1 = (Q + 1) / 2 */
|
2019-10-29 17:41:25 +00:00
|
|
|
if ((err = mp_exptmod(n, &t1, prime, &R)) != MP_OKAY) goto LBL_END;
|
2017-08-30 17:13:53 +00:00
|
|
|
/* R = n ^ ((Q + 1) / 2) mod prime */
|
2019-10-29 17:41:25 +00:00
|
|
|
if ((err = mp_exptmod(n, &Q, prime, &T)) != MP_OKAY) goto LBL_END;
|
2017-08-30 17:13:53 +00:00
|
|
|
/* T = n ^ Q mod prime */
|
2019-10-29 17:41:25 +00:00
|
|
|
if ((err = mp_copy(&S, &M)) != MP_OKAY) goto LBL_END;
|
2017-08-30 17:13:53 +00:00
|
|
|
/* M = S */
|
2019-10-20 15:28:13 +00:00
|
|
|
mp_set(&two, 2uL);
|
2015-04-17 20:46:11 +00:00
|
|
|
|
2018-07-09 18:46:22 +00:00
|
|
|
for (;;) {
|
2019-10-29 17:41:25 +00:00
|
|
|
if ((err = mp_copy(&T, &t1)) != MP_OKAY) goto LBL_END;
|
2017-08-30 17:13:53 +00:00
|
|
|
i = 0;
|
2018-07-09 18:46:22 +00:00
|
|
|
for (;;) {
|
2017-10-15 14:11:09 +00:00
|
|
|
if (mp_cmp_d(&t1, 1uL) == MP_EQ) break;
|
2019-10-29 17:41:25 +00:00
|
|
|
if ((err = mp_exptmod(&t1, &two, prime, &t1)) != MP_OKAY) goto LBL_END;
|
2017-08-30 17:13:53 +00:00
|
|
|
i++;
|
|
|
|
}
|
2017-10-15 17:57:12 +00:00
|
|
|
if (i == 0u) {
|
2019-10-29 17:41:25 +00:00
|
|
|
if ((err = mp_copy(&R, ret)) != MP_OKAY) goto LBL_END;
|
2019-05-19 15:16:13 +00:00
|
|
|
err = MP_OKAY;
|
2019-10-29 17:41:25 +00:00
|
|
|
goto LBL_END;
|
2017-08-30 17:13:53 +00:00
|
|
|
}
|
2019-10-29 17:41:25 +00:00
|
|
|
if ((err = mp_sub_d(&M, i, &t1)) != MP_OKAY) goto LBL_END;
|
|
|
|
if ((err = mp_sub_d(&t1, 1uL, &t1)) != MP_OKAY) goto LBL_END;
|
|
|
|
if ((err = mp_exptmod(&two, &t1, prime, &t1)) != MP_OKAY) goto LBL_END;
|
2017-08-30 17:13:53 +00:00
|
|
|
/* t1 = 2 ^ (M - i - 1) */
|
2019-10-29 17:41:25 +00:00
|
|
|
if ((err = mp_exptmod(&C, &t1, prime, &t1)) != MP_OKAY) goto LBL_END;
|
2017-08-30 17:13:53 +00:00
|
|
|
/* t1 = C ^ (2 ^ (M - i - 1)) mod prime */
|
2019-10-29 17:41:25 +00:00
|
|
|
if ((err = mp_sqrmod(&t1, prime, &C)) != MP_OKAY) goto LBL_END;
|
2017-08-30 17:13:53 +00:00
|
|
|
/* C = (t1 * t1) mod prime */
|
2019-10-29 17:41:25 +00:00
|
|
|
if ((err = mp_mulmod(&R, &t1, prime, &R)) != MP_OKAY) goto LBL_END;
|
2017-08-30 17:13:53 +00:00
|
|
|
/* R = (R * t1) mod prime */
|
2019-10-29 17:41:25 +00:00
|
|
|
if ((err = mp_mulmod(&T, &C, prime, &T)) != MP_OKAY) goto LBL_END;
|
2017-08-30 17:13:53 +00:00
|
|
|
/* T = (T * C) mod prime */
|
|
|
|
mp_set(&M, i);
|
|
|
|
/* M = i */
|
|
|
|
}
|
2015-04-17 20:46:11 +00:00
|
|
|
|
2019-10-29 17:41:25 +00:00
|
|
|
LBL_END:
|
2017-08-30 17:13:53 +00:00
|
|
|
mp_clear_multi(&t1, &C, &Q, &S, &Z, &M, &T, &R, &two, NULL);
|
2019-05-19 15:16:13 +00:00
|
|
|
return err;
|
2015-04-17 20:46:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|