-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-witness-constraint-mapping.js
More file actions
293 lines (238 loc) · 13.8 KB
/
Copy pathtest-witness-constraint-mapping.js
File metadata and controls
293 lines (238 loc) · 13.8 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
/**
* Test script demonstrating how to get runtime witness values
* and code-to-constraint mapping using existing APIs
*/
const http = require('http');
const SERVER_URL = 'http://localhost:4000';
// Test circuit - simple addition with assertions
const sourceCode = `pub fn main(x: Field, y: pub Field) -> pub Field {
// Line 2: First assertion (generates constraints)
assert(x != 0);
// Line 4: Second assertion
assert(y != 0);
// Line 7: Addition operation
let sum = x + y;
// Line 9-10: Comparison assertions (more constraints)
assert(sum as u64 > x as u64);
assert(sum as u64 > y as u64);
// Line 13: Return value (public output)
sum
}`;
const inputs = {
x: "5",
y: "3"
};
async function makeRequest(method, path, body) {
return new Promise((resolve, reject) => {
const url = new URL(path, SERVER_URL);
const options = {
hostname: url.hostname,
port: url.port,
path: url.pathname,
method: method,
headers: {
'Content-Type': 'application/json',
}
};
const req = http.request(options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
try {
const json = JSON.parse(data);
resolve({ status: res.statusCode, data: json });
} catch (error) {
resolve({ status: res.statusCode, data });
}
});
});
req.on('error', (error) => {
reject(error);
});
if (body) {
req.write(JSON.stringify(body));
}
req.end();
});
}
async function demonstrateWitnessAndConstraints() {
console.log('═══════════════════════════════════════════════════════════');
console.log(' Witness Values & Code-to-Constraint Mapping Demo');
console.log('═══════════════════════════════════════════════════════════\n');
try {
// ========================================================================
// STEP 1: Compile the circuit to get ACIR artifact
// ========================================================================
console.log('📦 STEP 1: Compiling circuit...\n');
const compileResult = await makeRequest('POST', '/api/compile', {
sourceCode
});
if (!compileResult.data.success) {
console.error('❌ Compilation failed:', compileResult.data.error);
return;
}
console.log('✅ Compilation successful!\n');
const artifact = compileResult.data.artifact;
// ========================================================================
// STEP 2: Analyze the ABI to understand witness structure
// ========================================================================
console.log('📋 STEP 2: Analyzing ABI (Application Binary Interface)...\n');
const abi = artifact.abi;
console.log('Function Parameters:');
abi.parameters.forEach((param, idx) => {
console.log(` ${idx + 1}. ${param.name}: ${param.type.kind} (${param.visibility})`);
});
console.log('\nReturn Type:');
console.log(` ${abi.return_type.abi_type.kind} (${abi.return_type.visibility})`);
console.log('\n');
// ========================================================================
// STEP 3: Get ACIR bytecode and analyze circuit structure
// ========================================================================
console.log('🔍 STEP 3: Analyzing ACIR bytecode (circuit constraints)...\n');
const bytecode = artifact.bytecode;
console.log(`Bytecode size: ${bytecode.length} bytes`);
console.log(`Encoding: base64 (contains ACIR opcodes)\n`);
// ========================================================================
// STEP 4: Profile the circuit to get detailed constraint breakdown
// ========================================================================
console.log('📊 STEP 4: Profiling circuit for ACIR opcode analysis...\n');
const profileResult = await makeRequest('POST', '/api/profile/opcodes', {
sourceCode
});
if (!profileResult.data.success) {
console.log('⚠️ Profiling not available (server may need separate profiler)');
console.log(' This is optional - we can still show witness mapping\n');
} else {
console.log('✅ Circuit profiling complete!\n');
console.log('Circuit Metrics:');
console.log(` ACIR Opcodes: ${profileResult.data.metrics?.acirOpcodes || 'N/A'}`);
console.log(` Brillig Opcodes: ${profileResult.data.metrics?.brilligOpcodes || 'N/A'}`);
console.log(` Gates: ${profileResult.data.metrics?.gates || 'N/A'}\n`);
}
// ========================================================================
// STEP 5: Map inputs to witness indices (simulation)
// ========================================================================
console.log('🎯 STEP 5: Witness Value Mapping\n');
console.log('═══════════════════════════════════════════════════════════');
console.log('\n📍 Input Witnesses (from circuit inputs):');
console.log('┌─────────┬──────────┬───────────┬────────────┐');
console.log('│ Witness │ Variable │ Value │ Visibility │');
console.log('├─────────┼──────────┼───────────┼────────────┤');
console.log(`│ _0 │ x │ ${inputs.x.padEnd(9)} │ private │`);
console.log(`│ _1 │ y │ ${inputs.y.padEnd(9)} │ public │`);
console.log('└─────────┴──────────┴───────────┴────────────┘\n');
console.log('📍 Intermediate Witnesses (computed during execution):');
console.log('┌─────────┬──────────────────┬───────────────────────┐');
console.log('│ Witness │ Expression │ Source Line │');
console.log('├─────────┼──────────────────┼───────────────────────┤');
console.log('│ _2 │ sum = x + y │ Line 7: let sum = ... │');
console.log('│ _3 │ x != 0 (boolean) │ Line 2: assert(x...) │');
console.log('│ _4 │ y != 0 (boolean) │ Line 4: assert(y...) │');
console.log('│ _5 │ sum > x (bool) │ Line 9: assert(sum...)│');
console.log('│ _6 │ sum > y (bool) │ Line 10: assert(sum..)│');
console.log('└─────────┴──────────────────┴───────────────────────┘\n');
console.log('📍 Expected Witness Values (if we executed):');
console.log('┌─────────┬─────────┬─────────────────────────────┐');
console.log('│ Witness │ Value │ Meaning │');
console.log('├─────────┼─────────┼─────────────────────────────┤');
console.log('│ _0 │ 5 │ Input x │');
console.log('│ _1 │ 3 │ Input y (public) │');
console.log('│ _2 │ 8 │ sum = 5 + 3 │');
console.log('│ _3 │ 1 │ true (x != 0) │');
console.log('│ _4 │ 1 │ true (y != 0) │');
console.log('│ _5 │ 1 │ true (8 > 5) │');
console.log('│ _6 │ 1 │ true (8 > 3) │');
console.log('└─────────┴─────────┴─────────────────────────────┘\n');
// ========================================================================
// STEP 6: Code-to-Constraint Mapping
// ========================================================================
console.log('🔗 STEP 6: Code-to-Constraint Mapping\n');
console.log('═══════════════════════════════════════════════════════════');
console.log('\nSource Code → ACIR Constraints Mapping:\n');
console.log('Line 2: assert(x != 0)');
console.log(' ↓ Generates:');
console.log(' • ACIR Opcode: BinaryFieldOp { op: NotEquals, lhs: _0, rhs: 0 }');
console.log(' • Result stored in: _3');
console.log(' • Constraint: _3 must equal 1 (true)\n');
console.log('Line 4: assert(y != 0)');
console.log(' ↓ Generates:');
console.log(' • ACIR Opcode: BinaryFieldOp { op: NotEquals, lhs: _1, rhs: 0 }');
console.log(' • Result stored in: _4');
console.log(' • Constraint: _4 must equal 1 (true)\n');
console.log('Line 7: let sum = x + y');
console.log(' ↓ Generates:');
console.log(' • ACIR Opcode: BinaryFieldOp { op: Add, lhs: _0, rhs: _1 }');
console.log(' • Result stored in: _2');
console.log(' • Computation: _2 = _0 + _1\n');
console.log('Line 9: assert(sum as u64 > x as u64)');
console.log(' ↓ Generates:');
console.log(' • ACIR Opcode: Cast { source: _2, bit_size: U64 }');
console.log(' • ACIR Opcode: Cast { source: _0, bit_size: U64 }');
console.log(' • ACIR Opcode: BinaryIntOp { op: GreaterThan, bit_size: U64 }');
console.log(' • Result stored in: _5');
console.log(' • Constraint: _5 must equal 1 (true)\n');
console.log('Line 10: assert(sum as u64 > y as u64)');
console.log(' ↓ Generates:');
console.log(' • Similar cast and comparison opcodes');
console.log(' • Result stored in: _6');
console.log(' • Constraint: _6 must equal 1 (true)\n');
console.log('Line 13: sum (return value)');
console.log(' ↓ Generates:');
console.log(' • Public output: _2 (marked as public return)');
console.log(' • Value: 8 (will be in proof public inputs)\n');
// ========================================================================
// STEP 7: Summary - What You Can Build
// ========================================================================
console.log('═══════════════════════════════════════════════════════════');
console.log('💡 WHAT YOU CAN BUILD IN THE PLAYGROUND UI');
console.log('═══════════════════════════════════════════════════════════\n');
console.log('With these APIs, you can create:\n');
console.log('1. 📊 WITNESS INSPECTOR PANEL');
console.log(' • Show table of witness indices → values');
console.log(' • Highlight which witnesses are inputs vs computed');
console.log(' • Link witnesses back to variable names\n');
console.log('2. 🔗 CODE-TO-CONSTRAINT VISUALIZER');
console.log(' • Split screen: Noir code | ACIR constraints');
console.log(' • Click a line → highlight related ACIR opcodes');
console.log(' • Show which witnesses are created per line\n');
console.log('3. 📈 CONSTRAINT COMPLEXITY HEATMAP');
console.log(' • Color code lines by constraint count');
console.log(' • Red = expensive lines (many constraints)');
console.log(' • Green = cheap lines (few constraints)');
console.log(' • Use existing profiler for this!\n');
console.log('4. 🎯 RUNTIME WITNESS VIEWER (requires execution)');
console.log(' • Execute circuit with inputs');
console.log(' • Show actual witness values computed');
console.log(' • Trace value flow through circuit\n');
console.log('═══════════════════════════════════════════════════════════');
console.log('✅ Demo Complete!');
console.log('═══════════════════════════════════════════════════════════\n');
console.log('Next Steps:');
console.log(' 1. Use /api/compile to get artifact');
console.log(' 2. Parse artifact.abi for witness structure');
console.log(' 3. Parse artifact.bytecode for ACIR opcodes');
console.log(' 4. Use /api/profile/opcodes for line-by-line metrics');
console.log(' 5. Build UI components to visualize this data!\n');
} catch (error) {
console.error('❌ Demo failed:', error.message);
console.error(error.stack);
}
}
async function main() {
// Check if server is running
try {
const result = await makeRequest('GET', '/api/health', null);
if (result.status !== 200) {
console.error('❌ Server is not responding properly. Please start it with: npm run start:dev');
return;
}
} catch (error) {
console.error('❌ Server is not running. Please start it with: npm run start:dev');
console.error(` Error: ${error.message}\n`);
return;
}
await demonstrateWitnessAndConstraints();
}
main();