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 Byte exposing (Byte)Size of a word.
Convert a list of bytes to an array of words of the given size.
fromBytes Bit32 ([ 0xDE, 0xAD, 0xBE, 0xEF ] |> List.map Byte.fromInt)
--> [ W 0xDEADBEEF ] |> Array.fromList
fromBytes Bit32 (
[ 0xDE, 0xAD, 0xBE, 0xEF
, 0x01, 0x23, 0x45, 0x67
] |> List.map Byte.fromInt)
--> [ W 0xDEADBEEF, W 0x01234567 ] |> Array.fromList
fromBytes Bit64 (
[ 0xDE, 0xAD, 0xBE, 0xEF
, 0x01, 0x23, 0x45, 0x67
] |> List.map Byte.fromInt)
--> [ 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 ] |> List.map Byte.fromInt
[ D 0 0 ] |> Array.fromList |> toBytes
--> [ 0, 0, 0, 0, 0, 0, 0, 0 ] |> List.map Byte.fromInt
[ W 16843010 ] |> Array.fromList |> toBytes
--> [ 1, 1, 1, 2 ] |> List.map Byte.fromIntConvert a list of words to a string of hexadecimal characters.
[ D 0xDEADBEEF 0x00112233, D 0x44556677 0x8899AABB ]
|> Array.fromList
|> toHex
--> "deadbeef00112233445566778899aabb"Modulo adds two words of the same type.
add (W 0x80000000)
(W 0x7FFFFFFF)
--> W 0xFFFFFFFF
add (W 0x80000000)
(W 0x80000003)
--> W 3
add (D 0 0xFFFFFFFF)
(D 0 1)
--> D 1 0
add (D 0xFFFFFFFF 0xFFFFFFFF)
(D 0 2)
--> D 0 1
add (W 0)
(D 0 0)
--> MismatchBitwise and.
Word.and
(W 0xFF00FF00)
(W 0xFFFF0000)
--> W 0xFF000000
Word.and
(D 0xFF00FF00 0xFFFF0000)
(D 0xFFFF0000 0xFF00FF00)
--> D 0xFF000000 0xFF000000Bitwise xor.
Word.xor
(W 0xFF00FF00)
(W 0x00FFFF00)
--> W 0xFFFF0000
Word.xor
(D 0xFF00FF00 0x00FFFF00)
(D 0x00FFFF00 0xFF00FF00)
--> D 0xFFFF0000 0xFFFF0000Bitwise complement.
Word.complement
(W 0x00FF00FF)
--> W 0xFF00FF00
Word.complement
(D 0x00FF00FF 0x00FF00FF)
--> D 0xFF00FF00 0xFF00FF00Rotate bits to the right by the given offset.
https://en.wikipedia.org/wiki/Bitwise_operation#Rotate_no_carry
rotateRightBy 4 (W 0xDEADBEEF)
--> W 0xFDEADBEE
rotateRightBy 4 (D 0xDDEEAADD 0xBBEEAAFF)
--> D 0xFDDEEAAD 0xDBBEEAAF
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)
--> W 0x7F
shiftRightZfBy 142 (W 0xFFFF)
--> W 0
shiftRightZfBy 8 (D 0x01234567 0x89abcdef)
--> D 0x00012345 0x6789abcd
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.