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
omega-mv / seed / helix / sign_ddc.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note seed/helix/sign_ddc.om: Pure-Omega RFC 8032 Ed25519 DDC Receipt Signer & Verifier. |
| 3 | note Compose after: |
| 4 | note protocol/omnet/om/sha512/sha512.om |
| 5 | note protocol/omnet/om/ed25519/ed25519.om |
| 6 | note protocol/uor-gpu/ed_sign.om |
| 7 | note |
| 8 | note Usage: |
| 9 | note sign_ddc selftest <msg_file> <pk_out> <sig_out> |
| 10 | note Uses a public, deterministic RFC 8032 self-test seed (explicitly NOT an operator secret) |
| 11 | note to sign <msg_file>, writes hex pubkey + sig, verifies valid signature (strict mode 0), |
| 12 | note and confirms a 1-bit message tamper is rejected. |
| 13 | note sign_ddc verify <msg_file> <pk_hex_file> <sig_hex_file> |
| 14 | note Verifies an RFC 8032 Ed25519 signature (strict mode 0) over <msg_file>. |
| 15 | |
| 16 | text sdUse "usage: sign_ddc selftest <msg> <pk_out> <sig_out> | verify <msg> <pk_hex> <sig_hex>\n" |
| 17 | text sdDom "HELIX-DDC-RFC8032-PUBLIC-TEST-AUTHORITY-V1" |
| 18 | text sdOkS "DDC_ED25519_SELFTEST PASS bytes=" |
| 19 | text sdOkPk " pubkey=" |
| 20 | text sdOkSg " sig=" |
| 21 | text sdOkTail " verify_valid=1 tamper_rejected=1\n" |
| 22 | text sdOkV "DDC_ED25519_VERIFY PASS bytes=" |
| 23 | text sdFailS "DDC_ED25519_SELFTEST FAIL\n" |
| 24 | text sdFailV "DDC_ED25519_VERIFY FAIL\n" |
| 25 | text sdNl "\n" |
| 26 | |
| 27 | fn sdWrt s view uchar * to void |
| 28 | r = write 1 s 0 len s |
| 29 | end |
| 30 | |
| 31 | fn sdNum v ulong to void |
| 32 | b = new uchar 24 0 |
| 33 | p = ulong 24 |
| 34 | x = v |
| 35 | while true bound 24 |
| 36 | p = sub p 1 |
| 37 | put b p uchar add 48 rem x 10 |
| 38 | x = div x 10 |
| 39 | if eq x 0 |
| 40 | break |
| 41 | end |
| 42 | end |
| 43 | r = write 1 b p sub 24 p |
| 44 | end |
| 45 | |
| 46 | fn sdHexCh v ulong to uchar |
| 47 | if lt v 10 |
| 48 | ret uchar add 48 v |
| 49 | end |
| 50 | ret uchar add 87 v |
| 51 | end |
| 52 | |
| 53 | fn sdParseNib c ulong to long |
| 54 | if and ge c 48 le c 57 |
| 55 | ret long sub c 48 |
| 56 | end |
| 57 | if and ge c 97 le c 102 |
| 58 | ret long add 10 sub c 97 |
| 59 | end |
| 60 | if and ge c 65 le c 70 |
| 61 | ret long add 10 sub c 65 |
| 62 | end |
| 63 | ret long -1 |
| 64 | end |
| 65 | |
| 66 | fn sdWriteHex buf view uchar 64 n ulong to void |
| 67 | o = new uchar 128 0 |
| 68 | for i ulong n bound 64 |
| 69 | b = ulong get buf i |
| 70 | put o mul i 2 sdHexCh shr b 4 |
| 71 | put o add mul i 2 1 sdHexCh and b 15 |
| 72 | end |
| 73 | r = write 1 o 0 mul n 2 |
| 74 | end |
| 75 | |
| 76 | fn sdSaveHex pathIdx ulong buf view uchar 64 n ulong to bool |
| 77 | pth = new uchar 512 0 |
| 78 | pl = argread pathIdx pth 0 510 |
| 79 | if lt pl 1 |
| 80 | ret false |
| 81 | end |
| 82 | fd = fopen pth 0 ulong pl 577 420 |
| 83 | if lt fd 0 |
| 84 | ret false |
| 85 | end |
| 86 | o = new uchar 129 0 |
| 87 | for i ulong n bound 64 |
| 88 | b = ulong get buf i |
| 89 | put o mul i 2 sdHexCh shr b 4 |
| 90 | put o add mul i 2 1 sdHexCh and b 15 |
| 91 | end |
| 92 | put o mul n 2 uchar 10 |
| 93 | wr = write int fd o 0 add mul n 2 1 |
| 94 | rc = fclose int fd |
| 95 | ret eq wr long add mul n 2 1 |
| 96 | end |
| 97 | |
| 98 | fn sdReadFile pathIdx ulong dst edit uchar 65536 maxN ulong to long |
| 99 | pth = new uchar 512 0 |
| 100 | pl = argread pathIdx pth 0 510 |
| 101 | if lt pl 1 |
| 102 | ret long -1 |
| 103 | end |
| 104 | fd = fopen pth 0 ulong pl 0 0 |
| 105 | if lt fd 0 |
| 106 | ret long -1 |
| 107 | end |
| 108 | tot = ulong 0 |
| 109 | while lt tot maxN bound 1024 |
| 110 | r = read int fd dst tot sub maxN tot |
| 111 | if le r 0 |
| 112 | break |
| 113 | end |
| 114 | tot = add tot ulong r |
| 115 | end |
| 116 | rc = fclose int fd |
| 117 | ret long tot |
| 118 | end |
| 119 | |
| 120 | fn sdReadHex32 pathIdx ulong dst edit uchar 32 to bool |
| 121 | buf = new uchar 65536 0 |
| 122 | n = sdReadFile pathIdx buf 256 |
| 123 | if lt n 64 |
| 124 | ret false |
| 125 | end |
| 126 | for i ulong 32 |
| 127 | hi = sdParseNib ulong get buf mul i 2 |
| 128 | lo = sdParseNib ulong get buf add mul i 2 1 |
| 129 | if or lt hi 0 lt lo 0 |
| 130 | ret false |
| 131 | end |
| 132 | put dst i uchar or shl ulong hi 4 ulong lo |
| 133 | end |
| 134 | ret true |
| 135 | end |
| 136 | |
| 137 | fn sdReadHex64 pathIdx ulong dst edit uchar 64 to bool |
| 138 | buf = new uchar 65536 0 |
| 139 | n = sdReadFile pathIdx buf 256 |
| 140 | if lt n 128 |
| 141 | ret false |
| 142 | end |
| 143 | for i ulong 64 |
| 144 | hi = sdParseNib ulong get buf mul i 2 |
| 145 | lo = sdParseNib ulong get buf add mul i 2 1 |
| 146 | if or lt hi 0 lt lo 0 |
| 147 | ret false |
| 148 | end |
| 149 | put dst i uchar or shl ulong hi 4 ulong lo |
| 150 | end |
| 151 | ret true |
| 152 | end |
| 153 | |
| 154 | fn sdDeriveSeed seed edit uchar 32 to void |
| 155 | m0 = new uchar 65536 0 |
| 156 | h0 = new uchar 64 0 |
| 157 | dlen = len sdDom |
| 158 | for i ulong dlen bound 64 |
| 159 | put m0 i get sdDom i |
| 160 | end |
| 161 | s512hash m0 dlen h0 |
| 162 | for i ulong 32 |
| 163 | put seed i get h0 i |
| 164 | end |
| 165 | end |
| 166 | |
| 167 | fn sdDoSelftest to int |
| 168 | msg = new uchar 65536 0 |
| 169 | n = sdReadFile 2 msg 65504 |
| 170 | if le n 0 |
| 171 | sdWrt sdFailS |
| 172 | ret 1 |
| 173 | end |
| 174 | seed = new uchar 32 0 |
| 175 | pk = new uchar 32 0 |
| 176 | sig = new uchar 64 0 |
| 177 | pk64 = new uchar 64 0 |
| 178 | sdDeriveSeed seed |
| 179 | ues_pubkey seed pk |
| 180 | ues_sign seed msg ulong n sig |
| 181 | ok1 = edverify pk msg ulong n sig 0 |
| 182 | put msg 0 xor get msg 0 uchar 1 |
| 183 | ok2 = edverify pk msg ulong n sig 0 |
| 184 | put msg 0 xor get msg 0 uchar 1 |
| 185 | if or ne ok1 1 ne ok2 0 |
| 186 | sdWrt sdFailS |
| 187 | ret 1 |
| 188 | end |
| 189 | for i ulong 32 |
| 190 | put pk64 i get pk i |
| 191 | end |
| 192 | if not sdSaveHex 3 pk64 32 |
| 193 | sdWrt sdFailS |
| 194 | ret 1 |
| 195 | end |
| 196 | if not sdSaveHex 4 sig 64 |
| 197 | sdWrt sdFailS |
| 198 | ret 1 |
| 199 | end |
| 200 | sdWrt sdOkS |
| 201 | sdNum ulong n |
| 202 | sdWrt sdOkPk |
| 203 | sdWriteHex pk64 32 |
| 204 | sdWrt sdOkSg |
| 205 | sdWriteHex sig 64 |
| 206 | sdWrt sdOkTail |
| 207 | ret 0 |
| 208 | end |
| 209 | |
| 210 | fn sdDoVerify to int |
| 211 | msg = new uchar 65536 0 |
| 212 | n = sdReadFile 2 msg 65504 |
| 213 | if le n 0 |
| 214 | sdWrt sdFailV |
| 215 | ret 1 |
| 216 | end |
| 217 | pk = new uchar 32 0 |
| 218 | sig = new uchar 64 0 |
| 219 | if not sdReadHex32 3 pk |
| 220 | sdWrt sdFailV |
| 221 | ret 1 |
| 222 | end |
| 223 | if not sdReadHex64 4 sig |
| 224 | sdWrt sdFailV |
| 225 | ret 1 |
| 226 | end |
| 227 | ok = edverify pk msg ulong n sig 0 |
| 228 | if ne ok 1 |
| 229 | sdWrt sdFailV |
| 230 | ret 1 |
| 231 | end |
| 232 | sdWrt sdOkV |
| 233 | sdNum ulong n |
| 234 | sdWrt sdNl |
| 235 | ret 0 |
| 236 | end |
| 237 | |
| 238 | fn main to int |
| 239 | if lt argcnt 5 |
| 240 | sdWrt sdUse |
| 241 | ret 2 |
| 242 | end |
| 243 | cmd = new uchar 16 0 |
| 244 | cl = argread 1 cmd 0 15 |
| 245 | if lt cl 1 |
| 246 | sdWrt sdUse |
| 247 | ret 2 |
| 248 | end |
| 249 | c0 = ulong get cmd 0 |
| 250 | if eq c0 115 |
| 251 | ret sdDoSelftest |
| 252 | end |
| 253 | if eq c0 118 |
| 254 | ret sdDoVerify |
| 255 | end |
| 256 | sdWrt sdUse |
| 257 | ret 2 |
| 258 | end |