Unsigned 32 or 64 bit integers and related operations.
This package was developed to facilitate computations for SHA-2. It contains the minimal set of functions required by those algorithms.
Examples below assume the following imports:
import Array
import Word.Hex as HexSize of a word.
Convert a list of bytes to an array of words of the given size.
fromBytes Bit32 [ 0xDE, 0xAD, 0xBE, 0xEF ]
--> [ W 0xDEADBEEF ] |> Array.fromList
fromBytes Bit32
[ 0xDE, 0xAD, 0xBE, 0xEF
, 0x01, 0x23, 0x45, 0x67
]
--> [ W 0xDEADBEEF, W 0x01234567 ] |> Array.fromList
fromBytes Bit64
[ 0xDE, 0xAD, 0xBE, 0xEF
, 0x01, 0x23, 0x45, 0x67
]
--> [ D 0xDEADBEEF 0x01234567 ] |> Array.fromListConvert a UTF8 string to an array of words of the given size.
fromUTF8 Bit32 "I ❤ UTF strings!" |> Array.toList
--> [ W 0x4920E29D -- 'I', ' ', 226, 157
--> , W 0xA4205554 -- 164, ' ', 'U', 'T'
--> , W 0x46207374 -- 'F', ' ', 's', 't'
--> , W 0x72696E67 -- 'r', 'i', 'n', 'g'
--> , W 0x73210000 -- 's', '!'
--> ]
fromUTF8 Bit64 "I ❤ UTF strings!" |> Array.toList
--> [ D 0x4920E29D 0xA4205554 -- 'I', ' ', 226, 157, 164, ' ', 'U', 'T'
--> , D 0x46207374 0x72696E67 -- 'F', ' ', 's', 't', 'r', 'i', 'n', 'g'
--> , D 0x73210000 0x00000000 -- 's', '!'
--> ]The integer zero as a word of the given size.
Convert an array of words to a list of bytes.
[ W 0 ] |> Array.fromList |> toBytes
--> [ 0, 0, 0, 0 ]
[ D 0 0 ] |> Array.fromList |> toBytes
--> [ 0, 0, 0, 0, 0, 0, 0, 0 ]
[ W 16843010 ] |> Array.fromList |> toBytes
--> [ 1, 1, 1, 2 ]Modulo adds two words of the same type.
add (W 0x80000000) (W 0x7FFFFFFF) |> Hex.fromWord
--> "ffffffff"
add (W 0x80000000) (W 0x80000003) |> Hex.fromWord
--> "00000003"
add (D 0 0xFFFFFFFF) (D 0 1) |> Hex.fromWord
--> "0000000100000000"
add (D 0xFFFFFFFF 0xFFFFFFFF) (D 0 2) |> Hex.fromWord
--> "0000000000000001"
add (W 0) (D 0 0)
--> MismatchBitwise and.
Word.and
(W 0xFF00FF00)
(W 0xFFFF0000) |> Hex.fromWord
--> "ff000000"
Word.and
(D 0xFF00FF00 0xFFFF0000)
(D 0xFFFF0000 0xFF00FF00) |> Hex.fromWord
--> "ff000000ff000000"Bitwise xor.
Word.xor
(W 0xFF00FF00)
(W 0x00FFFF00) |> Hex.fromWord
--> "ffff0000"
Word.xor
(D 0xFF00FF00 0x00FFFF00)
(D 0x00FFFF00 0xFF00FF00) |> Hex.fromWord
--> "ffff0000ffff0000"Bitwise complement.
Word.complement
(W 0x00FF00FF) |> Hex.fromWord
--> "ff00ff00"
Word.complement
(D 0x00FF00FF 0x00FF00FF) |> Hex.fromWord
--> "ff00ff00ff00ff00"Rotate bits to the left by the given offset.
https://en.wikipedia.org/wiki/Bitwise_operation#Rotate_no_carry
rotateLeftBy 4 (W 0xDEADBEEF) |> Hex.fromWord
--> "eadbeefd"
rotateLeftBy 4 (D 0xDDEEAADD 0xBBEEAAFF) |> Hex.fromWord
--> "deeaaddbbeeaaffd"
rotateLeftBy 7 Mismatch
--> MismatchRotate bits to the right by the given offset.
https://en.wikipedia.org/wiki/Bitwise_operation#Rotate_no_carry
rotateRightBy 4 (W 0xDEADBEEF) |> Hex.fromWord
--> "fdeadbee"
rotateRightBy 4 (D 0xDDEEAADD 0xBBEEAAFF) |> Hex.fromWord
--> "fddeeaaddbbeeaaf"
rotateRightBy 7 Mismatch
--> MismatchShift bits to the right by a given offset, filling new bits with zeros.
https://en.wikipedia.org/wiki/Bitwise_operation#Logical_shift
shiftRightZfBy 9 (W 0xFFFF) |> Hex.fromWord
--> "0000007f"
shiftRightZfBy 32 (W 0xFFFF) |> Hex.fromWord
--> "00000000"
shiftRightZfBy 8 (D 0x01234567 0x89abcdef) |> Hex.fromWord
--> "000123456789abcd"
shiftRightZfBy 4 Mismatch
--> MismatchConvert the given word size to a byte count.
Unsigned integers of size 32 or 64 bits.
Wa "word" of size 32 bitsDa "double word" of size 64 bitsMismatchvalue of any operation that mixes word typesThese constructors are provided as a convenience, but are unsafe. Use
fromBytesorfromUTF8to safely create arrays of words of the same type.