Ruby
2.7.2p137(2020-10-01revision5445e0435260b449decf2ac16f9d09bae3cafe72)
|
Go to the documentation of this file.
13 #define NewBN(klass) \
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!"); \
30 ossl_bn_free(
void *
ptr)
64 newbn = bn ? BN_dup(bn) : BN_new();
74 integer_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);
302 ossl_bn_to_i(
VALUE self)
310 if (!(txt = BN_bn2hex(bn))) {
320 ossl_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)) { \
382 ossl_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)) { \
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)) { \
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)) { \
464 ossl_raise(eBNError, NULL); \
466 SetBN(obj, result); \
529 if (!(
r1 = BN_new())) {
532 if (!(
r2 = BN_new())) {
547 #define BIGNUM_3c(func) \
549 ossl_bn_##func(VALUE self, VALUE other1, VALUE other2) \
551 BIGNUM *bn1, *bn2 = GetBNPtr(other1); \
552 BIGNUM *bn3 = GetBNPtr(other2), *result; \
555 obj = NewBN(rb_obj_class(self)); \
556 if (!(result = BN_new())) { \
557 ossl_raise(eBNError, NULL); \
559 if (!BN_##func(result, bn1, bn2, bn3, ossl_bn_ctx)) { \
561 ossl_raise(eBNError, NULL); \
563 SetBN(obj, result); \
595 #define BIGNUM_BIT(func) \
597 ossl_bn_##func(VALUE self, VALUE bit) \
601 if (!BN_##func(bn, NUM2INT(bit))) { \
602 ossl_raise(eBNError, NULL); \
642 if (BN_is_bit_set(bn, b)) {
648 #define BIGNUM_SHIFT(func) \
650 ossl_bn_##func(VALUE self, VALUE bits) \
652 BIGNUM *bn, *result; \
657 obj = NewBN(rb_obj_class(self)); \
658 if (!(result = BN_new())) { \
659 ossl_raise(eBNError, NULL); \
661 if (!BN_##func(result, bn, b)) { \
663 ossl_raise(eBNError, NULL); \
665 SetBN(obj, result); \
683 #define BIGNUM_SELF_SHIFT(func) \
685 ossl_bn_self_##func(VALUE self, VALUE bits) \
691 if (!BN_##func(bn, bn, b)) \
692 ossl_raise(eBNError, NULL); \
710 #define BIGNUM_RAND(func) \
712 ossl_bn_s_##func(int argc, VALUE *argv, VALUE klass) \
715 int bottom = 0, top = 0, b; \
716 VALUE bits, fill, odd, obj; \
718 switch (rb_scan_args(argc, argv, "12", &bits, &fill, &odd)) { \
720 bottom = (odd == Qtrue) ? 1 : 0; \
723 top = NUM2INT(fill); \
726 obj = NewBN(klass); \
727 if (!(result = BN_new())) { \
728 ossl_raise(eBNError, NULL); \
730 if (!BN_##func(result, b, top, bottom)) { \
732 ossl_raise(eBNError, NULL); \
734 SetBN(obj, result); \
750 #define BIGNUM_RAND_RANGE(func) \
752 ossl_bn_s_##func##_range(VALUE klass, VALUE range) \
754 BIGNUM *bn = GetBNPtr(range), *result; \
755 VALUE obj = NewBN(klass); \
756 if (!(result = BN_new())) { \
757 ossl_raise(eBNError, NULL); \
759 if (!BN_##func##_range(result, bn)) { \
761 ossl_raise(eBNError, NULL); \
763 SetBN(obj, result); \
816 if (!(result = BN_new())) {
819 if (!BN_generate_prime_ex(result, num, safe,
add, rem,
NULL)) {
828 #define BIGNUM_NUM(func) \
830 ossl_bn_##func(VALUE self) \
834 return INT2NUM(BN_##func(bn)); \
858 if (
self == other)
return self;
863 if (!BN_copy(bn1, bn2)) {
874 ossl_bn_uplus(
VALUE self)
884 ossl_bn_uminus(
VALUE self)
895 BN_set_negative(bn2, !BN_is_negative(bn2));
900 #define BIGNUM_CMP(func) \
902 ossl_bn_##func(VALUE self, VALUE other) \
904 BIGNUM *bn1, *bn2 = GetBNPtr(other); \
906 return INT2NUM(BN_##func(bn1, bn2)); \
941 other = try_convert_to_bn(other);
946 if (!BN_cmp(bn1, bn2)) {
982 ossl_bn_hash(
VALUE self)
990 len = BN_num_bytes(bn);
992 if (BN_bn2bin(bn,
buf) !=
len) {
1020 int checks = BN_prime_checks;
1055 VALUE vchecks, vtrivdiv;
1056 int checks = BN_prime_checks, do_trial_division = 1;
1060 if (!
NIL_P(vchecks)) {
1065 if (vtrivdiv ==
Qfalse) {
1066 do_trial_division = 0;
1068 switch (BN_is_prime_fasttest_ex(bn, checks,
ossl_bn_ctx, do_trial_division,
NULL)) {
VALUE rb_assoc_new(VALUE car, VALUE cdr)
#define BIGNUM_RAND_RANGE(func)
#define BIGNUM_SELF_SHIFT(func)
#define BIGNUM_SHIFT(func)
#define RSTRING_LENINT(str)
VALUE rb_define_module(const char *name)
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
VALUE rb_cstr_to_inum(const char *str, int base, int badcheck)
size_t strlen(const char *)
#define INTEGER_PACK_BIG_ENDIAN
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
st_index_t rb_memhash(const void *ptr, long len)
void rb_raise(VALUE exc, const char *fmt,...)
VALUE rb_obj_class(VALUE)
Equivalent to Object#class in Ruby.
int rb_integer_pack(VALUE val, void *words, size_t numwords, size_t wordsize, size_t nails, int flags)
VALUE ossl_buf2str(char *buf, int len)
#define rb_check_frozen(obj)
#define BIGNUM_BOOL1(func)
#define ALLOCV_N(type, v, n)
VALUE ossl_bn_new(const BIGNUM *bn)
void ossl_raise(VALUE exc, const char *fmt,...)
#define BIGNUM_RAND(func)
#define StringValuePtr(v)
BIGNUM * ossl_bn_value_ptr(volatile VALUE *ptr)
#define StringValueCStr(v)
RUBY_EXTERN VALUE rb_cObject
unsigned char buf[MIME_BUF_SIZE]
char str[HTML_ESCAPE_MAX_LEN+1]
#define RUBY_TYPED_FREE_IMMEDIATELY
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
#define RB_INTEGER_TYPE_P(obj)
size_t rb_absint_size(VALUE val, int *nlz_bits_ret)
VALUE rb_obj_is_kind_of(VALUE, VALUE)
Determines if obj is a kind of c.
void rb_define_alloc_func(VALUE, rb_alloc_func_t)