
Last chance! 50% off unlimited learning
Sale ends in
"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.
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)), ...)
"bigz"
.TRUE
(implicitly) till 2011."bigz"
, representing the argument
(x
or a
).
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 %% mis 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 bigz
s:
mod.bigz
, where the second operand's
value is used.
mod.bigz
, where the second operand's value is used.
## 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")
Run the code above in your browser using DataLab