eyemind 0.4.0 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +28 -3
- package/SPEC.md +595 -344
- package/package.json +1 -1
- package/src/builtins.js +26 -1
- package/src/digest.js +162 -0
- package/src/engine.js +3 -1
- package/src/proof.js +1 -1
- package/src/version.js +1 -1
package/package.json
CHANGED
package/src/builtins.js
CHANGED
|
@@ -15,7 +15,8 @@ import {
|
|
|
15
15
|
EyemindError, Struct, Var, codePoints, compareTerms, copyResolved, deref, identical, isGround, key,
|
|
16
16
|
listFromItems, properListItems, unify,
|
|
17
17
|
} from './terms.js';
|
|
18
|
-
import {
|
|
18
|
+
import { DIGESTS, utf8 } from './digest.js';
|
|
19
|
+
import { floatText, write } from './writer.js';
|
|
19
20
|
|
|
20
21
|
function comparison(holds) {
|
|
21
22
|
return (args, resolved, env) => holds(arith.compare(args[0], args[1], env));
|
|
@@ -191,6 +192,26 @@ function regexReplace(args, resolved, env, bind) {
|
|
|
191
192
|
return bind(args[3], source.replace(compiled, replacement));
|
|
192
193
|
}
|
|
193
194
|
|
|
195
|
+
// atom_lower(+A, ?L), atom_upper(+A, ?U): Unicode default case conversion,
|
|
196
|
+
// with its full mappings, so 'ß' becomes 'SS', and no locale.
|
|
197
|
+
function caseConversion(name, convert) {
|
|
198
|
+
return (args, resolved, env, bind) => bind(args[1], convert(text(name, resolved[0], 'atom')));
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// digest(+Algorithm, +A, ?Hex): the lowercase hexadecimal digest of A's UTF-8
|
|
202
|
+
// bytes, by md5, sha1, sha256 or sha512.
|
|
203
|
+
function digest(args, resolved, env, bind) {
|
|
204
|
+
const algorithm = DIGESTS.get(resolved[0]);
|
|
205
|
+
if (algorithm === undefined) throw new EyemindError(`digest/3 needs md5, sha1, sha256 or sha512, not ${write(resolved[0])}`);
|
|
206
|
+
return bind(args[2], algorithm(utf8(text('digest/3', resolved[1], 'atom'))));
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// term_text(+T, ?A): A is the canonical text of the ground term T.
|
|
210
|
+
function termText(args, resolved, env, bind) {
|
|
211
|
+
if (!isGround(args[0], env)) throw new EyemindError('term_text/2 needs a ground term');
|
|
212
|
+
return bind(args[1], write(copyResolved(args[0], env)));
|
|
213
|
+
}
|
|
214
|
+
|
|
194
215
|
const isNumberValue = (term) => typeof term === 'bigint' || typeof term === 'number';
|
|
195
216
|
|
|
196
217
|
export const TESTS = new Map([
|
|
@@ -224,6 +245,10 @@ export const TESTS = new Map([
|
|
|
224
245
|
['atom_length/2', atomLength],
|
|
225
246
|
['number_chars/2', numberChars],
|
|
226
247
|
['compare/3', compare],
|
|
248
|
+
['atom_lower/2', caseConversion('atom_lower/2', (a) => a.toLowerCase())],
|
|
249
|
+
['atom_upper/2', caseConversion('atom_upper/2', (a) => a.toUpperCase())],
|
|
250
|
+
['digest/3', digest],
|
|
251
|
+
['term_text/2', termText],
|
|
227
252
|
['regex_match/2', regexMatch],
|
|
228
253
|
['regex_replace/4', regexReplace],
|
|
229
254
|
]);
|
package/src/digest.js
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
// Message digests for digest/3: MD5, SHA-1, SHA-256 and SHA-512 of a text's
|
|
2
|
+
// UTF-8 bytes, as lowercase hexadecimal. Written out here, synchronous and
|
|
3
|
+
// without Node, so every host gives the same digest.
|
|
4
|
+
|
|
5
|
+
const encoder = new TextEncoder();
|
|
6
|
+
|
|
7
|
+
// A lone surrogate has no UTF-8 encoding; TextEncoder writes U+FFFD for it.
|
|
8
|
+
export function utf8(text) {
|
|
9
|
+
return encoder.encode(text);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const hex32 = (words, littleEndian = false) => words.map((w) => {
|
|
13
|
+
const value = littleEndian
|
|
14
|
+
? ((w & 0xff) << 24) | (((w >>> 8) & 0xff) << 16) | (((w >>> 16) & 0xff) << 8) | (w >>> 24)
|
|
15
|
+
: w;
|
|
16
|
+
return (value >>> 0).toString(16).padStart(8, '0');
|
|
17
|
+
}).join('');
|
|
18
|
+
|
|
19
|
+
// The message padded to a multiple of the block size, with its bit length
|
|
20
|
+
// at the end in lengthBytes bytes, big- or little-endian.
|
|
21
|
+
function pad(bytes, block, lengthBytes, littleEndian) {
|
|
22
|
+
const total = Math.ceil((bytes.length + 1 + lengthBytes) / block) * block;
|
|
23
|
+
const out = new Uint8Array(total);
|
|
24
|
+
out.set(bytes);
|
|
25
|
+
out[bytes.length] = 0x80;
|
|
26
|
+
let bits = BigInt(bytes.length) * 8n;
|
|
27
|
+
for (let i = 0; i < lengthBytes; i++) {
|
|
28
|
+
const position = littleEndian ? total - lengthBytes + i : total - 1 - i;
|
|
29
|
+
out[position] = Number(bits & 0xffn);
|
|
30
|
+
bits >>= 8n;
|
|
31
|
+
}
|
|
32
|
+
return out;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const rotl = (x, n) => (x << n) | (x >>> (32 - n));
|
|
36
|
+
const rotr = (x, n) => (x >>> n) | (x << (32 - n));
|
|
37
|
+
|
|
38
|
+
function md5(bytes) {
|
|
39
|
+
const s = [7, 12, 17, 22, 5, 9, 14, 20, 4, 11, 16, 23, 6, 10, 15, 21];
|
|
40
|
+
const k = Array.from({ length: 64 }, (_, i) => Math.floor(Math.abs(Math.sin(i + 1)) * 2 ** 32) | 0);
|
|
41
|
+
const m = pad(bytes, 64, 8, true);
|
|
42
|
+
let [a0, b0, c0, d0] = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476];
|
|
43
|
+
for (let offset = 0; offset < m.length; offset += 64) {
|
|
44
|
+
const w = Array.from({ length: 16 }, (_, i) => m[offset + 4 * i] | (m[offset + 4 * i + 1] << 8) |
|
|
45
|
+
(m[offset + 4 * i + 2] << 16) | (m[offset + 4 * i + 3] << 24));
|
|
46
|
+
let [a, b, c, d] = [a0, b0, c0, d0];
|
|
47
|
+
for (let i = 0; i < 64; i++) {
|
|
48
|
+
let f;
|
|
49
|
+
let g;
|
|
50
|
+
if (i < 16) [f, g] = [(b & c) | (~b & d), i];
|
|
51
|
+
else if (i < 32) [f, g] = [(d & b) | (~d & c), (5 * i + 1) % 16];
|
|
52
|
+
else if (i < 48) [f, g] = [b ^ c ^ d, (3 * i + 5) % 16];
|
|
53
|
+
else [f, g] = [c ^ (b | ~d), (7 * i) % 16];
|
|
54
|
+
const next = d;
|
|
55
|
+
d = c;
|
|
56
|
+
c = b;
|
|
57
|
+
b = (b + rotl((a + f + k[i] + w[g]) | 0, s[(i >> 4) * 4 + (i % 4)])) | 0;
|
|
58
|
+
a = next;
|
|
59
|
+
}
|
|
60
|
+
[a0, b0, c0, d0] = [(a0 + a) | 0, (b0 + b) | 0, (c0 + c) | 0, (d0 + d) | 0];
|
|
61
|
+
}
|
|
62
|
+
return hex32([a0, b0, c0, d0], true);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Big-endian 32-bit words of a padded 64-byte block.
|
|
66
|
+
const words = (m, offset, count) => Array.from({ length: count }, (_, i) => (m[offset + 4 * i] << 24) |
|
|
67
|
+
(m[offset + 4 * i + 1] << 16) | (m[offset + 4 * i + 2] << 8) | m[offset + 4 * i + 3]);
|
|
68
|
+
|
|
69
|
+
function sha1(bytes) {
|
|
70
|
+
const m = pad(bytes, 64, 8, false);
|
|
71
|
+
let h = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0];
|
|
72
|
+
for (let offset = 0; offset < m.length; offset += 64) {
|
|
73
|
+
const w = words(m, offset, 16);
|
|
74
|
+
for (let i = 16; i < 80; i++) w[i] = rotl(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1);
|
|
75
|
+
let [a, b, c, d, e] = h;
|
|
76
|
+
for (let i = 0; i < 80; i++) {
|
|
77
|
+
const [f, k] = i < 20 ? [(b & c) | (~b & d), 0x5a827999] : i < 40 ? [b ^ c ^ d, 0x6ed9eba1]
|
|
78
|
+
: i < 60 ? [(b & c) | (b & d) | (c & d), 0x8f1bbcdc] : [b ^ c ^ d, 0xca62c1d6];
|
|
79
|
+
const t = (rotl(a, 5) + f + e + k + w[i]) | 0;
|
|
80
|
+
[e, d, c, b, a] = [d, c, rotl(b, 30), a, t];
|
|
81
|
+
}
|
|
82
|
+
h = h.map((x, i) => (x + [a, b, c, d, e][i]) | 0);
|
|
83
|
+
}
|
|
84
|
+
return hex32(h);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const K256 = [
|
|
88
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
|
89
|
+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
|
90
|
+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
91
|
+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
|
92
|
+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
|
93
|
+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
94
|
+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
|
95
|
+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
|
|
96
|
+
];
|
|
97
|
+
|
|
98
|
+
function sha256(bytes) {
|
|
99
|
+
const m = pad(bytes, 64, 8, false);
|
|
100
|
+
let h = [0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19];
|
|
101
|
+
for (let offset = 0; offset < m.length; offset += 64) {
|
|
102
|
+
const w = words(m, offset, 16);
|
|
103
|
+
for (let i = 16; i < 64; i++) {
|
|
104
|
+
const s0 = rotr(w[i - 15], 7) ^ rotr(w[i - 15], 18) ^ (w[i - 15] >>> 3);
|
|
105
|
+
const s1 = rotr(w[i - 2], 17) ^ rotr(w[i - 2], 19) ^ (w[i - 2] >>> 10);
|
|
106
|
+
w[i] = (w[i - 16] + s0 + w[i - 7] + s1) | 0;
|
|
107
|
+
}
|
|
108
|
+
let [a, b, c, d, e, f, g, k] = h;
|
|
109
|
+
for (let i = 0; i < 64; i++) {
|
|
110
|
+
const t1 = (k + (rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25)) + ((e & f) ^ (~e & g)) + K256[i] + w[i]) | 0;
|
|
111
|
+
const t2 = ((rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22)) + ((a & b) ^ (a & c) ^ (b & c))) | 0;
|
|
112
|
+
[k, g, f, e, d, c, b, a] = [g, f, e, (d + t1) | 0, c, b, a, (t1 + t2) | 0];
|
|
113
|
+
}
|
|
114
|
+
h = h.map((x, i) => (x + [a, b, c, d, e, f, g, k][i]) | 0);
|
|
115
|
+
}
|
|
116
|
+
return hex32(h);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// SHA-512 works on 64-bit words, kept as BigInts.
|
|
120
|
+
const MASK64 = (1n << 64n) - 1n;
|
|
121
|
+
const K512 = `428a2f98d728ae22 7137449123ef65cd b5c0fbcfec4d3b2f e9b5dba58189dbbc 3956c25bf348b538 59f111f1b605d019
|
|
122
|
+
923f82a4af194f9b ab1c5ed5da6d8118 d807aa98a3030242 12835b0145706fbe 243185be4ee4b28c 550c7dc3d5ffb4e2 72be5d74f27b896f
|
|
123
|
+
80deb1fe3b1696b1 9bdc06a725c71235 c19bf174cf692694 e49b69c19ef14ad2 efbe4786384f25e3 0fc19dc68b8cd5b5 240ca1cc77ac9c65
|
|
124
|
+
2de92c6f592b0275 4a7484aa6ea6e483 5cb0a9dcbd41fbd4 76f988da831153b5 983e5152ee66dfab a831c66d2db43210 b00327c898fb213f
|
|
125
|
+
bf597fc7beef0ee4 c6e00bf33da88fc2 d5a79147930aa725 06ca6351e003826f 142929670a0e6e70 27b70a8546d22ffc 2e1b21385c26c926
|
|
126
|
+
4d2c6dfc5ac42aed 53380d139d95b3df 650a73548baf63de 766a0abb3c77b2a8 81c2c92e47edaee6 92722c851482353b a2bfe8a14cf10364
|
|
127
|
+
a81a664bbc423001 c24b8b70d0f89791 c76c51a30654be30 d192e819d6ef5218 d69906245565a910 f40e35855771202a 106aa07032bbd1b8
|
|
128
|
+
19a4c116b8d2d0c8 1e376c085141ab53 2748774cdf8eeb99 34b0bcb5e19b48a8 391c0cb3c5c95a63 4ed8aa4ae3418acb 5b9cca4f7763e373
|
|
129
|
+
682e6ff3d6b2b8a3 748f82ee5defb2fc 78a5636f43172f60 84c87814a1f0ab72 8cc702081a6439ec 90befffa23631e28 a4506cebde82bde9
|
|
130
|
+
bef9a3f7b2c67915 c67178f2e372532b ca273eceea26619c d186b8c721c0c207 eada7dd6cde0eb1e f57d4f7fee6ed178 06f067aa72176fba
|
|
131
|
+
0a637dc5a2c898a6 113f9804bef90dae 1b710b35131c471b 28db77f523047d84 32caab7b40c72493 3c9ebe0a15c9bebc 431d67c49c100d4c
|
|
132
|
+
4cc5d4becb3e42b6 597f299cfc657e2a 5fcb6fab3ad6faec 6c44198c4a475817`.split(/\s+/).map((x) => BigInt(`0x${x}`));
|
|
133
|
+
const rotr64 = (x, n) => ((x >> n) | (x << (64n - n))) & MASK64;
|
|
134
|
+
|
|
135
|
+
function sha512(bytes) {
|
|
136
|
+
const m = pad(bytes, 128, 16, false);
|
|
137
|
+
let h = `6a09e667f3bcc908 bb67ae8584caa73b 3c6ef372fe94f82b a54ff53a5f1d36f1 510e527fade682d1 9b05688c2b3e6c1f
|
|
138
|
+
1f83d9abfb41bd6b 5be0cd19137e2179`.split(/\s+/).map((x) => BigInt(`0x${x}`));
|
|
139
|
+
for (let offset = 0; offset < m.length; offset += 128) {
|
|
140
|
+
const w = [];
|
|
141
|
+
for (let i = 0; i < 16; i++) {
|
|
142
|
+
let x = 0n;
|
|
143
|
+
for (let j = 0; j < 8; j++) x = (x << 8n) | BigInt(m[offset + 8 * i + j]);
|
|
144
|
+
w.push(x);
|
|
145
|
+
}
|
|
146
|
+
for (let i = 16; i < 80; i++) {
|
|
147
|
+
const s0 = rotr64(w[i - 15], 1n) ^ rotr64(w[i - 15], 8n) ^ (w[i - 15] >> 7n);
|
|
148
|
+
const s1 = rotr64(w[i - 2], 19n) ^ rotr64(w[i - 2], 61n) ^ (w[i - 2] >> 6n);
|
|
149
|
+
w[i] = (w[i - 16] + s0 + w[i - 7] + s1) & MASK64;
|
|
150
|
+
}
|
|
151
|
+
let [a, b, c, d, e, f, g, k] = h;
|
|
152
|
+
for (let i = 0; i < 80; i++) {
|
|
153
|
+
const t1 = (k + (rotr64(e, 14n) ^ rotr64(e, 18n) ^ rotr64(e, 41n)) + ((e & f) ^ (~e & MASK64 & g)) + K512[i] + w[i]) & MASK64;
|
|
154
|
+
const t2 = ((rotr64(a, 28n) ^ rotr64(a, 34n) ^ rotr64(a, 39n)) + ((a & b) ^ (a & c) ^ (b & c))) & MASK64;
|
|
155
|
+
[k, g, f, e, d, c, b, a] = [g, f, e, (d + t1) & MASK64, c, b, a, (t1 + t2) & MASK64];
|
|
156
|
+
}
|
|
157
|
+
h = h.map((x, i) => (x + [a, b, c, d, e, f, g, k][i]) & MASK64);
|
|
158
|
+
}
|
|
159
|
+
return h.map((x) => x.toString(16).padStart(16, '0')).join('');
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export const DIGESTS = new Map([['md5', md5], ['sha1', sha1], ['sha256', sha256], ['sha512', sha512]]);
|
package/src/engine.js
CHANGED
|
@@ -1031,9 +1031,11 @@ function certify(solver, boundaries, operational = false) {
|
|
|
1031
1031
|
if (outcome.status === 'settled' && (!operational || positiveListings(program, outcome.listings))) {
|
|
1032
1032
|
for (const item of outcome.tables) tables.add(item);
|
|
1033
1033
|
for (const item of outcome.listings) listings.add(item);
|
|
1034
|
-
} else if (outcome.status === 'failed') {
|
|
1034
|
+
} else if (outcome.status === 'failed' && solver.haltCode === null) {
|
|
1035
1035
|
throw new EyemindError(`cannot certify this result: ${outcome.failures[0]?.[0] ?? 'its search certificate fails'}`);
|
|
1036
1036
|
}
|
|
1037
|
+
// A halted run stopped before its fixpoint, so its derived facts need not
|
|
1038
|
+
// be closed under the rules; a boundary they cannot settle stays trusted.
|
|
1037
1039
|
}
|
|
1038
1040
|
return {
|
|
1039
1041
|
// Tables in the order their calls were first met.
|
package/src/proof.js
CHANGED
|
@@ -57,7 +57,7 @@ function sameValue(a, b) {
|
|
|
57
57
|
// Whether term, read through env, is exactly target, variables included.
|
|
58
58
|
// The functions whose float results may differ between hosts' math libraries
|
|
59
59
|
// (SPEC Section 14), and how far apart, in units in the last place, a claimed
|
|
60
|
-
// result of one of them may be from the recomputed one (Section 11.
|
|
60
|
+
// result of one of them may be from the recomputed one (Section 11.8).
|
|
61
61
|
const HOST_MATH = new Set(['exp', 'log', 'log2', 'log10', 'sin', 'cos', 'tan', 'asin', 'acos', 'atan', 'atan2',
|
|
62
62
|
'sinh', 'cosh', 'tanh', 'pow']);
|
|
63
63
|
const HOST_MATH_ULPS = 4n;
|
package/src/version.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// The release this checkout is; ./release keeps it in step with package.json.
|
|
2
|
-
export const VERSION = '0.
|
|
2
|
+
export const VERSION = '0.5.1';
|