Learn R Programming

gmp (version 0.5-4)

bigz: Large Sized Integer Values

Description

Class "bigz" encodes arbitrarily large integers (via GMP)

Usage

as.bigz(a, mod = NA)
## S3 method for class 'bigz':
as.character(x, b = 10, \dots)## S3 method for class 'bigz':
is.na(x)
## S3 method for class 'bigz':
print(x, quote=FALSE, initLine = is.null(modulus(x)), ...)

Arguments

a
either integer, numeric (i.e., double) or character ve
b
base: from 2 to 36
x
numeric value
...
additional arguments passed to methods
mod
an integer, numeric, string or bigz of the internal modulus, see below.
quote
(for printing:) logical indicating if the numbers should be quoted (as characters are); the default used to be TRUE (implicitly) till 2011.
initLine
(for printing:) logical indicating if an initial line (with the class and length or dimension) should be printed. The default prints it for those cases where the class is not easily discernable from the print output.

Value

  • An Robject of (S3) class "bigz", representing the argument x.

Details

Bigz's are integers of arbitrary, but given length (means: only restricted by the host memory). Basic arithmetic operations can be performed on bigzs as addition, subtraction, multiplication, division, modulation (remainder of division), power, multiplicative inverse, calculating of the greatest common divisor, test whether the integer is prime and other operations needed when performing standard cryptographic operations.

For a review of basic arithmetics, see add.bigz.

Comparison are supported, i.e., "==", "!=", "<"< code="">, "<="< code="">, ">", and ">=".

Objects of class "bigz" may have an attribute mod which specifies a modulus that is applied after each arithmetic operation. When the object has such a modulus $m$, all arithmetic is performed modulo m, i.e., result <- mod.bigz(result, m) ## == result %% m is called after performing the arithmetic operation and the result will have the attribute mod set accordingly.

Powers of bigzs can only be performed, if either a modulus is going to be applied to the result bigz or if the exponent fits into an integer value. So, if you want to calculate a power in a finite group (modulo c), for large $c$ do not use a ^ b %% c, but rather as.bigz(a,c) ^ b.

The following rules for the result's modulus apply when performing arithmetic operations on bigzs:

  • If none of the operand has a modulus set, the result will not have a modulus.
  • If both operands have a different modulus, the result will not have a modulus, except in case ofmod.bigz, where the second operand's value is used.
  • If only one of the operands has a modulus or both have a common (the same), it is set and applied to the result, except in case ofmod.bigz, where the second operand's value is used.

References

The GNU MP Library, see http://gmplib.org

Examples

Run this code
## 1+1=2
a <- as.bigz(1)
a + a

## Two non-small Mersenne primes:
two <- as.bigz(2)
p1 <- two^107 -1 ; isprime(p1); p1
p2 <- two^127 -1 ; isprime(p2); p2


## Calculate c = x^e mod n
## --------------------------------------------------------------------
x <- as.bigz("0x123456789abcdef") # my secret message
e <- as.bigz(3) # something smelling like a dangerous public RSA exponent
(n <- p1 * p2) #  a product of two primes
as.character(n, b=16)# as both primes were Mersenne's..

## recreate the three numbers above [for demo below]:
n. <- n; x. <- x; e. <- e # save
Rev <- function() { n <<- n.; x <<- x.; e <<- e.}

# first way to do it right
modulus(x) <- n
c <- x ^ e ; c ; Rev()

# similar second way (makes more sense if you reuse e) to do it right
modulus(e) <- n
c2 <- x ^ e
stopifnot(identical(c2, c)) ; Rev()

# third way to do it right
c3 <- x ^ as.bigz(e, n) ; stopifnot(identical(c3, c))

# fourth way to do it right
c4 <- as.bigz(x, n) ^ e ; stopifnot(identical(c4, c))

# WRONG! (although very beautiful. Ok only for very small 'e' as here)
cc <- x ^ e %% n
cc == c

# Return result in hexa
as.character(c, b=16)

Run the code above in your browser using DataLab