From 7ebc44a8d838298394178687975cf9d2994ff8be Mon Sep 17 00:00:00 2001 From: Jann Horn Date: Sat, 26 Apr 2014 23:18:43 +0200 Subject: [PATCH] add a few more tools. most useful: round.c --- math/disclogbrute.c | 21 +++++++++++++++++++++ math/ordnung.c | 18 ++++++++++++++++++ math/round.c | 16 ++++++++++++++++ tools/writeonlynet.c | 27 +++++++++++++++++++++++++++ 4 files changed, 82 insertions(+) create mode 100644 math/disclogbrute.c create mode 100644 math/ordnung.c create mode 100644 math/round.c create mode 100644 tools/writeonlynet.c diff --git a/math/disclogbrute.c b/math/disclogbrute.c new file mode 100644 index 0000000..b297725 --- /dev/null +++ b/math/disclogbrute.c @@ -0,0 +1,21 @@ +#include +#include + +int main(int argc, char **argv) { + if (argc != 4) puts("bad invocation"), exit(1); + int p = atoi(argv[1]); + int a = atoi(argv[2]); + int b = atoi(argv[3]); + + int candidate_b = 1; + for (int candidate_log = 0; candidate_log < p; candidate_log++) { + if (candidate_b == b) { + printf("solution: discrete log is %d\n", candidate_log); + exit(0); + } + candidate_b = (candidate_b * a) % p; + } + + printf("no hits???\n"); + return 1; +} diff --git a/math/ordnung.c b/math/ordnung.c new file mode 100644 index 0000000..c49e893 --- /dev/null +++ b/math/ordnung.c @@ -0,0 +1,18 @@ +#include +#include + +int main(int argc, char **argv) { + if (argc != 3) puts("invocation:

"), exit(1); + int p = atoi(argv[1]); + int a = atoi(argv[2]); + + int n = a; + int order = 1; + while (n != 1) { + n = (((long long)n) * a) % p; + order++; + } + + printf("Die Ordnung ist %d.%s\n", order, (order==p-1)?" Primitives Element":""); + return 0; +} diff --git a/math/round.c b/math/round.c new file mode 100644 index 0000000..e422fd0 --- /dev/null +++ b/math/round.c @@ -0,0 +1,16 @@ +#include +#include + +int main(int argc, char **argv) { + if (argc != 2) xperror("invocation: round ", 0); + double target = strtod(argv[1], NULL); + char line[128]; + while (fgets(line, sizeof(line), stdin)) { + double n = strtod(line, NULL); + double quot = n / target; + quot = round(quot); + n = quot * target; + printf("%f\n", n); + } + return 0; +} diff --git a/tools/writeonlynet.c b/tools/writeonlynet.c new file mode 100644 index 0000000..3bef9f9 --- /dev/null +++ b/tools/writeonlynet.c @@ -0,0 +1,27 @@ +#include +#include + +int main(int argc, char **argv) { + if (argc != 3) xperror("invocation: ", 0); + int s = netopen(argv[1], argv[2], JH_TCP_HINTS); + if (s < 0) { + fprintf(stderr, "unable to connect: %s\n", gai_strerror(s)); + exit(1); + } + while (1) { + char buf[4096]; + fprintf(stderr, "reading...\n"); + ssize_t count = fail_on_neg(read(0, buf, sizeof(buf)), "read failed", 1); + if (count == 0) return 0; + fprintf(stderr, "read %zd chars, writing...\n", count); + ssize_t wcount = fail_on_neg(write(s, buf, count), "write failed", 1); + if (wcount == 0) { + fprintf(stderr, "zero write! ending.\n"); + return 1; + } + if (wcount != count) { + fprintf(stderr, "wrong write size! ending.\n"); + return 1; + } + } +} -- 2.20.1