ktonon/elm-word/1.0.0

NOTE: this package is not compatible with Elm 0.19.1

Word

Unsigned 32 or 64 bit integers and related operations.

Table of Contents

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)

Types

type Word
= W Int
| D Int Int
| Mismatch

Unsigned integers of size 32 or 64 bits.

  • W a "word" of size 32 bits
  • D a "double word" of size 64 bits
  • Mismatch value of any operation that mixes word types

These constructors are provided as a convenience, but are unsafe. Use fromBytes or fromUTF8 to safely create arrays of words of the same type.

type Size
= Bit32
| Bit64

Size of a word.

Constructors

fromBytes : Size -> List Byte -> Array 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.fromList
fromUTF8 : Size -> String -> Array Word

Convert 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', '!'
--> ]
zero : Size -> Word

The integer zero as a word of the given size.

Conversions

toBytes : Array Word -> List Byte

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.fromInt
toHex : Array Word -> String

Convert a list of words to a string of hexadecimal characters.

[ D 0xDEADBEEF 0x00112233, D 0x44556677 0x8899AABB ]
    |> Array.fromList
    |> toHex
--> "deadbeef00112233445566778899aabb"

Arithmetic

add : Word -> Word -> Word

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)
--> Mismatch

Bitwise

and : Word -> Word -> Word

Bitwise and.

Word.and
    (W 0xFF00FF00)
    (W 0xFFFF0000)
-->  W 0xFF000000


Word.and
    (D 0xFF00FF00 0xFFFF0000)
    (D 0xFFFF0000 0xFF00FF00)
-->  D 0xFF000000 0xFF000000
xor : Word -> Word -> Word

Bitwise xor.

Word.xor
    (W 0xFF00FF00)
    (W 0x00FFFF00)
-->  W 0xFFFF0000


Word.xor
    (D 0xFF00FF00 0x00FFFF00)
    (D 0x00FFFF00 0xFF00FF00)
-->  D 0xFFFF0000 0xFFFF0000
complement : Word -> Word

Bitwise complement.

Word.complement
    (W 0x00FF00FF)
-->  W 0xFF00FF00


Word.complement
    (D 0x00FF00FF 0x00FF00FF)
-->  D 0xFF00FF00 0xFF00FF00
rotateRightBy : Int -> Word -> Word

Rotate 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
--> Mismatch
shiftRightZfBy : Int -> Word -> Word

Shift 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
--> Mismatch

Misc

sizeInBytes : Size -> Int

Convert the given word size to a byte count.