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 / om / runtime / rx_mid.om
| 1 | om2 |
| 2 | note rx_mid.om -- Canonical AIEN Machine ID codec, comparison, and hashing in pure OM-2. |
| 3 | note Replaces the pure byte-codec, hex-codec, equality, ordering, and hash logic |
| 4 | note of src/runtime/aien_machine_id.h and src/runtime/aien_machine_id.c. |
| 5 | |
| 6 | const MID_OK long 0 |
| 7 | const MID_E_ARG long -1 |
| 8 | const MID_E_ZERO long -2 |
| 9 | const MID_E_FORMAT long -3 |
| 10 | const MID_E_CHECK long -4 |
| 11 | |
| 12 | fn is_zero_32 buf view uchar 32 to ulong |
| 13 | acc = uchar 0 |
| 14 | for i ulong 32 |
| 15 | acc = or acc get buf i |
| 16 | end |
| 17 | if eq acc 0 |
| 18 | ret 1 |
| 19 | end |
| 20 | ret 0 |
| 21 | end |
| 22 | |
| 23 | fn root_valid r ulong to ulong |
| 24 | if eq r 1 |
| 25 | ret 1 |
| 26 | end |
| 27 | if eq r 2 |
| 28 | ret 1 |
| 29 | end |
| 30 | if eq r 3 |
| 31 | ret 1 |
| 32 | end |
| 33 | ret 0 |
| 34 | end |
| 35 | |
| 36 | fn hex_nibble c uchar to long |
| 37 | if ge c 48 |
| 38 | if le c 57 |
| 39 | ret long sub c 48 |
| 40 | end |
| 41 | end |
| 42 | if ge c 97 |
| 43 | if le c 102 |
| 44 | ret add long sub c 97 10 |
| 45 | end |
| 46 | end |
| 47 | ret -1 |
| 48 | end |
| 49 | |
| 50 | fn nibble_hex v uchar to uchar |
| 51 | if lt v 10 |
| 52 | ret add v 48 |
| 53 | end |
| 54 | ret add sub v 10 97 |
| 55 | end |
| 56 | |
| 57 | export fn rx_mid_is_zero buf view uchar 32 to ulong |
| 58 | ret is_zero_32 buf |
| 59 | end |
| 60 | |
| 61 | export fn rx_mid_equal a view uchar 32 b view uchar 32 to ulong |
| 62 | for i ulong 32 |
| 63 | if ne get a i get b i |
| 64 | ret 0 |
| 65 | end |
| 66 | end |
| 67 | ret 1 |
| 68 | end |
| 69 | |
| 70 | export fn rx_mid_compare a view uchar 32 b view uchar 32 to long |
| 71 | for i ulong 32 |
| 72 | va = get a i |
| 73 | vb = get b i |
| 74 | if lt va vb |
| 75 | ret -1 |
| 76 | end |
| 77 | if gt va vb |
| 78 | ret 1 |
| 79 | end |
| 80 | end |
| 81 | ret 0 |
| 82 | end |
| 83 | |
| 84 | export fn rx_mid_hash buf view uchar 32 to ulong |
| 85 | h = ulong 0 |
| 86 | for i ulong 8 |
| 87 | b = ulong get buf i |
| 88 | h = or shl h 8 b |
| 89 | end |
| 90 | ret h |
| 91 | end |
| 92 | |
| 93 | note Pack the 40-byte header+id prefix of an AMID record (caller fills check bytes 40..43). |
| 94 | export fn rx_mid_pack_hdr root ulong id view uchar 32 rec edit uchar 44 to long |
| 95 | if eq root_valid root 0 |
| 96 | ret MID_E_ARG |
| 97 | end |
| 98 | if eq is_zero_32 id 1 |
| 99 | ret MID_E_ZERO |
| 100 | end |
| 101 | put rec 0 uchar 65 |
| 102 | put rec 1 uchar 77 |
| 103 | put rec 2 uchar 73 |
| 104 | put rec 3 uchar 68 |
| 105 | put rec 4 uchar 1 |
| 106 | put rec 5 uchar and root 255 |
| 107 | put rec 6 uchar 0 |
| 108 | put rec 7 uchar 0 |
| 109 | for i ulong 32 |
| 110 | put rec add i 8 get id i |
| 111 | end |
| 112 | ret MID_OK |
| 113 | end |
| 114 | |
| 115 | note Validate a 44-byte AMID record against expected 4-byte SHA-256 check prefix. |
| 116 | note On success, writes dst[0]=root and dst[1..32]=id. |
| 117 | export fn rx_mid_val_rec rec view uchar 44 chk view uchar 4 dst edit uchar 33 to long |
| 118 | if ne get rec 0 65 |
| 119 | ret MID_E_FORMAT |
| 120 | end |
| 121 | if ne get rec 1 77 |
| 122 | ret MID_E_FORMAT |
| 123 | end |
| 124 | if ne get rec 2 73 |
| 125 | ret MID_E_FORMAT |
| 126 | end |
| 127 | if ne get rec 3 68 |
| 128 | ret MID_E_FORMAT |
| 129 | end |
| 130 | if ne get rec 4 1 |
| 131 | ret MID_E_FORMAT |
| 132 | end |
| 133 | if ne get rec 6 0 |
| 134 | ret MID_E_FORMAT |
| 135 | end |
| 136 | if ne get rec 7 0 |
| 137 | ret MID_E_FORMAT |
| 138 | end |
| 139 | r = ulong get rec 5 |
| 140 | if eq root_valid r 0 |
| 141 | ret MID_E_FORMAT |
| 142 | end |
| 143 | for i ulong 4 |
| 144 | if ne get rec add i 40 get chk i |
| 145 | ret MID_E_CHECK |
| 146 | end |
| 147 | end |
| 148 | acc = uchar 0 |
| 149 | for j ulong 32 |
| 150 | b = get rec add j 8 |
| 151 | acc = or acc b |
| 152 | put dst add j 1 b |
| 153 | end |
| 154 | if eq acc 0 |
| 155 | ret MID_E_ZERO |
| 156 | end |
| 157 | put dst 0 get rec 5 |
| 158 | ret MID_OK |
| 159 | end |
| 160 | |
| 161 | note Encode 44 raw bytes to 88 lowercase hex ASCII bytes. |
| 162 | export fn rx_mid_to_hex rec view uchar 44 hex edit uchar 88 to void |
| 163 | for i ulong 44 |
| 164 | v = get rec i |
| 165 | hi = shr v 4 |
| 166 | lo = and v 15 |
| 167 | put hex mul i 2 nibble_hex hi |
| 168 | put hex add mul i 2 1 nibble_hex lo |
| 169 | end |
| 170 | end |
| 171 | |
| 172 | note Decode 88 lowercase hex ASCII bytes to 44 raw bytes. |
| 173 | export fn rx_mid_from_hex hex view uchar 88 rec edit uchar 44 to long |
| 174 | for i ulong 44 |
| 175 | hi = hex_nibble get hex mul i 2 |
| 176 | if lt hi 0 |
| 177 | ret MID_E_FORMAT |
| 178 | end |
| 179 | lo = hex_nibble get hex add mul i 2 1 |
| 180 | if lt lo 0 |
| 181 | ret MID_E_FORMAT |
| 182 | end |
| 183 | bv = or shl ulong hi 4 ulong lo |
| 184 | put rec i uchar and bv 255 |
| 185 | end |
| 186 | ret MID_OK |
| 187 | end |