ppad-fixed-0.1.3: Large fixed-width words and constant-time arithmetic.
Copyright(c) 2025 Jared Tobin
LicenseMIT
MaintainerJared Tobin <jared@ppad.tech>
Safe HaskellNone
LanguageHaskell2010

Data.Word.Wide

Description

Wide words, consisting of two Limbs.

Synopsis

Wide Words

data Wide Source #

Little-endian wide words.

Constructors

Wide !Limb2 

Instances

Instances details
Num Wide Source #

Note that fromInteger necessarily runs in variable time due to conversion from the variable-size, potentially heap-allocated Integer type.

Instance details

Defined in Data.Word.Wide

Methods

(+) :: Wide -> Wide -> Wide #

(-) :: Wide -> Wide -> Wide #

(*) :: Wide -> Wide -> Wide #

negate :: Wide -> Wide #

abs :: Wide -> Wide #

signum :: Wide -> Wide #

fromInteger :: Integer -> Wide #

Show Wide Source # 
Instance details

Defined in Data.Word.Wide

Methods

showsPrec :: Int -> Wide -> ShowS #

show :: Wide -> String #

showList :: [Wide] -> ShowS #

NFData Wide Source # 
Instance details

Defined in Data.Word.Wide

Methods

rnf :: Wide -> () #

Construction, Conversion

wide :: Word -> Word -> Wide Source #

Construct a Wide word from low and high Words.

to_vartime :: Integer -> Wide Source #

Convert an Integer to a Wide word in variable time.

>>> to_vartime 1
1

from_vartime :: Wide -> Integer Source #

Convert a Wide word to an Integer in variable time.

>>> from_vartime 1
1

Constant-time selection

select Source #

Arguments

:: Wide

a

-> Wide

b

-> Choice

c

-> Wide

result

Return a if c is truthy, otherwise return b.

>>> import qualified Data.Choice as C
>>> select 0 1 (C.true# ())
1

Bit Manipulation

or :: Wide -> Wide -> Wide Source #

Logical disjunction on Wide words.

and :: Wide -> Wide -> Wide Source #

Logical conjunction on Wide words.

xor :: Wide -> Wide -> Wide Source #

Logical exclusive-or on Wide words.

not :: Wide -> Wide Source #

Logical negation on Wide words.

Comparison

eq :: Wide -> Wide -> Choice Source #

Compare Wide words for equality in constant time.

>>> import qualified Data.Chocie as C
>>> C.decide (eq 1 1)
True

eq_vartime :: Wide -> Wide -> Bool Source #

Compare Wide words for equality in variable time.

>>> eq_vartime 1 1
True

Arithmetic

add :: Wide -> Wide -> Wide Source #

Wrapping addition on Wide words, computing 'a + b'.

add_o Source #

Arguments

:: Wide

augend

-> Wide

addend

-> (Wide, Word)

(sum, carry)

Overflowing addition on Wide words, computing 'a + b', returning the sum and carry bit.

sub :: Wide -> Wide -> Wide Source #

Wrapping subtraction on Wide words, computing 'a - b'.

mul :: Wide -> Wide -> Wide Source #

Wrapping multiplication on Wide words.

neg Source #

Arguments

:: Wide

argument

-> Wide

(wrapping) additive inverse

Wrapping negation on Wide words, producing an additive inverse.

>>> neg 1
340282366920938463463374607431768211455
>>> 1 + neg 1
>>> 0

Unboxed Arithmetic

add_o# Source #

Arguments

:: Limb2

augend

-> Limb2

addend

-> (# Limb2, Limb #)

(# sum, carry bit #)

Overflowing addition, computing 'a + b', returning the sum and a carry bit.

add_w# Source #

Arguments

:: Limb2

augend

-> Limb2

addend

-> Limb2

sum

Wrapping addition, computing 'a + b'.

sub_b# Source #

Arguments

:: Limb2

minuend

-> Limb2

subtrahend

-> (# Limb2, Limb #)

(# difference, borrow mask #)

Borrowing subtraction, computing 'a - b' and returning the difference with a borrow mask.

sub_w# Source #

Arguments

:: Limb2

minuend

-> Limb2

subtrahend

-> Limb2

difference

Wrapping subtraction, computing 'a - b'.

mul_w# Source #

Arguments

:: Limb2

multiplicand

-> Limb2

multiplier

-> Limb2

product

Wrapping multiplication, computing 'a b'.