14 #define WrapBN(klass, obj, bn) do { \
16 ossl_raise(rb_eRuntimeError, "BN wasn't initialized!"); \
18 (obj) = Data_Wrap_Struct((klass), 0, BN_clear_free, (bn)); \
21 #define GetBN(obj, bn) do { \
22 Data_Get_Struct((obj), BIGNUM, (bn)); \
24 ossl_raise(rb_eRuntimeError, "BN wasn't initialized!"); \
28 #define SafeGetBN(obj, bn) do { \
29 OSSL_Check_Kind((obj), cBN); \
48 newbn = bn ? BN_dup(bn) : BN_new();
64 }
else switch (
TYPE(obj)) {
97 if (!(bn = BN_new())) {
128 if (!BN_copy(bn, other)) {
190 len = BN_bn2mpi(bn,
NULL);
192 if (BN_bn2mpi(bn, (
unsigned char *)
RSTRING_PTR(str)) != len)
196 len = BN_num_bytes(bn);
198 if (BN_bn2bin(bn, (
unsigned char *)
RSTRING_PTR(str)) != len)
229 if (!(txt = BN_bn2dec(bn))) {
247 switch(
TYPE(other)) {
263 #define BIGNUM_BOOL1(func) \
270 ossl_bn_##func(VALUE self) \
274 if (BN_##func(bn)) { \
283 #define BIGNUM_1c(func) \
290 ossl_bn_##func(VALUE self) \
292 BIGNUM *bn, *result; \
295 if (!(result = BN_new())) { \
296 ossl_raise(eBNError, NULL); \
298 if (!BN_##func(result, bn, ossl_bn_ctx)) { \
300 ossl_raise(eBNError, NULL); \
302 WrapBN(CLASS_OF(self), obj, result); \
307 #define BIGNUM_2(func) \
314 ossl_bn_##func(VALUE self, VALUE other) \
316 BIGNUM *bn1, *bn2 = GetBNPtr(other), *result; \
319 if (!(result = BN_new())) { \
320 ossl_raise(eBNError, NULL); \
322 if (!BN_##func(result, bn1, bn2)) { \
324 ossl_raise(eBNError, NULL); \
326 WrapBN(CLASS_OF(self), obj, result); \
332 #define BIGNUM_2c(func) \
339 ossl_bn_##func(VALUE self, VALUE other) \
341 BIGNUM *bn1, *bn2 = GetBNPtr(other), *result; \
344 if (!(result = BN_new())) { \
345 ossl_raise(eBNError, NULL); \
347 if (!BN_##func(result, bn1, bn2, ossl_bn_ctx)) { \
349 ossl_raise(eBNError, NULL); \
351 WrapBN(CLASS_OF(self), obj, result); \
359 BIGNUM_2c(mod_inverse)
368 BIGNUM *bn1, *bn2 =
GetBNPtr(other), *r1, *r2;
373 if (!(r1 = BN_new())) {
376 if (!(r2 = BN_new())) {
391 #define BIGNUM_3c(func) \
398 ossl_bn_##func(VALUE self, VALUE other1, VALUE other2) \
400 BIGNUM *bn1, *bn2 = GetBNPtr(other1); \
401 BIGNUM *bn3 = GetBNPtr(other2), *result; \
404 if (!(result = BN_new())) { \
405 ossl_raise(eBNError, NULL); \
407 if (!BN_##func(result, bn1, bn2, bn3, ossl_bn_ctx)) { \
409 ossl_raise(eBNError, NULL); \
411 WrapBN(CLASS_OF(self), obj, result); \
419 #define BIGNUM_BIT(func) \
426 ossl_bn_##func(VALUE self, VALUE bit) \
430 if (!BN_##func(bn, NUM2INT(bit))) { \
431 ossl_raise(eBNError, NULL); \
436 BIGNUM_BIT(clear_bit)
437 BIGNUM_BIT(mask_bits)
451 if (BN_is_bit_set(bn, b)) {
457 #define BIGNUM_SHIFT(func) \
464 ossl_bn_##func(VALUE self, VALUE bits) \
466 BIGNUM *bn, *result; \
471 if (!(result = BN_new())) { \
472 ossl_raise(eBNError, NULL); \
474 if (!BN_##func(result, bn, b)) { \
476 ossl_raise(eBNError, NULL); \
478 WrapBN(CLASS_OF(self), obj, result); \
484 #define BIGNUM_SELF_SHIFT(func) \
491 ossl_bn_self_##func(VALUE self, VALUE bits) \
497 if (!BN_##func(bn, bn, b)) \
498 ossl_raise(eBNError, NULL); \
504 #define BIGNUM_RAND(func) \
511 ossl_bn_s_##func(int argc, VALUE *argv, VALUE klass) \
514 int bottom = 0, top = 0, b; \
515 VALUE bits, fill, odd, obj; \
517 switch (rb_scan_args(argc, argv, "12", &bits, &fill, &odd)) { \
519 bottom = (odd == Qtrue) ? 1 : 0; \
522 top = NUM2INT(fill); \
525 if (!(result = BN_new())) { \
526 ossl_raise(eBNError, NULL); \
528 if (!BN_##func(result, b, top, bottom)) { \
530 ossl_raise(eBNError, NULL); \
532 WrapBN(klass, obj, result); \
538 #define BIGNUM_RAND_RANGE(func) \
545 ossl_bn_s_##func##_range(VALUE klass, VALUE range) \
547 BIGNUM *bn = GetBNPtr(range), *result; \
549 if (!(result = BN_new())) { \
550 ossl_raise(eBNError, NULL); \
552 if (!BN_##func##_range(result, bn)) { \
554 ossl_raise(eBNError, NULL); \
556 WrapBN(klass, obj, result); \
577 VALUE vnum, vsafe, vadd, vrem, obj;
579 rb_scan_args(argc, argv,
"13", &vnum, &vsafe, &vadd, &vrem);
590 if (!(result = BN_new())) {
593 if (!BN_generate_prime(result, num, safe, add, rem,
NULL,
NULL)) {
597 WrapBN(klass, obj, result);
602 #define BIGNUM_NUM(func) \
609 ossl_bn_##func(VALUE self) \
613 return INT2FIX(BN_##func(bn)); \
625 if (
self == other)
return self;
630 if (!BN_copy(bn1, bn2)) {
636 #define BIGNUM_CMP(func) \
643 ossl_bn_##func(VALUE self, VALUE other) \
645 BIGNUM *bn1, *bn2 = GetBNPtr(other); \
647 return INT2FIX(BN_##func(bn1, bn2)); \
655 if (ossl_bn_cmp(
self, other) ==
INT2FIX(0)) {
674 int checks = BN_prime_checks;
706 VALUE vchecks, vtrivdiv;
707 int checks = BN_prime_checks, do_trial_division = 1;
711 if (!
NIL_P(vchecks)) {
717 do_trial_division = 0;