src/crypto/sha/packed/buffer.bend

bend-collections-laws-crypto@1.0.0.0 · 17 lines · 3 definitions

1import Base
2import bend-collections-laws-crypto@1.0.0.0/src/crypto/sha/state.bend as S
3import bend-collections-laws-crypto@1.0.0.0/src/crypto/sha/packed/packed.bend as P
4
5# Exactly eight big-endian U32 words (32 bytes), with no list allocation.
6def digest(s: S.State) -> Array<U32>:
7  match s:
8    case S.H{a,b,c,d,e,f,g,h}:
9      ANode{ANode{ANode{ALeaf{a},ALeaf{b}},ANode{ALeaf{c},ALeaf{d}}},ANode{ANode{ALeaf{e},ALeaf{f}},ANode{ALeaf{g},ALeaf{h}}}}
10
11def result(r: Maybe<&2,S.State>) -> Maybe<&1,Array<U32>>:
12  match r:
13    case None{}: None{}
14    case Some{s}: Some{digest(s)}
15
16def sha256(input: Array<U32>, length: Nat) -> Maybe<&1,Array<U32>>:
17  result(P.hash(input,length))