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)) {
196 len = BN_num_bytes(bn);
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); \
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); \
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); \
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;
#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.
#define rb_check_frozen(obj)
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
static VALUE ossl_bn_is_prime(int argc, VALUE *argv, VALUE self)
#define BIGNUM_RAND(func)
static VALUE ossl_bn_to_bn(VALUE self)
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
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)
#define StringValuePtr(v)
unsigned char buf[MIME_BUF_SIZE]
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_is_bit_set(VALUE self, VALUE bit)
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,...)
VALUE rb_assoc_new(VALUE car, VALUE cdr)
static VALUE ossl_bn_div(VALUE self, VALUE other)
static VALUE ossl_bn_copy(VALUE self, VALUE other)
VALUE rb_obj_is_kind_of(VALUE, VALUE)
RUBY_EXTERN VALUE rb_cObject
VALUE ossl_buf2str(char *buf, int len)
void ossl_raise(VALUE exc, const char *fmt,...)
#define RSTRING_LENINT(str)
VALUE rb_str_new(const char *, long)
VALUE ossl_bn_new(const BIGNUM *bn)
#define BIGNUM_SELF_SHIFT(func)
static struct tcltkip *VALUE self
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)