Keyboard Shortcuts N Next post
P Previous post
S Save / unsave
R Read aloud
T Toggle theme
/ Focus search
Esc Close panels
🔥
Ready to read...
Cryptography Cryptography Tutorial Java Java and Cryptography Mastery Series — Extended Module 17 - Advanced and Post Quantum Cryptography

Java Program for Schnorr Zero Knowledge Identification

Reviewed & accurate
AI Summary

Introduction

Claus-Peter Schnorr published "Efficient Identification and Signatures for Smart Cards" at CRYPTO 1989. His identification protocol is a sigma protocol built on the discrete logarithm problem in a prime-order group: the prover convinces the verifier that they know the discrete log of a public value y with respect to a generator g, without revealing the log x. The scheme is the discrete-log analogue of the RSA-based Guillou-Quisquater protocol and is the ancestor of modern Schnorr signatures (the FROST threshold signature, EdDSA, and Bitcoin's BIP-340 all derive from it).

For Java learners, Schnorr identification is the cleanest example of a public-coin sigma protocol in the discrete-log family. It exercises modular exponentiation, prime-order subgroup arithmetic, and the standard soundness argument based on the discrete-log assumption. The Fiat-Shamir transform of Schnorr identification produces the Schnorr signature scheme.

What is Schnorr Identification?

Schnorr identification is a three-move sigma protocol in the discrete-logarithm family. The setup is a prime p with a prime q dividing p-1, a generator g of the order-q subgroup of Z_p^*, and a public value y = g^x mod p where x is the secret. The prover aims to convince the verifier that they know x (the discrete log of y base g) without revealing x. The protocol has three messages: commitment (prover sends t = g^r), challenge (verifier sends random e in Z_q), response (prover sends s = r + x*e mod q).

The protocol is distinguished from RSA-based ZKP by using the discrete-log hardness assumption instead of the RSA assumption. This makes it naturally compatible with elliptic curves (replace Z_p^* with an elliptic curve group). It also yields much shorter public keys and signatures: a Schnorr signature is just a single group element and a single scalar, compared to RSA's two large integers.

How Schnorr Identification Works - Step by Step

Step 1: Setup. Pick a prime p and a prime q dividing p-1 (a "safe prime" structure or a "Schnorr group"). Pick a generator g of the order-q subgroup. Pick the secret x uniformly in Z_q and publish y = g^x mod p. For the demo we use p=23, q=11 (since 11 divides 22 = p-1), g=2 (which has order 11 mod 23 because 2^11 mod 23 = 1), x=3, so y = 2^3 mod 23 = 8.

Step 2: Commitment. The prover picks a fresh random r in Z_q (the order of the subgroup, not p-1) and computes t = g^r mod p. The prover sends t to the verifier. The commitment is statistically hiding because r is uniform and t is uniform in the subgroup.

Step 3: Challenge. The verifier picks a random challenge e in Z_q. The verifier sends e to the prover. The challenge must be unpredictable to the prover at commitment time.

Step 4: Response and verification. The prover computes s = r + x * e mod q and sends s. The verifier checks that g^s = t * y^e mod p. For honest provers, g^s = g^(r + x*e) = g^r * g^(x*e) = g^r * (g^x)^e = t * y^e. A cheating prover who doesn't know x can guess e in advance, pick s arbitrarily, and compute t = g^s * y^(-e); but if the verifier sends a different challenge the check fails.

Java Program for Schnorr Zero Knowledge Identification

import java.math.BigInteger;
import java.security.SecureRandom;

public class SchnorrZkpDemo {

    // p=23, q=11 (q | p-1), g=2 (order 11 mod 23), x=3 (private), y=8 (public)
    private static final BigInteger P = BigInteger.valueOf(23);
    private static final BigInteger Q = BigInteger.valueOf(11);
    private static final BigInteger G = BigInteger.valueOf(2);
    private static final BigInteger X = BigInteger.valueOf(3);
    private static final BigInteger Y = G.modPow(X, P); // 8

    // Prover commitment: t = g^r mod p
    public static BigInteger commit(BigInteger r) {
        return G.modPow(r, P);
    }

    // Prover response: s = (r + x*e) mod q
    public static BigInteger respond(BigInteger r, BigInteger e) {
        return r.add(X.multiply(e)).mod(Q);
    }

    // Verifier check: g^s == t * y^e mod p
    public static boolean verify(BigInteger t, BigInteger e, BigInteger s) {
        BigInteger left = G.modPow(s, P);
        BigInteger right = t.multiply(Y.modPow(e, P)).mod(P);
        return left.equals(right);
    }

    public static void main(String[] args) {
        System.out.println("Setup: p=" + P + " q=" + Q + " g=" + G
            + " y=" + Y);

        // Round 1: honest prover
        BigInteger r = BigInteger.valueOf(5);
        BigInteger t = commit(r);
        BigInteger e = BigInteger.valueOf(4);
        BigInteger s = respond(r, e);
        System.out.println("Prover: r=" + r + " t=" + t);
        System.out.println("Verifier challenge e=" + e);
        System.out.println("Prover response s=" + s);
        System.out.println("Verify: " + verify(t, e, s));

        // Round 2: cheating prover
        BigInteger sGuess = BigInteger.valueOf(7);
        BigInteger eGuess = BigInteger.valueOf(4);
        BigInteger yNegE = Y.modPow(eGuess, P).modInverse(P);
        BigInteger tGuess = G.modPow(sGuess, P).multiply(yNegE).mod(P);
        System.out.println("Cheat: pre-pick s=" + sGuess
            + " e_guess=" + eGuess);
        System.out.println("  computed t=" + tGuess);
        BigInteger eActual = BigInteger.valueOf(7);
        System.out.println("Verifier sends e=" + eActual);
        System.out.println("  Verify: " + verify(tGuess, eActual, sGuess));
    }
}

Code Explanation - Line by Line

The constants P, Q, G, X, Y encode the demo parameters. P=23 and Q=11 (with 11 dividing 22 = p-1) define a Schnorr group. G=2 generates the order-11 subgroup of Z_23^* (verify: 2^11 = 2048 mod 23 = 1, and 2, 4, 8, 16, 9, 18, 13, 3, 6, 12, 1 is the full subgroup). X=3 is the private key, Y=2^3 mod 23 = 8 is the public key.

The commit method computes t = G^r mod P, a single modular exponentiation. The prover picks r uniformly in Z_q (the order of the subgroup, NOT p-1), which is essential for the security proof. In the demo we use r=5, so t = 2^5 mod 23 = 32 mod 23 = 9.

The respond method computes s = (r + X*e) mod Q. Note that this is mod Q, not mod P: the response is a scalar in the exponent group, not a group element. With r=5, e=4, x=3, we get s = (5 + 12) mod 11 = 17 mod 11 = 6.

The verify method computes both sides of the equation g^s = t * y^e mod p and checks equality. For the honest round, g^6 mod 23 = 64 mod 23 = 18, and t * y^e = 9 * 8^4 mod 23. We have 8^2 = 64 mod 23 = 18, 8^4 = 18^2 = 324 mod 23 = 2. So t * y^e = 9 * 2 = 18, matching g^s = 18.

The cheat scenario pre-picks s=7 and guessed challenge e_guess=4, computes t = g^s * y^(-e_guess) mod p, and then receives e_actual=7. Since 7 ≠ 4 mod 11, the check fails. Specifically, t * y^7 = g^7 * y^(7-4) = g^7 * y^3 = 18 * 8^3 mod 23, and 8^3 = 6 mod 23, so t*y^7 = 18 * 6 = 108 mod 23 = 16, while g^7 = 2^7 mod 23 = 128 mod 23 = 18. 18 ≠ 16, so verification fails.

Sample Input and Output

Setup: p=23 q=11 g=2 y=8
Prover: r=5 t=9
Verifier challenge e=4
Prover response s=6
Verify: true
Cheat: pre-pick s=7 e_guess=4
  computed t=18
Verifier sends e=7
  Verify: false

The honest round verifies (g^s = t * y^e mod p) because the prover knows x. The cheat round fails because the prover committed assuming e=4 but received e=7; the gap is too large to cover by coincidence.

Common Mistakes to Avoid

1. Reducing the response mod p instead of mod q. The response s = r + x*e must be computed modulo q (the order of the subgroup), not modulo p. Mod p the equality g^s = t * y^e does not necessarily hold because g has order q, and exponents larger than q wrap around but BigInteger.modPow does not require reduced exponents; however, the algebra still works only if you reduce mod q.

2. Using a generator of the full Z_p^* group instead of the order-q subgroup. This leaks the Legendre symbol of the commitment, which can reveal one bit of the secret via the Pohlig-Hellman attack. Always use a generator of the prime-order subgroup.

3. Reusing r across protocol runs. If the same r is used twice with different challenges e1 and e2, an attacker can subtract the two responses to recover (e1 - e2) * x mod q, and then recover x by multiplying by the modular inverse of (e1 - e2). Always pick a fresh r per run.

4. Letting the challenge be too small. If e is drawn from {0, 1, ..., k-1} with small k, a cheating prover can pre-compute k forged commitments and respond to whichever challenge arrives. Production Schnorr uses 128-bit challenges; the BIP-340 variant uses 128-bit challenges derived from a hash.

Real-World Applications

Schnorr identification and its Fiat-Shamir-transformed signature variant are widely deployed. Bitcoin's BIP-340 Schnorr signatures (deployed in the Taproot upgrade, November 2021) use exactly this structure on the secp256k1 curve, replacing ECDSA for many use cases. The signature is approximately 64 bytes, smaller than the 70-72 bytes of DER-encoded ECDSA.

The FROST threshold Schnorr signature protocol (Komlo and Goldberg 2020) is used by Coinbase Custody, Ferve, and several threshold-signature-as-a-service providers. FROST runs a multi-party variant of Schnorr identification in the preprocessing phase to commit to nonces, then aggregates partial signatures in the signing phase. The result is indistinguishable from a single-signer Schnorr signature.

The MuSig2 protocol (Nick, Ruffing, Seurin 2021) is a multi-signature Schnorr variant used in Lightning Network and several Bitcoin custody solutions. It lets multiple parties jointly sign a single Schnorr signature that is indistinguishable from a single-signer signature, improving privacy and reducing on-chain footprint.

EdDSA (Ed25519 and Ed448), the standard signature scheme used in SSH, Tor, and Apple Secure Enclave, is a deterministic Schnorr signature variant on twisted Edwards curves. RFC 8032 standardizes the construction. The deterministic nonce derivation (using a hash of the secret key and message) eliminates the r-reuse vulnerability that plagues ECDSA.

Conclusion

Schnorr identification is the discrete-log analogue of the RSA-based sigma protocol, distinguished by short signatures and natural compatibility with elliptic curves. Our Java program demonstrated the honest three-move protocol and a failing cheat attempt. Next study the Fiat-Shamir transform of Schnorr (which produces Schnorr signatures), the FROST threshold protocol, and EdDSA on the Ed25519 curve.

Test Your Knowledge
How did you find this?

Comments

Join the discussion! Sign in with your Google or Blogger account, or comment as Anonymous - no account needed. For quick questions, also reach me on Telegram @cytestch.

Comments