| Copyright | (c) 2025 Jared Tobin |
|---|---|
| License | MIT |
| Maintainer | Jared Tobin <jared@ppad.tech> |
| Safe Haskell | None |
| Language | Haskell2010 |
Data.Word.Wider
Description
Wider words, consisting of four Limbs.
Synopsis
- data Wider = Wider !Limb4
- wider :: Word -> Word -> Word -> Word -> Wider
- to_vartime :: Integer -> Wider
- from_vartime :: Wider -> Integer
- eq_vartime :: Wider -> Wider -> Bool
- cmp_vartime :: Wider -> Wider -> Ordering
- lt :: Wider -> Wider -> Choice
- gt :: Wider -> Wider -> Choice
- odd :: Wider -> Choice
- select :: Wider -> Wider -> Choice -> Wider
- shl1 :: Wider -> Wider
- shr1 :: Wider -> Wider
- shl1_c :: Wider -> (# Wider, Choice #)
- shr1_c :: Wider -> (# Wider, Choice #)
- shr_limb :: Wider -> Int -> Wider
- shl_limb :: Wider -> Int -> Wider
- and :: Wider -> Wider -> Wider
- or :: Wider -> Wider -> Wider
- xor :: Wider -> Wider -> Wider
- not :: Wider -> Wider
- add_o :: Wider -> Wider -> (Wider, Word)
- add :: Wider -> Wider -> Wider
- add_mod :: Wider -> Wider -> Wider -> Wider
- sub :: Wider -> Wider -> Wider
- sub_b :: Wider -> Wider -> (Wider, Word)
- sub_mod :: Wider -> Wider -> Wider -> Wider
- sub_mod_c# :: Limb4 -> Limb -> Limb4 -> Limb4 -> Limb4
- mul :: Wider -> Wider -> Wider
- mul_c :: Wider -> Wider -> (Wider, Wider)
- sqr :: Wider -> (Wider, Wider)
Four-limb words
Little-endian wider words, consisting of four Limbs.
>>>1 :: Wider1
Constructors
| Wider !Limb4 |
wider :: Word -> Word -> Word -> Word -> Wider Source #
Construct a Wider word from four Words, provided in
little-endian order.
>>>wider 1 0 0 01
Comparison
eq_vartime :: Wider -> Wider -> Bool Source #
Compare Wider words for equality in variable time.
>>>eq_vartime 1 0False>>>eq_vartime 1 1True
lt :: Wider -> Wider -> Choice Source #
Constant-time less-than comparison between Wider values.
>>>import qualified Data.Choice as CT>>>CT.decide (lt 1 2)True>>>CT.decide (lt 1 1)False
gt :: Wider -> Wider -> Choice Source #
Constant-time greater-than comparison between Wider values.
>>>import qualified Data.Choice as CT>>>CT.decide (gt 1 2)False>>>CT.decide (gt 2 1)True
Parity
Constant-time selection
Return a if c is truthy, otherwise return b.
>>>import qualified Data.Choice as C>>>select 0 1 (C.true# ())1
Bit manipulation
shl1 :: Wider -> Wider Source #
Constant-time 1-bit shift-left.
>>>shl1 12>>>shl1 (2 ^ (255 :: Word))0
shl1_c :: Wider -> (# Wider, Choice #) Source #
Constant-time 1-bit shift-left with carry, with a Choice indicating
whether the highest bit was set.
shr1_c :: Wider -> (# Wider, Choice #) Source #
Constant-time 1-bit shift-right with carry, with a Choice
indicating whether the lowest bit was set.
Shift right by less than the number of bits in a Limb (e.g., by
a maximum of 63 bits on 64-bit architectures). The shift amount is
unchecked.
>>>shr_limb 2 11
Shift left by less than the number of bits in a Limb (e.g., by
a maximum of 63 bits on 64-bit architectures). The shift amount is
unchecked.
>>>shl_limb 2 11>>>shl_limb 1 639223372036854775808
Binary not.
>>>not 0115792089237316195423570985008687907853269984665640564039457584007913129639935>>>not (not 0)0
Arithmetic
Overflowing addition, computing 'a + b', returning the sum and a carry bit.
>>>add_o 1 1(2,0)>>>add_o 1 (2 ^ (256 :: Word) - 1)(0,1)
Modular addition.
Assumes that the sum is less than twice the modulus; this is not checked.
>>>add_mod 1 1 32>>>add_mod 1 2 30
Borrowing subtraction, computing 'a - b' and returning the difference with a borrow mask.
>>>sub_b 1 1(0,0)>>>sub_b 0 (2 ^ (256 :: Word) - 1)(1,18446744073709551615)
sub_mod :: Wider -> Wider -> Wider -> Wider Source #
Modular subtraction. Computes a - b mod m.
Assumes that the magnitude of the difference is less than the modulus (this is unchecked).
>>>sub_mod 1 1 40>>>sub_mod 2 3 43
Arguments
| :: Limb4 | minuend |
| -> Limb | carry bit |
| -> Limb4 | subtrahend |
| -> Limb4 | modulus |
| -> Limb4 | difference |
Modular subtraction with carry. Computes (# a, c #) - b mod m.