|
| 1 | +import sys |
| 2 | +import hashlib |
| 3 | +import secrets |
| 4 | +from PyQt5.QtWidgets import ( |
| 5 | + QApplication, QWidget, QVBoxLayout, QPushButton, QLabel, QLineEdit, |
| 6 | + QMessageBox, QHBoxLayout, QTextEdit |
| 7 | +) |
| 8 | +from PyQt5.QtGui import QFont |
| 9 | +from PyQt5.QtCore import Qt |
| 10 | + |
| 11 | +class CommitmentGameWindow(QWidget): |
| 12 | + def __init__(self): |
| 13 | + super().__init__() |
| 14 | + |
| 15 | + self.setWindowTitle("🔒 Commitment Game") |
| 16 | + self.setFixedSize(500, 500) |
| 17 | + self.setStyleSheet("background-color: #2C3E50;") |
| 18 | + |
| 19 | + layout = QVBoxLayout() |
| 20 | + |
| 21 | + title = QLabel("🔐 Commitment Scheme Simulation") |
| 22 | + title.setFont(QFont("Arial", 16, QFont.Bold)) |
| 23 | + title.setStyleSheet("color: white; padding: 10px;") |
| 24 | + title.setAlignment(Qt.AlignCenter) |
| 25 | + layout.addWidget(title) |
| 26 | + |
| 27 | + role_label = QLabel("🧑💻 You are the Prover. The system plays the Verifier.") |
| 28 | + role_label.setStyleSheet("color: #ECF0F1; padding: 5px;") |
| 29 | + role_label.setAlignment(Qt.AlignCenter) |
| 30 | + layout.addWidget(role_label) |
| 31 | + |
| 32 | + self.secret_input = QLineEdit() |
| 33 | + self.secret_input.setPlaceholderText("Enter your secret value") |
| 34 | + self.secret_input.setFont(QFont("Arial", 12)) |
| 35 | + self.secret_input.setStyleSheet("padding: 8px;") |
| 36 | + layout.addWidget(self.secret_input) |
| 37 | + |
| 38 | + commit_btn = QPushButton("🔒 Commit to Secret") |
| 39 | + commit_btn.setFont(QFont("Arial", 12)) |
| 40 | + commit_btn.setStyleSheet("margin: 10px; padding: 10px;") |
| 41 | + commit_btn.clicked.connect(self.generate_commitment) |
| 42 | + layout.addWidget(commit_btn) |
| 43 | + |
| 44 | + reveal_btn = QPushButton("🔓 Reveal to Verifier") |
| 45 | + reveal_btn.setFont(QFont("Arial", 12)) |
| 46 | + reveal_btn.setStyleSheet("margin: 10px; padding: 10px;") |
| 47 | + reveal_btn.clicked.connect(self.reveal_secret) |
| 48 | + layout.addWidget(reveal_btn) |
| 49 | + |
| 50 | + self.commitment_label = QLabel("Commitment: ...") |
| 51 | + self.commitment_label.setStyleSheet("color: #ECF0F1; margin: 10px;") |
| 52 | + layout.addWidget(self.commitment_label) |
| 53 | + |
| 54 | + self.transcript_box = QTextEdit() |
| 55 | + self.transcript_box.setReadOnly(True) |
| 56 | + self.transcript_box.setStyleSheet("background-color: #34495E; color: #F1C40F; padding: 10px;") |
| 57 | + layout.addWidget(self.transcript_box) |
| 58 | + |
| 59 | + self.result_label = QLabel("") |
| 60 | + self.result_label.setStyleSheet("color: #1ABC9C; font-weight: bold; padding: 10px;") |
| 61 | + layout.addWidget(self.result_label) |
| 62 | + |
| 63 | + self.setLayout(layout) |
| 64 | + |
| 65 | + self.nonce = None |
| 66 | + self.commitment = None |
| 67 | + |
| 68 | + def generate_commitment(self): |
| 69 | + secret = self.secret_input.text().strip() |
| 70 | + if not secret: |
| 71 | + QMessageBox.warning(self, "Input Error", "Please enter a secret value.") |
| 72 | + return |
| 73 | + |
| 74 | + self.nonce = secrets.token_hex(8) |
| 75 | + combined = secret + self.nonce |
| 76 | + self.commitment = hashlib.sha256(combined.encode()).hexdigest() |
| 77 | + |
| 78 | + self.commitment_label.setText(f"Commitment: {self.commitment}") |
| 79 | + self.transcript_box.clear() |
| 80 | + self.transcript_box.append("Prover commits to a secret using SHA-256(secret || nonce)") |
| 81 | + self.transcript_box.append(f"nonce: {self.nonce}") |
| 82 | + self.transcript_box.append("Commitment sent to Verifier") |
| 83 | + self.result_label.setText("✅ Secret committed. You may now reveal.") |
| 84 | + self.result_label.setStyleSheet("color: #1ABC9C; font-weight: bold; padding: 10px;") |
| 85 | + |
| 86 | + def reveal_secret(self): |
| 87 | + if not self.commitment: |
| 88 | + QMessageBox.warning(self, "No Commitment", "Please commit to a value first.") |
| 89 | + return |
| 90 | + |
| 91 | + secret = self.secret_input.text().strip() |
| 92 | + combined = secret + self.nonce |
| 93 | + check = hashlib.sha256(combined.encode()).hexdigest() |
| 94 | + |
| 95 | + self.transcript_box.append("\nProver reveals the secret and nonce...") |
| 96 | + self.transcript_box.append(f"secret: {secret}") |
| 97 | + self.transcript_box.append(f"recomputed hash: {check}") |
| 98 | + |
| 99 | + if check == self.commitment: |
| 100 | + self.result_label.setStyleSheet("color: #1ABC9C; font-weight: bold; padding: 10px;") |
| 101 | + self.result_label.setText("✅ Reveal successful. Verifier is convinced.") |
| 102 | + self.transcript_box.append("Verifier confirms: ✅ commitment is valid!") |
| 103 | + else: |
| 104 | + self.result_label.setStyleSheet("color: red; font-weight: bold; padding: 10px;") |
| 105 | + self.result_label.setText("❌ Reveal failed. Commitment mismatch.") |
| 106 | + self.transcript_box.append("Verifier says: ❌ mismatch in commitment!") |
0 commit comments