bic(1)

NAME
bic - simple arbitrary-precision calculator

SYNOPSIS
bic [-h]

DESCRIPTION

A simple calculator program that handles arbitrarily-large numbers. It's similar to the standard bc(1) calculator. However unlike "bc" it does not handle fractional numbers, only integers. It also does not implement bc's booleans, bit operations, functions, etc. It doesn't implement general base-changing only hexadecimal via the -h flag. It does implement variables and assignments, but they can only be one letter long.

The basic operations are the usual ones with the usual precedence, plus a couple extra:

x + y
    addition

x - y
    subtraction

x * y
    multiplication

x / y
    division

x % y
    remainder

-x  unary negation

x ^ y
    exponentiation

x!  factorial

You can use parentheses to group sub-expressions. Then there are the special functions:

sqrt( x )
    square root

gcd( x, y )
    greatest common divisor

lcm( x, y )
    least common multiple

modpow( x, y, z )
    modular exponentiation - equivalent to ( x ^ y ) % z, but much faster

modinv( x, y )
    modular inverse

random( x )
    random number in [0..x)

bits( x )
    number of bits in x

digits( x )
    number of digits in x, approximately

jacobi( x, y )
    the Jacobi symbol

isprime( x, y )
    check whether x is a prime number, to certainty y

genprime( x, y )
    generate a probable prime number x bits long, with certainty y

For the last two functions, the certainty just specifies how many times to run the probabilistic prime check. A 0 return means the number is definitely not prime. A 1 return means the number is probably prime, with an error probability of less than 1/4 ^ certainty. Note: in fact the error probability is much lower, that's just a theoretical bound. Using a certainty of 1 gives a theoretical error probability of 0.25, but in fact a large number that passes such a test is almost certainly prime. Using certainty more than, say, 5 is huge overkill. See "Finding Four Million Large Random Primes", Ronald Rivest, Advances in Cryptology: Proceedings of Crypto '91 - he estimates the chance of a 512-bit number falsely passing a single test at 10^-44.

EXAMPLE

Here's how you could use bic to do an RSA encryption and decryption.

First, decide how many bits long you want your keys to be. Let's say 500. Generate two prime numbers half that long (takes a couple seconds each):
    p = genprime(250,4)
    q = genprime(250,4)
Compute their product, and the product of one less than the primes:
    n = p * q
    o = (p-1) * (q-1)
Pick a public key - a random number less than o:
    e = random(o)
Compute the corresponding private key:
    d = modinv(e,o)
If this step fails (due to e not being relatively prime to o), pick another e. It may take a few tries.

Now you have your public and private keys. Pick a message to encrypt, up to 500 bits long:
    m = 12345678901234567890123456789012345678901234567890
Encrypt it with your public key (this takes a few seconds):
    c = modpow(m,e,n)
Decrypt it with your private key (also takes a few seconds):
    modpow(c,d,n)
    12345678901234567890123456789012345678901234567890
That's all there is to it!

SEE ALSO
liblmin(3)

AUTHOR
Copyright © 2000,2026 by Jef Poskanzer <jef@mail.acme.com>. All rights reserved.