divtest.c 873 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #include <openssl/bn.h>
  2. #include <openssl/rand.h>
  3. static int Rand(n)
  4. {
  5. unsigned char x[2];
  6. RAND_pseudo_bytes(x,2);
  7. return (x[0] + 2*x[1]);
  8. }
  9. static void bug(char *m, BIGNUM *a, BIGNUM *b)
  10. {
  11. printf("%s!\na=",m);
  12. BN_print_fp(stdout, a);
  13. printf("\nb=");
  14. BN_print_fp(stdout, b);
  15. printf("\n");
  16. fflush(stdout);
  17. }
  18. main()
  19. {
  20. BIGNUM *a=BN_new(), *b=BN_new(), *c=BN_new(), *d=BN_new(),
  21. *C=BN_new(), *D=BN_new();
  22. BN_RECP_CTX *recp=BN_RECP_CTX_new();
  23. BN_CTX *ctx=BN_CTX_new();
  24. for(;;) {
  25. BN_pseudo_rand(a,Rand(),0,0);
  26. BN_pseudo_rand(b,Rand(),0,0);
  27. if (BN_is_zero(b)) continue;
  28. BN_RECP_CTX_set(recp,b,ctx);
  29. if (BN_div(C,D,a,b,ctx) != 1)
  30. bug("BN_div failed",a,b);
  31. if (BN_div_recp(c,d,a,recp,ctx) != 1)
  32. bug("BN_div_recp failed",a,b);
  33. else if (BN_cmp(c,C) != 0 || BN_cmp(c,C) != 0)
  34. bug("mismatch",a,b);
  35. }
  36. }