cangrepp

Some cryptographic attacks
Log | Files | Refs | README

commit 0d004f8dc08eaf883f8a5d98b531b5dbd2662924
parent b09f9d502737ffd1bb850a4a737319c18cf7a62d
Author: olikru <olikru@tkruger.se>
Date:   Thu, 11 Jan 2024 14:07:20 +0100

clang format

Diffstat:
Mprecomputers/primorial16bit.c | 6+++---
Msmallfactor.c | 65++++++++++++++++++++++++++++++++---------------------------------
Msmallfactor.h | 2+-
Mtest_angrepp.c | 8+++-----
Mtools/hnpsolve.c | 54++++++++++++++++++++++++++----------------------------
5 files changed, 65 insertions(+), 70 deletions(-)

diff --git a/precomputers/primorial16bit.c b/precomputers/primorial16bit.c @@ -3,10 +3,10 @@ * 2^16). The result is printed to stdout. This is used to create the * assets/primorial16b.bin file. */ -#include <stdlib.h> -#include <stdio.h> #include <flint.h> #include <fmpz.h> +#include <stdio.h> +#include <stdlib.h> int main() { fmpz_t primorial; @@ -14,7 +14,7 @@ int main() { fmpz_primorial(primorial, 1 << 16); - if(fmpz_out_raw(stdout, primorial) == 0) { + if (fmpz_out_raw(stdout, primorial) == 0) { fprintf(stderr, "Error! Writing to stdout failed.\n"); exit(1); } diff --git a/smallfactor.c b/smallfactor.c @@ -2,59 +2,58 @@ #define N_EIGHT_BIT_PRIMES 54 const uint64_t EIGHT_BIT_PRIMES[N_EIGHT_BIT_PRIMES] = { - 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, - 31, 37, 41, 43, 47, 53, 59, 61, 67, - 71, 73, 79, 83, 89, 97, 101, 103, - 107, 109, 113, 127, 131, 137, 139, - 149, 151, 157, 163, 167, 173, 179, - 181, 191, 193, 197, 199, 211, 223, - 227, 229, 233, 239, 241, 251}; + 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, + 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, + 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, + 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251}; size_t smallfactor_euclidean(fmpz_t out, fmpz_t n) { size_t i; fmpz_t r; - FILE* primorial_file = fopen(SMALLFACTOR_PRIMORIAL_FILENAME, "rb"); + FILE *primorial_file = fopen(SMALLFACTOR_PRIMORIAL_FILENAME, "rb"); - if(primorial_file == NULL) { - fprintf(stderr, "Error! smallfactor_euclidean: " - "could not open asset file %s\n", SMALLFACTOR_PRIMORIAL_FILENAME); + if (primorial_file == NULL) { + fprintf(stderr, + "Error! smallfactor_euclidean: " + "could not open asset file %s\n", + SMALLFACTOR_PRIMORIAL_FILENAME); exit(EXIT_FAILURE); } fmpz_t primorial; fmpz_init(primorial); - if(fmpz_inp_raw(primorial, primorial_file) == 0) { + if (fmpz_inp_raw(primorial, primorial_file) == 0) { fprintf(stderr, "Error! smallfactor_euclidean: " - "could not read primorial from asset file\n"); + "could not read primorial from asset file\n"); exit(EXIT_FAILURE); } fmpz_gcd(out, n, primorial); - if(fmpz_cmp_ui(out, 1) > 0 && fmpz_cmp(out, n) < 0) { + if (fmpz_cmp_ui(out, 1) > 0 && fmpz_cmp(out, n) < 0) { // 1 < g < n return SMALLFACTOR_FOUND; - } else if(fmpz_cmp(out, n) == 0) { - // g = n, so n divides p - if(fmpz_cmp_ui(n, 1) == 0) { - // 1 only has trivial factors - return SMALLFACTOR_NOT_FOUND; - } else { - // n is either prime or has an 8-bit prime factor, trial divide - fmpz_init(r); - for(i = 0; i < N_EIGHT_BIT_PRIMES; i++) { - fmpz_set_ui(r, EIGHT_BIT_PRIMES[i]); - fmpz_tdiv_qr(out, r, n, r); - - if (fmpz_cmp_ui(r, 0) == 0) - break; - } - fmpz_clear(r); - - if(i < N_EIGHT_BIT_PRIMES) - return SMALLFACTOR_FOUND; + } else if (fmpz_cmp(out, n) == 0) { + // g = n, so n divides p + if (fmpz_cmp_ui(n, 1) == 0) { + // 1 only has trivial factors + return SMALLFACTOR_NOT_FOUND; + } else { + // n is either prime or has an 8-bit prime factor, trial divide + fmpz_init(r); + for (i = 0; i < N_EIGHT_BIT_PRIMES; i++) { + fmpz_set_ui(r, EIGHT_BIT_PRIMES[i]); + fmpz_tdiv_qr(out, r, n, r); + + if (fmpz_cmp_ui(r, 0) == 0) + break; } + fmpz_clear(r); + + if (i < N_EIGHT_BIT_PRIMES) + return SMALLFACTOR_FOUND; + } } return SMALLFACTOR_NOT_FOUND; diff --git a/smallfactor.h b/smallfactor.h @@ -1,9 +1,9 @@ #ifndef SMALLFACTOR_H #define SMALLFACTOR_H -#include <stdlib.h> #include <flint.h> #include <fmpz.h> +#include <stdlib.h> #define SMALLFACTOR_PRIMORIAL_FILENAME ("assets/primorial16b.bin") #define SMALLFACTOR_FOUND 1 diff --git a/test_angrepp.c b/test_angrepp.c @@ -1,7 +1,5 @@ -#include <stdlib.h> -#include <stdio.h> #include <assert.h> +#include <stdio.h> +#include <stdlib.h> -int main() { - printf("test unimplemented\n"); -} +int main() { printf("test unimplemented\n"); } diff --git a/tools/hnpsolve.c b/tools/hnpsolve.c @@ -1,31 +1,30 @@ -#include <stdlib.h> -#include <stdio.h> #include <flint.h> #include <fmpz_vec.h> #include <hnp.h> +#include <stdio.h> +#include <stdlib.h> #define LINE_SIZE_MAX 4096 -static int read_next_fmpz(fmpz_t res) -{ +static int read_next_fmpz(fmpz_t res) { char buffer[LINE_SIZE_MAX]; size_t i; int next_char = getchar(); - while(next_char == '\n' || next_char == ' ') + while (next_char == '\n' || next_char == ' ') next_char = getchar(); - if(next_char == EOF) + if (next_char == EOF) return -1; buffer[0] = next_char; i = 1; - while(i < LINE_SIZE_MAX-1) { + while (i < LINE_SIZE_MAX - 1) { next_char = getchar(); - if(next_char == EOF || next_char == '\n' || next_char == ' ') + if (next_char == EOF || next_char == '\n' || next_char == ' ') break; - buffer[i] = (char) next_char; + buffer[i] = (char)next_char; i++; } @@ -34,35 +33,34 @@ static int read_next_fmpz(fmpz_t res) return fmpz_set_str(res, buffer, 10); } -static int read_stdin_instance(fmpz** a, fmpz** t, slong* m, fmpz_t* B, fmpz_t* n) -{ +static int read_stdin_instance(fmpz **a, fmpz **t, slong *m, fmpz_t *B, + fmpz_t *n) { flint_scanf("%wd", m); - if(read_next_fmpz(*B)) + if (read_next_fmpz(*B)) return -1; - if(read_next_fmpz(*n)) + if (read_next_fmpz(*n)) return -1; *a = _fmpz_vec_init(*m); *t = _fmpz_vec_init(*m); - slong i; int fail = 0; - for(i = 0; i < *m; i++) { - if(read_next_fmpz(&(*t)[i])) { + for (i = 0; i < *m; i++) { + if (read_next_fmpz(&(*t)[i])) { fail = 1; break; } - if(read_next_fmpz(&(*a)[i])) { + if (read_next_fmpz(&(*a)[i])) { fail = 1; break; } } - if(fail) { + if (fail) { _fmpz_vec_clear(*a, *m); _fmpz_vec_clear(*t, *m); return -1; @@ -71,27 +69,27 @@ static int read_stdin_instance(fmpz** a, fmpz** t, slong* m, fmpz_t* B, fmpz_t* return 0; } -int main(int argc, char* argv[]) -{ +int main(int argc, char *argv[]) { slong m; - fmpz* a; - fmpz* t; + fmpz *a; + fmpz *t; fmpz_t B, n; fmpz_init(B); fmpz_init(n); - if(read_stdin_instance(&a, &t, &m, &B, &n) != 0) { + if (read_stdin_instance(&a, &t, &m, &B, &n) != 0) { fprintf(stderr, "ERROR! Bad input format?\n"); exit(1); } - fmpz res[m+1]; + fmpz res[m + 1]; slong i; - for(i = 0; i < m+1; i++) fmpz_init(&res[i]); + for (i = 0; i < m + 1; i++) + fmpz_init(&res[i]); - int ret = hidden_number_problem(res, m+1, t, a, m, n, B); - if(ret == 0) { - for(i = 0; i < m; i++) { + int ret = hidden_number_problem(res, m + 1, t, a, m, n, B); + if (ret == 0) { + for (i = 0; i < m; i++) { fmpz_print(&res[i]); printf(" "); }