Skip to content

Commit

Permalink
Add keccak_NNN family functions
Browse files Browse the repository at this point in the history
KECCAK-256 is needed by Ethereum, see following discussion for detail:

ethereum/EIPs#59
  • Loading branch information
dram committed Sep 9, 2018
1 parent 59af8d1 commit 2855692
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
5 changes: 5 additions & 0 deletions sources/sha3.sig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ signature SHA3 = sig
: (int * int * Word8Vector.vector * Word8.word * int)
-> Word8Vector.vector

val keccak_224 : Word8Vector.vector -> Word8Vector.vector
val keccak_256 : Word8Vector.vector -> Word8Vector.vector
val keccak_384 : Word8Vector.vector -> Word8Vector.vector
val keccak_512 : Word8Vector.vector -> Word8Vector.vector

val shake_128 : (Word8Vector.vector * int) -> Word8Vector.vector
val shake_256 : (Word8Vector.vector * int) -> Word8Vector.vector

Expand Down
9 changes: 9 additions & 0 deletions sources/sha3.sml
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,15 @@ fun keccak (rate : int,
end
end

fun keccak_224 (inputBytes : Word8Vector.vector) : Word8Vector.vector =
keccak (1152, 448, inputBytes, 0wx01, 224 div 8)
fun keccak_256 (inputBytes : Word8Vector.vector) : Word8Vector.vector =
keccak (1088, 512, inputBytes, 0wx01, 256 div 8)
fun keccak_384 (inputBytes : Word8Vector.vector) : Word8Vector.vector =
keccak (832, 768, inputBytes, 0wx01, 384 div 8)
fun keccak_512 (inputBytes : Word8Vector.vector) : Word8Vector.vector =
keccak (576, 1024, inputBytes, 0wx01, 512 div 8)

fun shake_128 (inputBytes : Word8Vector.vector,
outputByteLen : int) : Word8Vector.vector =
keccak (1344, 256, inputBytes, 0wx1f, outputByteLen)
Expand Down
8 changes: 8 additions & 0 deletions tests/main.sml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ fun main _ =
val () = (printResult "value1"
o bytesToString) value1

val () = (printResult "value1 KECCAK-256"
o bytesToString
o Sha3.keccak_256) value1

val () = (printResult "value1 SHA3-256"
o bytesToString
o Sha3.sha3_256) value1
Expand All @@ -24,6 +28,10 @@ fun main _ =
val () = (printResult "value2"
o bytesToString) value2

val () = (printResult "value2 KECCAK-256"
o bytesToString
o Sha3.keccak_256) value2

val () = (printResult "value2 SHA3-256"
o bytesToString
o Sha3.sha3_256) value2
Expand Down

0 comments on commit 2855692

Please sign in to comment.