@@ -9,86 +9,96 @@ import {
99 MERKLE_TREE_DEPTH ,
1010 reconstructMerkleRoot
1111} from "./utils/merkle.js" ;
12- import { TEST_KEYSTORE_DATA } from "./utils/test_keystore.js" ;
12+ import {
13+ TEST_CREDENTIALS ,
14+ TEST_KEYSTORE_PASSWORD ,
15+ TEST_MERKLE_ROOT
16+ } from "./utils/test_keystore.js" ;
1317
1418describe ( "RLN Proof Integration Tests" , function ( ) {
1519 this . timeout ( 30000 ) ;
1620
17- it ( "validate stored merkle proof data" , function ( ) {
18- const merkleProof = TEST_KEYSTORE_DATA . merkleProof . map ( ( p ) => BigInt ( p ) ) ;
19-
20- expect ( merkleProof ) . to . be . an ( "array" ) ;
21- expect ( merkleProof ) . to . have . lengthOf ( MERKLE_TREE_DEPTH ) ;
22-
23- for ( let i = 0 ; i < merkleProof . length ; i ++ ) {
24- const element = merkleProof [ 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- const keystore = Keystore . fromString ( TEST_KEYSTORE_DATA . keystoreJson ) ;
36- if ( ! keystore ) {
37- throw new Error ( "Failed to load test keystore" ) ;
38- }
39- const credentialHash = TEST_KEYSTORE_DATA . credentialHash ;
40- const password = TEST_KEYSTORE_DATA . password ;
41- const credential = await keystore . readCredential ( credentialHash , password ) ;
42- if ( ! credential ) {
43- throw new Error ( "Failed to unlock credential with provided password" ) ;
44- }
45-
46- const idCommitment = credential . identity . IDCommitmentBigInt ;
47-
48- const merkleProof = TEST_KEYSTORE_DATA . merkleProof . map ( ( p ) => BigInt ( p ) ) ;
49- const merkleRoot = BigInt ( TEST_KEYSTORE_DATA . merkleRoot ) ;
50- const membershipIndex = BigInt ( TEST_KEYSTORE_DATA . membershipIndex ) ;
51- const rateLimit = BigInt ( TEST_KEYSTORE_DATA . rateLimit ) ;
52-
53- const rateCommitment = calculateRateCommitment ( idCommitment , rateLimit ) ;
54-
55- const proofElementIndexes = getPathDirectionsFromIndex ( membershipIndex ) ;
56-
57- expect ( proofElementIndexes ) . to . have . lengthOf ( MERKLE_TREE_DEPTH ) ;
58-
59- const reconstructedRoot = reconstructMerkleRoot (
60- merkleProof ,
61- membershipIndex ,
62- rateCommitment
63- ) ;
64-
65- expect ( reconstructedRoot ) . to . equal (
66- merkleRoot ,
67- "Reconstructed root should match stored root"
68- ) ;
69-
70- const testMessage = new TextEncoder ( ) . encode ( "test" ) ;
71-
72- const proof = await rlnInstance . zerokit . generateRLNProof (
73- testMessage ,
74- new Date ( ) ,
75- credential . identity . IDSecretHash ,
76- merkleProof . map ( ( element ) =>
77- BytesUtils . bytes32FromBigInt ( element , "little" )
78- ) ,
79- proofElementIndexes . map ( ( index ) =>
80- BytesUtils . writeUintLE ( new Uint8Array ( 1 ) , index , 0 , 1 )
81- ) ,
82- Number ( rateLimit ) ,
83- 0
84- ) ;
85-
86- const isValid = rlnInstance . zerokit . verifyRLNProof (
87- BytesUtils . writeUintLE ( new Uint8Array ( 8 ) , testMessage . length , 0 , 8 ) ,
88- testMessage ,
89- proof ,
90- [ BytesUtils . bytes32FromBigInt ( merkleRoot , "little" ) ]
91- ) ;
92- expect ( isValid ) . to . be . true ;
21+ TEST_CREDENTIALS . forEach ( ( credential , index ) => {
22+ describe ( `Credential ${ index + 1 } ` , function ( ) {
23+ it ( "validate stored merkle proof data" , function ( ) {
24+ const merkleProof = credential . merkleProof . map ( ( p ) => BigInt ( p ) ) ;
25+
26+ expect ( merkleProof ) . to . be . an ( "array" ) ;
27+ expect ( merkleProof ) . to . have . lengthOf ( MERKLE_TREE_DEPTH ) ;
28+
29+ for ( let i = 0 ; i < merkleProof . length ; i ++ ) {
30+ const element = merkleProof [ i ] ;
31+ expect ( element ) . to . be . a (
32+ "bigint" ,
33+ `Proof element ${ i } should be a bigint`
34+ ) ;
35+ // Note: First element can be 0 for some tree positions (e.g., credential 3)
36+ }
37+ } ) ;
38+
39+ it ( "should generate a valid RLN proof" , async function ( ) {
40+ const rlnInstance = await RLNInstance . create ( ) ;
41+ const keystore = Keystore . fromString ( credential . keystoreJson ) ;
42+ if ( ! keystore ) {
43+ throw new Error ( "Failed to load test keystore" ) ;
44+ }
45+ const credentialHash = credential . credentialHash ;
46+ const decrypted = await keystore . readCredential (
47+ credentialHash ,
48+ TEST_KEYSTORE_PASSWORD
49+ ) ;
50+ if ( ! decrypted ) {
51+ throw new Error ( "Failed to unlock credential with provided password" ) ;
52+ }
53+
54+ const idCommitment = decrypted . identity . IDCommitmentBigInt ;
55+
56+ const merkleProof = credential . merkleProof . map ( ( p ) => BigInt ( p ) ) ;
57+ const merkleRoot = BigInt ( TEST_MERKLE_ROOT ) ;
58+ const membershipIndex = BigInt ( credential . membershipIndex ) ;
59+ const rateLimit = BigInt ( credential . rateLimit ) ;
60+
61+ const rateCommitment = calculateRateCommitment ( idCommitment , rateLimit ) ;
62+
63+ const proofElementIndexes = getPathDirectionsFromIndex ( membershipIndex ) ;
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+ new Date ( ) ,
83+ decrypted . identity . IDSecretHash ,
84+ merkleProof . map ( ( element ) =>
85+ BytesUtils . bytes32FromBigInt ( element , "little" )
86+ ) ,
87+ proofElementIndexes . map ( ( idx ) =>
88+ BytesUtils . writeUintLE ( new Uint8Array ( 1 ) , idx , 0 , 1 )
89+ ) ,
90+ Number ( rateLimit ) ,
91+ 0
92+ ) ;
93+
94+ const isValid = rlnInstance . zerokit . verifyRLNProof (
95+ BytesUtils . writeUintLE ( new Uint8Array ( 8 ) , testMessage . length , 0 , 8 ) ,
96+ testMessage ,
97+ proof ,
98+ [ BytesUtils . bytes32FromBigInt ( merkleRoot , "little" ) ]
99+ ) ;
100+ expect ( isValid ) . to . be . true ;
101+ } ) ;
102+ } ) ;
93103 } ) ;
94104} ) ;
0 commit comments