Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/si/rosetta/tasks/sha-256/sha-256.om # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / si / rosetta / tasks / sha-256 / sha-256.om` 215 lines ยท 4207 bytes Raw Blame History 1 note Rosetta Code task "SHA-256": https://rosettacode.org/wiki/SHA-256 2 note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 3 om2 4 text nl "\n" 5 text m1 "abc" 6 text m2 "Rosetta code" 7 text m3 "The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog." 8 global kt uint 64 9 global hs uint 8 10 global w uint 64 11 global blk uchar 128 12 note emit writes every byte of a to stdout; a short write is retried, an error exits 1 13 fn emit a view uchar * to void 14 o = ulong 0 15 while lt o len a bound 4096 16 k = write 1 a o sub len a o 17 if le k 0 18 exit 1 19 end 20 o = add o ulong k 21 end 22 end 23 note emitx writes n as 8 lowercase hex digits 24 fn emitx n uint to void 25 d = new uchar 8 0 26 v = n 27 for i ulong 8 28 q = uchar and v 15 29 if lt q 10 30 put d sub 7 i add q 48 31 else 32 put d sub 7 i add q 87 33 end 34 v = shr v 4 35 end 36 emit d 37 end 38 note FIPS 180-4 constants, copied mechanically from aien-dev/omega-mv om/sha256/sha256-om2.om 39 fn kinit to void 40 put kt 0 0x428a2f98 41 put kt 1 0x71374491 42 put kt 2 0xb5c0fbcf 43 put kt 3 0xe9b5dba5 44 put kt 4 0x3956c25b 45 put kt 5 0x59f111f1 46 put kt 6 0x923f82a4 47 put kt 7 0xab1c5ed5 48 put kt 8 0xd807aa98 49 put kt 9 0x12835b01 50 put kt 10 0x243185be 51 put kt 11 0x550c7dc3 52 put kt 12 0x72be5d74 53 put kt 13 0x80deb1fe 54 put kt 14 0x9bdc06a7 55 put kt 15 0xc19bf174 56 put kt 16 0xe49b69c1 57 put kt 17 0xefbe4786 58 put kt 18 0x0fc19dc6 59 put kt 19 0x240ca1cc 60 put kt 20 0x2de92c6f 61 put kt 21 0x4a7484aa 62 put kt 22 0x5cb0a9dc 63 put kt 23 0x76f988da 64 put kt 24 0x983e5152 65 put kt 25 0xa831c66d 66 put kt 26 0xb00327c8 67 put kt 27 0xbf597fc7 68 put kt 28 0xc6e00bf3 69 put kt 29 0xd5a79147 70 put kt 30 0x06ca6351 71 put kt 31 0x14292967 72 put kt 32 0x27b70a85 73 put kt 33 0x2e1b2138 74 put kt 34 0x4d2c6dfc 75 put kt 35 0x53380d13 76 put kt 36 0x650a7354 77 put kt 37 0x766a0abb 78 put kt 38 0x81c2c92e 79 put kt 39 0x92722c85 80 put kt 40 0xa2bfe8a1 81 put kt 41 0xa81a664b 82 put kt 42 0xc24b8b70 83 put kt 43 0xc76c51a3 84 put kt 44 0xd192e819 85 put kt 45 0xd6990624 86 put kt 46 0xf40e3585 87 put kt 47 0x106aa070 88 put kt 48 0x19a4c116 89 put kt 49 0x1e376c08 90 put kt 50 0x2748774c 91 put kt 51 0x34b0bcb5 92 put kt 52 0x391c0cb3 93 put kt 53 0x4ed8aa4a 94 put kt 54 0x5b9cca4f 95 put kt 55 0x682e6ff3 96 put kt 56 0x748f82ee 97 put kt 57 0x78a5636f 98 put kt 58 0x84c87814 99 put kt 59 0x8cc70208 100 put kt 60 0x90befffa 101 put kt 61 0xa4506ceb 102 put kt 62 0xbef9a3f7 103 put kt 63 0xc67178f2 104 end 105 fn iv to void 106 put hs 0 0x6a09e667 107 put hs 1 0xbb67ae85 108 put hs 2 0x3c6ef372 109 put hs 3 0xa54ff53a 110 put hs 4 0x510e527f 111 put hs 5 0x9b05688c 112 put hs 6 0x1f83d9ab 113 put hs 7 0x5be0cd19 114 end 115 fn bs0 x uint to uint 116 ret xor xor rotr x 2 rotr x 13 rotr x 22 117 end 118 fn bs1 x uint to uint 119 ret xor xor rotr x 6 rotr x 11 rotr x 25 120 end 121 fn ss0 x uint to uint 122 ret xor xor rotr x 7 rotr x 18 shr x 3 123 end 124 fn ss1 x uint to uint 125 ret xor xor rotr x 17 rotr x 19 shr x 10 126 end 127 fn ch e uint f uint g uint to uint 128 ret xor and e f and not e g 129 end 130 fn maj a uint b uint c uint to uint 131 ret xor xor and a b and a c and b c 132 end 133 note compress the 64-byte block blk[o .. o+64) into hs 134 fn compress o ulong to void 135 for i ulong 16 136 j = add o mul i 4 137 x = shl uint get blk j 24 138 x = or x shl uint get blk add j 1 16 139 x = or x shl uint get blk add j 2 8 140 x = or x uint get blk add j 3 141 put w i x 142 end 143 for i ulong 48 144 x = add get w i ss0 get w add i 1 145 x = add x get w add i 9 146 x = add x ss1 get w add i 14 147 put w add i 16 x 148 end 149 a = get hs 0 150 b = get hs 1 151 c = get hs 2 152 d = get hs 3 153 e = get hs 4 154 f = get hs 5 155 g = get hs 6 156 h = get hs 7 157 for i ulong 64 158 t1 = add h bs1 e 159 t1 = add t1 ch e f g 160 t1 = add t1 get kt i 161 t1 = add t1 get w i 162 t2 = add bs0 a maj a b c 163 h = g 164 g = f 165 f = e 166 e = add d t1 167 d = c 168 c = b 169 b = a 170 a = add t1 t2 171 end 172 put hs 0 add get hs 0 a 173 put hs 1 add get hs 1 b 174 put hs 2 add get hs 2 c 175 put hs 3 add get hs 3 d 176 put hs 4 add get hs 4 e 177 put hs 5 add get hs 5 f 178 put hs 6 add get hs 6 g 179 put hs 7 add get hs 7 h 180 end 181 note one or two blocks: messages up to 119 bytes (longer traps 3 at the first put past blk) 182 fn sha a view uchar * to void 183 n = len a 184 for i ulong 128 185 put blk i 0 186 end 187 for i ulong n 188 put blk i get a i 189 end 190 put blk n 128 191 total = ulong 64 192 if gt n 55 193 total = 128 194 end 195 bits = mul n 8 196 for i ulong 8 197 put blk sub total add i 1 uchar and shr bits mul i 8 255 198 end 199 iv 200 compress 0 201 if eq total 128 202 compress 64 203 end 204 for i ulong 8 205 emitx get hs i 206 end 207 emit nl 208 end 209 fn main to int 210 kinit 211 sha m1 212 sha m2 213 sha m3 214 ret 0 215 end