| Copyright | (c) Herbert Valerio Riedel 2014 |
|---|---|
| License | BSD3 |
| Maintainer | ghc-devs@haskell.org |
| Stability | provisional |
| Portability | non-portable (GHC Extensions) |
| Safe Haskell | None |
| Language | Haskell2010 |
GHC.Integer.GMP.Internals
Contents
Description
This modules provides access to theInteger constructors and exposes some highly optimized GMP-operations.
Note that sinceinteger-gmp does not depend onbase, error reporting via exceptions,error, orundefined is not available. Instead, the low-level functions will crash the runtime if called with invalid arguments.
See alsoGHC Commentary: Libraries/Integer.
Integer typeInvariant:Jn# andJp# are used iff value doesn't fit inS#
Useful properties resulting from the invariants:
Constructors
| S# !Int# | |
| Jp# !BigNat | iff value in |
| Jn# !BigNat | iff value in |
Integer operationsmoduleGHC.Integer
Integer operationsbitInteger ::Int# ->IntegerSource#
Integer for which onlyn-th bit is set. Undefined behaviour for negativen values.
popCountInteger ::Integer ->Int#Source#
Count number of set bits. For negative arguments returns negative population count of negated argument.
gcdExtInteger ::Integer ->Integer -> (#Integer,Integer#)Source#
Extended euclidean algorithm.
Fora andb, compute their greatest common divisorg and the coefficients satisfyingas +bt =g.
Since: integer-gmp-0.5.1.0
powModInteger ::Integer ->Integer ->Integer ->IntegerSource#
"" computes basepowModIntegerbemb raised to exponente moduloabs(m).
Negative exponents are supported if an inverse modulom exists.
Warning: It's advised to avoid calling this primitive with negative exponents unless it is guaranteed the inverse exists, as failure to do so will likely cause program abortion due to a divide-by-zero fault. See alsorecipModInteger.
Future versions ofinteger_gmp may not support negativee values anymore.
Since: integer-gmp-0.5.1.0
powModSecInteger ::Integer ->Integer ->Integer ->IntegerSource#
"" computes basepowModSecIntegerbemb raised to exponente modulom. It is required thate >= 0 andm is odd.
This is a "secure" variant ofpowModInteger using thempz_powm_sec() function which is designed to be resilient to side channel attacks and is therefore intended for cryptographic applications.
This primitive is only available when the underlying GMP library supports it (GMP >= 5). Otherwise, it internally falls back to, and a warning will be emitted when used.powModInteger
Since: integer-gmp-1.0.2.0
recipModInteger ::Integer ->Integer ->IntegerSource#
"" computes the inverse ofrecipModIntegerxmx modulom. If the inverse exists, the return valuey will satisfy0 <y < abs(m), otherwise the result is0.
Since: integer-gmp-0.5.1.0
IntegerBigNat typeType representingraw arbitrary-precision Naturals
This is common type used byNatural andInteger. As this type consists of a single constructor wrapping aByteArray# it can be unpacked.
Essential invariants:
ByteArray# size is an exact multiple ofWord# size0 which is represented as a 1-limb.Constructors
| BN#ByteArray# |
CAF representing the value0 :: BigNat
Special 0-sized bigNat returned in case of arithmetic underflow
This is currently only returned by the following operations:
Other operations such asquotBigNat may returnnullBigNat as well as a dummy/place-holder value instead ofundefined since we can't throw exceptions. But that behaviour should not be relied upon.
NB:isValidBigNat# nullBigNat is false
BigNatbyteArrayToBigNat# ::ByteArray# ->GmpSize# ->BigNatSource#
ConstructBigNat from existingByteArray# containingnGmpLimbs in least-significant-first order.
If possibleByteArray#, will be used directly (i.e. sharedwithout cloning theByteArray# into a newly allocated one)
Note: size parameter (timessizeof(GmpLimb)) must be less or equal to itssizeofByteArray#.
wordToBigNat2 ::Word# ->Word# ->BigNatSource#
Construct BigNat from 2 limbs. The first argument is the most-significant limb.
bigNatToInt ::BigNat ->Int#Source#
Equivalent toword2Int# .bigNatToWord
bigNatToWord ::BigNat ->Word#Source#
Same asindexBigNat# bn 0#
indexBigNat# ::BigNat ->GmpSize# ->GmpLimb#Source#
Extractn-th (0-based) limb inBigNat.n must be less than size as reported bysizeofBigNat#.
BigNat arithmetic operationsminusBigNat ::BigNat ->BigNat ->BigNatSource#
ReturnsnullBigNat (seeisNullBigNat#) in case of underflow
minusBigNatWord ::BigNat ->GmpLimb# ->BigNatSource#
ReturnsnullBigNat (seeisNullBigNat#) in case of underflow
quotRemBigNat ::BigNat ->BigNat -> (#BigNat,BigNat#)Source#
If divisor is zero,(# is returnednullBigNat,nullBigNat #)
powModBigNat ::BigNat ->BigNat ->BigNat ->BigNatSource#
Version ofpowModInteger operating onBigNats
Since: integer-gmp-1.0.0.0
powModBigNatWord ::BigNat ->BigNat ->GmpLimb# ->GmpLimb#Source#
Version ofpowModInteger forWord#-sized moduli
Since: integer-gmp-1.0.0.0
recipModBigNat ::BigNat ->BigNat ->BigNatSource#
Version ofrecipModInteger operating onBigNats
Since: integer-gmp-1.0.0.0
BigNat logic operationsbitBigNat ::Int# ->BigNatSource#
Specialised version of
bitBigNat = shiftLBigNat (wordToBigNat 1##)
avoiding a few redundant allocations
BigNat comparison predicatesgcdInt ::Int# ->Int# ->Int#Source#
Compute greatest common divisor.
Warning: result may become negative if (at least) one argument isminBound
powModWord ::GmpLimb# ->GmpLimb# ->GmpLimb# ->GmpLimb#Source#
Version ofpowModInteger operating onWord#s
Since: integer-gmp-1.0.0.0
recipModWord ::GmpLimb# ->GmpLimb# ->GmpLimb#Source#
Version ofrecipModInteger operating onWord#s
Since: integer-gmp-1.0.0.0
testPrimeInteger ::Integer ->Int# ->Int#Source#
Probalistic Miller-Rabin primality test.
"" determines whethertestPrimeIntegernkn is prime and returns one of the following results:
2# is returned ifn is definitely prime,1# ifn is aprobable prime, or0# ifn is definitely not a prime.Thek argument controls how many test rounds are performed for determining aprobable prime. For more details, seeGMP documentation for `mpz_probab_prime_p()`.
Since: integer-gmp-0.5.1.0
testPrimeBigNat ::BigNat ->Int# ->Int#Source#
Version oftestPrimeInteger operating onBigNats
Since: integer-gmp-1.0.0.0
testPrimeWord# ::GmpLimb# ->Int# ->Int#Source#
Version oftestPrimeInteger operating onWord#s
Since: integer-gmp-1.0.0.0
nextPrimeInteger ::Integer ->IntegerSource#
Compute next prime greater thann probalistically.
According to the GMP documentation, the underlying functionmpz_nextprime() "uses a probabilistic algorithm to identify primes. For practical purposes it's adequate, the chance of a composite passing will be extremely small."
Since: integer-gmp-0.5.1.0
nextPrimeBigNat ::BigNat ->BigNatSource#
Version ofnextPrimeInteger operating onBigNats
Since: integer-gmp-1.0.0.0
nextPrimeWord# ::GmpLimb# ->GmpLimb#Source#
Version ofnextPrimeInteger operating onWord#s
Since: integer-gmp-1.0.0.0
sizeInBaseBigNat ::BigNat ->Int# ->Word#Source#
Version ofsizeInBaseInteger operating onBigNat
Since: integer-gmp-1.0.0.0
sizeInBaseInteger ::Integer ->Int# ->Word#Source#
Compute number of digits (without sign) in givenbase.
This function wrapsmpz_sizeinbase() which has some implementation pecularities to take into account:
sizeInBaseInteger 0base = 1" (see also comment inexportIntegerToMutableByteArray).base >= 2# andbase <= 256# (Note: the documentation claims that onlybase <= 62# is supported, however the actual implementation supports up to base 256).base is a power of 2, the result will be exact. In other cases (e.g. forbase = 10#), the resultmay be 1 digit too large sometimes.sizeInBaseIntegeri 2#" can be used to determine the most significant bit ofi.Since: integer-gmp-0.5.1.0
sizeInBaseWord# ::Word# ->Int# ->Word#Source#
Version ofsizeInBaseInteger operating onWord#
Since: integer-gmp-1.0.0.0
exportBigNatToAddr ::BigNat ->Addr# ->Int# ->IOWordSource#
Version ofexportIntegerToAddr operating onBigNats.
exportIntegerToAddr ::Integer ->Addr# ->Int# ->IOWordSource#
DumpInteger (without sign) toaddr in base-256 representation.
exportIntegerToAddriaddreSee description ofexportIntegerToMutableByteArray for more details.
Since: integer-gmp-1.0.0.0
exportWordToAddr ::Word ->Addr# ->Int# ->IOWordSource#
Version ofexportIntegerToAddr operating onWords.
exportBigNatToMutableByteArray ::BigNat ->MutableByteArray#RealWorld ->Word# ->Int# ->IOWordSource#
Version ofexportIntegerToMutableByteArray operating onBigNats.
Since: integer-gmp-1.0.0.0
exportIntegerToMutableByteArray ::Integer ->MutableByteArray#RealWorld ->Word# ->Int# ->IOWordSource#
DumpInteger (without sign) to mutable byte-array in base-256 representation.
The call
exportIntegerToMutableByteArrayimbaoffsetmsbfwrites
IntegeriMutableByteArray#mba starting atoffsetmsbf is1# or least significant byte first ifmsbf is0#, andUse "" to compute the exact number of bytes written in advance forsizeInBaseIntegeri 256#i /= 0. In case ofi == 0,exportIntegerToMutableByteArray will write and report zero bytes written, whereassizeInBaseInteger report one byte.
It's recommended to avoid callingexportIntegerToMutableByteArray for small integers as this function would currently convert those to big integers in msbf to callmpz_export().
Since: integer-gmp-1.0.0.0
exportWordToMutableByteArray ::Word ->MutableByteArray#RealWorld ->Word# ->Int# ->IOWordSource#
Version ofexportIntegerToMutableByteArray operating onWords.
Since: integer-gmp-1.0.0.0
importBigNatFromAddr ::Addr# ->Word# ->Int# ->IOBigNatSource#
Version ofimportIntegerFromAddr constructing aBigNat
importIntegerFromAddr ::Addr# ->Word# ->Int# ->IOIntegerSource#
ReadInteger (without sign) from memory location ataddr in base-256 representation.
importIntegerFromAddraddrsizemsbfSee description ofimportIntegerFromByteArray for more details.
Since: integer-gmp-1.0.0.0
importBigNatFromByteArray ::ByteArray# ->Word# ->Word# ->Int# ->BigNatSource#
Version ofimportIntegerFromByteArray constructing aBigNat
importIntegerFromByteArray ::ByteArray# ->Word# ->Word# ->Int# ->IntegerSource#
ReadInteger (without sign) from byte-array in base-256 representation.
The call
importIntegerFromByteArraybaoffsetsizemsbfreads
size bytes from theByteArray#ba starting atoffsetmsbf is1# or least significant byte first ifmsbf is0#, andIntegerSince: integer-gmp-1.0.0.0
Produced byHaddock version 2.20.0