Skip to content

Commit 1ddbf28

Browse files
authored
Merge pull request #553 from btoews/ossl_bn_mod_sqrt
Add BN#mod_sqrt
2 parents e25de6b + 5befde7 commit 1ddbf28

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

ext/openssl/ossl_bn.c

+24-12
Original file line numberDiff line numberDiff line change
@@ -577,22 +577,33 @@ BIGNUM_2c(gcd)
577577
*/
578578
BIGNUM_2c(mod_sqr)
579579

580+
#define BIGNUM_2cr(func) \
581+
static VALUE \
582+
ossl_bn_##func(VALUE self, VALUE other) \
583+
{ \
584+
BIGNUM *bn1, *bn2 = GetBNPtr(other), *result; \
585+
VALUE obj; \
586+
GetBN(self, bn1); \
587+
obj = NewBN(rb_obj_class(self)); \
588+
if (!(result = BN_##func(NULL, bn1, bn2, ossl_bn_ctx))) \
589+
ossl_raise(eBNError, NULL); \
590+
SetBN(obj, result); \
591+
return obj; \
592+
}
593+
580594
/*
595+
* Document-method: OpenSSL::BN#mod_sqrt
596+
* call-seq:
597+
* bn.mod_sqrt(bn2) => aBN
598+
*/
599+
BIGNUM_2cr(mod_sqrt)
600+
601+
/*
602+
* Document-method: OpenSSL::BN#mod_inverse
581603
* call-seq:
582604
* bn.mod_inverse(bn2) => aBN
583605
*/
584-
static VALUE
585-
ossl_bn_mod_inverse(VALUE self, VALUE other)
586-
{
587-
BIGNUM *bn1, *bn2 = GetBNPtr(other), *result;
588-
VALUE obj;
589-
GetBN(self, bn1);
590-
obj = NewBN(rb_obj_class(self));
591-
if (!(result = BN_mod_inverse(NULL, bn1, bn2, ossl_bn_ctx)))
592-
ossl_raise(eBNError, "BN_mod_inverse");
593-
SetBN(obj, result);
594-
return obj;
595-
}
606+
BIGNUM_2cr(mod_inverse)
596607

597608
/*
598609
* call-seq:
@@ -1234,6 +1245,7 @@ Init_ossl_bn(void)
12341245
rb_define_method(cBN, "mod_sub", ossl_bn_mod_sub, 2);
12351246
rb_define_method(cBN, "mod_mul", ossl_bn_mod_mul, 2);
12361247
rb_define_method(cBN, "mod_sqr", ossl_bn_mod_sqr, 1);
1248+
rb_define_method(cBN, "mod_sqrt", ossl_bn_mod_sqrt, 1);
12371249
rb_define_method(cBN, "**", ossl_bn_exp, 1);
12381250
rb_define_method(cBN, "mod_exp", ossl_bn_mod_exp, 2);
12391251
rb_define_method(cBN, "gcd", ossl_bn_gcd, 1);

test/openssl/test_bn.rb

+6
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ def test_mod_sqr
174174
assert_equal(0, 59.to_bn.mod_sqr(59))
175175
end
176176

177+
def test_mod_sqrt
178+
assert_equal(3, 4.to_bn.mod_sqrt(5))
179+
assert_equal(0, 5.to_bn.mod_sqrt(5))
180+
assert_raise(OpenSSL::BNError) { 3.to_bn.mod_sqrt(5) }
181+
end
182+
177183
def test_mod_inverse
178184
assert_equal(2, 3.to_bn.mod_inverse(5))
179185
assert_raise(OpenSSL::BNError) { 3.to_bn.mod_inverse(6) }

0 commit comments

Comments
 (0)