Class: OpenSSL::BN
| Relationships & Source Files | |
| Super Chains via Extension / Inclusion / Inheritance | |
|
Instance Chain:
self,
Comparable
|
|
| Inherits: | Object |
| Defined in: | ext/openssl/ossl_bn.c, ext/openssl/lib/openssl/bn.rb |
Class Method Summary
- .generate_prime
-
.new ⇒ BN
constructor
Construct a new ::OpenSSL BIGNUM object.
-
.pseudo_rand
.pseudo_rand(bits [, fill [, odd]]) -> aBN.
- .pseudo_rand_range(range) ⇒ BN
-
.rand
.rand(bits [, fill [, odd]]) -> aBN.
- .rand_range(range) ⇒ BN
Instance Attribute Summary
- #negative? ⇒ Boolean readonly
- #odd? ⇒ Boolean readonly
- #one? ⇒ Boolean readonly
- #zero? ⇒ Boolean readonly
Instance Method Summary
- #%(bn2) ⇒ BN
- #*(bn2) ⇒ BN
- #**(bn2) ⇒ BN
- #+(bn2) ⇒ BN
- #+ ⇒ BN
- #-(bn2) ⇒ BN
- #- ⇒ BN
- #/
- #<<(bits) ⇒ BN
- #<=>(bn2) ⇒ Integer (also: #cmp)
-
#==(obj) ⇒ Boolean
(also: #===)
Returns
trueonly if obj has the same value as bn. -
#===(obj) ⇒ Boolean
Alias for #==.
- #>>(bits) ⇒ BN
- #bit_set? ⇒ Boolean
- #clear_bit!(bit) ⇒ self
-
#cmp(bn2) ⇒ Integer
Alias for #<=>.
- #coerce(other)
- #copy ⇒ Integer (also: #initialize_copy)
-
#eql?(obj) ⇒ Boolean
Returns
trueonly if obj is aBNwith the same value as bn. - #gcd(bn2) ⇒ BN
-
#hash ⇒ Integer
Returns a hash code for this object.
-
#initialize_copy ⇒ Integer
Alias for #copy.
- #lshift!(bits) ⇒ self
- #mask_bits!
- #mod_add(bn1, bn2) ⇒ BN
- #mod_exp(bn1, bn2) ⇒ BN
- #mod_inverse(bn2) ⇒ BN
- #mod_mul(bn1, bn2) ⇒ BN
- #mod_sqr(bn2) ⇒ BN
- #mod_sub(bn1, bn2) ⇒ BN
- #num_bits ⇒ Integer
- #num_bytes ⇒ Integer
- #pretty_print(q)
-
#prime? ⇒ Boolean
Performs a Miller-Rabin probabilistic primality test with checks iterations.
-
#prime_fasttest? ⇒ Boolean
Performs a Miller-Rabin primality test.
- #rshift!(bits) ⇒ self
- #set_bit!(bit) ⇒ self
- #sqr ⇒ BN
- #to_bn
- #to_i ⇒ Integer (also: #to_int)
-
#to_int ⇒ Integer
Alias for #to_i.
-
#to_s ⇒ String
Parameters * base - ::Integer.
- #ucmp(bn2) ⇒ Integer
Constructor Details
.new ⇒ BN
.new(bn) ⇒ BN
.new(integer) ⇒ BN
.new(string) ⇒ BN
.new(string, 0 | 2 | 10 | 16) ⇒ BN
BN
.new(bn) ⇒ BN
.new(integer) ⇒ BN
.new(string) ⇒ BN
.new(string, 0 | 2 | 10 | 16) ⇒ BN
Construct a new ::OpenSSL BIGNUM object.
# File 'ext/openssl/ossl_bn.c', line 184
static VALUE
ossl_bn_initialize(int argc, VALUE *argv, VALUE self)
{
BIGNUM *bn;
VALUE str, bs;
int base = 10;
if (rb_scan_args(argc, argv, "11", &str, &bs) == 2) {
base = NUM2INT(bs);
}
if (RB_INTEGER_TYPE_P(str)) {
GetBN(self, bn);
integer_to_bnptr(str, bn);
return self;
}
if (RTEST(rb_obj_is_kind_of(str, cBN))) {
BIGNUM *other;
GetBN(self, bn);
GetBN(str, other); /* Safe - we checked kind_of? above */
if (!BN_copy(bn, other)) {
ossl_raise(eBNError, NULL);
}
return self;
}
GetBN(self, bn);
switch (base) {
case 0:
if (!BN_mpi2bn((unsigned char *)StringValuePtr(str), RSTRING_LENINT(str), bn)) {
ossl_raise(eBNError, NULL);
}
break;
case 2:
if (!BN_bin2bn((unsigned char *)StringValuePtr(str), RSTRING_LENINT(str), bn)) {
ossl_raise(eBNError, NULL);
}
break;
case 10:
if (!BN_dec2bn(&bn, StringValueCStr(str))) {
ossl_raise(eBNError, NULL);
}
break;
case 16:
if (!BN_hex2bn(&bn, StringValueCStr(str))) {
ossl_raise(eBNError, NULL);
}
break;
default:
ossl_raise(rb_eArgError, "invalid radix %d", base);
}
return self;
}
Class Method Details
.generate_prime
[ GitHub ].pseudo_rand
.pseudo_rand(bits [, fill [, odd]]) -> aBN
.pseudo_rand_range(range) ⇒ BN
.rand
.rand(bits [, fill [, odd]]) -> aBN
.rand_range(range) ⇒ BN
Instance Attribute Details
#negative? ⇒ Boolean (readonly)
[ GitHub ]
# File 'ext/openssl/ossl_bn.c', line 378
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;
}
#odd? ⇒ Boolean (readonly)
[ GitHub ]
#one? ⇒ Boolean (readonly)
[ GitHub ]
#zero? ⇒ Boolean (readonly)
[ GitHub ]
Instance Method Details
#%(bn2) ⇒ BN
#*(bn2) ⇒ BN
#**(bn2) ⇒ BN
#+(bn2) ⇒ BN
#+ ⇒ BN
# File 'ext/openssl/ossl_bn.c', line 870
static VALUE
ossl_bn_uplus(VALUE self)
{
return self;
}
#-(bn2) ⇒ BN
#- ⇒ BN
# File 'ext/openssl/ossl_bn.c', line 880
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;
}
#/
[ GitHub ]
#<<(bits) ⇒ BN
#<=>(bn2) ⇒ Integer Also known as: #cmp
#==(obj) ⇒ Boolean Also known as: #===
Returns true only if obj has the same value as bn. Contrast this with #eql?, which requires obj to be BN.
# File 'ext/openssl/ossl_bn.c', line 932
static VALUE
ossl_bn_eq(VALUE self, VALUE other)
{
BIGNUM *bn1, *bn2;
GetBN(self, bn1);
other = try_convert_to_bn(other);
if (NIL_P(other))
return Qfalse;
GetBN(other, bn2);
if (!BN_cmp(bn1, bn2)) {
return Qtrue;
}
return Qfalse;
}
#==(obj) ⇒ Boolean
#===(obj) ⇒ Boolean
Boolean
#===(obj) ⇒ Boolean
Alias for #==.
#>>(bits) ⇒ BN
#bit_set? ⇒ Boolean
#clear_bit!(bit) ⇒ self
Alias for #<=>.
#coerce(other)
[ GitHub ]# File 'ext/openssl/ossl_bn.c', line 322
static VALUE
ossl_bn_coerce(VALUE self, VALUE other)
{
switch(TYPE(other)) {
case T_STRING:
self = ossl_bn_to_s(0, NULL, self);
break;
case T_FIXNUM:
case T_BIGNUM:
self = ossl_bn_to_i(self);
break;
default:
if (!RTEST(rb_obj_is_kind_of(other, cBN))) {
ossl_raise(rb_eTypeError, "Don't know how to coerce");
}
}
return rb_assoc_new(other, self);
}
#copy ⇒ Integer Also known as: #initialize_copy
# File 'ext/openssl/ossl_bn.c', line 848
static VALUE
ossl_bn_copy(VALUE self, VALUE other)
{
BIGNUM *bn1, *bn2;
rb_check_frozen(self);
if (self == other) return self;
GetBN(self, bn1);
bn2 = GetBNPtr(other);
if (!BN_copy(bn1, bn2)) {
ossl_raise(eBNError, NULL);
}
return self;
}
#eql?(obj) ⇒ Boolean
Returns true only if obj is a BN with the same value as bn. Contrast this with #==, which performs type conversions.
# File 'ext/openssl/ossl_bn.c', line 957
static VALUE
ossl_bn_eql(VALUE self, VALUE other)
{
BIGNUM *bn1, *bn2;
if (!rb_obj_is_kind_of(other, cBN))
return Qfalse;
GetBN(self, bn1);
GetBN(other, bn2);
return BN_cmp(bn1, bn2) ? Qfalse : Qtrue;
}
#gcd(bn2) ⇒ BN
#hash ⇒ Integer
Returns a hash code for this object.
See also Object#hash.
# File 'ext/openssl/ossl_bn.c', line 978
static VALUE
ossl_bn_hash(VALUE self)
{
BIGNUM *bn;
VALUE tmp, hash;
unsigned char *buf;
int len;
GetBN(self, bn);
len = BN_num_bytes(bn);
buf = ALLOCV(tmp, len);
if (BN_bn2bin(bn, buf) != len) {
ALLOCV_END(tmp);
ossl_raise(eBNError, "BN_bn2bin");
}
hash = ST2FIX(rb_memhash(buf, len));
ALLOCV_END(tmp);
return hash;
}
Alias for #copy.
#lshift!(bits) ⇒ self
#mask_bits!
[ GitHub ]
#mod_add(bn1, bn2) ⇒ BN
#mod_exp(bn1, bn2) ⇒ BN
#mod_inverse(bn2) ⇒ BN
#mod_mul(bn1, bn2) ⇒ BN
#mod_sqr(bn2) ⇒ BN
#mod_sub(bn1, bn2) ⇒ BN
#num_bits ⇒ Integer
#num_bytes ⇒ Integer
#pretty_print(q)
[ GitHub ]
#prime? ⇒ Boolean
#prime?(checks) ⇒ Boolean
Boolean
#prime?(checks) ⇒ Boolean
Performs a Miller-Rabin probabilistic primality test with checks iterations. If checks is not specified, a number of iterations is used that yields a false positive rate of at most 2^-80 for random input.
Parameters
-
checks - integer
# File 'ext/openssl/ossl_bn.c', line 1012
static VALUE
ossl_bn_is_prime(int argc, VALUE *argv, VALUE self)
{
BIGNUM *bn;
VALUE vchecks;
int checks = BN_prime_checks;
if (rb_scan_args(argc, argv, "01", &vchecks) == 1) {
checks = NUM2INT(vchecks);
}
GetBN(self, bn);
switch (BN_is_prime_ex(bn, checks, ossl_bn_ctx, NULL)) {
case 1:
return Qtrue;
case 0:
return Qfalse;
default:
ossl_raise(eBNError, NULL);
}
/* not reachable */
return Qnil;
}
#prime_fasttest? ⇒ Boolean
#prime_fasttest?(checks) ⇒ Boolean
#prime_fasttest?(checks, trial_div) ⇒ Boolean
Boolean
#prime_fasttest?(checks) ⇒ Boolean
#prime_fasttest?(checks, trial_div) ⇒ Boolean
Performs a Miller-Rabin primality test. This is same as #prime? except this first attempts trial divisions with some small primes.
Parameters
-
checks - integer
-
trial_div - boolean
# File 'ext/openssl/ossl_bn.c', line 1048
static VALUE
ossl_bn_is_prime_fasttest(int argc, VALUE *argv, VALUE self)
{
BIGNUM *bn;
VALUE vchecks, vtrivdiv;
int checks = BN_prime_checks, do_trial_division = 1;
rb_scan_args(argc, argv, "02", &vchecks, &vtrivdiv);
if (!NIL_P(vchecks)) {
checks = NUM2INT(vchecks);
}
GetBN(self, bn);
/* handle true/false */
if (vtrivdiv == Qfalse) {
do_trial_division = 0;
}
switch (BN_is_prime_fasttest_ex(bn, checks, ossl_bn_ctx, do_trial_division, NULL)) {
case 1:
return Qtrue;
case 0:
return Qfalse;
default:
ossl_raise(eBNError, NULL);
}
/* not reachable */
return Qnil;
}
#rshift!(bits) ⇒ self
#set_bit!(bit) ⇒ self
#sqr ⇒ BN
#to_bn
[ GitHub ]# File 'ext/openssl/ossl_bn.c', line 316
static VALUE
ossl_bn_to_bn(VALUE self)
{
return self;
}
#to_i ⇒ Integer Also known as: #to_int
# File 'ext/openssl/ossl_bn.c', line 298
static VALUE
ossl_bn_to_i(VALUE self)
{
BIGNUM *bn;
char *txt;
VALUE num;
GetBN(self, bn);
if (!(txt = BN_bn2hex(bn))) {
ossl_raise(eBNError, NULL);
}
num = rb_cstr_to_inum(txt, 16, Qtrue);
OPENSSL_free(txt);
return num;
}
Alias for #to_i.
#to_s ⇒ String
#to_s(base) ⇒ String
String
#to_s(base) ⇒ String
Parameters
-
base - ::Integer Valid values:
-
0 - MPI
-
2 - binary
-
10 - the default
-
16 - hex
-
# File 'ext/openssl/ossl_bn.c', line 254
static VALUE
ossl_bn_to_s(int argc, VALUE *argv, VALUE self)
{
BIGNUM *bn;
VALUE str, bs;
int base = 10, len;
char *buf;
if (rb_scan_args(argc, argv, "01", &bs) == 1) {
base = NUM2INT(bs);
}
GetBN(self, bn);
switch (base) {
case 0:
len = BN_bn2mpi(bn, NULL);
str = rb_str_new(0, len);
if (BN_bn2mpi(bn, (unsigned char *)RSTRING_PTR(str)) != len)
ossl_raise(eBNError, NULL);
break;
case 2:
len = BN_num_bytes(bn);
str = rb_str_new(0, len);
if (BN_bn2bin(bn, (unsigned char *)RSTRING_PTR(str)) != len)
ossl_raise(eBNError, NULL);
break;
case 10:
if (!(buf = BN_bn2dec(bn))) ossl_raise(eBNError, NULL);
str = ossl_buf2str(buf, rb_long2int(strlen(buf)));
break;
case 16:
if (!(buf = BN_bn2hex(bn))) ossl_raise(eBNError, NULL);
str = ossl_buf2str(buf, rb_long2int(strlen(buf)));
break;
default:
ossl_raise(rb_eArgError, "invalid radix %d", base);
}
return str;
}