-
Notifications
You must be signed in to change notification settings - Fork 1
/
bristol2btcscript.js
274 lines (245 loc) · 9.01 KB
/
bristol2btcscript.js
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
const fs = require('fs');
const process = require('process');
const msgpack = require("@msgpack/msgpack");
const arr = msgpack.decode(fs.readFileSync(process.argv[2]));
function arr2circuit(arr) {
let p = 0;
let n = arr[p];
++p;
let m = arr[p];
++p;
let q = arr[p];
let O = Array(m).fill(0);
let A = Array(q).fill(0);
let B = Array(q).fill(0);
let G = Array(q).fill(0);
let i;
for (i = 0; i < q; i++) {
++p;
A[i] = arr[p];
}
for (i = 0; i < q; i++) {
++p;
B[i] = arr[p];
}
for (i = 0; i < q; i++) {
++p;
switch (arr[p]) {
case 8: G[i] = "AND"; break;
case 6: G[i] = "XOR"; break;
case 3: G[i] = "NOT"; break;
case 15: G[i] = "ONE"; break;
case 14: G[i] = "OR"; break;
default: throw "unknown gate: " + arr[p];
}
}
for (i = 0; i < m; i++) {
++p;
O[i] = arr[p];
}
return { m, n, q, O, A, B, G, dummy: n }
}
const circuit = arr2circuit(arr);
let stackToWire = []
for (let i = 0; i < circuit.n; i++) {
stackToWire.unshift(i);
console.log("// input: " + i);
console.log("<0>");
}
console.log("// output wires: " + JSON.stringify(circuit.O));
function findWireValueOnStack(stackToWire, wire) {
for (let i = 0; i < stackToWire.length; i++) {
if (stackToWire[i] == wire) {
return i;
}
}
throw "wire value " + wire + " not found";
}
function findWireValueOnStackNoThrow(stackToWire, wire) {
for (let i = 0; i < stackToWire.length; i++) {
if (stackToWire[i] == wire) {
return i;
}
}
return -1;
}
function pushWireValueOnStack(stackToWire, wire) {
// console.log("// pushing " + wire);
// console.log("// before: " + JSON.stringify(stackToWire));
stackToWire.unshift(wire);
// console.log("// after: " + JSON.stringify(stackToWire));
if (stackToWire.length > 900) {
console.log(stackToWire);
throw "too high stack";
}
}
function removeWireValueFromStack(stackToWire, wire) {
// console.log("// removing: " + wire);
// console.log("// before: " + JSON.stringify(stackToWire));
stackToWire.splice(findWireValueOnStack(stackToWire, wire), 1);
// console.log("// after: " + JSON.stringify(stackToWire));
}
for (let i = 0; i < circuit.q; i++) {
const outputWire = circuit.n + 1 + i;
const gateType = circuit.G[i];
if (["AND", "OR", "XOR"].includes(gateType)) {
console.log("// " + JSON.stringify({a: circuit.A[i], b: circuit.B[i], gate: gateType, output: outputWire}));
const leftWireOnStack = findWireValueOnStack(stackToWire, circuit.A[i]);
console.log("<" + (leftWireOnStack) + ">");
console.log("OP_PICK");
stackToWire.unshift(0xFFFFFFF);
const rightWireOnStack = findWireValueOnStack(stackToWire, circuit.B[i]);
console.log("<" + (rightWireOnStack) + ">");
console.log("OP_PICK");
if (gateType == "AND") {
console.log("OP_BOOLAND");
} else if (gateType == "OR") {
console.log("OP_BOOLOR");
} else if (gateType == "XOR") {
console.log("// OP_BOOLXOR start");
console.log("OP_NUMNOTEQUAL");
console.log("// OP_BOOLXOR end");
}
stackToWire.shift();
pushWireValueOnStack(stackToWire, outputWire);
/*
let left_found = false;
let right_found = false;
for (let j = i + 1; j < circuit.q; j++) {
if (circuit.gates[j].inputs.includes(gate.inputs[0])) {
left_found = true;
}
if (gates[j].inputs.includes(gate.inputs[1])) {
right_found = true;
}
}
console.log("// " + JSON.stringify(gate));
console.log("// " + JSON.stringify({left_found, right_found}));
const leftWireOnStack = findWireValueOnStack(stackToWire, gate.inputs[0]);
console.log("<" + (leftWireOnStack) + ">");
console.log("OP_PICK");
// stack: [left wire]
stackToWire.unshift(0xFFFFFFF);
const rightWireOnStack = findWireValueOnStack(stackToWire, gate.inputs[1]);
console.log("<" + (rightWireOnStack) + ">");
console.log("OP_PICK");
stackToWire.unshift(0xFFFFFFF);
// stack: 0: [right wire] 1: [left wire]
if (gate.type == "AND") {
console.log("OP_BOOLAND");
} else if (gate.type == "OR") {
console.log("OP_BOOLOR");
} else if (gate.type == "XOR") {
console.log("// OP_BOOLXOR start");
console.log("OP_NUMNOTEQUAL");
console.log("// OP_BOOLXOR end");
}
stackToWire.shift();
stackToWire.shift();
// stack: 0: [right op left]
pushWireValueOnStack(stackToWire, gate.output);
// if we don't use it anymore, get rid of it
if (gate.inputs[0] == gate.inputs[1] && !left_found) {
if (gate.inputs[0] > numWires - numOutputs + 1 && gate.inputs[0] < numWires) {
console.log("// refusing to get rid of output " + gate.inputs[0]);
} else {
console.log("// get rid of wire " + gate.inputs[0]);
const leftWireOnStack = findWireValueOnStack(stackToWire, gate.inputs[0]);
console.log("<" + leftWireOnStack + ">");
console.log("OP_ROLL");
console.log("OP_DROP");
removeWireValueFromStack(stackToWire, gate.inputs[0]);
}
} else if (!left_found) {
if (gate.inputs[0] > numWires - numOutputs + 1 && gate.inputs[0] < numWires) {
console.log("// refusing to get rid of output " + gate.inputs[0]);
} else {
console.log("// get rid of wire " + gate.inputs[0]);
const leftWireOnStack = findWireValueOnStack(stackToWire, gate.inputs[0]);
console.log("<" + leftWireOnStack + ">");
console.log("OP_ROLL");
console.log("OP_DROP");
removeWireValueFromStack(stackToWire, gate.inputs[0]);
}
}
if (!right_found && gate.inputs[0] != gate.inputs[1]) {
if (gate.inputs[1] > numWires - numOutputs - 1 && gate.inputs[1] < numWires) {
console.log("// refusing to get rid of right output " + gate.inputs[1]);
} else {
const newWire = findWireValueOnStack(stackToWire, gate.inputs[1]);
console.log("// get rid of wire right " + gate.inputs[1]);
console.log("<" + newWire + ">");
console.log("OP_ROLL");
console.log("OP_DROP");
removeWireValueFromStack(stackToWire, gate.inputs[1]);
}
}
*/
console.log("// end gate");
} else if (["NOT"].includes(gateType)) {
console.log("// " + JSON.stringify({a: circuit.A[i], b: circuit.B[i], gate: circuit.G[i], output: outputWire}));
if (circuit.A[i] == circuit.dummy) {
throw "left side is using dummy wire";
}
const leftWireOnStack = findWireValueOnStack(stackToWire, circuit.A[i]);
console.log("<" + (leftWireOnStack) + ">");
console.log("OP_PICK");
console.log("OP_NOT");
pushWireValueOnStack(stackToWire, outputWire);
console.log("// end gate");
} else {
throw "unknown gate type: " + gateType;
}
function gcWire(stackToWire, index, circuit, wire) {
let found = false;
if (circuit.dummy == wire) {
return;
}
if (circuit.O.includes(wire)) {
return;
}
for (let j = index + 1; j < circuit.q; j++) {
if (circuit.A[j] == wire || circuit.B[j] == wire) {
found = true;
break;
}
}
if (found) {
return;
}
let stackIndex = findWireValueOnStackNoThrow(stackToWire, wire);
if (stackIndex !== -1) {
console.log("// GC'ing wire " + wire);
console.log("<" + (stackIndex) + ">");
console.log("OP_ROLL");
console.log("OP_DROP");
removeWireValueFromStack(stackToWire, wire);
}
}
gcWire(stackToWire, i, circuit, circuit.A[i]);
gcWire(stackToWire, i, circuit, circuit.B[i]);
}
/* for (let i = 0; i < circuit.q; i++) {
const outputWire = circuit.n + 1 + i;
if (!circuit.O.includes(outputWire)) {
const leftWireOnStack = findWireValueOnStack(stackToWire, outputWire);
removeWireValueFromStack(stackToWire, outputWire);
console.log("// removing non-final output wire " + outputWire);
console.log("<" + leftWireOnStack + ">");
console.log("OP_ROLL");
console.log("OP_DROP");
}
} */
for (let i = 0; i < circuit.n; i++) {
if (!circuit.O.includes(i)) {
const leftWireOnStack = findWireValueOnStackNoThrow(stackToWire, i);
if (leftWireOnStack == -1) {
continue;
}
removeWireValueFromStack(stackToWire, i);
console.log("// removing unused?? non-output input wire " + i);
console.log("<" + leftWireOnStack + ">");
console.log("OP_ROLL");
console.log("OP_DROP");
}
}