# bigz

0th

Percentile

##### Large Sized Integer Values

Class "bigz" encodes arbitrarily large integers (via GMP). A simple S3 class (internally a raw vector), it has been registered as formal (S4) class (via setOldClass), too.

Keywords
arith
##### Usage
as.bigz(a, mod = NA)
"as.character"(x, b = 10, ...)
is.bigz(x)
"is.na"(x)
"print"(x, quote=FALSE, initLine = is.null(modulus(x)), ...)
##### Arguments
a
either integer, numeric (i.e., double) or character vector.

If character: the strings either start with 0x for hexadecimal, 0b for binary, 0 for octal, or without a 0* prefix for decimal values. Formatting errors are signalled as with stop.

b
base: from 2 to 36
x
a “big integer number” (vector), of class "bigz".
...
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.
##### 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 a “modulus”, accessible via modulus(), currently as an attribute mod. When the object has such a modulus $m$, arithmetic is performed “modulo m”, mathematically “within the ring $Z/mZ$”. For many operations, this means

   result <- mod.bigz(result, m)  ## == result %% m

is called after performing the arithmetic operation and the result will have the attribute mod set accordingly. This however does not apply, e.g., for /, where $a / b = a b^(-1)$ and $b^(-1)$ is the multiplicate inverse of $b$ with respect to ring arithmetic, or NA with a warning when the inverse does not exist. The warning can be turned off via options("gmp:warnModMismatch" = FALSE)

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 of mod.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 used for the arithmetic operations, except in case of mod.bigz, where the second operand's value is used.

##### Value

An R object of (S3) class "bigz", representing the argument (x or a).

##### Note

    x <- as.bigz(1234567890123456789012345678901234567890)

will not work as R converts the number to a double, losing precision and only then convert to a "bigz" object.

    x <- as.bigz("1234567890123456789012345678901234567890")


##### References

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

##### Aliases
• bigz
• bigz-class
• as.bigz
• is.bigz
• as.character.bigz
• as.double.bigz
• print.bigz
• is.na.bigz
• biginteger_as
• biginteger_as_character
##### Examples
## 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), is.bigz(c2)) ; 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)

# Depict the "S4-class" bigz, i.e., the formal (S4) methods:
if(require("Rmpfr")) # mostly interesting there
showMethods(class="bigz")

Documentation reproduced from package gmp, version 0.5-13, License: GPL

### Community examples

Looks like there are no examples yet.