-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-proof.ts
More file actions
422 lines (376 loc) · 12.1 KB
/
Copy pathgenerate-proof.ts
File metadata and controls
422 lines (376 loc) · 12.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/**
* Phase 5: ZK Proof Generation
*
* Generates cryptographic proof that reserves >= liabilities
*
* This implementation uses a practical approach with:
* - Cryptographic commitments (keccak256 hashes)
* - Merkle root binding
* - Timestamp binding
* - Signature-based attestation
*
* Note: Full ZK-SNARK implementation would require:
* - Compiled circom circuit (requires circom binary)
* - Trusted setup ceremony
* - Powers of Tau
*
* This provides cryptographic proof suitable for demonstration and auditing.
* For production, integrate with full circom/snarkjs pipeline.
*
* Usage:
* npx tsx scripts/generate-proof.ts [epoch-id]
* npx tsx scripts/generate-proof.ts epoch_1738525000000
*/
import { ethers } from 'ethers';
import * as fs from 'fs/promises';
import * as path from 'path';
import { keccak256 } from 'ethers';
// Types
interface ReservesData {
epoch: string;
reserves: {
native: string;
nativeFormatted: string;
};
liabilities: {
total: string;
totalFormatted: string;
participantCount: number;
};
solvency: {
isSolvent: boolean;
ratio: string;
excess: string;
excessFormatted: string;
};
timestamp: number;
}
interface MerkleMetadata {
root: string;
leafCount: number;
treeDepth: number;
totalLiabilities: string;
generatedAt: string;
}
interface WitnessData {
reserves_total: string;
liabilities_sum: string;
merkle_root: string;
timestamp: number;
is_solvent: boolean;
}
interface ProofData {
version: string;
type: string;
epoch: string;
publicSignals: {
merkle_root: string;
timestamp: number;
is_solvent: boolean;
};
proof: {
commitment: string;
witness_hash: string;
reserves_commitment: string;
liabilities_commitment: string;
solvency_assertion: string;
};
metadata: {
reserves_total: string;
liabilities_sum: string;
excess: string;
ratio: string;
participantCount: number;
generatedAt: string;
};
verification: {
canVerify: boolean;
verificationSteps: string[];
};
}
/**
* Find latest epoch directory
*/
async function findLatestEpoch(): Promise<string | null> {
const epochsDir = path.join(process.cwd(), 'solvency', 'epochs');
try {
const entries = await fs.readdir(epochsDir, { withFileTypes: true });
const epochDirs = entries
.filter(e => e.isDirectory() && e.name.startsWith('epoch_'))
.map(e => e.name)
.sort()
.reverse();
return epochDirs.length > 0 ? epochDirs[0] : null;
} catch {
return null;
}
}
/**
* Read reserves data
*/
async function readReservesData(epochPath: string): Promise<ReservesData> {
const reservesPath = path.join(epochPath, 'reserves.json');
try {
const content = await fs.readFile(reservesPath, 'utf-8');
return JSON.parse(content);
} catch (error: any) {
throw new Error(`Failed to read reserves.json: ${error.message}`);
}
}
/**
* Read Merkle metadata
*/
async function readMerkleMetadata(epochPath: string): Promise<MerkleMetadata> {
const metadataPath = path.join(epochPath, 'merkle_metadata.json');
try {
const content = await fs.readFile(metadataPath, 'utf-8');
return JSON.parse(content);
} catch (error: any) {
throw new Error(`Failed to read merkle_metadata.json: ${error.message}`);
}
}
/**
* Generate witness data (private inputs to the circuit)
*/
function generateWitness(
reserves: ReservesData,
merkle: MerkleMetadata
): WitnessData {
return {
reserves_total: reserves.reserves.native,
liabilities_sum: reserves.liabilities.total,
merkle_root: merkle.root,
timestamp: reserves.timestamp,
is_solvent: reserves.solvency.isSolvent
};
}
/**
* Generate cryptographic commitments
*
* These are one-way hashes that commit to values without revealing them.
* Verifiers can check commitments match without knowing the values.
*/
function generateCommitments(witness: WitnessData) {
// Commitment to reserves (hides exact amount)
const reservesCommitment = keccak256(
ethers.AbiCoder.defaultAbiCoder().encode(
['uint256', 'bytes32', 'uint256'],
[witness.reserves_total, witness.merkle_root, witness.timestamp]
)
);
// Commitment to liabilities (hides exact amount)
const liabilitiesCommitment = keccak256(
ethers.AbiCoder.defaultAbiCoder().encode(
['uint256', 'bytes32', 'uint256'],
[witness.liabilities_sum, witness.merkle_root, witness.timestamp]
)
);
// Commitment to entire witness (proves all data is consistent)
const witnessHash = keccak256(
ethers.AbiCoder.defaultAbiCoder().encode(
['uint256', 'uint256', 'bytes32', 'uint256', 'bool'],
[
witness.reserves_total,
witness.liabilities_sum,
witness.merkle_root,
witness.timestamp,
witness.is_solvent
]
)
);
// Solvency assertion: proves reserves >= liabilities
// This would be the ZK-SNARK proof in full implementation
const solvencyAssertion = keccak256(
ethers.AbiCoder.defaultAbiCoder().encode(
['bytes32', 'bytes32', 'bool'],
[reservesCommitment, liabilitiesCommitment, witness.is_solvent]
)
);
// Master commitment binding all elements
const masterCommitment = keccak256(
ethers.AbiCoder.defaultAbiCoder().encode(
['bytes32', 'bytes32', 'bytes32'],
[witnessHash, solvencyAssertion, witness.merkle_root]
)
);
return {
masterCommitment,
witnessHash,
reservesCommitment,
liabilitiesCommitment,
solvencyAssertion
};
}
/**
* Generate proof data structure
*/
function generateProof(
witness: WitnessData,
reserves: ReservesData,
merkle: MerkleMetadata
): ProofData {
const commitments = generateCommitments(witness);
const proof: ProofData = {
version: '1.0.0',
type: 'solvency-proof-commitment-scheme',
epoch: reserves.epoch,
publicSignals: {
merkle_root: witness.merkle_root,
timestamp: witness.timestamp,
is_solvent: witness.is_solvent
},
proof: {
commitment: commitments.masterCommitment,
witness_hash: commitments.witnessHash,
reserves_commitment: commitments.reservesCommitment,
liabilities_commitment: commitments.liabilitiesCommitment,
solvency_assertion: commitments.solvencyAssertion
},
metadata: {
reserves_total: reserves.reserves.native,
liabilities_sum: reserves.liabilities.total,
excess: reserves.solvency.excess,
ratio: reserves.solvency.ratio,
participantCount: reserves.liabilities.participantCount,
generatedAt: new Date().toISOString()
},
verification: {
canVerify: true,
verificationSteps: [
'1. Verify merkle_root matches merkle_metadata.json',
'2. Verify timestamp is recent and not in future',
'3. Recompute witness_hash from reserves + liabilities + merkle_root',
'4. Verify witness_hash matches proof.witness_hash',
'5. Verify reserves >= liabilities (check is_solvent = true)',
'6. Recompute all commitments and verify they match',
'7. Check master commitment binds all elements together'
]
}
};
return proof;
}
/**
* Save proof to file
*/
async function saveProof(epochPath: string, proof: ProofData): Promise<void> {
const proofPath = path.join(epochPath, 'proof.json');
await fs.writeFile(proofPath, JSON.stringify(proof, null, 2), 'utf-8');
console.log(`💾 Saved proof to: proof.json`);
}
/**
* Save public signals (what's revealed in the proof)
*/
async function savePublicSignals(epochPath: string, proof: ProofData): Promise<void> {
const signalsPath = path.join(epochPath, 'publicSignals.json');
const publicSignals = {
merkle_root: proof.publicSignals.merkle_root,
timestamp: proof.publicSignals.timestamp,
is_solvent: proof.publicSignals.is_solvent,
commitment: proof.proof.commitment
};
await fs.writeFile(signalsPath, JSON.stringify(publicSignals, null, 2), 'utf-8');
console.log(`💾 Saved public signals to: publicSignals.json`);
}
/**
* Save witness (private, for auditing only)
*/
async function saveWitness(epochPath: string, witness: WitnessData): Promise<void> {
const witnessPath = path.join(epochPath, 'witness.json');
await fs.writeFile(witnessPath, JSON.stringify(witness, null, 2), 'utf-8');
console.log(`💾 Saved witness to: witness.json (private)`);
}
/**
* Main execution
*/
async function main() {
const args = process.argv.slice(2);
let epochId: string | null = null;
// Determine epoch
if (args.length > 0) {
epochId = args[0];
} else {
console.log('⏳ No epoch specified, finding latest...');
epochId = await findLatestEpoch();
}
if (!epochId) {
console.error('\n❌ Error: No epoch found\n');
console.log('Usage: npx tsx scripts/generate-proof.ts <epoch-id>\n');
console.log('Or scan reserves first to create epoch data\n');
process.exit(1);
}
const epochPath = path.join(process.cwd(), 'solvency', 'epochs', epochId);
console.log('\n🔐 ZK Proof Generator - Phase 5\n');
console.log('═'.repeat(60));
console.log(`📁 Epoch: ${epochId}`);
console.log(`📂 Path: ${epochPath}\n`);
// Verify epoch directory exists
try {
await fs.access(epochPath);
} catch {
console.error(`❌ Epoch directory not found: ${epochPath}\n`);
console.log('Tip: Run reserves scanner first\n');
process.exit(1);
}
try {
// Step 1: Read input data
console.log('1️⃣ Reading input data...');
const reserves = await readReservesData(epochPath);
const merkle = await readMerkleMetadata(epochPath);
console.log(`📊 Reserves: ${reserves.reserves.nativeFormatted} AVAX`);
console.log(`📊 Liabilities: ${reserves.liabilities.totalFormatted} AVAX`);
console.log(`🌳 Merkle Root: ${merkle.root}`);
console.log(`✅ Solvent: ${reserves.solvency.isSolvent}`);
// Verify solvency before generating proof
if (!reserves.solvency.isSolvent) {
console.log('\n⚠️ WARNING: System is INSOLVENT!');
console.log('Cannot generate valid solvency proof when reserves < liabilities');
console.log('\nProof generation will continue for demonstration purposes,');
console.log('but the proof will indicate INSOLVENT status.\n');
}
// Step 2: Generate witness
console.log('\n2️⃣ Generating witness data...');
const witness = generateWitness(reserves, merkle);
console.log(`🔢 Witness generated with ${Object.keys(witness).length} signals`);
// Step 3: Generate proof
console.log('\n3️⃣ Generating cryptographic proof...');
const proof = generateProof(witness, reserves, merkle);
console.log(`🔐 Generated proof with commitment: ${proof.proof.commitment.slice(0, 20)}...`);
// Step 4: Save outputs
console.log('\n4️⃣ Saving outputs...');
await saveProof(epochPath, proof);
await savePublicSignals(epochPath, proof);
await saveWitness(epochPath, witness);
console.log('\n' + '═'.repeat(60));
console.log('🎉 Proof generation complete!\n');
console.log('📋 Generated Files:');
console.log(` - proof.json (complete proof)`);
console.log(` - publicSignals.json (public verification data)`);
console.log(` - witness.json (private audit trail)\n`);
console.log('🔐 Proof Summary:');
console.log(` Type: ${proof.type}`);
console.log(` Merkle Root: ${proof.publicSignals.merkle_root}`);
console.log(` Is Solvent: ${proof.publicSignals.is_solvent ? '✅ YES' : '❌ NO'}`);
console.log(` Commitment: ${proof.proof.commitment}\n`);
console.log('🔍 Verification:');
console.log(` Run: npx tsx scripts/verify-proof.ts ${epochId}\n`);
// Exit with appropriate code
process.exit(reserves.solvency.isSolvent ? 0 : 1);
} catch (error: any) {
console.error('\n❌ Error:', error.message);
console.error(error.stack);
process.exit(1);
}
}
// Run if called directly
if (typeof require !== 'undefined' && require.main === module) {
main().catch(console.error);
}
export {
generateWitness,
generateCommitments,
generateProof,
saveProof,
savePublicSignals,
saveWitness,
findLatestEpoch
};