Home | History | Annotate | Line # | Download | only in div
      1  1.1  christos #include <openssl/bn.h>
      2  1.1  christos #include <openssl/rand.h>
      3  1.1  christos 
      4  1.1  christos static int Rand(void)
      5  1.1  christos {
      6  1.1  christos     unsigned char x[2];
      7  1.1  christos     RAND_bytes(x, 2);
      8  1.1  christos     return (x[0] + 2 * x[1]);
      9  1.1  christos }
     10  1.1  christos 
     11  1.1  christos static void bug(const char *m, BIGNUM *a, BIGNUM *b)
     12  1.1  christos {
     13  1.1  christos     printf("%s!\na=", m);
     14  1.1  christos     BN_print_fp(stdout, a);
     15  1.1  christos     printf("\nb=");
     16  1.1  christos     BN_print_fp(stdout, b);
     17  1.1  christos     printf("\n");
     18  1.1  christos     fflush(stdout);
     19  1.1  christos     exit(1);
     20  1.1  christos }
     21  1.1  christos 
     22  1.1  christos int
     23  1.1  christos main(int argc, char *argv[])
     24  1.1  christos {
     25  1.1  christos     BIGNUM *a = BN_new(), *b = BN_new(), *c = BN_new(), *d = BN_new(),
     26  1.1  christos         *C = BN_new(), *D = BN_new();
     27  1.1  christos     BN_RECP_CTX *recp = BN_RECP_CTX_new();
     28  1.1  christos     BN_CTX *ctx = BN_CTX_new();
     29  1.1  christos     int i = 0;
     30  1.1  christos 
     31  1.1  christos     for (i = 0; i < 10000; i++) {
     32  1.2    rillig         BN_rand(a, Rand(), 0, 0);
     33  1.2    rillig         BN_rand(b, Rand(), 0, 0);
     34  1.1  christos         if (BN_is_zero(b))
     35  1.1  christos             continue;
     36  1.1  christos 
     37  1.1  christos         BN_RECP_CTX_set(recp, b, ctx);
     38  1.1  christos         if (BN_div(C, D, a, b, ctx) != 1)
     39  1.1  christos             bug("BN_div failed", a, b);
     40  1.1  christos         if (BN_div_recp(c, d, a, recp, ctx) != 1)
     41  1.1  christos             bug("BN_div_recp failed", a, b);
     42  1.1  christos         else if (BN_cmp(c, C) != 0 || BN_cmp(c, C) != 0)
     43  1.1  christos             bug("mismatch", a, b);
     44  1.1  christos     }
     45  1.1  christos     exit(0);
     46  1.1  christos }
     47