-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbounded_solvers.js
More file actions
311 lines (266 loc) · 8.1 KB
/
Copy pathbounded_solvers.js
File metadata and controls
311 lines (266 loc) · 8.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/**
* Bounded Solvers for Multi-Agent Safety and Validation Tiers.
* Implements:
* 1. Linear-Time Interval Arithmetic Solver (Alternative B)
* 2. Polynomial-Time Horn-Clause Datalog Solver (Alternative A)
*
* Mathematically guarantees that constraint checks never enter exponential-time
* or undecidable paths, preventing denial-of-service or hang vulnerabilities.
*/
// ==========================================
// 1. INTERVAL ARITHMETIC SOLVER (Alternative B)
// ==========================================
class Interval {
constructor(min = -Infinity, max = Infinity) {
this.min = Number(min);
this.max = Number(max);
if (this.min > this.max) {
throw new Error(`Invalid interval bounds: [${min}, ${max}]`);
}
}
static from(val) {
if (val instanceof Interval) return val;
if (Array.isArray(val)) return new Interval(val[0], val[1]);
return new Interval(val, val);
}
add(other) {
const o = Interval.from(other);
return new Interval(this.min + o.min, this.max + o.max);
}
subtract(other) {
const o = Interval.from(other);
return new Interval(this.min - o.max, this.max - o.min);
}
multiply(other) {
const o = Interval.from(other);
const p1 = this.min * o.min;
const p2 = this.min * o.max;
const p3 = this.max * o.min;
const p4 = this.max * o.max;
return new Interval(
Math.min(p1, p2, p3, p4),
Math.max(p1, p2, p3, p4)
);
}
intersect(other) {
const o = Interval.from(other);
const newMin = Math.max(this.min, o.min);
const newMax = Math.min(this.max, o.max);
if (newMin > newMax) return null; // Empty intersection
return new Interval(newMin, newMax);
}
union(other) {
const o = Interval.from(other);
return new Interval(Math.min(this.min, o.min), Math.max(this.max, o.max));
}
contains(val) {
return val >= this.min && val <= this.max;
}
isLessThan(val) {
return this.max < val;
}
isPossiblyLessThan(val) {
return this.min < val;
}
toString() {
return `[${this.min}, ${this.max}]`;
}
}
/**
* Propagates transactions sequentially to verify ledger invariant safety.
* Returns true if balances are guaranteed to stay non-negative.
*
* @param {Object} startingBalances Map of names to intervals/values
* @param {Array} transfers List of {from, to, amount} objects where amount can be interval or value
* @returns {Object} { safe, guaranteedUnsafe, balances }
*/
function analyzeLedgerSafety(startingBalances, transfers) {
const balances = {};
for (const [name, val] of Object.entries(startingBalances)) {
balances[name] = Interval.from(val);
}
let step = 0;
for (const tx of transfers) {
const amount = Interval.from(tx.amount || 0);
const fromBal = balances[tx.from] || new Interval(0, 0);
const toBal = balances[tx.to] || new Interval(0, 0);
// Apply the transaction rules
balances[tx.from] = fromBal.subtract(amount);
balances[tx.to] = toBal.add(amount);
step++;
// Safety Invariant: check if the 'from' account balance could possibly drop below 0
if (balances[tx.from].isPossiblyLessThan(0)) {
const isGuaranteed = balances[tx.from].isLessThan(0);
return {
safe: false,
guaranteedUnsafe: isGuaranteed,
violatingStep: step,
violatingAccount: tx.from,
violatingBalance: balances[tx.from].toString(),
balances
};
}
}
return {
safe: true,
guaranteedUnsafe: false,
balances
};
}
// ==========================================
// 2. HORN-CLAUSE DATALOG SOLVER (Alternative A)
// ==========================================
class DatalogFact {
constructor(relation, args) {
this.relation = String(relation);
this.args = args.map(String);
}
key() {
return `${this.relation}(${this.args.join(',')})`;
}
toString() {
return this.key();
}
}
class DatalogRule {
/**
* @param {Object} head { relation: string, args: string[] }
* @param {Array} body array of { relation: string, args: string[] }
*/
constructor(head, body) {
this.head = head;
this.body = body;
}
}
class DatalogEvaluator {
constructor() {
// Map of relationName -> Set of DatalogFact string keys
this.factsByKey = new Set();
// Map of relationName -> array of DatalogFact objects
this.database = new Map();
this.rules = [];
}
addFact(relation, args) {
const fact = new DatalogFact(relation, args);
const key = fact.key();
if (!this.factsByKey.has(key)) {
this.factsByKey.add(key);
if (!this.database.has(relation)) {
this.database.set(relation, []);
}
this.database.get(relation).push(fact);
return true;
}
return false;
}
addRule(head, body) {
this.rules.push(new DatalogRule(head, body));
}
isVariable(term) {
return typeof term === 'string' && term.length > 0 && term[0] === term[0].toUpperCase() && term[0] !== term[0].toLowerCase();
}
/**
* Unifies a literal against a concrete fact given an existing variable substitution map.
* Returns a new substitution map if unification succeeds, or null if it fails.
*/
unify(literal, fact, sub) {
if (literal.args.length !== fact.args.length) return null;
const nextSub = { ...sub };
for (let i = 0; i < literal.args.length; i++) {
const term = literal.args[i];
const val = fact.args[i];
if (this.isVariable(term)) {
if (term in nextSub) {
if (nextSub[term] !== val) return null;
} else {
nextSub[term] = val;
}
} else {
if (term !== val) return null;
}
}
return nextSub;
}
/**
* Evaluates a rule body recursively, generating all valid substitution mappings.
*/
evaluateBody(body, idx = 0, currentSub = {}) {
if (idx === body.length) {
return [currentSub];
}
const literal = body[idx];
const relationFacts = this.database.get(literal.relation) || [];
let subs = [];
for (const fact of relationFacts) {
const nextSub = this.unify(literal, fact, currentSub);
if (nextSub) {
subs = subs.concat(this.evaluateBody(body, idx + 1, nextSub));
}
}
return subs;
}
/**
* Evaluates rules bottom-up using fixed-point iteration.
* Guaranteed to terminate in polynomial time.
* Max iterations parameter protects against system limits.
*
* @param {number} maxIterations safety cap to prevent CPU exhaustion
* @returns {number} number of iterations run
*/
solve(maxIterations = 100) {
let iterations = 0;
let newFactAdded = true;
while (newFactAdded && iterations < maxIterations) {
newFactAdded = false;
iterations++;
const factsToInsert = [];
for (const rule of this.rules) {
const substitutions = this.evaluateBody(rule.body);
for (const sub of substitutions) {
// Instantiate the head literal using the substitutions
const instantiatedArgs = rule.head.args.map(arg => {
if (this.isVariable(arg)) {
if (!(arg in sub)) {
throw new Error(`Unbound variable ${arg} in rule head for relation ${rule.head.relation}`);
}
return sub[arg];
}
return arg;
});
const candidateFact = new DatalogFact(rule.head.relation, instantiatedArgs);
if (!this.factsByKey.has(candidateFact.key())) {
factsToInsert.push(candidateFact);
}
}
}
for (const fact of factsToInsert) {
if (this.addFact(fact.relation, fact.args)) {
newFactAdded = true;
}
}
}
return iterations;
}
query(relation, args) {
const results = [];
const relationFacts = this.database.get(relation) || [];
for (const fact of relationFacts) {
const sub = this.unify({ relation, args }, fact, {});
if (sub) {
results.push(fact.args);
}
}
return results;
}
hasFact(relation, args) {
const fact = new DatalogFact(relation, args);
return this.factsByKey.has(fact.key());
}
}
module.exports = {
Interval,
analyzeLedgerSafety,
DatalogFact,
DatalogRule,
DatalogEvaluator
};