BigDigits C++ OOP Interface
This is the C++ object-oriented interface to our BigDigits library. It requires C++11 or later. It uses no external libraries except the standard ones.
Introduction | Download | Manual | Compiling | Test code | Programmer's Notes | Revision history | Contact us
Updated version 1.0.1 released 5 May 2026.
See Latest changes
Introduction
The core is the BigDigit class which can represent large non-negative integers suitable for use in standard cryptographic computations, like RSA.
+ - * / % += -= *= /= %= & &= | |= << >> <<= >>= ~ ++ -- < <= == >= >
The BigDigit class has methods to do modular arithmetic and bit manipulations. It has an internal "pretty good" random number generator (RNG), with an option to call your own independent secure RNG.
Set the values of BigDigit objects using the constructor form, a direct assign, or the
from_str function.
You can pass an ordinary unsigned integer constant up the value of ULONG_MAX (0xffffffff). For larger numbers use a string.
Memory is automatically freed (unless the program is aborted).
#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);
See the test code for examples in how to use.
Download
dibigd-oop-1.0.1.zip (98 kB)Manual
Read the Documentation
Compiling
See Compiling.
Test code
- t_dibigdBasic.cpp
- Some basic tests using dibigd C++ OOP interface to BigDigits. [source]
- t_dibigdRSAKeygen.cpp
- Generate a new 1025-bit RSA key and test that RSA algorithm works on random data. [source]
- t_dibigdRSAEncrypt.cpp
- Encrypt data using RSA PKCS#1 v1.5 standard. [source]
- t_dibigdRSASign.cpp
- Sign data using RSA PKCS-1_5 standard . [source]
- t_dibigdRSACrack.cpp
- Crack RSA if used incorrectly to three recipients. [source]
- t_dibigdDSA.cpp
- EXAMPLE OF THE DSA from APPENDIX 5 FIPS PUB 186-2. [source]
- t_dibigdDHGen.cpp
- Generate Diffie-Hellman domain parameters (p,q,g) . [source]
- t_dibigdDHKeyExch.cpp
- Perform Diffie-Hellman key exchange. [source]
- t_dibigdPoly1305.cpp
- Compute a Poly1305 authentication tag. [source]
- t_dibigdRandomTests.cpp
- Test the internal random number generator and the prime generator. [source]
Programmer's Notes
We’ve never really been a big fan of C++
“C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off”
–Bjarne Stroustrup
but we got frustrated when making some new programs with the BigDigits ANSI C interface.
So we started experimenting with making a C++ OOP version, and, boy, were we impressed with what we found.
About half of the functions in the BigDigits ANSI C interface involved assignments, simple arithmetical operations,comparisons and bitwise operations. All of these can be replaced with operator overloads in C++, and very effectively once you’ve mastered how to do it (thanks ChatGPT for your help with this).
All remaining methods that operate on the C++ BigDigit
class do so without affecting the original object (except those that
begin with set_).
Clean up of memory allocation is done automatically.
The C++ code takes maybe 2-3% longer to run than the equivalent ANSI C “bd” code, but is much simpler to write. And it compiles blazingly fast.
Revision History
- Latest v1.0.1 (2026-05-05)
-
- Fixed constructors to allow direct assignment from a string, e.g.
instead of using theBigDigit b = "0xdeadbeefcafebabe";from_strfunction1.- This function is now strictly redundant, but we keep because it's a convenient link for the documentation to describe how the string parsing works.
- Updated test code files
t_dibigd*.cppto reflect this change.
- Fixed constructors to allow direct assignment from a string, e.g.
- Version 1.0.0 (2026-04-29)
- First release.
Contact us
To contact us or comment on this page, please send us a message.
This page first published 29 April 2026. Last updated 10 May 2026.

