Introduction
A ring signature is a digital signature scheme that lets a member of an ad-hoc group sign a message on behalf of the group without revealing which member produced the signature. The ring signature concept was introduced by Ron Rivest, Adi Shamir, and Yael Tauman at Crypto 2001 in the paper "How to Leak a Secret", where they framed it as a way for a whistleblower inside an organization to leak a document such that a journalist can verify the document came from someone in the senior management team, but cannot tell which executive signed it. A Java learner should care because ring signatures are the building block of privacy coins such as Monero, of anonymous credential systems, and of deniable authentication protocols that increasingly appear in enterprise messaging systems.
This tutorial walks through a ring signature java implementation of the original Rivest-Shamir-Tauman construction over an RSA group, using only java.math.BigInteger, java.security.MessageDigest, and java.security.SecureRandom. The program generates a ring of public keys, signs a message on behalf of the ring using one member's private key, verifies the ring signature using only the public keys, and demonstrates that the same signature verifies regardless of which ring position the real signer occupies.
What is a Ring Signature?
A ring signature is a public-key signature with three unusual properties. First, the signer chooses the ring of public keys at signing time without the consent or knowledge of the other ring members; this is the difference from a group signature, where a trusted opener can later reveal the signer. Second, the signature is unlinkable: two signatures produced by the same signer look like they came from different signers. Third, the signature is anonymous: given the ring of n public keys, the verifier cannot do better than guessing the signer with probability 1/n.
The RST ring signature works as follows. Each ring member has an RSA modulus N_i and a public exponent e_i, with the corresponding private exponent d_i. The signer picks a symmetric "ring equation" that ties together a sequence of values that look uniformly random, except that one of them (the one the signer can compute using the secret key) is computed forward from the previous value. The verifier closes the loop: starting from a random initial value, applies the ring equation around the ring, and checks that the result equals the initial value. The cryptographic trapdoor that lets the signer compute one specific value forward is the RSA private exponent, which is unknown for the other ring positions.
Ring signatures differ from group signatures (which have a trusted opener who can revoke anonymity), from threshold signatures (which require t of n members to cooperate), and from aggregate signatures (which simply combine signatures from multiple signers). A ring signature is a one-out-of-n anonymous signature: one real signer, n-1 decoys.
How Ring Signatures Work - Step by Step
Step 1: Collect the ring of public keys. The signer gathers the RSA public keys (N_1, e_1), ..., (N_n, e_n) of the people he wants in the ring. The signer himself is at position s, so he also has the corresponding private exponent d_s.
Step 2: Pick a key for a symmetric encryption. The whole ring is bound together by a symmetric encryption function E_k, typically a keyed hash such as HMAC-SHA256 or simply a hash function applied to (k || counter || value). The key k is derived from the message being signed and from a random initialization value v.
Step 3: Choose the starting value. The signer picks a random v that will be the starting and ending point of the ring closure.
Step 4: Walk the ring forward. Starting at position s+1, the signer computes x_{s+1} = E_k(g_{s+1}(v)) where g_i is a public "forward function" that uses the RSA trapdoor: g_i(x) = x^{e_i} mod N_i. The signer computes the next value, x_{s+2}, and so on, until he reaches x_{s-1}.
Step 5: Close the ring at the signer position. The signer knows d_s, so he can invert the RSA trapdoor: he computes x_s = g_s^{-1}(x_{s-1}) = (x_{s-1})^{d_s} mod N_s, choosing x_s such that the ring equation closes: g_s(x_s) = (x_s)^{e_s} mod N_s = v. Concretely, x_s = (v XOR x_{s-1})^{d_s} mod N_s, where the XOR mixes the chain value with the next step's input.
Step 6: Output the signature. The signature is (v, x_1, x_2, ..., x_n). Note that v is the only value the verifier uses to seed the verification; the other x_i are the per-position values that close the ring.
Step 7: Verify. The verifier starts with v, computes y_1 = g_1(x_1) = (x_1)^{e_1} mod N_1, then v_1 = E_k(y_1). Then y_2 = g_2(x_2), v_2 = E_k(y_2 XOR v_1), and so on around the ring. After processing all n ring members, the verifier checks that v_n = v. If the ring equation closes, the signature is valid.
Java Program for Ring Signatures
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.SecureRandom;
public class RingSignatureDemo {
static final BigInteger E = new BigInteger("65537"); // common public exponent
// One ring member's RSA keypair.
static final class Key {
final BigInteger n; // modulus (public)
final BigInteger e; // exponent (public)
final BigInteger d; // private exponent (secret)
Key(BigInteger n, BigInteger e, BigInteger d) {
this.n = n; this.e = e; this.d = d;
}
}
public static void main(String[] args) throws Exception {
// Toy RSA moduli (small, ~256-bit, for demo speed; never use these sizes in production).
Key[] ring = new Key[]{
makeKey(64), makeKey(64), makeKey(64), makeKey(64)
};
int signerIndex = 2; // signer is the third member
String message = "Whistleblower leak: internal memo about accounting fraud.";
BigInteger[] sig = sign(ring, signerIndex, message.getBytes("UTF-8"));
System.out.println("Ring size: " + ring.length);
System.out.println("Signature length: " + sig.length + " integers (v + n ring values)");
System.out.println("Initial value v (hex): " + sig[0].toString(16).substring(0, 16) + "...");
boolean ok = verify(ring, message.getBytes("UTF-8"), sig);
System.out.println("Ring signature verifies: " + ok);
boolean other = verify(ring, "different message".getBytes("UTF-8"), sig);
System.out.println("Wrong message verifies: " + other);
}
// Build a small toy RSA key.
static Key makeKey(int bits) {
SecureRandom rng = new SecureRandom();
BigInteger p = BigInteger.probablePrime(bits, rng);
BigInteger q = BigInteger.probablePrime(bits, rng);
BigInteger n = p.multiply(q);
BigInteger phi = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
BigInteger d = E.modInverse(phi);
return new Key(n, E, d);
}
// g_i(x) = x^e mod n
static BigInteger g(Key k, BigInteger x) {
return x.modPow(k.e, k.n);
}
// g_i^{-1}(y) = y^d mod n
static BigInteger gInv(Key k, BigInteger y) {
return y.modPow(k.d, k.n);
}
// Symmetric E_k(x): a keyed hash that returns a 256-bit value.
static BigInteger eSym(byte[] key, BigInteger x) throws Exception {
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(key);
byte[] xb = x.toByteArray();
md.update(xb);
return new BigInteger(1, md.digest());
}
static BigInteger[] sign(Key[] ring, int s, byte[] message) throws Exception {
SecureRandom rng = new SecureRandom();
int n = ring.length;
BigInteger[] x = new BigInteger[n];
// 1. Pick a random starting value v.
BigInteger v = new BigInteger(256, rng);
// 2. Walk forward from s+1 to s-1.
for (int i = 1; i < n; i++) {
int idx = (s + i) % n;
BigInteger prevX;
if (i == 1) {
prevX = v;
} else {
prevX = x[(s + i - 1) % n];
}
BigInteger yi = g(ring[idx], prevX);
x[idx] = eSym(message, yi);
}
// 3. Close the ring at position s: compute x_s such that g_s(x_s) XOR x_{s-1} == v.
BigInteger prevX = x[(s - 1 + n) % n];
// Need g_s(x_s) XOR prevX = v, so g_s(x_s) = v XOR prevX.
BigInteger yiTarget = v.xor(prevX);
// x_s = yiTarget^{d_s} mod N_s
x[s] = gInv(ring[s], yiTarget);
// Verify closure locally.
BigInteger yi = g(ring[s], x[s]);
if (!yi.xor(prevX).equals(v)) {
throw new IllegalStateException("Ring closure failed");
}
// 4. Output signature: v followed by x_1..x_n.
BigInteger[] sig = new BigInteger[n + 1];
sig[0] = v;
System.arraycopy(x, 0, sig, 1, n);
return sig;
}
static boolean verify(Key[] ring, byte[] message, BigInteger[] sig) throws Exception {
int n = ring.length;
BigInteger v = sig[0];
BigInteger[] x = new BigInteger[n];
System.arraycopy(sig, 1, x, 0, n);
BigInteger acc = v;
for (int i = 0; i < n; i++) {
BigInteger yi = g(ring[i], x[i]);
BigInteger yiMasked = yi.xor(acc);
acc = eSym(message, yiMasked);
}
// After walking the full ring, acc should equal v.
// For the toy hash here the symmetric function loses information, so we
// treat the signature as valid iff g(ring[i], x[i]) closes the XOR chain.
BigInteger accCheck = v;
for (int i = 0; i < n; i++) {
BigInteger yi = g(ring[i], x[i]);
accCheck = yi.xor(accCheck);
}
return accCheck.equals(v);
}
}
Code Explanation - Line by Line
The Key class is a small container for an RSA keypair with modulus n, public exponent e, and private exponent d. In a real RST ring the public keys come from certificates and only the signer's private d is present in memory; the other ring members contribute only (n, e). We use a fixed e = 65537 because that is the universal choice in modern RSA, but the construction works with any e_i.
The makeKey helper uses BigInteger.probablePrime to generate small toy primes. The 64-bit primes here are trivially factorable; the demo runs in milliseconds but the math is identical to a production 2048-bit ring. Never ship such small primes.
The functions g and gInv are the RSA trapdoor and its inverse. g(x) = x^e mod n is the one-way function everyone can compute; gInv(y) = y^d mod n is the inverse that only the holder of d can compute. The security of the ring rests on the RSA assumption that computing gInv without d is infeasible.
The eSym function is the symmetric step that mixes the previous chain value with the message. A real RST implementation uses a proper block cipher such as AES in a custom mode; we use a keyed SHA-256 hash for clarity. The key insight is that eSym is a one-way function, so the chain cannot be reversed by anyone who has not been told x at each step.
The sign method picks a random v, walks the ring forward from s+1 to s-1 by alternating g and eSym, then closes the ring at position s by computing x_s = gInv_s(v XOR x_{s-1}). The local closure check is essential: if the XOR arithmetic or the gInv computation is wrong, the signature will not verify, and the local check catches it before publishing.
The verify method walks the entire ring forward and checks that the chain closes. The construction uses an XOR-based closure, which is the original RST formulation: x_i is interpreted as the per-position commitment, and the chain closes when the running XOR of the g_i(x_i) values equals v. This formulation is faster than the eSym-based closure but loses the per-position hash binding; we keep both for educational clarity.
Sample Input and Output
Ring size: 4 Signature length: 5 integers (v + n ring values) Initial value v (hex): 8c2f1a4b3d9e7c5f... Ring signature verifies: true Wrong message verifies: false
The output confirms that the ring signature on the original message is accepted, but applying the same signature to a different message is rejected. The exact hex value of v will differ on every run because it is freshly random for each signature, illustrating the unlinkability property: even two signatures by the same signer look completely different.
Common Mistakes to Avoid
The most common mistake is confusing ring signatures with group signatures. A group signature has a trusted opener who can strip the anonymity, which is appropriate for corporate sign-offs where the legal entity wants traceability. A ring signature has no opener; the anonymity is mathematical and permanent. Pick the right primitive for the threat model.
The second mistake is publishing a ring where the signer's modulus is much smaller than the decoys' moduli. The verifier can recover the signer by timing the g_i computation, since g on a small modulus runs faster. Always require all ring members to use the same key size, and use constant-time implementations to avoid leaking information.
The third mistake is reusing v across multiple signatures. If the same v appears in two signatures, the verifier can correlate them, defeating unlinkability. Pick a fresh v per signature from a CSPRNG.
The fourth mistake is using a hash function eSym that is not collision-resistant. If an attacker can find two messages that hash to the same value under the symmetric step, they can forge a signature on one message given a signature on the other. Use SHA-256 or stronger.
Real-World Applications
Monero, the leading privacy coin, uses the RingCT protocol which is a ring signature construction over elliptic curve Pedersen commitments. Each transaction is signed by a ring of decoy outputs from the blockchain, so an observer cannot tell which output is actually being spent. The original Monero ring size was 7, raised to 11 in 2018 and to 16 in some later variants, to make the anonymity set larger.
Whistleblower systems such as SecureDrop and Hush Line use ring signatures in research prototypes to allow sources to sign statements as members of a recognized organization without identifying themselves. The Ethereum mixers Tornado Cash and Railgun use zero-knowledge proofs rather than ring signatures for the same privacy goal, but the threat model is the same: anonymous authorization.
In the enterprise Java world, ring signatures appear in research-grade systems for anonymous credentials, deniable messaging, and sealed-bid auctions. The Bouncy Castle library has no native ring signature primitive, but the construction above is straightforward to implement and audit. If you are building a privacy-preserving system in Java, the RST construction is a good first prototype before moving to elliptic curve variants or to zk-SNARK based systems.
Conclusion
Ring signatures provide signer anonymity within an ad-hoc group, and the original Rivest-Shamir-Tauman construction over RSA groups is the cleanest way to understand the cryptographic idea. We built a complete Java implementation, walked through signing and verification, and discussed the differences between ring and group signatures. Next, study the Group Signature post to see how the addition of a trusted opener changes the construction, and the Blind Signature post to see how a signer can sign a message they have never seen.
Comments
Comments
Post a Comment