diff options
author | Kazuki Yamaguchi <k@rhe.jp> | 2016-12-26 23:11:16 +0900 |
---|---|---|
committer | Kazuki Yamaguchi <k@rhe.jp> | 2016-12-26 23:11:16 +0900 |
commit | f297f18deb5bc9669e9defc88aa0af454856fe08 (patch) | |
tree | 336ad137223c3166bb2179f8510e1be09a305a70 /ext/openssl | |
parent | 5165effe4f5edd410715cb36cfc199f760ee320b (diff) | |
parent | 95a500a72917bb7eccf60a96da68fa938cef8f69 (diff) | |
download | ruby-openssl-f297f18deb5bc9669e9defc88aa0af454856fe08.tar.gz |
Merge branch 'topic/bn-updates'
Tests for OpenSSL::BN are re-written. OpenSSL::BN now implements unary+
operator, unary- operator and negative? method.
* topic/bn-updates:
bn: implement OpenSSL::BN#negative?
bn: implement unary {plus,minus} operators for OpenSSL::BN
bn: refine tests
Diffstat (limited to 'ext/openssl')
-rw-r--r-- | ext/openssl/ossl_bn.c | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/ext/openssl/ossl_bn.c b/ext/openssl/ossl_bn.c index 4e371cb2..dfe1b268 100644 --- a/ext/openssl/ossl_bn.c +++ b/ext/openssl/ossl_bn.c @@ -377,6 +377,21 @@ BIGNUM_BOOL1(is_one) */ BIGNUM_BOOL1(is_odd) +/* + * call-seq: + * bn.negative? => true | false + */ +static VALUE +ossl_bn_is_negative(VALUE self) +{ + BIGNUM *bn; + + GetBN(self, bn); + if (BN_is_zero(bn)) + return Qfalse; + return BN_is_negative(bn) ? Qtrue : Qfalse; +} + #define BIGNUM_1c(func) \ static VALUE \ ossl_bn_##func(VALUE self) \ @@ -856,6 +871,37 @@ ossl_bn_copy(VALUE self, VALUE other) return self; } +/* + * call-seq: + * +bn -> aBN + */ +static VALUE +ossl_bn_uplus(VALUE self) +{ + return self; +} + +/* + * call-seq: + * -bn -> aBN + */ +static VALUE +ossl_bn_uminus(VALUE self) +{ + VALUE obj; + BIGNUM *bn1, *bn2; + + GetBN(self, bn1); + obj = NewBN(cBN); + bn2 = BN_dup(bn1); + if (!bn2) + ossl_raise(eBNError, "BN_dup"); + SetBN(obj, bn2); + BN_set_negative(bn2, !BN_is_negative(bn2)); + + return obj; +} + #define BIGNUM_CMP(func) \ static VALUE \ ossl_bn_##func(VALUE self, VALUE other) \ @@ -1068,6 +1114,9 @@ Init_ossl_bn(void) rb_define_method(cBN, "num_bits", ossl_bn_num_bits, 0); /* num_bits_word */ + rb_define_method(cBN, "+@", ossl_bn_uplus, 0); + rb_define_method(cBN, "-@", ossl_bn_uminus, 0); + rb_define_method(cBN, "+", ossl_bn_add, 1); rb_define_method(cBN, "-", ossl_bn_sub, 1); rb_define_method(cBN, "*", ossl_bn_mul, 1); @@ -1101,6 +1150,7 @@ Init_ossl_bn(void) rb_define_method(cBN, "one?", ossl_bn_is_one, 0); /* is_word */ rb_define_method(cBN, "odd?", ossl_bn_is_odd, 0); + rb_define_method(cBN, "negative?", ossl_bn_is_negative, 0); /* zero * one |