Movatterモバイル変換


[0]ホーム

URL:


integer-gmp-1.0.2.0: Integer library based on GMP

Copyright(c) Herbert Valerio Riedel 2014
LicenseBSD3
Maintainerghc-devs@haskell.org
Stabilityprovisional
Portabilitynon-portable (GHC Extensions)
Safe HaskellNone
LanguageHaskell2010

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.

Synopsis

TheInteger type

dataIntegerSource#

Invariant:Jn# andJp# are used iff value doesn't fit inS#

Useful properties resulting from the invariants:

Constructors

S# !Int#

iff value in[minBound::Int, maxBound::Int] range

Jp# !BigNat

iff value in]maxBound::Int, +inf[ range

Jn# !BigNat

iff value in]-inf, minBound::Int[ range

Instances
EqIntegerSource# 
Instance details

Defined inGHC.Integer.Type

OrdIntegerSource# 
Instance details

Defined inGHC.Integer.Type

isValidInteger# ::Integer ->Int#Source#

Test whether all internal invariants are satisfied byInteger value

Returns1# if valid,0# otherwise.

This operation is mostly useful for test-suites and/or code which constructsInteger values directly.

BasicInteger operations

moduleGHC.Integer

AdditionalInteger operations

bitInteger ::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.

gcdInteger ::Integer ->Integer ->IntegerSource#

Compute greatest common divisor.

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

lcmInteger ::Integer ->Integer ->IntegerSource#

Compute least common multiple.

sqrInteger ::Integer ->IntegerSource#

SquareInteger

powModInteger ::Integer ->Integer ->Integer ->IntegerSource#

"powModIntegerbem" computes baseb 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#

"powModSecIntegerbem" computes baseb 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 topowModInteger, and a warning will be emitted when used.

Since: integer-gmp-1.0.2.0

recipModInteger ::Integer ->Integer ->IntegerSource#

"recipModIntegerxm" computes the inverse ofx modulom. If the inverse exists, the return valuey will satisfy0 <y < abs(m), otherwise the result is0.

Since: integer-gmp-0.5.1.0

Additional conversion operations toInteger

wordToNegInteger ::Word# ->IntegerSource#

bigNatToInteger ::BigNat ->IntegerSource#

bigNatToNegInteger ::BigNat ->IntegerSource#

TheBigNat type

dataBigNatSource#

Type 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# size
  • limbs are stored in least-significant-limb-first order,
  • the most-significant limb must be non-zero, except for
  • 0 which is represented as a 1-limb.

Constructors

BN#ByteArray# 
Instances
EqBigNatSource# 
Instance details

Defined inGHC.Integer.Type

OrdBigNatSource# 
Instance details

Defined inGHC.Integer.Type

typeGmpLimb =WordSource#

Type representing a GMP Limb

typeGmpLimb# =Word#Source#

typeGmpSize =IntSource#

Count ofGmpLimbs, must be positive (unless specified otherwise).

typeGmpSize# =Int#Source#

isValidBigNat# ::BigNat ->Int#Source#

Test whether all internal invariants are satisfied byBigNat value

Returns1# if valid,0# otherwise.

This operation is mostly useful for test-suites and/or code which constructsInteger values directly.

sizeofBigNat# ::BigNat ->GmpSize#Source#

Return number of limbs contained inBigNat.

zeroBigNat ::BigNatSource#

CAF representing the value0 :: BigNat

oneBigNat ::BigNatSource#

CAF representing the value1 :: BigNat

nullBigNat ::BigNatSource#

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

Conversions to/fromBigNat

byteArrayToBigNat# ::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#.

wordToBigNat ::Word# ->BigNatSource#

Construct 1-limbBigNat fromWord#

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 operations

plusBigNat ::BigNat ->BigNat ->BigNatSource#

plusBigNatWord ::BigNat ->GmpLimb# ->BigNatSource#

minusBigNat ::BigNat ->BigNat ->BigNatSource#

ReturnsnullBigNat (seeisNullBigNat#) in case of underflow

minusBigNatWord ::BigNat ->GmpLimb# ->BigNatSource#

ReturnsnullBigNat (seeisNullBigNat#) in case of underflow

timesBigNat ::BigNat ->BigNat ->BigNatSource#

timesBigNatWord ::BigNat ->GmpLimb# ->BigNatSource#

sqrBigNat ::BigNat ->BigNatSource#

SquareBigNat

quotRemBigNat ::BigNat ->BigNat -> (#BigNat,BigNat#)Source#

If divisor is zero,(#nullBigNat,nullBigNat #) is returned

quotRemBigNatWord ::BigNat ->GmpLimb# -> (#BigNat,GmpLimb##)Source#

Note: Result of div/0 undefined

quotBigNatWord ::BigNat ->GmpLimb# ->BigNatSource#

quotBigNat ::BigNat ->BigNat ->BigNatSource#

remBigNat ::BigNat ->BigNat ->BigNatSource#

remBigNatWord ::BigNat ->GmpLimb# ->Word#Source#

div/0 not checked

gcdBigNat ::BigNat ->BigNat ->BigNatSource#

gcdBigNatWord ::BigNat ->Word# ->Word#Source#

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 operations

shiftRBigNat ::BigNat ->Int# ->BigNatSource#

shiftLBigNat ::BigNat ->Int# ->BigNatSource#

testBitBigNat ::BigNat ->Int# ->BoolSource#

clearBitBigNat ::BigNat ->Int# ->BigNatSource#

complementBitBigNat ::BigNat ->Int# ->BigNatSource#

setBitBigNat ::BigNat ->Int# ->BigNatSource#

andBigNat ::BigNat ->BigNat ->BigNatSource#

xorBigNat ::BigNat ->BigNat ->BigNatSource#

popCountBigNat ::BigNat ->Int#Source#

orBigNat ::BigNat ->BigNat ->BigNatSource#

bitBigNat ::Int# ->BigNatSource#

Specialised version of

bitBigNat = shiftLBigNat (wordToBigNat 1##)

avoiding a few redundant allocations

BigNat comparison predicates

isZeroBigNat ::BigNat ->BoolSource#

Test ifBigNat value is equal to zero.

isNullBigNat# ::BigNat ->Int#Source#

Test for special 0-sizedBigNat representing underflows.

compareBigNatWord ::BigNat ->GmpLimb# ->OrderingSource#

compareBigNat ::BigNat ->BigNat ->OrderingSource#

eqBigNatWord ::BigNat ->GmpLimb# ->BoolSource#

eqBigNatWord# ::BigNat ->GmpLimb# ->Int#Source#

eqBigNat ::BigNat ->BigNat ->BoolSource#

eqBigNat# ::BigNat ->BigNat ->Int#Source#

gtBigNatWord# ::BigNat ->GmpLimb# ->Int#Source#

Miscellaneous GMP-provided operations

gcdInt ::Int# ->Int# ->Int#Source#

Compute greatest common divisor.

Warning: result may become negative if (at least) one argument isminBound

gcdWord ::Word# ->Word# ->Word#Source#

Compute greatest common divisor.

Since: integer-gmp-1.0.0.0

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

Primality tests

testPrimeInteger ::Integer ->Int# ->Int#Source#

Probalistic Miller-Rabin primality test.

"testPrimeIntegernk" determines whethern is prime and returns one of the following results:

  • 2# is returned ifn is definitely prime,
  • 1# ifn is aprobable prime, or
  • 0# 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

Import/export functions

Compute size of serialisation

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).
  • This function is only defined ifbase >= 2# andbase <= 256# (Note: the documentation claims that onlybase <= 62# is supported, however the actual implementation supports up to base 256).
  • Ifbase 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

Export

exportBigNatToAddr ::BigNat ->Addr# ->Int# ->IOWordSource#

Version ofexportIntegerToAddr operating onBigNats.

exportIntegerToAddr ::Integer ->Addr# ->Int# ->IOWordSource#

DumpInteger (without sign) toaddr in base-256 representation.

exportIntegerToAddriaddre

See 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

exportIntegerToMutableByteArrayimbaoffsetmsbf

writes

  • theIntegeri
  • into theMutableByteArray#mba starting atoffset
  • with most significant byte first ifmsbf is1# or least significant byte first ifmsbf is0#, and
  • returns number of bytes written.

Use "sizeInBaseIntegeri 256#" to compute the exact number of bytes written in advance fori /= 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

Import

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.

importIntegerFromAddraddrsizemsbf

See 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

importIntegerFromByteArraybaoffsetsizemsbf

reads

  • size bytes from theByteArray#ba starting atoffset
  • with most significant byte first ifmsbf is1# or least significant byte first ifmsbf is0#, and
  • returns a newInteger

Since: integer-gmp-1.0.0.0

Produced byHaddock version 2.20.0


[8]ページ先頭

©2009-2025 Movatter.jp