omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / drafts / rosettacode / sha-256.wiki
| 1 | =={{header|Omegese}}== |
| 2 | {{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} |
| 3 | Omega has no imports, so the small output helpers (<code>emit</code> and friends) are part of the program. |
| 4 | <syntaxhighlight lang="text"> |
| 5 | om2 |
| 6 | text nl "\n" |
| 7 | text m1 "abc" |
| 8 | text m2 "Rosetta code" |
| 9 | text m3 "The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog." |
| 10 | global kt uint 64 |
| 11 | global hs uint 8 |
| 12 | global w uint 64 |
| 13 | global blk uchar 128 |
| 14 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 15 | fn emit a view uchar * to void |
| 16 | o = ulong 0 |
| 17 | while lt o len a bound 4096 |
| 18 | k = write 1 a o sub len a o |
| 19 | if le k 0 |
| 20 | exit 1 |
| 21 | end |
| 22 | o = add o ulong k |
| 23 | end |
| 24 | end |
| 25 | note emitx writes n as 8 lowercase hex digits |
| 26 | fn emitx n uint to void |
| 27 | d = new uchar 8 0 |
| 28 | v = n |
| 29 | for i ulong 8 |
| 30 | q = uchar and v 15 |
| 31 | if lt q 10 |
| 32 | put d sub 7 i add q 48 |
| 33 | else |
| 34 | put d sub 7 i add q 87 |
| 35 | end |
| 36 | v = shr v 4 |
| 37 | end |
| 38 | emit d |
| 39 | end |
| 40 | note FIPS 180-4 constants, copied mechanically from aien-dev/omega-mv om/sha256/sha256-om2.om |
| 41 | fn kinit to void |
| 42 | put kt 0 0x428a2f98 |
| 43 | put kt 1 0x71374491 |
| 44 | put kt 2 0xb5c0fbcf |
| 45 | put kt 3 0xe9b5dba5 |
| 46 | put kt 4 0x3956c25b |
| 47 | put kt 5 0x59f111f1 |
| 48 | put kt 6 0x923f82a4 |
| 49 | put kt 7 0xab1c5ed5 |
| 50 | put kt 8 0xd807aa98 |
| 51 | put kt 9 0x12835b01 |
| 52 | put kt 10 0x243185be |
| 53 | put kt 11 0x550c7dc3 |
| 54 | put kt 12 0x72be5d74 |
| 55 | put kt 13 0x80deb1fe |
| 56 | put kt 14 0x9bdc06a7 |
| 57 | put kt 15 0xc19bf174 |
| 58 | put kt 16 0xe49b69c1 |
| 59 | put kt 17 0xefbe4786 |
| 60 | put kt 18 0x0fc19dc6 |
| 61 | put kt 19 0x240ca1cc |
| 62 | put kt 20 0x2de92c6f |
| 63 | put kt 21 0x4a7484aa |
| 64 | put kt 22 0x5cb0a9dc |
| 65 | put kt 23 0x76f988da |
| 66 | put kt 24 0x983e5152 |
| 67 | put kt 25 0xa831c66d |
| 68 | put kt 26 0xb00327c8 |
| 69 | put kt 27 0xbf597fc7 |
| 70 | put kt 28 0xc6e00bf3 |
| 71 | put kt 29 0xd5a79147 |
| 72 | put kt 30 0x06ca6351 |
| 73 | put kt 31 0x14292967 |
| 74 | put kt 32 0x27b70a85 |
| 75 | put kt 33 0x2e1b2138 |
| 76 | put kt 34 0x4d2c6dfc |
| 77 | put kt 35 0x53380d13 |
| 78 | put kt 36 0x650a7354 |
| 79 | put kt 37 0x766a0abb |
| 80 | put kt 38 0x81c2c92e |
| 81 | put kt 39 0x92722c85 |
| 82 | put kt 40 0xa2bfe8a1 |
| 83 | put kt 41 0xa81a664b |
| 84 | put kt 42 0xc24b8b70 |
| 85 | put kt 43 0xc76c51a3 |
| 86 | put kt 44 0xd192e819 |
| 87 | put kt 45 0xd6990624 |
| 88 | put kt 46 0xf40e3585 |
| 89 | put kt 47 0x106aa070 |
| 90 | put kt 48 0x19a4c116 |
| 91 | put kt 49 0x1e376c08 |
| 92 | put kt 50 0x2748774c |
| 93 | put kt 51 0x34b0bcb5 |
| 94 | put kt 52 0x391c0cb3 |
| 95 | put kt 53 0x4ed8aa4a |
| 96 | put kt 54 0x5b9cca4f |
| 97 | put kt 55 0x682e6ff3 |
| 98 | put kt 56 0x748f82ee |
| 99 | put kt 57 0x78a5636f |
| 100 | put kt 58 0x84c87814 |
| 101 | put kt 59 0x8cc70208 |
| 102 | put kt 60 0x90befffa |
| 103 | put kt 61 0xa4506ceb |
| 104 | put kt 62 0xbef9a3f7 |
| 105 | put kt 63 0xc67178f2 |
| 106 | end |
| 107 | fn iv to void |
| 108 | put hs 0 0x6a09e667 |
| 109 | put hs 1 0xbb67ae85 |
| 110 | put hs 2 0x3c6ef372 |
| 111 | put hs 3 0xa54ff53a |
| 112 | put hs 4 0x510e527f |
| 113 | put hs 5 0x9b05688c |
| 114 | put hs 6 0x1f83d9ab |
| 115 | put hs 7 0x5be0cd19 |
| 116 | end |
| 117 | fn bs0 x uint to uint |
| 118 | ret xor xor rotr x 2 rotr x 13 rotr x 22 |
| 119 | end |
| 120 | fn bs1 x uint to uint |
| 121 | ret xor xor rotr x 6 rotr x 11 rotr x 25 |
| 122 | end |
| 123 | fn ss0 x uint to uint |
| 124 | ret xor xor rotr x 7 rotr x 18 shr x 3 |
| 125 | end |
| 126 | fn ss1 x uint to uint |
| 127 | ret xor xor rotr x 17 rotr x 19 shr x 10 |
| 128 | end |
| 129 | fn ch e uint f uint g uint to uint |
| 130 | ret xor and e f and not e g |
| 131 | end |
| 132 | fn maj a uint b uint c uint to uint |
| 133 | ret xor xor and a b and a c and b c |
| 134 | end |
| 135 | note compress the 64-byte block blk[o .. o+64) into hs |
| 136 | fn compress o ulong to void |
| 137 | for i ulong 16 |
| 138 | j = add o mul i 4 |
| 139 | x = shl uint get blk j 24 |
| 140 | x = or x shl uint get blk add j 1 16 |
| 141 | x = or x shl uint get blk add j 2 8 |
| 142 | x = or x uint get blk add j 3 |
| 143 | put w i x |
| 144 | end |
| 145 | for i ulong 48 |
| 146 | x = add get w i ss0 get w add i 1 |
| 147 | x = add x get w add i 9 |
| 148 | x = add x ss1 get w add i 14 |
| 149 | put w add i 16 x |
| 150 | end |
| 151 | a = get hs 0 |
| 152 | b = get hs 1 |
| 153 | c = get hs 2 |
| 154 | d = get hs 3 |
| 155 | e = get hs 4 |
| 156 | f = get hs 5 |
| 157 | g = get hs 6 |
| 158 | h = get hs 7 |
| 159 | for i ulong 64 |
| 160 | t1 = add h bs1 e |
| 161 | t1 = add t1 ch e f g |
| 162 | t1 = add t1 get kt i |
| 163 | t1 = add t1 get w i |
| 164 | t2 = add bs0 a maj a b c |
| 165 | h = g |
| 166 | g = f |
| 167 | f = e |
| 168 | e = add d t1 |
| 169 | d = c |
| 170 | c = b |
| 171 | b = a |
| 172 | a = add t1 t2 |
| 173 | end |
| 174 | put hs 0 add get hs 0 a |
| 175 | put hs 1 add get hs 1 b |
| 176 | put hs 2 add get hs 2 c |
| 177 | put hs 3 add get hs 3 d |
| 178 | put hs 4 add get hs 4 e |
| 179 | put hs 5 add get hs 5 f |
| 180 | put hs 6 add get hs 6 g |
| 181 | put hs 7 add get hs 7 h |
| 182 | end |
| 183 | note one or two blocks: messages up to 119 bytes (longer traps 3 at the first put past blk) |
| 184 | fn sha a view uchar * to void |
| 185 | n = len a |
| 186 | for i ulong 128 |
| 187 | put blk i 0 |
| 188 | end |
| 189 | for i ulong n |
| 190 | put blk i get a i |
| 191 | end |
| 192 | put blk n 128 |
| 193 | total = ulong 64 |
| 194 | if gt n 55 |
| 195 | total = 128 |
| 196 | end |
| 197 | bits = mul n 8 |
| 198 | for i ulong 8 |
| 199 | put blk sub total add i 1 uchar and shr bits mul i 8 255 |
| 200 | end |
| 201 | iv |
| 202 | compress 0 |
| 203 | if eq total 128 |
| 204 | compress 64 |
| 205 | end |
| 206 | for i ulong 8 |
| 207 | emitx get hs i |
| 208 | end |
| 209 | emit nl |
| 210 | end |
| 211 | fn main to int |
| 212 | kinit |
| 213 | sha m1 |
| 214 | sha m2 |
| 215 | sha m3 |
| 216 | ret 0 |
| 217 | end |
| 218 | </syntaxhighlight> |
| 219 | {{out}} |
| 220 | <pre> |
| 221 | ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad |
| 222 | 764faf5c61ac315f1497f9dfa542713965b785e5cc2f707d6468d7d1124cdfcf |
| 223 | 635241ac823ee4a81fbb410c92be616b0a89191083d8d7b5d232c823dc8df4f5 |
| 224 | </pre> |