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)) {
194 len = BN_num_bytes(bn);
227 if (!(txt = BN_bn2dec(bn))) {
245 switch(
TYPE(other)) {
261 #define BIGNUM_BOOL1(func) \
268 ossl_bn_##func(VALUE self) \
272 if (BN_##func(bn)) { \
281 #define BIGNUM_1c(func) \
288 ossl_bn_##func(VALUE self) \
290 BIGNUM *bn, *result; \
293 if (!(result = BN_new())) { \
294 ossl_raise(eBNError, NULL); \
296 if (!BN_##func(result, bn, ossl_bn_ctx)) { \
298 ossl_raise(eBNError, NULL); \
300 WrapBN(CLASS_OF(self), obj, result); \
305 #define BIGNUM_2(func) \
312 ossl_bn_##func(VALUE self, VALUE other) \
314 BIGNUM *bn1, *bn2 = GetBNPtr(other), *result; \
317 if (!(result = BN_new())) { \
318 ossl_raise(eBNError, NULL); \
320 if (!BN_##func(result, bn1, bn2)) { \
322 ossl_raise(eBNError, NULL); \
324 WrapBN(CLASS_OF(self), obj, result); \
330 #define BIGNUM_2c(func) \
337 ossl_bn_##func(VALUE self, VALUE other) \
339 BIGNUM *bn1, *bn2 = GetBNPtr(other), *result; \
342 if (!(result = BN_new())) { \
343 ossl_raise(eBNError, NULL); \
345 if (!BN_##func(result, bn1, bn2, ossl_bn_ctx)) { \
347 ossl_raise(eBNError, NULL); \
349 WrapBN(CLASS_OF(self), obj, result); \
366 BIGNUM *bn1, *bn2 =
GetBNPtr(other), *r1, *r2;
371 if (!(r1 = BN_new())) {
374 if (!(r2 = BN_new())) {
389 #define BIGNUM_3c(func) \
396 ossl_bn_##func(VALUE self, VALUE other1, VALUE other2) \
398 BIGNUM *bn1, *bn2 = GetBNPtr(other1); \
399 BIGNUM *bn3 = GetBNPtr(other2), *result; \
402 if (!(result = BN_new())) { \
403 ossl_raise(eBNError, NULL); \
405 if (!BN_##func(result, bn1, bn2, bn3, ossl_bn_ctx)) { \
407 ossl_raise(eBNError, NULL); \
409 WrapBN(CLASS_OF(self), obj, result); \
417 #define BIGNUM_BIT(func) \
424 ossl_bn_##func(VALUE self, VALUE bit) \
428 if (!BN_##func(bn, NUM2INT(bit))) { \
429 ossl_raise(eBNError, NULL); \
449 if (BN_is_bit_set(bn, b)) {
455 #define BIGNUM_SHIFT(func) \
462 ossl_bn_##func(VALUE self, VALUE bits) \
464 BIGNUM *bn, *result; \
469 if (!(result = BN_new())) { \
470 ossl_raise(eBNError, NULL); \
472 if (!BN_##func(result, bn, b)) { \
474 ossl_raise(eBNError, NULL); \
476 WrapBN(CLASS_OF(self), obj, result); \
482 #define BIGNUM_SELF_SHIFT(func) \
489 ossl_bn_self_##func(VALUE self, VALUE bits) \
495 if (!BN_##func(bn, bn, b)) \
496 ossl_raise(eBNError, NULL); \
502 #define BIGNUM_RAND(func) \
509 ossl_bn_s_##func(int argc, VALUE *argv, VALUE klass) \
512 int bottom = 0, top = 0, b; \
513 VALUE bits, fill, odd, obj; \
515 switch (rb_scan_args(argc, argv, "12", &bits, &fill, &odd)) { \
517 bottom = (odd == Qtrue) ? 1 : 0; \
520 top = NUM2INT(fill); \
523 if (!(result = BN_new())) { \
524 ossl_raise(eBNError, NULL); \
526 if (!BN_##func(result, b, top, bottom)) { \
528 ossl_raise(eBNError, NULL); \
530 WrapBN(klass, obj, result); \
536 #define BIGNUM_RAND_RANGE(func) \
543 ossl_bn_s_##func##_range(VALUE klass, VALUE range) \
545 BIGNUM *bn = GetBNPtr(range), *result; \
547 if (!(result = BN_new())) { \
548 ossl_raise(eBNError, NULL); \
550 if (!BN_##func##_range(result, bn)) { \
552 ossl_raise(eBNError, NULL); \
554 WrapBN(klass, obj, result); \
575 VALUE vnum, vsafe, vadd, vrem, obj;
577 rb_scan_args(argc, argv,
"13", &vnum, &vsafe, &vadd, &vrem);
588 if (!(result = BN_new())) {
591 if (!BN_generate_prime(result, num, safe, add, rem,
NULL,
NULL)) {
595 WrapBN(klass, obj, result);
600 #define BIGNUM_NUM(func) \
607 ossl_bn_##func(VALUE self) \
611 return INT2FIX(BN_##func(bn)); \
623 if (
self == other)
return self;
628 if (!BN_copy(bn1, bn2)) {
634 #define BIGNUM_CMP(func) \
641 ossl_bn_##func(VALUE self, VALUE other) \
643 BIGNUM *bn1, *bn2 = GetBNPtr(other); \
645 return INT2FIX(BN_##func(bn1, bn2)); \
653 if (ossl_bn_cmp(
self, other) ==
INT2FIX(0)) {
672 int checks = BN_prime_checks;
704 VALUE vchecks, vtrivdiv;
705 int checks = BN_prime_checks, do_trial_division = 1;
709 if (!
NIL_P(vchecks)) {
715 do_trial_division = 0;
#define BIGNUM_RAND_RANGE(func)
size_t strlen(const char *)
static VALUE ossl_bn_alloc(VALUE klass)
static int is_zero(VALUE x)
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
static VALUE ossl_bn_is_prime(int argc, VALUE *argv, VALUE self)
#define BIGNUM_RAND(func)
static VALUE ossl_bn_to_bn(VALUE self)
#define RSTRING_PTR(string)
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
VALUE rb_obj_is_kind_of(VALUE, VALUE)
VALUE rb_ary_new3(long n,...)
static int is_one(VALUE x)
static VALUE ossl_bn_is_prime_fasttest(int argc, VALUE *argv, VALUE self)
#define BIGNUM_SHIFT(func)
#define rb_define_copy_func(klass, func)
#define BIGNUM_BOOL1(func)
RUBY_EXTERN VALUE rb_cObject
static VALUE ossl_bn_to_i(VALUE self)
static VALUE ossl_bn_eql(VALUE self, VALUE other)
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
static VALUE ossl_bn_coerce(VALUE self, VALUE other)
static VALUE ossl_bn_initialize(int argc, VALUE *argv, VALUE self)
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
unsigned char buf[MIME_BUF_SIZE]
VALUE rb_assoc_new(VALUE car, VALUE cdr)
static VALUE ossl_bn_copy(VALUE self, VALUE other)
register unsigned int len
VALUE ossl_buf2str(char *buf, int len)
void ossl_raise(VALUE exc, const char *fmt,...)
VALUE ossl_bn_new(const BIGNUM *bn)
#define BIGNUM_SELF_SHIFT(func)
#define StringValuePtr(v)
#define RSTRING_LENINT(str)
#define rb_check_frozen(obj)
BIGNUM * GetBNPtr(VALUE obj)
VALUE rb_define_module(const char *name)
VALUE rb_cstr_to_inum(const char *str, int base, int badcheck)
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
static VALUE ossl_bn_s_generate_prime(int argc, VALUE *argv, VALUE klass)
static VALUE ossl_bn_to_s(int argc, VALUE *argv, VALUE self)
#define WrapBN(klass, obj, bn)
VALUE rb_str_new(const char *, long)