14 TypedData_Wrap_Struct((klass), &ossl_bn_type, 0)
15#define SetBN(obj, bn) do { \
17 ossl_raise(rb_eRuntimeError, "BN wasn't initialized!"); \
19 RTYPEDDATA_DATA(obj) = (bn); \
22#define GetBN(obj, bn) do { \
23 TypedData_Get_Struct((obj), BIGNUM, &ossl_bn_type, (bn)); \
25 ossl_raise(rb_eRuntimeError, "BN wasn't initialized!"); \
30ossl_bn_free(
void *
ptr)
64 newbn = bn ? BN_dup(bn) : BN_new();
74integer_to_bnptr(
VALUE obj, BIGNUM *orig)
80 unsigned char bin[
sizeof(
long)];
82 unsigned long un =
labs(
n);
84 for (
i =
sizeof(
long) - 1; 0 <=
i;
i--) {
89 bn = BN_bin2bn(
bin,
sizeof(
bin), orig);
93 BN_set_negative(bn, 1);
107 bn = BN_bin2bn(
bin, (
int)
len, orig);
112 BN_set_negative(bn, 1);
128 bn = integer_to_bnptr(
obj,
NULL);
141 tmp = try_convert_to_bn(*
ptr);
166 if (!(bn = BN_new())) {
198 integer_to_bnptr(
str, bn);
208 if (!BN_copy(bn, other)) {
277 len = BN_num_bytes(bn);
302ossl_bn_to_i(
VALUE self)
310 if (!(txt = BN_bn2hex(bn))) {
320ossl_bn_to_bn(
VALUE self)
328 switch(
TYPE(other)) {
330 self = ossl_bn_to_s(0,
NULL,
self);
334 self = ossl_bn_to_i(
self);
344#define BIGNUM_BOOL1(func) \
346 ossl_bn_##func(VALUE self) \
350 if (BN_##func(bn)) { \
382ossl_bn_is_negative(
VALUE self)
392#define BIGNUM_1c(func) \
394 ossl_bn_##func(VALUE self) \
396 BIGNUM *bn, *result; \
399 obj = NewBN(rb_obj_class(self)); \
400 if (!(result = BN_new())) { \
401 ossl_raise(eBNError, NULL); \
403 if (BN_##func(result, bn, ossl_bn_ctx) <= 0) { \
405 ossl_raise(eBNError, NULL); \
407 SetBN(obj, result); \
418#define BIGNUM_2(func) \
420 ossl_bn_##func(VALUE self, VALUE other) \
422 BIGNUM *bn1, *bn2 = GetBNPtr(other), *result; \
425 obj = NewBN(rb_obj_class(self)); \
426 if (!(result = BN_new())) { \
427 ossl_raise(eBNError, NULL); \
429 if (BN_##func(result, bn1, bn2) <= 0) { \
431 ossl_raise(eBNError, NULL); \
433 SetBN(obj, result); \
451#define BIGNUM_2c(func) \
453 ossl_bn_##func(VALUE self, VALUE other) \
455 BIGNUM *bn1, *bn2 = GetBNPtr(other), *result; \
458 obj = NewBN(rb_obj_class(self)); \
459 if (!(result = BN_new())) { \
460 ossl_raise(eBNError, NULL); \
462 if (BN_##func(result, bn1, bn2, ossl_bn_ctx) <= 0) { \
464 ossl_raise(eBNError, NULL); \
466 SetBN(obj, result); \
512 BIGNUM *bn1, *bn2 =
GetBNPtr(other), *result;
539 if (!(
r1 = BN_new())) {
542 if (!(
r2 = BN_new())) {
557#define BIGNUM_3c(func) \
559 ossl_bn_##func(VALUE self, VALUE other1, VALUE other2) \
561 BIGNUM *bn1, *bn2 = GetBNPtr(other1); \
562 BIGNUM *bn3 = GetBNPtr(other2), *result; \
565 obj = NewBN(rb_obj_class(self)); \
566 if (!(result = BN_new())) { \
567 ossl_raise(eBNError, NULL); \
569 if (BN_##func(result, bn1, bn2, bn3, ossl_bn_ctx) <= 0) { \
571 ossl_raise(eBNError, NULL); \
573 SetBN(obj, result); \
605#define BIGNUM_BIT(func) \
607 ossl_bn_##func(VALUE self, VALUE bit) \
611 if (BN_##func(bn, NUM2INT(bit)) <= 0) { \
612 ossl_raise(eBNError, NULL); \
652 if (BN_is_bit_set(bn, b)) {
658#define BIGNUM_SHIFT(func) \
660 ossl_bn_##func(VALUE self, VALUE bits) \
662 BIGNUM *bn, *result; \
667 obj = NewBN(rb_obj_class(self)); \
668 if (!(result = BN_new())) { \
669 ossl_raise(eBNError, NULL); \
671 if (BN_##func(result, bn, b) <= 0) { \
673 ossl_raise(eBNError, NULL); \
675 SetBN(obj, result); \
693#define BIGNUM_SELF_SHIFT(func) \
695 ossl_bn_self_##func(VALUE self, VALUE bits) \
701 if (BN_##func(bn, bn, b) <= 0) \
702 ossl_raise(eBNError, NULL); \
720#define BIGNUM_RAND(func) \
722 ossl_bn_s_##func(int argc, VALUE *argv, VALUE klass) \
725 int bottom = 0, top = 0, b; \
726 VALUE bits, fill, odd, obj; \
728 switch (rb_scan_args(argc, argv, "12", &bits, &fill, &odd)) { \
730 bottom = (odd == Qtrue) ? 1 : 0; \
733 top = NUM2INT(fill); \
736 obj = NewBN(klass); \
737 if (!(result = BN_new())) { \
738 ossl_raise(eBNError, NULL); \
740 if (BN_##func(result, b, top, bottom) <= 0) { \
742 ossl_raise(eBNError, NULL); \
744 SetBN(obj, result); \
760#define BIGNUM_RAND_RANGE(func) \
762 ossl_bn_s_##func##_range(VALUE klass, VALUE range) \
764 BIGNUM *bn = GetBNPtr(range), *result; \
765 VALUE obj = NewBN(klass); \
766 if (!(result = BN_new())) { \
767 ossl_raise(eBNError, NULL); \
769 if (BN_##func##_range(result, bn) <= 0) { \
771 ossl_raise(eBNError, NULL); \
773 SetBN(obj, result); \
826 if (!(result = BN_new())) {
829 if (!BN_generate_prime_ex(result, num, safe,
add, rem,
NULL)) {
838#define BIGNUM_NUM(func) \
840 ossl_bn_##func(VALUE self) \
844 return INT2NUM(BN_##func(bn)); \
868 if (
self == other)
return self;
873 if (!BN_copy(bn1, bn2)) {
884ossl_bn_uplus(
VALUE self)
894ossl_bn_uminus(
VALUE self)
905 BN_set_negative(bn2, !BN_is_negative(bn2));
910#define BIGNUM_CMP(func) \
912 ossl_bn_##func(VALUE self, VALUE other) \
914 BIGNUM *bn1, *bn2 = GetBNPtr(other); \
916 return INT2NUM(BN_##func(bn1, bn2)); \
951 other = try_convert_to_bn(other);
956 if (!BN_cmp(bn1, bn2)) {
992ossl_bn_hash(
VALUE self)
1000 len = BN_num_bytes(bn);
1002 if (BN_bn2bin(bn,
buf) !=
len) {
1030 int checks = BN_prime_checks;
1065 VALUE vchecks, vtrivdiv;
1066 int checks = BN_prime_checks, do_trial_division = 1;
1070 if (!
NIL_P(vchecks)) {
1075 if (vtrivdiv ==
Qfalse) {
1076 do_trial_division = 0;
1078 switch (BN_is_prime_fasttest_ex(bn, checks,
ossl_bn_ctx, do_trial_division,
NULL)) {
char str[HTML_ESCAPE_MAX_LEN+1]
VALUE rb_define_class_under(VALUE, const char *, VALUE)
Defines a class under the namespace of outer.
VALUE rb_define_module(const char *)
void rb_define_alias(VALUE, const char *, const char *)
Defines an alias of a method.
VALUE rb_cObject
Object class.
void rb_raise(VALUE exc, const char *fmt,...)
VALUE rb_obj_class(VALUE)
Equivalent to Object#class in Ruby.
VALUE rb_obj_is_kind_of(VALUE, VALUE)
Determines if obj is a kind of c.
VALUE ossl_buf2str(char *buf, int len)
void ossl_raise(VALUE exc, const char *fmt,...)
#define BIGNUM_RAND(func)
BIGNUM * ossl_bn_value_ptr(volatile VALUE *ptr)
VALUE ossl_bn_new(const BIGNUM *bn)
#define BIGNUM_SHIFT(func)
#define BIGNUM_SELF_SHIFT(func)
#define BIGNUM_BOOL1(func)
#define BIGNUM_RAND_RANGE(func)