Skip to content

Commit 6fae2bd

Browse files
committed
bn: update documentation of OpenSSL::BN#initialize and #to_s
Clarify that BN.new(str, 2) and bn.to_s(2) handles binary string in big-endian, and the sign of the bignum is ignored. Reference: #431
1 parent 485a6d6 commit 6fae2bd

File tree

1 file changed

+36
-14
lines changed

1 file changed

+36
-14
lines changed

ext/openssl/ossl_bn.c

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -223,12 +223,29 @@ ossl_bn_alloc(VALUE klass)
223223

224224
/*
225225
* call-seq:
226-
* OpenSSL::BN.new(bn) => aBN
227-
* OpenSSL::BN.new(integer) => aBN
228-
* OpenSSL::BN.new(string) => aBN
229-
* OpenSSL::BN.new(string, 0 | 2 | 10 | 16) => aBN
226+
* OpenSSL::BN.new(bn) -> aBN
227+
* OpenSSL::BN.new(integer) -> aBN
228+
* OpenSSL::BN.new(string, base = 10) -> aBN
230229
*
231-
* Construct a new OpenSSL BIGNUM object.
230+
* Construct a new \OpenSSL BIGNUM object.
231+
*
232+
* If +bn+ is an Integer or OpenSSL::BN, a new instance of OpenSSL::BN
233+
* representing the same value is returned. See also Integer#to_bn for the
234+
* short-hand.
235+
*
236+
* If a String is given, the content will be parsed according to +base+.
237+
*
238+
* +string+::
239+
* The string to be parsed.
240+
* +base+::
241+
* The format. Must be one of the following:
242+
* - +0+ - MPI format. See the man page BN_mpi2bn(3) for details.
243+
* - +2+ - Variable-length and big-endian binary encoding of a positive
244+
* number.
245+
* - +10+ - Decimal number representation, with a leading '-' for a negative
246+
* number.
247+
* - +16+ - Hexadeciaml number representation, with a leading '-' for a
248+
* negative number.
232249
*/
233250
static VALUE
234251
ossl_bn_initialize(int argc, VALUE *argv, VALUE self)
@@ -296,16 +313,21 @@ ossl_bn_initialize(int argc, VALUE *argv, VALUE self)
296313

297314
/*
298315
* call-seq:
299-
* bn.to_s => string
300-
* bn.to_s(base) => string
316+
* bn.to_s(base = 10) -> string
301317
*
302-
* === Parameters
303-
* * _base_ - Integer
304-
* Valid values:
305-
* * 0 - MPI
306-
* * 2 - binary
307-
* * 10 - the default
308-
* * 16 - hex
318+
* Returns the string representation of the bignum.
319+
*
320+
* BN.new can parse the encoded string to convert back into an OpenSSL::BN.
321+
*
322+
* +base+::
323+
* The format. Must be one of the following:
324+
* - +0+ - MPI format. See the man page BN_bn2mpi(3) for details.
325+
* - +2+ - Variable-length and big-endian binary encoding. The sign of
326+
* the bignum is ignored.
327+
* - +10+ - Decimal number representation, with a leading '-' for a negative
328+
* bignum.
329+
* - +16+ - Hexadeciaml number representation, with a leading '-' for a
330+
* negative bignum.
309331
*/
310332
static VALUE
311333
ossl_bn_to_s(int argc, VALUE *argv, VALUE self)

0 commit comments

Comments
 (0)