DI Management Home > BigDigits > bdcalc

bdcalc - a calculator for large natural numbers

bdcalc is a command-line calculator and mini-programming language that works with very large unsigned integers, the natural numbers ℕ = { 0,1,2,…} used to carry out computations in cryptography. The numbers can theoretically be of unlimited size.

bdcalc has built-in functions (see below) to carry out number theoretical computations such as modular exponentiation and modular inversion, as used in the RSA and Diffie-Hellman algorithms. You can generate random prime numbers of a given bit length and test for primality.

bdcalc provides a mini-programming environment with assignments to stored variables, conditional statements (if-then-else) and control loops (for, while, repeat). See the documentation for more details.

Recommended reading

Join Amazon Student FREE Two-Day Shipping for College Students

Affiliate disclosure: we get a small commission for purchases made through the above links

You can use bdcalc in interactive mode, typing commands on the console until you type quit to exit. Alternatively you can read input directly from a script file using the -file option; or you can enter your input in one line on the command line, perhaps as part of a batch file.

2016-06-17: Released new version 2.2 - see What's new
2016-07-26: Updated version 2.2.1 Download it now.

Example session | Built-in Functions | Example Scripts | Documentation | Download | What's new | Acknowledgements | Contact Us

Example session

bdcalc lets you assign numbers to variables, do arithmetic and call functions in the way you would expect.

> a=99
> b=a*11
> ? a+b
> println("a =",a,"b =",b)
a = 99 b = 1089
> p=genprime(128)
> !p
> bitlen(p)
> ??isprime(p)
> x=1;y=2;z=x+y;?z
> for i in (1..15) do print(prime(i), " ") done; println("")
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47

Built-in Functions

Function Returns Remarks
bitlen(X) the bit length l of the integer X 2l−1 ≤ x < 2l
bytelen(X) the length L of integer X in bytes  
cbrt(X) the truncated integer cube root of X 3√{x} ⎦
compl(X,n) the bitwise NOT of the rightmost n bits of X  
fillbytes(n,b) an integer of n bytes each set to b mod 256
gcd(X,Y,Z,...) the greatest common divisor of X,Y,Z,...
genprime(n) a random prime p of length exactly n bits 2n−1 ≤ p < 2n
getbit(X,n) value 0 or 1 of bit n of X =(bl−1…bn…b1b0)  
getbyte(X,n) value of byte n of X  
iif(B,X,Y) X if B is true, otherwise Y [Note 1]
isprime(X) true if X is prime, otherwise false [Note 2]
jacobi(X,M) the Jacobi symbol (X | M) in {0,1,2},  2 ⇒ −1 [Note 3]
max(X,Y,Z,...) the maximum of X,Y,Z,...
min(X,Y,Z,...) the minimum of X,Y,Z,...
modexp(X,Y,M) X raised to the power Y modulo M xy mod m
modinv(X,M) the inverse of X modulo M x−1 mod m
modmul(X,Y,M) X multiplied by Y modulo M xy mod m
modpowof2(X,n) X with all bits cleared at positions ≥ nx mod 2n
pow(X,n) X raised to the power n xn
prime(n) the n-th prime number 1 ≤ n ≤ 104
randbits(n) a random integer r of length at most n bits 0 ≤ r < 2n
random(M) an integer r generated at random from [0, M-1] 0 ≤ r < M
revbytes(X,[n]) X as an n-byte integer with its byte order reversed  
setbit(X,n,b) X =(bl−1…bn…b1b0) with bit n set to value b b ∈ {0,1}
setbyte(X,n,b) X with byte n set to value b mod 256  
sha1(X,n) the SHA-1 hash of the rightmost n bits of X [Note 4]
sha256(X,n) the SHA-256 hash of the rightmost n bits of X [Note 4]
sqrt(X) the truncated integer square root of X ⎣√x ⎦
square(X) the square of X x2

new in version 2.2

For notes and examples of use, see the manual.

Displaying numbers in decimal and hexadecimal

To enter a number in hexadecimal, prefix it with "0x". It will display automatically in decimal. To display a number in hexadecimal, print with the "!" command.

> 0xdeadbeefcafebabebeddeddecadedeed
> ! 295990755076957304711832595911747231469

Example scripts

How to find a quadratic residue by exhaustive search (source)
Compute discrete logarithm (source)
Write out the first 1000 primes (source)
Make an RSA key pair and test it (source)
Make an RSA key pair of exact bit length (source)
Perform RSA calculation with private key in CRT quintuple form (source)
Crack RSA if used incorrectly to three recipients (source)
Example of the rDSA algorithm from ANSI X9.31 (source)
Example of DSA from Appendix 5 FIPS PUB 186-2 (source)
Perform RSA calculation with private key in CRT quintuple form (source)
Generate domain parameters for Diffie-Hellman (source)
Perform Diffie-Hellman key exchange using parameters generated above (source)
Poly1305 Example and Test Vector (source)


bdcalc manual in PDF

bdcalc manual in HTML


Windows: Download the latest bdcalc installation program now. Use either

Unzip the zip file and run the setup.exe program inside it, or download the exe program directly and run it. The latest version is released 26 July 2016.

Windows binary: bdcalc is a simple stand-alone Windows EXE file just 135 kB in size. If you just want the plain bdcalc.exe file to install yourself, plus the help and sample scripts, download bdcalc-files.zip (381 kB) [md5=1d594263ec22d75f6a72d6ea1591f764].

Linux x86 binary: Compiled with static library included. bdcalc-linux.x86.zip (529 kB) [md5=29dd079e2c5dc1b60c12893de65a3c40] (install notes)

Mac OSX binary: bdcalc-osx.zip (340 kB) [md5=c03926a7936c7970bef85dedf5c40e67] (install notes)

Please note it is a breach of copyright to put a copy of these installation files on another server or to distribute them in any manner except by providing a link to this page.

bdcalc is free software. Please read the licence conditions.

What's new in v2.2?

New version 2.2 released 17 June 2016. This fixes up a few annoyances we had with print and help and adds a couple of new functions.



Any comments, feedback, questions: please send us a message.

Version 1 of bdcalc was first released on 12 May 2013 and version 2.2 on 17 June 2016 (v2.2.1 on 26 July 2016). This page last updated 26 July 2016