|
| 1 | +package org.xrpl.xrpl4j.crypto.mpt.bulletproofs; |
| 2 | + |
| 3 | +import com.google.common.hash.Hashing; |
| 4 | +import org.bouncycastle.math.ec.ECPoint; |
| 5 | +import org.xrpl.xrpl4j.crypto.mpt.Secp256k1Operations; |
| 6 | + |
| 7 | +import java.math.BigInteger; |
| 8 | +import java.nio.charset.StandardCharsets; |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +public class ChallengeUtils { |
| 12 | + |
| 13 | + /** |
| 14 | + * Builds the challenge hash for the Schnorr proof. |
| 15 | + * |
| 16 | + * <p>The challenge is computed as: SHA256("MPT_POK_SK_REGISTER" || P || T [|| contextId])</p> |
| 17 | + * |
| 18 | + * @param publicKey The public key point P. |
| 19 | + * @param T The commitment point (k * G). |
| 20 | + * @param contextId The optional 32-byte context identifier. Can be null. |
| 21 | + * |
| 22 | + * @return A 32-byte challenge hash. |
| 23 | + */ |
| 24 | + public static byte[] secretKeyProofChallenge(ECPoint publicKey, ECPoint T, byte[] contextId) { |
| 25 | + String DOMAIN_SEPARATOR = "MPT_POK_SK_REGISTER"; |
| 26 | + |
| 27 | + byte[] domainBytes = DOMAIN_SEPARATOR.getBytes(StandardCharsets.UTF_8); |
| 28 | + byte[] pkBytes = Secp256k1Operations.serializeCompressed(publicKey); |
| 29 | + byte[] tBytes = Secp256k1Operations.serializeCompressed(T); |
| 30 | + |
| 31 | + int contextIdLength = (contextId != null) ? 32 : 0; |
| 32 | + byte[] hashInput = new byte[domainBytes.length + 33 + 33 + contextIdLength]; |
| 33 | + int offset = 0; |
| 34 | + System.arraycopy(domainBytes, 0, hashInput, offset, domainBytes.length); |
| 35 | + offset += domainBytes.length; |
| 36 | + System.arraycopy(pkBytes, 0, hashInput, offset, 33); |
| 37 | + offset += 33; |
| 38 | + System.arraycopy(tBytes, 0, hashInput, offset, 33); |
| 39 | + offset += 33; |
| 40 | + if (contextId != null) { |
| 41 | + System.arraycopy(contextId, 0, hashInput, offset, 32); |
| 42 | + } |
| 43 | + |
| 44 | + byte[] sha256Hash = Hashing.sha256().hashBytes(hashInput).asBytes(); |
| 45 | + |
| 46 | + // Reduce modulo curve order (equivalent to secp256k1_mpt_scalar_reduce32 in C) |
| 47 | + BigInteger hashInt = new BigInteger(1, sha256Hash); |
| 48 | + BigInteger reduced = hashInt.mod(Secp256k1Operations.getCurveOrder()); |
| 49 | + return Secp256k1Operations.toBytes32(reduced); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Computes the Fiat-Shamir challenge hash. |
| 54 | + * |
| 55 | + * <p>Hash( Domain || {R_i, S_i, Pk_i} || Tm || {TrG_i, TrP_i} || TxID )</p> |
| 56 | + */ |
| 57 | + public static byte[] samePlaintextProofChallenge( |
| 58 | + int n, |
| 59 | + List<ECPoint> R, |
| 60 | + List<ECPoint> S, |
| 61 | + List<ECPoint> Pk, |
| 62 | + ECPoint Tm, |
| 63 | + List<ECPoint> TrG, |
| 64 | + List<ECPoint> TrP, |
| 65 | + byte[] txId |
| 66 | + ) { |
| 67 | + String DOMAIN_SEPARATOR = "MPT_POK_SAME_PLAINTEXT_PROOF"; |
| 68 | + // Calculate total size for buffer |
| 69 | + // Domain + n*(R+S+Pk) + Tm + n*(TrG+TrP) + txId |
| 70 | + int domainLen = DOMAIN_SEPARATOR.getBytes(StandardCharsets.UTF_8).length; |
| 71 | + int pointsLen = (3 * n + 1 + 2 * n) * 33; // (R,S,Pk)*n + Tm + (TrG,TrP)*n |
| 72 | + int txIdLen = (txId != null) ? 32 : 0; |
| 73 | + |
| 74 | + byte[] buffer = new byte[domainLen + pointsLen + txIdLen]; |
| 75 | + int offset = 0; |
| 76 | + |
| 77 | + // Domain separator |
| 78 | + byte[] domainBytes = DOMAIN_SEPARATOR.getBytes(StandardCharsets.UTF_8); |
| 79 | + System.arraycopy(domainBytes, 0, buffer, offset, domainBytes.length); |
| 80 | + offset += domainBytes.length; |
| 81 | + |
| 82 | + // Public inputs: {R_i, S_i, Pk_i} |
| 83 | + for (int i = 0; i < n; i++) { |
| 84 | + byte[] rBytes = Secp256k1Operations.serializeCompressed(R.get(i)); |
| 85 | + System.arraycopy(rBytes, 0, buffer, offset, 33); |
| 86 | + offset += 33; |
| 87 | + |
| 88 | + byte[] sBytes = Secp256k1Operations.serializeCompressed(S.get(i)); |
| 89 | + System.arraycopy(sBytes, 0, buffer, offset, 33); |
| 90 | + offset += 33; |
| 91 | + |
| 92 | + byte[] pkBytes = Secp256k1Operations.serializeCompressed(Pk.get(i)); |
| 93 | + System.arraycopy(pkBytes, 0, buffer, offset, 33); |
| 94 | + offset += 33; |
| 95 | + } |
| 96 | + |
| 97 | + // Commitments: Tm |
| 98 | + byte[] tmBytes = Secp256k1Operations.serializeCompressed(Tm); |
| 99 | + System.arraycopy(tmBytes, 0, buffer, offset, 33); |
| 100 | + offset += 33; |
| 101 | + |
| 102 | + // Commitments: {TrG_i, TrP_i} |
| 103 | + for (int i = 0; i < n; i++) { |
| 104 | + byte[] trgBytes = Secp256k1Operations.serializeCompressed(TrG.get(i)); |
| 105 | + System.arraycopy(trgBytes, 0, buffer, offset, 33); |
| 106 | + offset += 33; |
| 107 | + |
| 108 | + byte[] trpBytes = Secp256k1Operations.serializeCompressed(TrP.get(i)); |
| 109 | + System.arraycopy(trpBytes, 0, buffer, offset, 33); |
| 110 | + offset += 33; |
| 111 | + } |
| 112 | + |
| 113 | + // Context (tx_id) |
| 114 | + if (txId != null) { |
| 115 | + System.arraycopy(txId, 0, buffer, offset, 32); |
| 116 | + } |
| 117 | + |
| 118 | + // SHA256 and reduce to scalar |
| 119 | + byte[] hash = Hashing.sha256().hashBytes(buffer).asBytes(); |
| 120 | + return Secp256k1Operations.reduceToScalar(hash); |
| 121 | + } |
| 122 | + |
| 123 | + |
| 124 | + /** |
| 125 | + * Computes the challenge hash for the equality plaintext proof. |
| 126 | + */ |
| 127 | + public static byte[] plaintextEqualityProofChallenge( |
| 128 | + ECPoint c1, |
| 129 | + ECPoint c2, |
| 130 | + ECPoint publicKey, |
| 131 | + ECPoint mG, |
| 132 | + ECPoint T1, |
| 133 | + ECPoint T2, |
| 134 | + byte[] contextId |
| 135 | + ) { |
| 136 | + String DOMAIN_SEPARATOR = "MPT_POK_PLAINTEXT_PROOF"; |
| 137 | + // Calculate total size: domain + c1 + c2 + pk + [mG] + T1 + T2 + context |
| 138 | + int size = DOMAIN_SEPARATOR.length() + 33 + 33 + 33 + 33 + 33 + 32; |
| 139 | + if (mG != null) { |
| 140 | + size += 33; |
| 141 | + } |
| 142 | + |
| 143 | + byte[] hashInput = new byte[size]; |
| 144 | + int offset = 0; |
| 145 | + |
| 146 | + // Domain separator |
| 147 | + byte[] domainBytes = DOMAIN_SEPARATOR.getBytes(StandardCharsets.UTF_8); |
| 148 | + System.arraycopy(domainBytes, 0, hashInput, offset, domainBytes.length); |
| 149 | + offset += domainBytes.length; |
| 150 | + |
| 151 | + // C1, C2, Pk |
| 152 | + byte[] c1Bytes = Secp256k1Operations.serializeCompressed(c1); |
| 153 | + System.arraycopy(c1Bytes, 0, hashInput, offset, 33); |
| 154 | + offset += 33; |
| 155 | + |
| 156 | + byte[] c2Bytes = Secp256k1Operations.serializeCompressed(c2); |
| 157 | + System.arraycopy(c2Bytes, 0, hashInput, offset, 33); |
| 158 | + offset += 33; |
| 159 | + |
| 160 | + byte[] pkBytes = Secp256k1Operations.serializeCompressed(publicKey); |
| 161 | + System.arraycopy(pkBytes, 0, hashInput, offset, 33); |
| 162 | + offset += 33; |
| 163 | + |
| 164 | + // mG (only if nonzero) |
| 165 | + if (mG != null) { |
| 166 | + byte[] mGBytes = Secp256k1Operations.serializeCompressed(mG); |
| 167 | + System.arraycopy(mGBytes, 0, hashInput, offset, 33); |
| 168 | + offset += 33; |
| 169 | + } |
| 170 | + |
| 171 | + // T1, T2 |
| 172 | + byte[] t1Bytes = Secp256k1Operations.serializeCompressed(T1); |
| 173 | + System.arraycopy(t1Bytes, 0, hashInput, offset, 33); |
| 174 | + offset += 33; |
| 175 | + |
| 176 | + byte[] t2Bytes = Secp256k1Operations.serializeCompressed(T2); |
| 177 | + System.arraycopy(t2Bytes, 0, hashInput, offset, 33); |
| 178 | + offset += 33; |
| 179 | + |
| 180 | + // Context ID (always required) |
| 181 | + System.arraycopy(contextId, 0, hashInput, offset, 32); |
| 182 | + |
| 183 | + // SHA256 and reduce mod curve order |
| 184 | + byte[] sha256Hash = Hashing.sha256().hashBytes(hashInput).asBytes(); |
| 185 | + BigInteger hashInt = new BigInteger(1, sha256Hash); |
| 186 | + BigInteger reduced = hashInt.mod(Secp256k1Operations.getCurveOrder()); |
| 187 | + return Secp256k1Operations.toBytes32(reduced); |
| 188 | + } |
| 189 | + |
| 190 | + public static byte[] pedersenLinkProofChallenge( |
| 191 | + ECPoint c1, ECPoint c2, ECPoint pk, ECPoint pcm, |
| 192 | + ECPoint T1, ECPoint T2, ECPoint T3, |
| 193 | + byte[] contextId |
| 194 | + ) { |
| 195 | + String DOMAIN_SEPARATOR = "MPT_ELGAMAL_PEDERSEN_LINK"; |
| 196 | + byte[] domainBytes = DOMAIN_SEPARATOR.getBytes(StandardCharsets.UTF_8); |
| 197 | + int contextLen = (contextId != null) ? 32 : 0; |
| 198 | + int bufferSize = domainBytes.length + (7 * 33) + contextLen; |
| 199 | + |
| 200 | + byte[] buffer = new byte[bufferSize]; |
| 201 | + int offset = 0; |
| 202 | + |
| 203 | + System.arraycopy(domainBytes, 0, buffer, offset, domainBytes.length); |
| 204 | + offset += domainBytes.length; |
| 205 | + |
| 206 | + offset = Secp256k1Operations.appendPoint(buffer, offset, c1); |
| 207 | + offset = Secp256k1Operations.appendPoint(buffer, offset, c2); |
| 208 | + offset = Secp256k1Operations.appendPoint(buffer, offset, pk); |
| 209 | + offset = Secp256k1Operations.appendPoint(buffer, offset, pcm); |
| 210 | + offset = Secp256k1Operations.appendPoint(buffer, offset, T1); |
| 211 | + offset = Secp256k1Operations.appendPoint(buffer, offset, T2); |
| 212 | + offset = Secp256k1Operations.appendPoint(buffer, offset, T3); |
| 213 | + |
| 214 | + if (contextId != null) { |
| 215 | + System.arraycopy(contextId, 0, buffer, offset, 32); |
| 216 | + } |
| 217 | + |
| 218 | + byte[] hash = Hashing.sha256().hashBytes(buffer).asBytes(); |
| 219 | + return Secp256k1Operations.reduceToScalar(hash); |
| 220 | + } |
| 221 | + |
| 222 | +} |
0 commit comments