aboutsummaryrefslogtreecommitdiffstats
path: root/examples/sha256.aup
diff options
context:
space:
mode:
authoreunhhu <[email protected]>2026-10-13 06:16:12 +0100
committereunhhu <[email protected]>2026-10-13 06:16:12 +0100
commit6209ddb42b953902828972b2e09f0296a51fe565 (patch)
tree51be65e5831760c3d416f9ed1431f39dfd569e6a /examples/sha256.aup
parent557a69ef5102292eceb25f823ebcfc989810ab0c (diff)
downloadaup-6209ddb42b953902828972b2e09f0296a51fe565.tar.gz
aup-6209ddb42b953902828972b2e09f0296a51fe565.zip
Formats, joins, slices, lets of bytes and text, while and && and || that decide alone (Pure 027's stage 2a): {v:08x}, {p:.2f} (Python's mini-language, [0][width][.digits][d x X b o f]), a + b of texts or of bytes, b[a .. c] of bytes and of texts (a cut inside a character failing as encoding), byte(n), let s = bytes or text (16 of each a handler, s = s + e appending in place), while c { } as a count to 2^53 that breaks when c is false, && and || computing their right side only where it decides (an AND or OR still where it cannot fail: comparisons, states and fields, a walked item's fields, a number plus a small literal, what the left side computed; every module of the apps and benchmarks byte for byte the same but json's); examples/json.aup, examples/sha256.aup in formats and joined bytes; 14 diagnostics, one accepted form, 144 random formats against Python's format() and an app cutting at commas
Diffstat (limited to 'examples/sha256.aup')
-rw-r--r--examples/sha256.aup23
1 files changed, 6 insertions, 17 deletions
diff --git a/examples/sha256.aup b/examples/sha256.aup
index 2558e55..8d00383 100644
--- a/examples/sha256.aup
+++ b/examples/sha256.aup
@@ -3,9 +3,6 @@ state digest: text
input hash = message { data: bytes }
fn rotr(x, n) = (x >> n | x << (32 - n)) & 0xffffffff
-# The byte at j of the message padded to whole blocks: 0x80 after it, its length in bits in the last 8.
-fn padded(data, size, total, j) = (if j < size { data[j] } else if j == size { 0x80 }
- else if j >= total - 8 { i64(size) * 8 >> 8 * (total - 1 - j) & 0xff } else { 0 })
on hash {
let k: list of i64 = [
@@ -22,13 +19,13 @@ on hash {
0x1f83d9ab, 0x5be0cd19
let w: list of i64
for t in 0 .. 64 { w.append(0) }
- let size = hash.data.bytes
- let total = (floor((size + 8) / 64) + 1) * 64
- for block in 0 .. total / 64 {
+ let m = hash.data + byte(0x80) # padded: 0x80, zeros, the length in bits in the last 8 bytes
+ while m.bytes % 64 != 56 { m = m + byte(0) }
+ for s in 0 .. 8 { m = m + byte(i64(hash.data.bytes) * 8 >> 56 - 8 * s & 0xff) }
+ for block in 0 .. m.bytes / 64 {
for t in 0 .. 16 {
let j = block * 64 + t * 4
- w[t] = (padded(hash.data, size, total, j) << 24 | padded(hash.data, size, total, j + 1) << 16 |
- padded(hash.data, size, total, j + 2) << 8 | padded(hash.data, size, total, j + 3))
+ w[t] = m[j] << 24 | m[j + 1] << 16 | m[j + 2] << 8 | m[j + 3]
}
for t in 16 .. 64 {
let x = w[t - 15]
@@ -44,13 +41,5 @@ on hash {
h0, h1, h2, h3 = (h0 + a) & 0xffffffff, (h1 + b) & 0xffffffff, (h2 + c) & 0xffffffff, (h3 + d) & 0xffffffff
h4, h5, h6, h7 = (h4 + e) & 0xffffffff, (h5 + f) & 0xffffffff, (h6 + g) & 0xffffffff, (h7 + hh) & 0xffffffff
}
- let h: list of i64 = [h0, h1, h2, h3, h4, h5, h6, h7]
- let hex: list of i64
- for i in 0 .. 8 {
- for s in 0 .. 8 {
- let nibble = h[i] >> 28 - 4 * s & 0xf
- hex.append(if nibble < 10 { 48 + nibble } else { 87 + nibble })
- }
- }
- digest = text(bytes(hex))
+ digest = "{h0:08x}{h1:08x}{h2:08x}{h3:08x}{h4:08x}{h5:08x}{h6:08x}{h7:08x}"
}