Merge pull request #578 from PX4/fix_mp_prime_strong_lucas_lefridge_compilation
[libtommath.git] / mp_prime_rand.c
blob5351aefe43ffe1b6e7d28f4b929e9e381f521bba
1 #include "tommath_private.h"
2 #ifdef MP_PRIME_RAND_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
4 /* SPDX-License-Identifier: Unlicense */
6 /* makes a truly random prime of a given size (bits),
8 * Flags are as follows:
10 * MP_PRIME_BBS - make prime congruent to 3 mod 4
11 * MP_PRIME_SAFE - make sure (p-1)/2 is prime as well (implies MP_PRIME_BBS)
12 * MP_PRIME_2MSB_ON - make the 2nd highest bit one
14 * You have to supply a callback which fills in a buffer with random bytes. "dat" is a parameter you can
15 * have passed to the callback (e.g. a state or something). This function doesn't use "dat" itself
16 * so it can be NULL
20 /* This is possibly the mother of all prime generation functions, muahahahahaha! */
21 mp_err mp_prime_rand(mp_int *a, int t, int size, int flags)
23 uint8_t *tmp, maskAND, maskOR_msb, maskOR_lsb;
24 int bsize, maskOR_msb_offset;
25 bool res;
26 mp_err err;
28 /* sanity check the input */
29 if (size <= 1) {
30 return MP_VAL;
33 /* MP_PRIME_SAFE implies MP_PRIME_BBS */
34 if ((flags & MP_PRIME_SAFE) != 0) {
35 flags |= MP_PRIME_BBS;
38 /* calc the byte size */
39 bsize = (size>>3) + ((size&7)?1:0);
41 /* we need a buffer of bsize bytes */
42 tmp = (uint8_t *) MP_MALLOC((size_t)bsize);
43 if (tmp == NULL) {
44 return MP_MEM;
47 /* calc the maskAND value for the MSbyte*/
48 maskAND = ((size&7) == 0) ? 0xFFu : (uint8_t)(0xFFu >> (8 - (size & 7)));
50 /* calc the maskOR_msb */
51 maskOR_msb = 0;
52 maskOR_msb_offset = ((size & 7) == 1) ? 1 : 0;
53 if ((flags & MP_PRIME_2MSB_ON) != 0) {
54 maskOR_msb |= (uint8_t)(0x80 >> ((9 - size) & 7));
57 /* get the maskOR_lsb */
58 maskOR_lsb = 1u;
59 if ((flags & MP_PRIME_BBS) != 0) {
60 maskOR_lsb |= 3u;
63 do {
64 /* read the bytes */
65 if ((err = s_mp_rand_source(tmp, (size_t)bsize)) != MP_OKAY) {
66 goto LBL_ERR;
69 /* work over the MSbyte */
70 tmp[0] &= maskAND;
71 tmp[0] |= (uint8_t)(1 << ((size - 1) & 7));
73 /* mix in the maskORs */
74 tmp[maskOR_msb_offset] |= maskOR_msb;
75 tmp[bsize-1] |= maskOR_lsb;
77 /* read it in */
78 /* TODO: casting only for now until all lengths have been changed to the type "size_t"*/
79 if ((err = mp_from_ubin(a, tmp, (size_t)bsize)) != MP_OKAY) {
80 goto LBL_ERR;
83 /* is it prime? */
84 if ((err = mp_prime_is_prime(a, t, &res)) != MP_OKAY) {
85 goto LBL_ERR;
87 if (!res) {
88 continue;
91 if ((flags & MP_PRIME_SAFE) != 0) {
92 /* see if (a-1)/2 is prime */
93 if ((err = mp_sub_d(a, 1uL, a)) != MP_OKAY) {
94 goto LBL_ERR;
96 if ((err = mp_div_2(a, a)) != MP_OKAY) {
97 goto LBL_ERR;
100 /* is it prime? */
101 if ((err = mp_prime_is_prime(a, t, &res)) != MP_OKAY) {
102 goto LBL_ERR;
105 } while (!res);
107 if ((flags & MP_PRIME_SAFE) != 0) {
108 /* restore a to the original value */
109 if ((err = mp_mul_2(a, a)) != MP_OKAY) {
110 goto LBL_ERR;
112 if ((err = mp_add_d(a, 1uL, a)) != MP_OKAY) {
113 goto LBL_ERR;
117 err = MP_OKAY;
118 LBL_ERR:
119 MP_FREE_BUF(tmp, (size_t)bsize);
120 return err;
123 #endif