File: bigint.yo

package info (click to toggle)
bobcat 6.02.02-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 13,960 kB
  • sloc: cpp: 18,954; fortran: 5,617; makefile: 2,787; sh: 659; perl: 401; ansic: 26
file content (758 lines) | stat: -rw-r--r-- 34,872 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
includefile(include/header)

COMMENT(manpage, section, releasedate, archive, short name)
manpage(FBB::BigInt)(3bobcat)(_CurYrs_)(libbobcat-dev__CurVers_)
                    (Big Integers)

manpagename(FBB::BigInt)(Arithmetic on Integers of Unlimited Size)

manpagesynopsis()
    bf(#include <bobcat/bigint>)nl()
    Linking option: tt(-lbobcat -lcrypto)

manpagedescription()

This class is defined as a wrapper class around the em(openSSL) tt(BN) series
of functions, offering members to perform arithmetic on integral values of
unlimited sizes. Members are offered to generate primes and to perform all
kinds of common arithmetic operations on tt(BigInt) objects. Also, conversions
to characters and standard numerical value types are offered.

Below, the phrase em(the object) may also refer to the object's value. The
context in which this occurs will make clear that the object's value rather
than the object as-is is referred to.

Various constructors accept tt(BIGNUM) arguments. Type tt(BIGNUM) is the type
containing an integer of unlimited precision as defined by OpenSSL.
tt(BIGNUM's) definition is
        verb(
    typedef struct bignum_st BIGNUM;

    struct bignum_st
    {
        BN_ULONG *d;    // Pointer to an array of 'BN_BITS2' bit chunks.
        int top;        // Index of last used d +1.
        // The next are internal book keeping for bn_expand.
        int dmax;       // Size of the d array.
        int neg;        // one if the number is negative
        int flags;
    };
        )

Signs of tt(BigInt) are handled in a special way. Whether a tt(BigInt) is
negative or positive is determined by its sign-flag, and not by a sign bit as
is the case with tt(int) typed values. Since tt(BigInt) values have unlimited
precision shifting values to the left won't change their signs.

Operators return either a reference to the current (modified) object or return
a tt(BigInt) object containing the computed value. The rule followed here was
to implement the operators analogously to the way the operators work on
tt(int) type values and variables. E.g., tt(operator+()) returns a tt(BigInt)
value whereas tt(operator+=()) returns a tt(BigInt &) reference.

All members modifying their objects return a reference to the current
(modified) object. All members not modifying the current object return a
tt(BigInt) object. If both members exists performing the same
functionality the name of the member returning a tt(BigInt) object ends
in a tt(c) (const) (e.g., tt(addMod) and tt(addModc)).

Almost all operators, members and constructors (except for the default
constructor) throw tt(Exception) exceptions on failure.

manpagesection(INHERITS FROM)
    -

manpagesection(TYPE)

    The class bf(BigInt) defines the type tt(Word), which is equal to the type
tt(BN_ULONG) used by tt(OpenSSL) to store integral values of unlimited
precision. A tt(Word) is an tt(unsigned long), which is, depending on the
architecture, usually 64 or 32 bits long.

manpagesection(ENUMERATIONS)
    bf(Msb)nl()
    This (most significant bit) enumeration is used when generating a
        cryptographically strong random number. Its values are:
    itemization(
    itb(MSB_UNKNOWN)
       The most significant bit may be 0 or 1.
    itb(MSB_IS_ONE)
        The most significant bit is guaranteed to be 1.
    itb(TOP_TWO_BITS_ONE)
        The two most significant bits are guaranteed to be 1, resulting in a
        product of two values each containing tt(nBits) having tt(2 * nBits)
        bits.
    )

    bf(Lsb)nl()
    This (least significant bit) enumeration is used when generating random
        numbers, ensuring that the resulting value is either odd or even.
    itemization(
    itb(EVEN)
        The random value will be an even value;
    itb(ODD)
        The random value will be an odd value.
    )

manpagesection(CONSTRUCTORS)

    itemization(
    itb(BigInt())
        The default constructor initializes a tt(BigInt) value to 0;
    itb(explicit BigInt(BIGNUM const &value))
        This constructor initializes a tt(BigInt) from a tt(const BIGNUM);
    itb(explicit BigInt(BIGNUM const *value))
        This constructor initializes a tt(BigInt) from a pointer to a
        tt(const BIGNUM);
    itb(explicit BigInt(BIGNUM *value)) This constructor initializes a
        tt(BigInt) from a pointer to a tt(BIGNUM) (the tt(BIGNUM) value
        pointed to by tt(value) is em(not) mondified by the constructor.
        This constructor is a mere wrapper around the previous
        constructor). Note that none of the constructors expecting a
        tt(BIGNUM) argument modify their argument. If the memory used by the
        tt(BIGNUM) argument must be returned to the common pool an explicit
        bf(BN_free)(3ssl) call is required;
    itb(BigInt(Type value))
       This constructor is defined as a member template. Any type that can be
        converted using a static cast to an tt(unsigned long) can be used as
        argument to this constructor. Promotion is allowed, so in many
        situations where tt(BigInt)s are expected a plain numerical value can
        be used as well;
    itb(BigInt(char const *bigEndian, size_t length, bool negative = false))
       This constructor initializes a tt(BigInt) from tt(length) big-endian
        encoded bytes stored in tt(bigEndian) (having its most significant
        value at index 0). This constructor interprets the tt(char) values
        pointed at by tt(bigEndian) as unsigned values. Use this constructor
        to reconstruct a tt(BigInt) object from the data made available by the
        tt(bigEndian) member (most significant byte at index 0). If the number
        represents a negative value, then provide a third argument tt(true);
    itb(explicit BigInt(std::string const &bigEndian, bool negative = false))
       This constructor initializes a tt(BigInt) from the bytes stored in
        tt(bigEndian), which must be big-endian encoded (having its most
        significant value at index 0). This constructor interprets the
        tt(char) values stored in tt(bigEndian) as unsigned values. If the
        number that is stored in tt(bigEndian) represents a negative value,
        then provide a second argument tt(true);
    itb(BigInt(size_t length, char const *littleEndian, bool negative = false))
       This constructor initializes a tt(BigInt) from tt(length) little-endian
        encoded bytes stored in tt(littleEndian) (having its least significant
        value at index 0). This constructor interprets the tt(char)
        values pointed at by tt(littleEndian) as unsigned values. Use this
        constructor to reconstruct a tt(BigInt) object from the data made
        available by the tt(littleEndian) member (most significant byte at
        index 0). If the number represents a negative value, then provide a
        third argument tt(true);
    itb(explicit BigInt(BigInt::Little endian, std::string littleEndian, bool
        negative = false))
       This constructor initializes a tt(BigInt) from the bytes stored in
        tt(littleEndian), which must be little-endian encoded (having its
        least significant value at index 0). This constructor interprets the
        tt(char) values stored in tt(littleEndian) as unsigned values. If the
        number that is stored in tt(littleEndian) represents a negative value,
        then provide a third argument tt(true). The consructor's first
        parameter is used to distinguish this constructor from the constructor
        expecting a t(string) whose bytes represent a big-endian encoded
        value, and is not used by this constructor itself. It can be specified
        as tt(BigInt::Little{}).
    )

    Copy and move constructors (and assignment operators) are available.

manpagesection(MEMBER FUNCTIONS)
    itemization(
    itb(BigInt &addMod(BigInt const &rhs, BigInt const &mod) )
       tt(Rhs) is added (modulo tt(mod)) to the current object;

    itb(BigInt addModc(BigInt const &rhs, BigInt const &mod) )
       The sum (modulo tt(mod)) of the current object and tt(rhs) is returned;

    itb(BigInt::Word at(size_t index) const)
       Returns the tt(Word) at tt(index). E.g., on a 32 bit architecture, if
        the bf(BigInt) value equals 2+sups(33), then tt(at(0)) returns 0, and
        tt(at(1)) returns 2. If tt(index) equals or exceeds the value returned
        by tt(nWords) an tt(FBB::Exception) is thrown;

    itb(BIGNUM const &bignum() const)
       A reference to the tt(BIGNUM) value maintained by the current
        tt(BigInt) object is returned;

    itb(char *bigEndian() const)
       The value represented by the current object is stored in a series of
        tt(char) typed values in big-endian order. If a value consists of 5
        tt(char)s the eight most significant bits will be stored in the
        tt(char) having index value 0, the eight least significant bits will
        be stored in the tt(char) having index value 4. When needed simply
        swap tt(char[i]) with tt(char[j]) (i = 0 .. nBytes/2, j = nBytes-1
        .. nBytes/2) to convert to little-endian order or use the member
        tt(littleEndian) to receive the representation in little-endian
        order. The return value consists of a series of tt(sizeInBytes()) (see
        below) dynamically allocated tt(char) values. The caller of
        tt(bigEndian) owns the allocated memory and should eventually delete
        it again using tt(delete[]). Note that the current object's em(sign)
        cannot be inferred from the return value;

    itb(BigInt &clearBit(size_t index))
       The current object's bit at index position tt(index) is cleared;

    itb(BigInt clearBit(size_t index) const)
       A copy of the current object having its bit at index position
        tt(index) cleared;

    itb(BigInt &div(BigInt *remainder, BigInt const &rhs))
       The current object is divided by tt(rhs). The division's remainder
        is returned in tt(*remainder);

    itb(BigInt divc(BigInt *remainder, BigInt const &rhs) const)
       The quotient of the current object and tt(rhs) is returned. The
        division's remainder is returned in tt(*remainder);

    itb(int compare(BigInt const &rsh) const)
       Using signed values, if the current object is smaller than tt(rhs) -1
        is returned; if they are equal 0 is returned; if the current object is
        larger than tt(ths) 1 is returned (see also tt(uCompare));

    itb(BigInt &exp(BigInt const &exponent))
       The current object is raised to the power tt(exponent);

    itb(BigInt expc(BigInt const &exponent) const)
       The current object raised to the power tt(exponent) is returned;

    itb(BigInt &expMod(BigInt const &exponent, BigInt const &mod))
       The current object is raised to the power tt(exponent) modulo tt(mod);

    itb(BigInt expModc(BigInt const &exponent, BigInt const &mod) const)
       The current object raised to the power tt(exponent) modulo tt(mod) is
        returned;

    itb(BigInt &gcd(BigInt const &rhs))
       The greatest common divisor (gcd) of the current object and tt(rhs) is
        assigned to the current object. To compute the least common multiple
        (lcm) the following relationship can be used:
       verb(
    lcm(a, b) = a * b / a.gcd(b)
        )

    itb(BigInt gcdc(BigInt const &rhs) const)
       The greatest common divisor (gcd) of the current object and tt(rhs) is
        returned. To compute the least common multiple (lcm) the following
        relationship can be used:
       verb(
    lcm(a, b) = a * b / a.gcd(b)
        )

    itb(bool hasBit(size_t index))
        tt(True) is returned if the bit at index position tt(index) has been
        set, tt(false) otherwise;

    itb(BigInt &inverseMod(BigInt const &mod))
       The inverse of the current object modulo tt(mod) is assigned to the
        current object. This is the value tt(ret) for which the following
        expression holds true:
       verb(
        (*this * ret) % mod = 1
       )

    itb(BigInt inverseModc(BigInt const &mod) const)
       This  inverse of the current object modulo tt(mod) is returned;

    itb(bool isNegative() const)
       Returns tt(true) if the current object contains a negative value;

    itb(bool isOdd() const)
       Returns tt(true) if the current object is an odd value;

    itb(bool isOne() const)
       Returns tt(true) if the current object equals one (1);

    itb(BigInt &isqrt())
       The current object's integer square root value is assigned to the
        current object. The integer square root of a value tt(x) is the
        biggest integral value whose square does not exceed tt(x). E.g.,
        tt(isqrt(17) == 4). An tt(Exception) exception is thrown if the current
        object's value is smaller than one;

    itb(BigInt isqrtc() const)
       The integer square root of the current object is returned. An
        tt(Exception) exception is thrown if the current object's value is
        smaller than one;

    itb(bool isZero() const)
       Returns tt(true) if the current object equals zero (0);

    itb(char *littleEndian() const)
       The value represented by the current object is stored in a series of
        tt(char) typed values in little-endian order. If a value consists of 5
        tt(char)s the eight least significant bits will be stored in the
        tt(char) having index value 0. To receive the bytes in big-endian
        order the member tt(bigEndian) can be used. The return value consists
        of a series of tt(sizeInBytes()) (see below) dynamically allocated
        tt(char) values. The caller of tt(littleEndian) owns the allocated
        memory and should eventually delete it again using tt(delete[]). Note
        that the current object's em(sign) cannot be inferred from the return
        value;

    itb(BigInt &lshift())
       The current object's bits are shifted one bit to the left. The object's
        sign remains unaltered;

    itb(BigInt lshiftc())
       The current object's bits shifted one bit to the left are returned. The
        object's sign will be equal to the current object's sign;

    itb(BigInt &lshift(size_t nBits))
       The current object's bits are shifted tt(nBits) to the left. The
        object's sign remains unaltered;

    itb(BigInt lshiftc(size_t nBits) const)
       The current object's bits shifted tt(nBits) bit to the left are
        returned. The object's sign will be equal to the current object's
        sign;

    itb(BigInt &maskBits(size_t lowerNBits))
        The  current object's tt(lowerNBits) lower bits are kept, its
        higher order bits are cleared. The object's sign is not affected;

    itb(BigInt maskBitsc(size_t lowerNBits) const)
       A copy of the current object is returned having all but its
        tt(lowerNBits) lower bits cleared. The sign of the returned object
        will be equal to the current object's sign;

    itb(size_t maxWordIndex() const)
       Returns the maximum tt(Word)-index that can be used with the tt(at)
        and tt(setWord) members for the current bf(BigInt) value;

    itb(BigInt &mulMod(BigInt const &rhs, BigInt const &mod))
       The current object is multiplied  (modulo tt(mod)) by tt(rhs);

    itb(BigInt mulModc(BigInt const &rhs, BigInt const &mod) const)
       The current object multiplied (modulo tt(mod)) by tt(rhs) is returned;

    itb(BigInt &negate())
       The current object's value is negated (i.e., the value changes its
        sign);

    itb(BigInt negatec() const)
       The  negated value of the current object is returned;

    itb(size_t nWords() const)
       The number of `words' required to store the bf(BigInt) value is
        returned. Note that the returned value depends on the architecture's
        number of bytes per word. For 32-bit architectures there are four
        bytes per word, for 64-bit architectures eight bytes per word;

    itb(BigInt &rshift())
       The current object's bits are shifted one bit to the right. The object's
        sign remains unaltered;

    itb(BigInt rshiftc())
       The current object's bits shifted one bit to the right are returned. The
        object's sign will be equal to the current object's sign;

    itb(BigInt &rshift(size_t nBits))
       The current object's bits are shifted tt(nBits) to the right. The
        object's sign remains unaltered;

    itb(BigInt rshiftc(size_t nBits) const)
       The current object's bits shifted tt(nBits) bit to the right are
        returned. The object's sign will be equal to the current object's
        sign;

    itb(BigInt &setBit(size_t index))
       The bit at index position tt(index) is set;

    itb(BigInt setBitc(size_t index) const)
       A copy of the current object is returned having its bit at index
        position tt(index) set;

    itb(BigInt &setBit(size_t index, bool value))
       The bit at index position tt(index) is set to tt(value);

    itb(BigInt setBitc(size_t index, bool value) const)
       A copy of the current object is returned having its bit at index
        position tt(index) set to tt(value);

    itb(BigInt &setNegative(bool negative))
       The current object's sign will be set to negative if the function's
        argument is tt(true), it will be set to positive if the function's
        argument is tt(false);

    itb(BigInt setNegativec(bool negative) const)
       A copy of the current object is return having a negative sign if the
        function's argument is tt(true) and a positive sign if the function's
        argument is tt(false);

    itb(void setWord(size_t index, BigInt::Word value))
       Assigns tt(value) to the tt(Word) at tt(index). E.g., on a 32 bit
        architecture, if the bf(BigInt) value equals 2+sups(33), then
        after tt(setWord(1, 1)) the value has become 2+sups(32). If tt(index)
        exceeds the value returned by tt(nWords) an tt(FBB::Exception) is
        thrown;

    itb(size_t size() const)
       The number of significant em(bits) required to store the current
        tt(BIGNUM) value is returned;


    itb(size_t sizeInBytes() const)
       The number of bytes required to store the current tt(BIGNUM) value is
        returned;

    itb(size_t constexpr sizeOfWord() const)
       bf(BigInt) values are stored in units of `words', which are unsigned
        long values. These values may consist of, e.g., 32 or 64 bits. The
        number of bytes occupied by a `word' is returned: 4 for a 32 bit
        value, 8 for a 64 bit value, and possibly other values, depending on
        specific architecture peculiarities. The value returned by this
        member, therefore, is architecture dependent;

    itb(BigInt &sqr())
      The current object's value is squared;

    itb(BigInt sqrc() const)
      The square of the current object is returned;

    itb(BigInt &sqrMod(BigInt const &mod) const)
      The current object's value is squared modulo tt(mod);

    itb(BigInt sqrModc(BigInt const &mod) const)
      The square (modulo tt(mod)) of the current object is returned;

    itb(BigInt &subMod(BigInt const &rhs, BigInt const &mod))
       tt(Rhs) is subtracted modulo tt(mod) from the current object;

    itb(BigInt subModc(BigInt const &rhs, BigInt const &mod) const)
      The difference (modulo tt(mod)) of the current object and tt(rhs) is
        returned;

    itb(void swap(BigInt &other))
       The current object swaps its value with tt(other);

    itb(BigInt &tildeBits())
       All the bits in
        the bytes of the current object and the sign of the current object
        are toggled.
       So, after
            verb(
        Bigint b(5);
        b.tildeBits();
            )
       tt(b) contains the value -250. Also see the discussion with
        tt(operator~()) below;

    itb(BigInt tildeBitsc() const)
       A copy of the current object whose bits are toggled is returned;

    itb(BigInt &tildeInt())
       The `tilde' operation is performed on the current object using the
        standard tt(int) semantics. E.g., ~5 results in -6.  Also see the
        discussion with tt(operator~()) below;

    itb(BigInt tildeIntc() const)
       A copy of the current object is returned to which the `tilde' operation
        has been performed using the standard tt(int) semantics;

    itb(unsigned long ulong() const)
       The absolute value stored in the current object is returned as an
        unsigned long. If it cannot be represented by an unsigned long it
        returns tt(0xffffffffL);

    itb(int uCompare(BigInt const &rsh) const)
       Using absolute values, if the current object is smaller than tt(rhs) -1
        is returned; if they are equal 0 is returned; if the current object is
        larger than tt(ths) 1 is returned (see also tt(uCompare)).
    )

manpagesection(OVERLOADED OPERATORS)

Except for some operators all operators perform their intuitive
operations. Where that isn't completely true an explanatory remark is
provided. E.g., tt(operator*()) multiplies two tt(BigInt)s, possibly promoting
one of the operands; tt(operator*=()) multiplies the lhs by the rhs
tt(BigInt), possibly promoting the rhs operand.

    Here are the available operators:

    bf(Unary operators:)

    itemization(
    itb(bool operator bool() const)
       Returns tt(true) if the tt(BigInt) value is unequal zero, otherwise
        tt(false) is returned;

    itb(BigInt &operator++())
       Unary prefix increment operator;
    itb(BigInt operator++(int))
       Unary postfix increment operator;
    itb(BigInt &operator--())
       Unary prefix decrement operator;
    itb(BigInt operator--(int))
       Unary postfix decrement operator;
    itb(BigInt operator-())
       Unary negation operator;
    itb(int operator[](size_t idx) const)
       With tt(BigInt) objects it returns the bit-value of the object's
        tt(idx)th bit as the value 0 or 1;

    itb(BigInt::Bit operator[](size_t idx))
       With non-const tt(BigInt) objects it returns a reference to the
        bit-value of the object's tt(idx)th bit. When used as em(lvalue)
        assigning a 0 or non-zero value to the operator's return value will
        either clear or set the bit.  Likewise, the following arithmetic
        assignment operators may be used: binary or (tt(|=)), binary and
        (tt(&=)) or binary xor (tt(^=)). When used as em(rvalue) the value of
        the object's tt(idx)th bit is returned as a tt(bool) value. When
        inseerted into a tt(std::ostream) the bit's value is displayed as 0 or
        1;

    itb(BigInt operator~())
       This operator is em(not) implemented as it cannot be implemented so
        that it matches the actions of this operator when applied to tt(int)
        type values;

        When used on tt(int) values this operator toggles all the tt(int)'s
        bits. E.g., ~5 represents -6, and ~-6 again equals five. The -6 is the
        result of the sign bit of tt(int) values. The obvious implementation
        of tt(BigInt::operator~()) is to toggle all the value's bits and to
        toggle its sign bit. For 5 this would result in -250: 5, being 101
        (binary), fits in one byte, so ~5 becomes 11111010 (binary), which is
        250. Its sign must be reversed as well, so it becomes -250.  This
        clearly differs from the value represented by the tt(int) constant ~5:
        when constructing tt(BigInt(~5)), the value -6 is obtained.

       It is possible to change the implementation. E.g., after
            verb(
        Bigint b(5);
        b = ~b;
            )
       tt(~b) could be implemented so that it results in the value -6. But
        this too leads to unexpected results. While tt(5 & ~5 == 0), this
        would no longer hold true for tt(BigInt) objects: Assuming tt(b)
        contains 5 then tt(b & ~b) would expand to (binary) tt(101 &
        (negative)110) which equals (binary) 100;

       Since either implementation produces unexpected results
        tt(BigInt::operator~()) was not implemented. Instead two members are
        offered: tt(tildeBits()), toggling all the bits of all the
       tt(BigInt) bytes and toggling its sign (so
            verb(
        Bigint b(5);
        b.tildeBits();
            )
       changes tt(b)'s value into -250), and tt(tildeInt()) changing the
        object's value into the value that would have been obtained if a
       tt(BigInt) was a mere tt(int) (so
        verb(
        Bigint b(5);
        b.tildeInt();
            )
       changes tt(b)'s value into -6).
    )

    bf(Binary operators:)

    itemization(
    itb(BigInt operator*(BigInt const &lhs, BigInt const &rhs))
    itb(BigInt operator/(BigInt const &lhs, BigInt const &rhs))
       This operator returns the quotient of the tt(lhs) object divided by the
        tt(rhs) object. The remainder is lost (The member tt(div) performs the
        division and makes the remainder available as well);
    itb(BigInt operator%(BigInt const &lhs, BigInt const &rhs))
    itb(BigInt operator+(BigInt const &lhs, BigInt const &rhs))
    itb(BigInt operator-(BigInt const &lhs, BigInt const &rhs))
    itb(BigInt operator<<(BigInt const &lhs, size_t nBits))
        See also the tt(lshift) member;
    itb(BigInt operator>>=(BigInt const &lhs, size_t nBits))
       See also the tt(rshift) member;
    itb(BigInt operator&(BigInt const &lhs, BigInt const &rhs))
        This operator returns a tt(BigInt) value consisting of the
        tt(bit_and)-ed bits and sign flags of lhs and rhs
        operands. Consequently, if one operand is positive, the resulting
        value will be positive;
    itb(BigInt operator|(BigInt const &lhs, BigInt const &rhs))
        This operator returns a tt(BigInt) value consisting of the
        tt(bit_or)-ed bits and sign flags of lhs and rhs
        operands. Consequently, if either operand is negative, the result will
        be negative;
    itb(BigInt operator^(BigInt const &lhs, BigInt const &rhs))
        This operator returns a tt(BigInt) value consisting of the
        tt(bit_xor)-ed bits and sign flags of lhs and rhs
        operands. Consequently, if exactly one operand is negative, the result
        will be negative.
    )

    bf((Arithmetic) assignment operator(s):)

    itemization(
    itb(BigInt &operator*=(BigInt const &rhs))
    itb(BigInt &operator/=(BigInt const &rhs))
       This operator assigns the result of the (integer) division of the current
        tt(BigInt) object by tt(ths) to the current object. The remainder is
        lost. The member tt(div) divides and makes the remainder available as
        well;
    itb(BigInt &operator%=(BigInt const &rhs))
    itb(BigInt &operator+=(BigInt const &rhs))
    itb(BigInt &operator-=(BigInt const &rhs))
    itb(BigInt &operator<<=(size_t nBits))
        See also the tt(lshift) members;
    itb(BigInt &operator>>=(size_t nBits))
        See also the tt(rshift) members;
    itb(BigInt &operator&=(BigInt const &rhs))
        This operator tt(bit_and)s the bits and sign flags of the current
        object and the rhs operand;
    itb(BigInt &operator|=(BigInt const &rhs))
        This operator tt(bit_or)s the bits and sign flags of the current
        object and the rhs operand;
    itb(BigInt &operator^=(BigInt const &rhs))
        This operator tt(bit_xor)s the bits and sign flags of the current
        object and the rhs operand.
    )

manpagesection(STATIC MEMBERS)

    All members returning a tt(BigInt) computed from a set of arguments and
not requiring an existing tt(BigInt) object are defined as static members.
The first tt(diophantus) member, returning a tt(long long) value, also is a
static member.

    itemization(
    itb(long long diophantus(long long *factor1, long long *factor2,
                             long long value1, long long value2))
       The integral solution of tt(factor1 * value1 + factor2 * value2 = gcd)
        is computed. The function returns the greatest common divisor
        (tt(gcd)) of tt(value1) and tt(value2), and returns their
        multiplication factors in, respectively, tt(*factor1) and
        tt(*factor2). The solution is not unique: another solution is obtained
        by adding tt(k * value2) to tt(factor1) and subtracting tt(k * value1)
        from tt(factor2). For values exceeding tt(std::numeric_limits<long,
        long>::max()) the next member can be used;

    itb(BigInt diophantus(BigInt *factor1, BigInt *factor2,
                            BigInt const &value1, BigInt const &value2))
       The integral solution of tt(factor1 * value1 + factor2 * value2 = gcd)
        is computed. The function returns the greatest common divisor
        (tt(gcd)) of tt(value1) and tt(value2), and returns their
        multiplication factors in, respectively, tt(*factor1) and
        tt(*factor2). The solution is not unique: another solution is obtained
        by adding tt(k * value2) to tt(factor1) and subtracting tt(k * value1)
        from tt(factor2);


    itb(BigInt fromText(std::string text, int mode = 0))
       This member converts a textual representation of a number to a
        tt(BigInt) value. Conversion continues until the end of tt(text) or
        until a character outside of an expected range is encountered;

       The expected range may be preset by specifying tt(mode) as tt(ios::dec,
        ios::oct,) or tt(ios::hex) or (the default) the expected range is
        determined by tt(fromText) itself by inspecting the characters in
        tt(text).

        By default if tt(text) contains hexadecimal characters then
        tt(fromText) assumes that the number is represented as a hexadecimal
        value (e.g., tt("abc") is converted to the (decimal) value 2748); if
        tt(text) starts with 0 and contains only characters in the range 0
        until (including) 7 then tt(fromText) assumes the number is
        represented as an octal value (e.g., tt("01234") is converted to the
        (decimal) value 668). Otherwise a decimal value is assumed.

        If the text does not represent a valid numerical value (of the
        given extraction mode) then a tt(FBB::Exception) exception is thrown
        (tt(fromText: text does not represent a BigInt value));

    itb(BigInt rand(size_t size, Msb msb = MSB_IS_ONE, Lsb lsb = ODD))
       This member returns a cryptographically strong pseudo-random number
        of tt(size) bits. The most significant bit(s) can be controlled by
        tt(msb) (by default bf(MSB_IS_ONE)), the least significant bit can be
        controlled by tt(lsb) (by default bf(ODD)). Before calling this
        member for real the random number generator must have been seeded.

        From the bf(RAND_add)(3ssl) man-page:

       OpenSSL makes sure that the PRNG state is unique for each thread. On
        systems that provide tt(/dev/urandom), the randomness device is used
        to seed the PRNG transparently. However, on all other systems, the
        application is responsible for seeding the PRNG by calling
        bf(RAND_add)(3ssl), bf(RAND_egd)(3ssl), bf(RAND_load_file)(3ssl), or
        bf(RAND_seed)(3ssl);

    itb(BigInt randRange(BigInt const &max))
       This member returns a cryptographically strong pseudo-random number in
        the range tt(0 <= number < max). Before calling this member for real
        the random number generator must have been seeded (see also bf(rand),
        described above);

    itb(BigInt setBigEndian(std::string const &bytes))
       The tt(bytes.length()) bytes of tt(bytes) are used to compute a
        tt(BigInt) object which is returned by this function. The characters
        in tt(bytes) are interpreted as a series of bytes in big-endian
        order. See also the member function tt(bigEndian()) above. The
        returned tt(BigInt) has a positive value;

    itb(BigInt prime(size_t nBits,
                            BigInt const *mod = 0, BigInt const *rem = 0,
                            PrimeType primeType = ANY))
       This member returns a prime number of tt(bBits) bits. If both tt(mod)
        and tt(rem) are non-zero, the condition prime % mod == rem.
        (E.g., use tt(prime % mod == 1) in order to suit a given
        generator). The parameter tt(primeType) can be tt(ANY), tt((prime - 1)
        / 2) may or may not be a prime. If it is tt(SAFE) then  tt((prime - 1)
        / 2) will be a (so-called em(safe)) prime;

    itb(BigInt pseudoRand(size_t size, Msb msb = MSB_IS_ONE, Lsb lsb =
            ODD))
       This member merely calls tt(BigInt::rand);

    itb(BigInt pseudoRandRange(BigInt const &max))
       This member merely calls tt(BigInt::randRange).
    )

manpagesection(FREE FUNCTIONS IN THE FBB NAMESPACE)

    itemization(
    itb(std::ostream &operator<<(ostream &out, BigInt const &value))
       Inserts tt(value) into the provided tt(ostream). If the tt(hex)
        manipulator has been inserted into the stream before inserting the
        tt(BigInt) value the value will be displayed as a hexadecimal value
        (without a leading tt(0x)); if the tt(oct) manipulator has been
        inserted the value will be represented as an octal value (starting
        with a 0). The value will be displayed as a decimal value if the
        tt(dec) manipulator is active. If the tt(BigInt) value is negative its
        value will be preceded by a minus character.

        This conversion isn't very fast. For faster conversion consider using
        the LDC class (cf. bf(ldc)(3bobcat)) in statements like
       verb(
BigInt value;           // contains a positive value
                        // insert value using decimal digits:
std::cout << LDC{ value };
        )

    itb(std::istream &operator>>(istream &in, BigInt &value))
       Extracts tt(value) from the provided tt(istream). Depending on the
        currently set extraction mode (tt(dec, oct,) or tt(hex)) the matching
        set of characters will be extracted from tt(in) and converted to a
        number which is stored in tt(value). Extraction stops at EOF or at the
        first character outside of the range of characters matching the
        extraction mode. if no numerical characters were extracted the
        stream's tt(failbit) is set. The extracted value may be preceded
        by a minus character, resulting in an extracted negative value.
    )

manpagesection(EXAMPLE)
        verbinclude(../../bigint/driver/example.cc)

manpagefiles()
    em(bobcat/bigint) - defines the class interface

manpageseealso()
    bf(bobcat)(7), bf(diffiehellman)(3bobcat), bf(ldc)(3bobcat),
    bf(RAND_add)(3ssl), bf(RAND_egd)(3ssl),
    bf(RAND_load_file)(3ssl), bf(RAND_seed)(3).

    For tt(BIGNUM):nl()
    tt(https://www.openssl.org/docs/man1.0.2/man3/bn_sub_words.html)

manpagebugs()
    None Reported.

includefile(include/trailer)