g: dcrypto: add debug function to print primes

When compilation is enabled, this function prints all prime numbers
generated using the PRIME_DELTAS array.

BRANCH=cr50
BUG=none
TEST=verified that prime numbers are printed out when running rsa_test.py

Change-Id: I37961aad146c4aeecca9a84550f313450e6c5853
Signed-off-by: Vadim Bendebury <vbendeb@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/683074
Reviewed-by: Shawn N <shawnn@chromium.org>
This commit is contained in:
Vadim Bendebury
2017-09-25 13:50:08 -07:00
committed by chrome-bot
parent 0309b55817
commit aeea9974b2

View File

@@ -3,6 +3,10 @@
* found in the LICENSE file.
*/
#ifdef PRINT_PRIMES
#include "console.h"
#endif
#include "dcrypto.h"
#include "internal.h"
@@ -1210,6 +1214,27 @@ static int bn_probable_prime(const struct LITE_BIGNUM *p)
return 1;
}
/* #define PRINT_PRIMES to enable printing predefined prime numbers' set. */
static void print_primes(uint16_t prime)
{
#ifdef PRINT_PRIMES
static uint16_t num_per_line;
static uint16_t max_printed;
if (prime <= max_printed)
return;
if (!(num_per_line++ % 8)) {
if (num_per_line == 1)
ccprintf("Prime numbers:");
ccprintf("\n");
cflush();
}
max_printed = prime;
ccprintf(" %6d", prime);
#endif
}
int DCRYPTO_bn_generate_prime(struct LITE_BIGNUM *p)
{
int i;
@@ -1232,6 +1257,7 @@ int DCRYPTO_bn_generate_prime(struct LITE_BIGNUM *p)
uint16_t rem;
prime += (PRIME_DELTAS[i] << 1);
print_primes(prime);
rem = bn_mod_word16(p, prime);
/* Skip marking odd offsets (i.e. even candidates). */
for (j = (rem == 0) ? 0 : prime - rem;