src/crypto/blake/blake3/hex.bend

bend-collections@1.0.0.0 · 26 lines · 6 definitions

1import Base
2
3def hex_digit_if(x: U32, small: Bool) -> Char:
4  match small:
5    case True{}:
6      Chr{(48 + x : U32)}
7    case False{}:
8      Chr{(87 + x : U32)}
9
10def hex_digit(+x: U32) -> Char:
11  hex_digit_if(x, U32.is_lt(x, 10))
12
13def hex_byte(+x: U32) -> String:
14  SCon{hex_digit(U32.shrn(x,4n)),SCon{hex_digit(U32.and(x,15)),SNil{}}}
15
16def hex_word(+x: U32) -> String:
17  hex_byte(U32.and(x,255)) ++ hex_byte(U32.and(U32.shrn(x,8n),255)) ++ hex_byte(U32.and(U32.shrn(x,16n),255)) ++ hex_byte(U32.shrn(x,24n))
18
19def hex_walk(n: Nat, +index: U32, pair: Array<U32> & U32) -> String:
20  match n pair:
21    case 0n Tuple{a,w}: hex_word(w)
22    case 1n+p Tuple{a,w}: hex_word(w) ++ hex_walk(p,U32.inc(index),Array.get(U32,a,index))
23
24# The digest words as lowercase hex, bytes in little-endian word order.
25def hex(a: Array<U32>) -> String:
26  hex_walk(7n,1,Array.get(U32,a,0))