cometa-0.1.0.0: Idiomatic Haskell bindings to cardano-c
Safe HaskellSafe-Inferred
LanguageHaskell2010

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

Documentation

data Radix Source #

A radix from 2 through 62. Above 36, uppercase digits precede lowercase.

Instances

Instances details
Show Radix Source # 
Instance details

Defined in Cometa.Encoding.Integer

Methods

showsPrec :: Int -> Radix -> ShowS #

show :: Radix -> String #

showList :: [Radix] -> ShowS #

Eq Radix Source # 
Instance details

Defined in Cometa.Encoding.Integer

Methods

(==) :: Radix -> Radix -> Bool #

(/=) :: Radix -> Radix -> Bool #

Ord Radix Source # 
Instance details

Defined in Cometa.Encoding.Integer

Methods

compare :: Radix -> Radix -> Ordering #

(<) :: Radix -> Radix -> Bool #

(<=) :: Radix -> Radix -> Bool #

(>) :: Radix -> Radix -> Bool #

(>=) :: Radix -> Radix -> Bool #

max :: Radix -> Radix -> Radix #

min :: Radix -> Radix -> Radix #

mkRadix :: Int -> Either Error Radix Source #

Validate a radix; other values return InvalidIntegerRadix.

radixValue :: Radix -> Int Source #

Observe the validated radix.

binary :: Radix Source #

Base 2.

decimal :: Radix Source #

Base 10.

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.

data ByteOrder Source #

Order of unsigned magnitude bytes. Neither order carries a sign bit.

Constructors

LittleEndian 
BigEndian 

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.