|
| 1 | +import { expect } from "chai"; |
| 2 | + |
| 3 | +import { Keystore } from "./keystore/index.js"; |
| 4 | +import { RLNInstance } from "./rln.js"; |
| 5 | +import { BytesUtils } from "./utils/index.js"; |
| 6 | +import { |
| 7 | + calculateRateCommitment, |
| 8 | + extractPathDirectionsFromProof, |
| 9 | + MERKLE_TREE_DEPTH, |
| 10 | + reconstructMerkleRoot |
| 11 | +} from "./utils/merkle.js"; |
| 12 | +import { TEST_KEYSTORE_DATA } from "./utils/test_keystore.js"; |
| 13 | + |
| 14 | +describe("RLN Proof Integration Tests", function () { |
| 15 | + this.timeout(30000); |
| 16 | + |
| 17 | + it("validate stored merkle proof data", function () { |
| 18 | + // Convert stored merkle proof strings to bigints |
| 19 | + const merkleProof = TEST_KEYSTORE_DATA.merkleProof.map((p) => BigInt(p)); |
| 20 | + |
| 21 | + expect(merkleProof).to.be.an("array"); |
| 22 | + expect(merkleProof).to.have.lengthOf(MERKLE_TREE_DEPTH); // RLN uses fixed depth merkle tree |
| 23 | + |
| 24 | + merkleProof.forEach((element, i) => { |
| 25 | + expect(element).to.be.a( |
| 26 | + "bigint", |
| 27 | + `Proof element ${i} should be a bigint` |
| 28 | + ); |
| 29 | + expect(element).to.not.equal(0n, `Proof element ${i} should not be zero`); |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + it("should generate a valid RLN proof", async function () { |
| 34 | + const rlnInstance = await RLNInstance.create(); |
| 35 | + // Load credential from test keystore |
| 36 | + const keystore = Keystore.fromString(TEST_KEYSTORE_DATA.keystoreJson); |
| 37 | + if (!keystore) { |
| 38 | + throw new Error("Failed to load test keystore"); |
| 39 | + } |
| 40 | + const credentialHash = TEST_KEYSTORE_DATA.credentialHash; |
| 41 | + const password = TEST_KEYSTORE_DATA.password; |
| 42 | + const credential = await keystore.readCredential(credentialHash, password); |
| 43 | + if (!credential) { |
| 44 | + throw new Error("Failed to unlock credential with provided password"); |
| 45 | + } |
| 46 | + |
| 47 | + const idCommitment = credential.identity.IDCommitmentBigInt; |
| 48 | + |
| 49 | + const merkleProof = TEST_KEYSTORE_DATA.merkleProof.map((p) => BigInt(p)); |
| 50 | + const merkleRoot = BigInt(TEST_KEYSTORE_DATA.merkleRoot); |
| 51 | + const membershipIndex = BigInt(TEST_KEYSTORE_DATA.membershipIndex); |
| 52 | + const rateLimit = BigInt(TEST_KEYSTORE_DATA.rateLimit); |
| 53 | + |
| 54 | + const rateCommitment = calculateRateCommitment(idCommitment, rateLimit); |
| 55 | + |
| 56 | + const proofElementIndexes = extractPathDirectionsFromProof( |
| 57 | + merkleProof, |
| 58 | + rateCommitment, |
| 59 | + merkleRoot |
| 60 | + ); |
| 61 | + if (!proofElementIndexes) { |
| 62 | + throw new Error("Failed to extract proof element indexes"); |
| 63 | + } |
| 64 | + |
| 65 | + expect(proofElementIndexes).to.have.lengthOf(MERKLE_TREE_DEPTH); |
| 66 | + |
| 67 | + const reconstructedRoot = reconstructMerkleRoot( |
| 68 | + merkleProof, |
| 69 | + membershipIndex, |
| 70 | + rateCommitment |
| 71 | + ); |
| 72 | + |
| 73 | + expect(reconstructedRoot).to.equal( |
| 74 | + merkleRoot, |
| 75 | + "Reconstructed root should match stored root" |
| 76 | + ); |
| 77 | + |
| 78 | + const testMessage = new TextEncoder().encode("test"); |
| 79 | + |
| 80 | + const proof = await rlnInstance.zerokit.generateRLNProof( |
| 81 | + testMessage, |
| 82 | + Number(membershipIndex), |
| 83 | + new Date(), |
| 84 | + credential.identity.IDSecretHash, |
| 85 | + merkleProof.map((proof) => BytesUtils.fromBigInt(proof, 32, "little")), |
| 86 | + proofElementIndexes.map((index) => |
| 87 | + BytesUtils.writeUIntLE(new Uint8Array(1), index, 0, 1) |
| 88 | + ), |
| 89 | + Number(rateLimit), |
| 90 | + 0 |
| 91 | + ); |
| 92 | + |
| 93 | + const isValid = rlnInstance.zerokit.verifyRLNProof( |
| 94 | + BytesUtils.writeUIntLE(new Uint8Array(8), testMessage.length, 0, 8), |
| 95 | + testMessage, |
| 96 | + proof, |
| 97 | + [BytesUtils.fromBigInt(merkleRoot, 32, "little")] |
| 98 | + ); |
| 99 | + expect(isValid).to.be.true; |
| 100 | + }); |
| 101 | +}); |
0 commit comments