bi_bc(1)

NAME
bi_bc - simple arbitrary-precision calculator

SYNOPSIS
bi_bc

DESCRIPTION

A simple calculator program that handles arbitrarily-large numbers. It's similar to the standard "bc" calculator, but can handle larger numbers, goes somewhat faster, and has some more special functions. However unlike "bc" it does not handle fractional numbers, only integers. It also does not implement bc's booleans, bit operations, functions, etc. 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

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. Each iteration that the number passes through means it has at least a 50% chance of being prime. 10 iterations means the chance is at least a thousand to one in favor of primality, or to put it another way, less than a one in a thousand chance that the number is actually composite. With 20 iterations, the chance goes to less than one in a million.

EXAMPLE

Here's how you could use bi_bc 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,20)
    q = genprime(250,20)
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
bi_factor(1), bigint(3)

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