| Safe Haskell | Safe-Inferred |
|---|---|
| Language | Haskell2010 |
Cometa.Encoding.Integer
Description
Exact integer text, magnitude bytes and modular arithmetic. Values are
ordinary Haskell Integers; no native object or mutable buffer is exposed.
Validate a Radix once, parse text with fromText, then pass the integer to
metadata or Plutus-data constructors. fromTextAuto recognizes native-style
radix prefixes. Modular operations report invalid domains through Either.
Synopsis
- data Radix
- mkRadix :: Int -> Either Error Radix
- radixValue :: Radix -> Int
- binary :: Radix
- decimal :: Radix
- hexadecimal :: Radix
- fromText :: Radix -> Text -> Either Error Integer
- fromTextAuto :: Text -> Either Error Integer
- toText :: Radix -> Integer -> Text
- toUpperText :: Radix -> Integer -> Either Error Text
- data ByteOrder
- fromBytes :: ByteOrder -> ByteString -> Integer
- toBytes :: ByteOrder -> Integer -> ByteString
- modularInverse :: Integer -> Integer -> Either Error Integer
- modularPower :: Integer -> Integer -> Integer -> Either Error Integer
Documentation
A radix from 2 through 62. Above 36, uppercase digits precede lowercase.
mkRadix :: Int -> Either Error Radix Source #
Validate a radix; other values return InvalidIntegerRadix.
radixValue :: Radix -> Int Source #
Observe the validated radix.
hexadecimal :: Radix Source #
Base 16.
fromText :: Radix -> Text -> Either Error Integer Source #
Parse a native-compatible complete textual prefix: an optional minus sign,
digits, and ASCII whitespace. A plus sign and explicit radix prefixes are
rejected. Parsing stops at the first NUL. Letter case is ignored through
base 36; bases 37..62 use 0..9,A..Z,a..z. Empty or invalid digits return
InvalidIntegerText. Whitespace before the minus sign is accepted.
fromTextAuto :: Text -> Either Error Integer Source #
Detect base 16 from 0x0X, base 2 from 0b0B, base 8 from a
leading zero, and base 10 otherwise. Prefix detection follows the optional
minus sign before internal whitespace is removed, matching native base 0.
toText :: Radix -> Integer -> Text Source #
Render the exact integer without a radix prefix or whitespace. Bases
2..36 use lowercase letters; bases 37..62 use 0..9,A..Z,a..z. Zero is 0.
The minus sign is outside the unsigned digit conversion.
toUpperText :: Radix -> Integer -> Either Error Text Source #
Uppercase rendering for bases 2..36, equivalent to native negative
rendering bases -2..-36. Larger radices return InvalidIntegerRadix with
the corresponding negative base, since changing case would change digits.
Order of unsigned magnitude bytes. Neither order carries a sign bit.
Constructors
| LittleEndian | |
| BigEndian |
Instances
| Bounded ByteOrder Source # | |
| Enum ByteOrder Source # | |
Defined in Cometa.Encoding.Integer Methods succ :: ByteOrder -> ByteOrder # pred :: ByteOrder -> ByteOrder # fromEnum :: ByteOrder -> Int # enumFrom :: ByteOrder -> [ByteOrder] # enumFromThen :: ByteOrder -> ByteOrder -> [ByteOrder] # enumFromTo :: ByteOrder -> ByteOrder -> [ByteOrder] # enumFromThenTo :: ByteOrder -> ByteOrder -> ByteOrder -> [ByteOrder] # | |
| Show ByteOrder Source # | |
| Eq ByteOrder Source # | |
| Ord ByteOrder Source # | |
fromBytes :: ByteOrder -> ByteString -> Integer Source #
Decode unsigned magnitude bytes; empty input denotes zero. Leading zero bytes are accepted. This is independent of host byte order.
toBytes :: ByteOrder -> Integer -> ByteString Source #
Minimal unsigned magnitude bytes of the absolute value. Zero has one zero byte; positive and negative integers with the same magnitude agree.
modularInverse :: Integer -> Integer -> Either Error Integer Source #
Inverse in [0,abs modulus). Moduli 0 and +/-1 return InvalidModulus;
zero or noncoprime values return NoModularInverse. Unlike the native void
function, failure never leaves an unspecified previous result. Variable time.
modularPower :: Integer -> Integer -> Integer -> Either Error Integer Source #
Exact modular power using repeated squaring, with negative exponents
interpreted through modularInverse. Zero modulus returns InvalidModulus;
a missing inverse returns NoModularInverse. Negative moduli use their
absolute magnitude. Exponent zero returns 1, including modulus +/-1, as in
the native API; nonzero powers modulo +/-1 return 0. Negative exponents
with those moduli are rejected. Variable time; no constant-time guarantee.