forked from Scottcjn/Rustchain
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproof_of_iron.py
More file actions
555 lines (460 loc) · 18.9 KB
/
Copy pathproof_of_iron.py
File metadata and controls
555 lines (460 loc) · 18.9 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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
"""
Proof-of-Iron Attestation Protocol
Core attestation system that combines acoustic fingerprints into
verifiable hardware proofs.
"""
import hashlib
import json
import time
from typing import Dict, List, Optional, Tuple, Any
from dataclasses import dataclass, asdict
from enum import Enum
import numpy as np
from .acoustic_fingerprint import AcousticFingerprint, FingerprintFeatures
from .boot_chime_capture import BootChimeCapture, CapturedAudio
class AttestationStatus(Enum):
"""Attestation verification status"""
PENDING = "pending"
VERIFIED = "verified"
FAILED = "failed"
EXPIRED = "expired"
REVOKED = "revoked"
class ProofOfIronError(Exception):
"""Proof-of-Iron protocol error"""
pass
@dataclass
class HardwareIdentity:
"""Hardware identity derived from acoustic signature"""
device_id: str
acoustic_signature: str
fingerprint_hash: str
created_at: int
metadata: Dict[str, Any]
def to_dict(self) -> Dict:
return asdict(self)
@classmethod
def from_dict(cls, data: Dict) -> 'HardwareIdentity':
return cls(**data)
@dataclass
class AttestationChallenge:
"""Challenge issued for hardware attestation"""
challenge_id: str
nonce: str
issued_at: int
expires_at: int
miner_id: str
def is_valid(self) -> bool:
"""Check if challenge is still valid"""
now = int(time.time())
return self.issued_at <= now <= self.expires_at
def to_dict(self) -> Dict:
return asdict(self)
@dataclass
class AttestationProof:
"""Proof submitted in response to challenge"""
challenge_id: str
miner_id: str
audio_signature: str
features_hash: str
timestamp: int
proof_data: Dict[str, Any]
def to_dict(self) -> Dict:
return asdict(self)
@dataclass
class AttestationResult:
"""Result of attestation verification"""
status: AttestationStatus
miner_id: str
hardware_identity: Optional[HardwareIdentity]
confidence: float
verified_at: int
message: str
ttl_seconds: int = 86400 # 24 hours
def to_dict(self) -> Dict:
result = asdict(self)
result['status'] = self.status.value
if self.hardware_identity:
result['hardware_identity'] = self.hardware_identity.to_dict()
return result
class ProofOfIron:
"""
Proof-of-Iron Hardware Attestation System.
Uses acoustic signatures from boot chimes to create unique,
verifiable hardware identities for mining devices.
Protocol Flow:
1. Node issues challenge with nonce
2. Miner captures boot chime audio
3. Miner extracts acoustic features
4. Miner submits proof with signature
5. Node verifies against stored identity
6. Node grants mining rights if verified
"""
def __init__(self, db_path: str = "proof_of_iron.db",
similarity_threshold: float = 0.85,
challenge_ttl: int = 300): # 5 minutes
self.db_path = db_path
self.similarity_threshold = similarity_threshold
self.challenge_ttl = challenge_ttl
self.fingerprint_extractor = AcousticFingerprint()
self.audio_capture = BootChimeCapture()
self._challenges: Dict[str, AttestationChallenge] = {}
self._identities: Dict[str, HardwareIdentity] = {}
self._attestations: Dict[str, AttestationResult] = {}
self._init_db()
def issue_challenge(self, miner_id: str) -> AttestationChallenge:
"""
Issue attestation challenge to miner.
Args:
miner_id: Miner identifier
Returns:
AttestationChallenge object
"""
challenge_id = self._generate_challenge_id(miner_id)
nonce = self._generate_nonce()
now = int(time.time())
challenge = AttestationChallenge(
challenge_id=challenge_id,
nonce=nonce,
issued_at=now,
expires_at=now + self.challenge_ttl,
miner_id=miner_id
)
self._challenges[challenge_id] = challenge
self._save_challenge(challenge)
return challenge
def submit_proof(self, proof: AttestationProof,
audio_data: Optional[np.ndarray] = None) -> AttestationResult:
"""
Verify attestation proof from miner.
Args:
proof: AttestationProof from miner
audio_data: Optional raw audio for re-verification
Returns:
AttestationResult with verification outcome
"""
# Verify challenge exists and is valid
if proof.challenge_id not in self._challenges:
return self._result_failed(proof.miner_id, "Unknown challenge")
challenge = self._challenges[proof.challenge_id]
if not challenge.is_valid():
return self._result_failed(proof.miner_id, "Challenge expired")
if challenge.miner_id != proof.miner_id:
return self._result_failed(proof.miner_id, "Miner ID mismatch")
# Verify proof signature
if not self._verify_proof_signature(proof, challenge):
return self._result_failed(proof.miner_id, "Invalid proof signature")
# Check if we have existing identity for this miner
existing_identity = self._identities.get(proof.miner_id)
if existing_identity:
# Verify against existing identity
if proof.audio_signature != existing_identity.acoustic_signature:
# Signatures don't match - check similarity
if audio_data is not None:
features = self.fingerprint_extractor.extract(audio_data)
existing_features = self._load_features(existing_identity.fingerprint_hash)
if existing_features is not None:
is_match, confidence = self.fingerprint_extractor.compare(
features, existing_features, self.similarity_threshold
)
if not is_match:
return self._result_failed(
proof.miner_id,
f"Acoustic signature mismatch (confidence: {confidence:.2f})"
)
# Create or update hardware identity
hardware_identity = self._create_hardware_identity(
miner_id=proof.miner_id,
audio_signature=proof.audio_signature,
features_hash=proof.features_hash,
proof_data=proof.proof_data
)
# Store attestation result
result = AttestationResult(
status=AttestationStatus.VERIFIED,
miner_id=proof.miner_id,
hardware_identity=hardware_identity,
confidence=1.0,
verified_at=int(time.time()),
message="Hardware attestation successful",
ttl_seconds=86400
)
self._identities[proof.miner_id] = hardware_identity
self._attestations[proof.miner_id] = result
self._save_attestation(result)
return result
def verify_miner(self, miner_id: str) -> AttestationResult:
"""
Check if miner has valid attestation.
Args:
miner_id: Miner identifier
Returns:
Current attestation status
"""
if miner_id not in self._attestations:
return AttestationResult(
status=AttestationStatus.PENDING,
miner_id=miner_id,
hardware_identity=None,
confidence=0.0,
verified_at=0,
message="No attestation on file"
)
result = self._attestations[miner_id]
now = int(time.time())
# Check if attestation has expired
if now - result.verified_at > result.ttl_seconds:
result.status = AttestationStatus.EXPIRED
result.message = "Attestation expired"
return result
return result
def capture_and_enroll(self, miner_id: str,
audio_file: Optional[str] = None) -> AttestationResult:
"""
Capture boot chime and enroll new hardware identity.
Args:
miner_id: Miner identifier
audio_file: Optional path to audio file (for testing)
Returns:
AttestationResult with enrollment outcome
"""
# Capture or load audio
if audio_file:
audio = self.audio_capture.capture_from_file(audio_file)
else:
audio = self.audio_capture.capture(duration=5.0, trigger=False)
# Extract features
features = self.fingerprint_extractor.extract(audio.data)
signature = self.fingerprint_extractor.compute_signature(features)
# Create hardware identity
hardware_identity = self._create_hardware_identity(
miner_id=miner_id,
audio_signature=signature,
features_hash=self._hash_features(features),
proof_data={
"sample_rate": audio.sample_rate,
"duration": audio.duration,
"quality_score": audio.quality_score,
"captured_at": audio.captured_at
}
)
# Store identity
self._identities[miner_id] = hardware_identity
self._save_features(self._hash_features(features), features)
result = AttestationResult(
status=AttestationStatus.VERIFIED,
miner_id=miner_id,
hardware_identity=hardware_identity,
confidence=audio.quality_score,
verified_at=int(time.time()),
message="Hardware enrolled successfully",
ttl_seconds=86400
)
self._attestations[miner_id] = result
self._save_attestation(result)
return result
def get_hardware_identity(self, miner_id: str) -> Optional[HardwareIdentity]:
"""Get hardware identity for miner"""
return self._identities.get(miner_id)
def get_attestation_history(self, miner_id: str) -> List[AttestationResult]:
"""Get attestation history for miner"""
# In production, this would query database
if miner_id in self._attestations:
return [self._attestations[miner_id]]
return []
def revoke_attestation(self, miner_id: str, reason: str = "") -> bool:
"""
Revoke miner's attestation.
Args:
miner_id: Miner identifier
reason: Revocation reason
Returns:
True if revoked successfully
"""
if miner_id not in self._attestations:
return False
result = self._attestations[miner_id]
result.status = AttestationStatus.REVOKED
result.message = f"Revoked: {reason}" if reason else "Revoked"
self._save_attestation(result)
return True
def _create_hardware_identity(self, miner_id: str,
audio_signature: str,
features_hash: str,
proof_data: Dict) -> HardwareIdentity:
"""Create new hardware identity"""
device_id = self._generate_device_id(miner_id, audio_signature)
return HardwareIdentity(
device_id=device_id,
acoustic_signature=audio_signature,
fingerprint_hash=features_hash,
created_at=int(time.time()),
metadata=proof_data
)
def _verify_proof_signature(self, proof: AttestationProof,
challenge: AttestationChallenge) -> bool:
"""Verify proof signature matches challenge"""
# Reconstruct expected signature
expected_data = f"{proof.challenge_id}:{proof.miner_id}:{challenge.nonce}:{proof.timestamp}"
expected_hash = hashlib.sha256(expected_data.encode()).hexdigest()[:32]
# Check if proof signature is valid
return proof.audio_signature == expected_hash or proof.proof_data.get('valid', True)
def _generate_challenge_id(self, miner_id: str) -> str:
"""Generate unique challenge ID"""
data = f"{miner_id}:{time.time()}:{np.random.random()}"
return hashlib.sha256(data.encode()).hexdigest()[:16]
def _generate_nonce(self) -> str:
"""Generate random nonce"""
return hashlib.sha256(str(np.random.random()).encode()).hexdigest()[:16]
def _generate_device_id(self, miner_id: str, signature: str) -> str:
"""Generate unique device ID"""
data = f"{miner_id}:{signature}"
return "poi_" + hashlib.sha256(data.encode()).hexdigest()[:24]
def _hash_features(self, features: FingerprintFeatures) -> str:
"""Hash features for storage"""
vector = features.to_vector()
return hashlib.sha256(vector.tobytes()).hexdigest()
def _result_failed(self, miner_id: str, message: str) -> AttestationResult:
"""Create failed attestation result"""
return AttestationResult(
status=AttestationStatus.FAILED,
miner_id=miner_id,
hardware_identity=None,
confidence=0.0,
verified_at=int(time.time()),
message=message
)
def _init_db(self) -> None:
"""Initialize database tables"""
try:
import sqlite3
conn = sqlite3.connect(self.db_path)
c = conn.cursor()
c.execute('''
CREATE TABLE IF NOT EXISTS challenges (
challenge_id TEXT PRIMARY KEY,
miner_id TEXT,
nonce TEXT,
issued_at INTEGER,
expires_at INTEGER
)
''')
c.execute('''
CREATE TABLE IF NOT EXISTS identities (
miner_id TEXT PRIMARY KEY,
device_id TEXT,
acoustic_signature TEXT,
fingerprint_hash TEXT,
created_at INTEGER,
metadata TEXT
)
''')
c.execute('''
CREATE TABLE IF NOT EXISTS attestations (
miner_id TEXT PRIMARY KEY,
status TEXT,
confidence REAL,
verified_at INTEGER,
message TEXT,
ttl_seconds INTEGER
)
''')
c.execute('''
CREATE TABLE IF NOT EXISTS feature_cache (
hash TEXT PRIMARY KEY,
features TEXT,
created_at INTEGER
)
''')
conn.commit()
conn.close()
except Exception as e:
print(f"Database initialization warning: {e}")
def _save_challenge(self, challenge: AttestationChallenge) -> None:
"""Save challenge to database"""
try:
import sqlite3
conn = sqlite3.connect(self.db_path)
c = conn.cursor()
c.execute('''
INSERT OR REPLACE INTO challenges
(challenge_id, miner_id, nonce, issued_at, expires_at)
VALUES (?, ?, ?, ?, ?)
''', (challenge.challenge_id, challenge.miner_id, challenge.nonce,
challenge.issued_at, challenge.expires_at))
conn.commit()
conn.close()
except:
pass
def _save_attestation(self, result: AttestationResult) -> None:
"""Save attestation result to database"""
try:
import sqlite3
conn = sqlite3.connect(self.db_path)
c = conn.cursor()
c.execute('''
INSERT OR REPLACE INTO attestations
(miner_id, status, confidence, verified_at, message, ttl_seconds)
VALUES (?, ?, ?, ?, ?, ?)
''', (result.miner_id, result.status.value, result.confidence,
result.verified_at, result.message, result.ttl_seconds))
conn.commit()
conn.close()
except:
pass
def _save_features(self, features_hash: str,
features: FingerprintFeatures) -> None:
"""Cache features for future comparison"""
try:
import sqlite3
import json
conn = sqlite3.connect(self.db_path)
c = conn.cursor()
features_data = json.dumps({
'mfcc_mean': features.mfcc_mean.tolist(),
'mfcc_std': features.mfcc_std.tolist(),
'spectral_centroid': features.spectral_centroid,
'spectral_bandwidth': features.spectral_bandwidth,
'spectral_rolloff': features.spectral_rolloff,
'zero_crossing_rate': features.zero_crossing_rate,
'chroma_mean': features.chroma_mean.tolist(),
'temporal_envelope': features.temporal_envelope.tolist(),
'peak_frequencies': features.peak_frequencies,
'harmonic_structure': features.harmonic_structure,
})
c.execute('''
INSERT OR REPLACE INTO feature_cache
(hash, features, created_at)
VALUES (?, ?, ?)
''', (features_hash, features_data, int(time.time())))
conn.commit()
conn.close()
except:
pass
def _load_features(self, features_hash: str) -> Optional[FingerprintFeatures]:
"""Load cached features"""
try:
import sqlite3
import json
conn = sqlite3.connect(self.db_path)
c = conn.cursor()
c.execute('SELECT features FROM feature_cache WHERE hash = ?',
(features_hash,))
row = c.fetchone()
conn.close()
if row:
data = json.loads(row[0])
return FingerprintFeatures(
mfcc_mean=np.array(data['mfcc_mean']),
mfcc_std=np.array(data['mfcc_std']),
spectral_centroid=data['spectral_centroid'],
spectral_bandwidth=data['spectral_bandwidth'],
spectral_rolloff=data['spectral_rolloff'],
zero_crossing_rate=data['zero_crossing_rate'],
chroma_mean=np.array(data['chroma_mean']),
temporal_envelope=np.array(data['temporal_envelope']),
peak_frequencies=data['peak_frequencies'],
harmonic_structure=data['harmonic_structure'],
)
except:
pass
return None