aboutsummaryrefslogtreecommitdiffstats
path: root/ext/openssl
diff options
context:
space:
mode:
authorRick Mark <rick.mark@coinbase.com>2021-04-01 12:29:21 -0700
committerRick Mark <rick.mark@coinbase.com>2021-04-01 12:29:21 -0700
commit0321b1e945f89d08ab3632fe8bb3b7726086e9bb (patch)
treed3b22086f46eb438e1893ea28b3269dabf4ad902 /ext/openssl
parent69f89d4ae967f87e355dbbe05fbc039a083b6792 (diff)
downloadruby-openssl-0321b1e945f89d08ab3632fe8bb3b7726086e9bb.tar.gz
BN.abs and BN uplus
Adds standard math abs fuction and revises uplus to return a duplicated object due to BN mutability
Diffstat (limited to 'ext/openssl')
-rw-r--r--ext/openssl/ossl_bn.c31
1 files changed, 30 insertions, 1 deletions
diff --git a/ext/openssl/ossl_bn.c b/ext/openssl/ossl_bn.c
index 1d43e457..ac3f8382 100644
--- a/ext/openssl/ossl_bn.c
+++ b/ext/openssl/ossl_bn.c
@@ -936,7 +936,17 @@ ossl_bn_copy(VALUE self, VALUE other)
static VALUE
ossl_bn_uplus(VALUE self)
{
- return 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);
+
+ return obj;
}
/*
@@ -960,6 +970,24 @@ ossl_bn_uminus(VALUE self)
return obj;
}
+/*
+ * call-seq:
+ * bn.abs -> aBN
+ */
+static VALUE
+ossl_bn_abs(VALUE self)
+{
+ BIGNUM *bn1;
+
+ GetBN(self, bn1);
+ if (BN_is_negative(bn1)) {
+ return ossl_bn_uminus(self);
+ }
+ else {
+ return ossl_bn_uplus(self);
+ }
+}
+
#define BIGNUM_CMP(func) \
static VALUE \
ossl_bn_##func(VALUE self, VALUE other) \
@@ -1176,6 +1204,7 @@ Init_ossl_bn(void)
rb_define_method(cBN, "+@", ossl_bn_uplus, 0);
rb_define_method(cBN, "-@", ossl_bn_uminus, 0);
+ rb_define_method(cBN, "abs", ossl_bn_abs, 0);
rb_define_method(cBN, "+", ossl_bn_add, 1);
rb_define_method(cBN, "-", ossl_bn_sub, 1);