13#if HAVE_RB_EXT_RACTOR_SAFE
18 TypedData_Wrap_Struct((klass), &ossl_bn_type, 0)
19#define SetBN(obj, bn) do { \
21 ossl_raise(rb_eRuntimeError, "BN wasn't initialized!"); \
23 RTYPEDDATA_DATA(obj) = (bn); \
26#define GetBN(obj, bn) do { \
27 TypedData_Get_Struct((obj), BIGNUM, &ossl_bn_type, (bn)); \
29 ossl_raise(rb_eRuntimeError, "BN wasn't initialized!"); \
34ossl_bn_free(
void *
ptr)
68 newbn = bn ? BN_dup(bn) : BN_new();
78integer_to_bnptr(
VALUE obj, BIGNUM *orig)
84 unsigned char bin[
sizeof(
long)];
86 unsigned long un = labs(n);
88 for (i =
sizeof(
long) - 1; 0 <= i; i--) {
93 bn = BN_bin2bn(
bin,
sizeof(
bin), orig);
97 BN_set_negative(bn, 1);
111 bn = BN_bin2bn(
bin, (
int)
len, orig);
116 BN_set_negative(bn, 1);
123try_convert_to_bn(
VALUE obj)
132 bn = integer_to_bnptr(obj,
NULL);
145 tmp = try_convert_to_bn(*
ptr);
158#if HAVE_RB_EXT_RACTOR_SAFE
162 BN_CTX *ctx = (BN_CTX *)
ptr;
180 if (!(ctx = BN_CTX_new())) {
189static BN_CTX *gv_ossl_bn_ctx;
194 if (gv_ossl_bn_ctx ==
NULL) {
195 if (!(gv_ossl_bn_ctx = BN_CTX_new())) {
199 return gv_ossl_bn_ctx;
205 BN_CTX_free(gv_ossl_bn_ctx);
206 gv_ossl_bn_ctx =
NULL;
211ossl_bn_alloc(
VALUE klass)
216 if (!(bn = BN_new())) {
251 integer_to_bnptr(
str, bn);
261 if (!BN_copy(bn, other)) {
271 if (!BN_mpi2bn((
unsigned char *)
ptr, RSTRING_LENINT(
str), bn)) {
277 if (!BN_bin2bn((
unsigned char *)
ptr, RSTRING_LENINT(
str), bn)) {
330 len = BN_num_bytes(bn);
355ossl_bn_to_i(
VALUE self)
363 if (!(txt = BN_bn2hex(bn))) {
373ossl_bn_to_bn(
VALUE self)
381 switch(
TYPE(other)) {
383 self = ossl_bn_to_s(0,
NULL, self);
387 self = ossl_bn_to_i(self);
397#define BIGNUM_BOOL1(func) \
399 ossl_bn_##func(VALUE self) \
403 if (BN_##func(bn)) { \
435ossl_bn_is_negative(
VALUE self)
445#define BIGNUM_1c(func) \
447 ossl_bn_##func(VALUE self) \
449 BIGNUM *bn, *result; \
452 obj = NewBN(rb_obj_class(self)); \
453 if (!(result = BN_new())) { \
454 ossl_raise(eBNError, NULL); \
456 if (BN_##func(result, bn, ossl_bn_ctx) <= 0) { \
458 ossl_raise(eBNError, NULL); \
460 SetBN(obj, result); \
471#define BIGNUM_2(func) \
473 ossl_bn_##func(VALUE self, VALUE other) \
475 BIGNUM *bn1, *bn2 = GetBNPtr(other), *result; \
478 obj = NewBN(rb_obj_class(self)); \
479 if (!(result = BN_new())) { \
480 ossl_raise(eBNError, NULL); \
482 if (BN_##func(result, bn1, bn2) <= 0) { \
484 ossl_raise(eBNError, NULL); \
486 SetBN(obj, result); \
504#define BIGNUM_2c(func) \
506 ossl_bn_##func(VALUE self, VALUE other) \
508 BIGNUM *bn1, *bn2 = GetBNPtr(other), *result; \
511 obj = NewBN(rb_obj_class(self)); \
512 if (!(result = BN_new())) { \
513 ossl_raise(eBNError, NULL); \
515 if (BN_##func(result, bn1, bn2, ossl_bn_ctx) <= 0) { \
517 ossl_raise(eBNError, NULL); \
519 SetBN(obj, result); \
565 BIGNUM *bn1, *bn2 =
GetBNPtr(other), *result;
585 VALUE klass, obj1, obj2;
592 if (!(
r1 = BN_new())) {
595 if (!(
r2 = BN_new())) {
610#define BIGNUM_3c(func) \
612 ossl_bn_##func(VALUE self, VALUE other1, VALUE other2) \
614 BIGNUM *bn1, *bn2 = GetBNPtr(other1); \
615 BIGNUM *bn3 = GetBNPtr(other2), *result; \
618 obj = NewBN(rb_obj_class(self)); \
619 if (!(result = BN_new())) { \
620 ossl_raise(eBNError, NULL); \
622 if (BN_##func(result, bn1, bn2, bn3, ossl_bn_ctx) <= 0) { \
624 ossl_raise(eBNError, NULL); \
626 SetBN(obj, result); \
658#define BIGNUM_BIT(func) \
660 ossl_bn_##func(VALUE self, VALUE bit) \
664 if (BN_##func(bn, NUM2INT(bit)) <= 0) { \
665 ossl_raise(eBNError, NULL); \
705 if (BN_is_bit_set(bn, b)) {
711#define BIGNUM_SHIFT(func) \
713 ossl_bn_##func(VALUE self, VALUE bits) \
715 BIGNUM *bn, *result; \
720 obj = NewBN(rb_obj_class(self)); \
721 if (!(result = BN_new())) { \
722 ossl_raise(eBNError, NULL); \
724 if (BN_##func(result, bn, b) <= 0) { \
726 ossl_raise(eBNError, NULL); \
728 SetBN(obj, result); \
746#define BIGNUM_SELF_SHIFT(func) \
748 ossl_bn_self_##func(VALUE self, VALUE bits) \
754 if (BN_##func(bn, bn, b) <= 0) \
755 ossl_raise(eBNError, NULL); \
773#define BIGNUM_RAND(func) \
775 ossl_bn_s_##func(int argc, VALUE *argv, VALUE klass) \
778 int bottom = 0, top = 0, b; \
779 VALUE bits, fill, odd, obj; \
781 switch (rb_scan_args(argc, argv, "12", &bits, &fill, &odd)) { \
783 bottom = (odd == Qtrue) ? 1 : 0; \
786 top = NUM2INT(fill); \
789 obj = NewBN(klass); \
790 if (!(result = BN_new())) { \
791 ossl_raise(eBNError, NULL); \
793 if (BN_##func(result, b, top, bottom) <= 0) { \
795 ossl_raise(eBNError, NULL); \
797 SetBN(obj, result); \
813#define BIGNUM_RAND_RANGE(func) \
815 ossl_bn_s_##func##_range(VALUE klass, VALUE range) \
817 BIGNUM *bn = GetBNPtr(range), *result; \
818 VALUE obj = NewBN(klass); \
819 if (!(result = BN_new())) { \
820 ossl_raise(eBNError, NULL); \
822 if (BN_##func##_range(result, bn) <= 0) { \
824 ossl_raise(eBNError, NULL); \
826 SetBN(obj, result); \
865 VALUE vnum, vsafe, vadd, vrem, obj;
879 if (!(result = BN_new())) {
882 if (!BN_generate_prime_ex(result,
num, safe,
add, rem,
NULL)) {
891#define BIGNUM_NUM(func) \
893 ossl_bn_##func(VALUE self) \
897 return INT2NUM(BN_##func(bn)); \
921 if (self == other)
return self;
926 if (!BN_copy(bn1, bn2)) {
937ossl_bn_uplus(
VALUE self)
947ossl_bn_uminus(
VALUE self)
958 BN_set_negative(bn2, !BN_is_negative(bn2));
963#define BIGNUM_CMP(func) \
965 ossl_bn_##func(VALUE self, VALUE other) \
967 BIGNUM *bn1, *bn2 = GetBNPtr(other); \
969 return INT2NUM(BN_##func(bn1, bn2)); \
1004 other = try_convert_to_bn(other);
1009 if (!BN_cmp(bn1, bn2)) {
1045ossl_bn_hash(
VALUE self)
1053 len = BN_num_bytes(bn);
1055 if (BN_bn2bin(bn,
buf) !=
len) {
1083 int checks = BN_prime_checks;
1118 VALUE vchecks, vtrivdiv;
1119 int checks = BN_prime_checks, do_trial_division = 1;
1123 if (!
NIL_P(vchecks)) {
1128 if (vtrivdiv ==
Qfalse) {
1129 do_trial_division = 0;
1131 switch (BN_is_prime_fasttest_ex(bn, checks,
ossl_bn_ctx, do_trial_division,
NULL)) {
1155#ifdef HAVE_RB_EXT_RACTOR_SAFE
VALUE rb_assoc_new(VALUE car, VALUE cdr)
int rb_integer_pack(VALUE val, void *words, size_t numwords, size_t wordsize, size_t nails, int flags)
size_t rb_absint_size(VALUE val, int *nlz_bits_ret)
VALUE rb_cstr_to_inum(const char *str, int base, int badcheck)
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
char str[HTML_ESCAPE_MAX_LEN+1]
#define RSTRING_PTR(string)
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
VALUE rb_define_module(const char *name)
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
void rb_raise(VALUE exc, const char *fmt,...)
VALUE rb_cObject
Object class.
VALUE rb_obj_class(VALUE)
VALUE rb_obj_is_kind_of(VALUE, VALUE)
Determines if obj is a kind of c.
unsigned gcd(unsigned a, unsigned b)
#define INTEGER_PACK_BIG_ENDIAN
st_index_t rb_memhash(const void *ptr, long len)
#define rb_str_new(str, len)
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
typedef long(ZCALLBACK *tell_file_func) OF((voidpf opaque
VALUE ossl_buf2str(char *buf, int len)
void ossl_raise(VALUE exc, const char *fmt,...)
#define BIGNUM_RAND(func)
void ossl_bn_ctx_free(void)
BIGNUM * ossl_bn_value_ptr(volatile VALUE *ptr)
VALUE ossl_bn_new(const BIGNUM *bn)
BN_CTX * ossl_bn_ctx_get(void)
#define BIGNUM_SHIFT(func)
#define BIGNUM_SELF_SHIFT(func)
#define BIGNUM_BOOL1(func)
#define BIGNUM_RAND_RANGE(func)
void * rb_ractor_local_storage_ptr(rb_ractor_local_key_t key)
void rb_ractor_local_storage_ptr_set(rb_ractor_local_key_t key, void *ptr)
rb_ractor_local_key_t rb_ractor_local_storage_ptr_newkey(const struct rb_ractor_local_storage_type *type)
#define StringValuePtr(v)
#define StringValueCStr(v)
@ RUBY_TYPED_FREE_IMMEDIATELY
#define RB_INTEGER_TYPE_P(obj)
size_t strlen(const char *)