Zero-Knowledge Proof Demo - Problem

Implement a simplified zero-knowledge proof protocol to demonstrate the concept where a prover can convince a verifier that they know a secret without revealing the secret itself.

For this problem, we'll implement a basic discrete logarithm zero-knowledge proof protocol:

1. Setup Phase: Given a prime p, generator g, and public key y = g^x mod p where x is the secret

2. Proof Phase: The prover generates a random r, computes commitment t = g^r mod p

3. Challenge Phase: The verifier sends a random challenge c

4. Response Phase: The prover computes response s = (r + c*x) mod (p-1)

5. Verification: The verifier checks if g^s ≡ t * y^c (mod p)

Given the protocol parameters, simulate one round of this zero-knowledge proof and return whether the verification succeeds.

Input & Output

Example 1 — Basic Zero-Knowledge Proof
$ Input: p = 23, g = 2, y = 4, x = 7, r = 5, c = 3
Output: true
💡 Note: Step by step: t = 2^5 mod 23 = 9, s = (5 + 3×7) mod 22 = 4, verify: 2^4 mod 23 = 16 and (9 × 4^3) mod 23 = 16. Since 16 = 16, verification succeeds.
Example 2 — Failed Verification
$ Input: p = 17, g = 3, y = 5, x = 4, r = 6, c = 2
Output: false
💡 Note: t = 3^6 mod 17 = 15, s = (6 + 2×4) mod 16 = 14, verify: 3^14 mod 17 = 16 but (15 × 5^2) mod 17 = 1. Since 16 ≠ 1, verification fails.
Example 3 — Small Prime Case
$ Input: p = 11, g = 2, y = 3, x = 8, r = 1, c = 1
Output: true
💡 Note: t = 2^1 mod 11 = 2, s = (1 + 1×8) mod 10 = 9, verify: 2^9 mod 11 = 6 and (2 × 3^1) mod 11 = 6. Since 6 = 6, proof is valid.

Constraints

  • 2 ≤ p ≤ 109 (p is prime)
  • 1 ≤ g < p (g is generator)
  • 1 ≤ y < p (y = gx mod p)
  • 1 ≤ x < p-1 (secret key)
  • 0 ≤ r < p-1 (random nonce)
  • 1 ≤ c < p-1 (challenge)

Visualization

Tap to expand
PROTOCOL SETUPp = 23g = 2y = 4Public ParametersSECRET: x = 7(Known only to prover)Relationship:y = g^x mod p4 = 2^7 mod 23 ✓Random values:r = 5, c = 3PROOF GENERATION1Commitment: t = g^r mod pt = 2^5 mod 23 = 92Challenge received: c = 33Response: s = (r + c×x) mod (p-1)s = (5 + 3×7) mod 22 = 4Proof Triple:t = 9c = 3s = 4Sent to verifier(secret x never revealed!)VERIFICATIONCheck equation:g^s ≡ t × y^c (mod p)Left side:g^s mod p = 2^4 mod 23 = 16Right side:t × y^c mod p= 9 × 4^3 mod 23 = 1616 = 16 ✓VERIFIED!Prover knows secret xwithout revealing itKey Insight:The verifier can mathematically confirm the prover knows the secret discrete logarithmwithout the secret ever being transmitted or revealed - this is zero-knowledge!TutorialsPoint - Zero-Knowledge Proof Demo | Discrete Logarithm Protocol
Asked in
Meta 15 Coinbase 12 Chainlink 8
34.7K Views
Medium Frequency
~35 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen