BigDigits C++ OOP  1.0.1
The C++ OOP interface to the BigDigits library.

Introduction

This is the C++ object-oriented interface to the BigDigits library. It requires C++11 or later. It uses no external libraries except the standard ones.

The core is the BigDigit class which can represent large non-negative integers up to the memory limits of your machine.

This C++ OOP code is a wrapper around the original ANSI C BigDigits "bd" code. It uses operator overloading, which replaces all the "bd" functions which added, shifted or compared BIGD values with C++ overloaded operators, all with their original meaning.

+ - * / % += -= *= /= %= & &= | |= << >> <<= >>= ~ ++ -- < <= == >= >

In general, the BigDigit methods do not modify the object's value, except for those methods beginning with "set".

Memory for BigDigit objects is automatically freed, unless the program is aborted.

The BigDigits library is provided free of charge under a MPL-2.0 license. You can download it from https://di-mgt.com.au/bigdigits.html.

Using

Embed the header for dibigd.hpp in your main file. Set the values of BigDigit objects using the constructor form, a direct assign, or the from_str() function.

#include "dibigd.hpp"
using namespace dibigd;
BigDigit a(666); // Initialize with constructor
a.print("a="); // a=666
BigDigit b = 333; // Assignment
b.print("b="); // b=333
BigDigit c; // Default value zero
c.print("c="); // 0
c = a + b;
c.print("c="); // c=999
// Passing large integer constants > ULONG_MAX gives a compiler error, e.g.
// BigDigit E = 0xdeabdeafcafebabe0001; // error C2177: constant too big
// So pass large integer constants as a string
BigDigit A("0xdeabdeafcafebabe0001");
A.printhex("A=");
BigDigit B = "1051535798700858318651393";
B.printhex("B=");
// Test A equal to B
cout << std::boolalpha << "(A==B) is " << (A == B) << endl; // true
// Chained assignment works
BigDigit x, y, z;
x = y = "0xdeabdeafcafebabe0001";
x.printhex("x="); y.printhex("y=");
assert(x == y);
z = x * y;
z.printhex("z=x*y=");
// This works, too
(x * y).printhex("x*y= ");
// 0xc1ae899c24a058f7f6f19146aea03b01757c0001
// Mini-RSA computation
BigDigit p, q;
// Two 128-bit primes we know work
p = "0xb8d1148a26f846d45a1e3a7838d15679";
q = "0xf306941e24049c27bb2bed5043de9939";
p.printhex("p="); q.printhex("q=");
BigDigit n = p * q;
n.printhex("n=");
BigDigit e = 0x10001;
BigDigit phi = (p - 1) * (q - 1);
assert(phi.gcd(e) == 1); // Required for RSA to work
// Encrypt random message
BigDigit m = BigDigit().set_rand_bits(100);
m.printhex("m= ");
// ciphertext, c = m^e mod n
BigDigit ct = m.mod_exp(e, n);
ct.printhex("ct=");
// Decrypt with private key, d
// d = e^-1 mod phi
BigDigit d = e.mod_inv(phi);
// decrypted m' = c^d mod n
BigDigit dt = ct.mod_exp(d, n);
dt.printhex("dt=");
cout << std::boolalpha << "(dt==m) is " << (dt == m) << endl; // true
assert(dt == m);
The C++ OOP interface to the BigDigits library.

Exceptions

The only exceptions thrown are std::runtime_error when

  • A result using subtraction becomes negative; or
  • A number is divided by zero

and std::invalid_argument if an invalid character is found when using dibigd::from_str() or dibigd::Bvec::from_hex().

Compiling

To compile your source code, say, mycode.cpp, have the following files present when compiling

mycode.cpp
dibigd.cpp
dibigd.hpp
bigdall.c
bigdall.h

The files bigdall.c and bigdall.h are a convenient merge of all the separate source and include files needed for the ANSI C BigDigits "bd" interface.

// mycode.cpp
#include <iostream>
#include "dibigd.hpp"
using namespace dibigd;
int main() {
std::cout << "C++ interface version is " << dibigd::version_interface() << std::endl;
BigDigit p = "0xfffffffffffffffffffffffffffffffeffffffffffffffff";
p.printhex("p=");
std::cout << "p is " << (p.is_prime() ? "prime" : "NOT prime") << std::endl; // p is prime
}
std::string version_interface()
Return version number of this C++ interface.

Using the Visual Studio IDE, make sure you use the Default compile language option, so both .c and .cpp files compile properly.

Properties > Configuration Properties > C/C++ > Advanced > Compile As = Default

Using the MSVC command line

CL /DNO_WINGUI /EHsc mycode.cpp dibigd.cpp bigdall.c

Use /DNO_WINGUI to avoid having to link to user32.lib, and /EHsc to enable C++ exceptions. Do not use either of the /TC or /TP compile flags.

Using gcc, compile the .c file separately using gcc and then use g++ to compile the .cpp sources.

gcc -c bigdall.c
g++ mycode.cpp dibigd.cpp bigdall.o -o mycode

Byte vectors and BigDigits

Utilities are provided to convert between the BigDigit integer type and an array of bytes (octets), stored in std::vector<unsigned char>.

BigDigit b(0xdeadbeef);
std::vector<unsigned char> barr = b.to_octets();
Bvec::print_hex(barr); // de ad be ef
// Specify exact length
barr = b.to_octets(6);
Bvec::print_hex(barr); // 00 00 de ad be ef

The dibigd::BigDigit::to_octets method takes the bitstring representation of a BigDigit object in big-endian order (most significant bit first) and splits into 8-bit octets reading left to right. The number of octets to be decoded can be specified to ensure leading zero octets are included. dibigd::BigDigit::from_octets does the reverse, but leading zero octets are ignored.

Note the significance of leading zeros when decoding from a string representation.

// When decoding from a string to a BigDigit
// the leading zeros are ignored.
BigDigit a("0xDEADBEEF");
BigDigit b("0x000000DEADBEEF");
a.printhex("a="); // a=0xdeadbeef
b.printhex("b="); // b=0xdeadbeef
cout << "(a==b)=" << bool2str(a == b) << endl; // true
// But when decoding to a Bvec, the leading zeros matter
std::vector<unsigned char> bvA = Bvec::from_hex("0xDEADBEEF");
std::vector<unsigned char> bvB = Bvec::from_hex("0x000000DEADBEEF");
Bvec::print_hex("bvA=", bvA, ":"); // bvA=de:ad:be:ef
Bvec::print_hex("bvB=", bvB, ":"); // bvB=00:00:00:de:ad:be:ef
cout << "(bvA==bvB)=" << bool2str(bvA == bvB) << endl; // false
// Decode Bvec back to a BigDigit, leading zero octets ignored
BigDigit c = from_octets(bvB); // bvB=00:00:00:de:ad:be:ef
c.printhex("from_octets(bvB)="); // from_octets(bvB)=0xdeadbeef
BigDigit from_octets(const std::vector< unsigned char > bv)
Convert a byte array to an equivalent BigDigit.
Copyright © 2026 D.I. Management Services Pty Limited ABN 78 083 210 584 Australia. All rights reserved. <https://di-mgt.com.au/bigdigits.html> Generated on Tue May 5 2026 15:15:13 by Doxygen 1.9.1.