cangrepp

Some cryptographic attacks
Log | Files | Refs | README

lcgfloyd.c (956B)


      1 #include <errno.h>
      2 #include <stdio.h>
      3 #include <stdlib.h>
      4 
      5 #include <cyclefind.h>
      6 
      7 uint64_t a;
      8 uint64_t b;
      9 uint64_t M;
     10 
     11 static uint64_t
     12 f(uint64_t x)
     13 {
     14   return (a * x + b) % M;
     15 }
     16 
     17 static void
     18 print_usage(char *argv[])
     19 {
     20   printf("Usage: %s <a> <b> <x0> <M>\n"
     21          "   a  : multiplier constant\n"
     22          "   b  : additive constant\n"
     23          "   x0 : starting value\n"
     24          "   M  : modulus\n",
     25          argv[0]);
     26 }
     27 
     28 int
     29 main(int argc, char *argv[])
     30 {
     31   if (argc != 5) {
     32     print_usage(argv);
     33     exit(EXIT_FAILURE);
     34   }
     35 
     36   errno = 0;
     37   a = strtoull(argv[1], NULL, 10);
     38   b = strtoull(argv[2], NULL, 10);
     39   uint64_t x0 = strtoull(argv[3], NULL, 10);
     40   M = strtoull(argv[4], NULL, 10);
     41 
     42   if (errno != 0) {
     43     fprintf(stderr,
     44             "Error! Could not interpret command line arguments\n");
     45     exit(EXIT_FAILURE);
     46   }
     47 
     48   uint64_t mu;
     49   uint64_t lambda = cyclefind_floyd(&mu, x0, &f);
     50 
     51   printf("mu = %llu, lambda = %llu\n", mu, lambda);
     52 }