cangrepp

Some cryptographic attacks
Log | Files | Refs | README

commit 59fb30b24cf6820587882a348ee993fc4d244180
parent a57758a0daaeadc9b665bb79fde015d3ff4cb814
Author: olikru <olikru@tkruger.se>
Date:   Thu,  7 Mar 2024 13:47:03 +0100

pierre reads stdin

Diffstat:
MMakefile | 3++-
Aexamples/pierry.txt | 1+
Afmpzio.c | 64++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Afmpzio.h | 9+++++++++
Mtools/hnpsolve.c | 4++++
Mtools/pierre.c | 34++++++++++++++++++----------------
6 files changed, 98 insertions(+), 17 deletions(-)

diff --git a/Makefile b/Makefile @@ -20,7 +20,8 @@ OBJS=\ smallfactor.o\ hnp.o\ cyclefind.o\ -pierre.o +pierre.o\ +fmpzio.o TOOLS=\ hnpsolve\ lcgfloyd\ diff --git a/examples/pierry.txt b/examples/pierry.txt @@ -0,0 +1 @@ +dbf7b5cea3ec01cac9ffa3f11280fd1c46d22cb2ee34caef48c953cadbd6f87ceb7dcee5f11a59d377fc9302d0f701ba62d66ca124873f816b1acb1e73d34838575a8df310680cc4d6702c3080286d40878b7c21f14688f33e50c064aa722cdd61bf1c680d43b72ba15d8e5c7c38f612506a46104c13af8338467fc77aef4bce954ddb8e6317578278b99f6bb9e25fa523d74bb5213281edd4b99a29ba203c058771fccee343681762f70e4cb9bf475ceeec3d6e6c03b992fb31d608d0bc23833087c9f808282eacbfdf3f77ba3c9d567d94ba78ba9f93237feaf57737c01501f910e6812264465f17de4a397e50c1331ea4dafdf19a2013c5398463c3520035 diff --git a/fmpzio.c b/fmpzio.c @@ -0,0 +1,64 @@ +#include <fmpz.h> +#include <stdio.h> + +#include "fmpzio.h" + +#define LINE_SIZE_MAX 8192 + +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 == ' ') + next_char = getchar(); + + if (next_char == EOF) + return -1; + + buffer[0] = next_char; + i = 1; + while (i < LINE_SIZE_MAX - 1) { + next_char = getchar(); + if (next_char == EOF || next_char == '\n' || next_char == ' ') + break; + + buffer[i] = (char)next_char; + + i++; + } + buffer[i] = '\0'; + + return fmpz_set_str(res, buffer, 10); +} + +int +read_next_hex_fmpz(fmpz_t res) +{ + char buffer[LINE_SIZE_MAX]; + size_t i; + + int next_char = getchar(); + while (next_char == '\n' || next_char == ' ') + next_char = getchar(); + + if (next_char == EOF) + return -1; + + buffer[0] = next_char; + i = 1; + while (i < LINE_SIZE_MAX - 1) { + next_char = getchar(); + if (next_char == EOF || next_char == '\n' || next_char == ' ') + break; + + buffer[i] = (char)next_char; + + i++; + } + buffer[i] = '\0'; + + return fmpz_set_str(res, buffer, 16); +} diff --git a/fmpzio.h b/fmpzio.h @@ -0,0 +1,9 @@ +#ifndef _FMPZIO_H_ +#define _FMPZIO_H_ + +#include <fmpz.h> + +int read_next_fmpz(fmpz_t res); +int read_next_hex_fmpz(fmpz_t res); + +#endif diff --git a/tools/hnpsolve.c b/tools/hnpsolve.c @@ -4,6 +4,9 @@ #include <stdio.h> #include <stdlib.h> +#include <fmpzio.h> + +/* #define LINE_SIZE_MAX 4096 static int @@ -34,6 +37,7 @@ 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) diff --git a/tools/pierre.c b/tools/pierre.c @@ -1,28 +1,30 @@ -#include <stdlib.h> +#include <fmpz.h> #include <stdint.h> #include <stdio.h> -#include <fmpz.h> +#include <stdlib.h> + +#include "fmpzio.h" #include "pierre.h" #define DEFAULT_LIMIT 100000000 -int main(int argc, char* argv[]) -{ - if(argc != 2) { - printf("Usage: %s <modulus>\n", argv[0]); - return(-1); - } - fmpz_t n, r; - fmpz_init(n); - fmpz_set_str(n, argv[1], 10); - fmpz_init(r); +int +main() +{ + fmpz_t read, factor; + fmpz_init(factor); + fmpz_init(read); - fermat_factor(r, n, DEFAULT_LIMIT); + while(read_next_hex_fmpz(read) == 0) { + fermat_factor(factor, read, DEFAULT_LIMIT); + fmpz_print(factor); + printf("\n"); + } - fmpz_print(r); - printf("\n"); + fmpz_clear(factor); + fmpz_clear(read); - return(0); + return 0; }