1- use accless_abe4:: { UserAttribute , decrypt, encrypt, iota:: Iota , keygen, setup, tau:: Tau } ;
1+ use accless_abe4:: {
2+ UserAttribute , decrypt, encrypt,
3+ iota:: Iota ,
4+ keygen,
5+ scheme:: types:: { MPK , PartialMPK } ,
6+ setup,
7+ tau:: Tau ,
8+ } ;
9+ use ark_serialize:: { CanonicalDeserialize , CanonicalSerialize } ;
10+ use base64:: engine:: { Engine as _, general_purpose} ;
211use std:: collections:: HashSet ;
312use template_graph:: { TemplateGraph , policy_compiler} ;
413
14+ ///
15+ /// # Description
16+ ///
17+ /// Helper method to construct a full MPK from a template graph.
18+ ///
19+ fn get_full_mpk_from_template_graph ( template_graph : & TemplateGraph ) -> MPK {
20+ let mut mpk = MPK :: new ( ) ;
21+
22+ // User authority
23+ let user_mpk_bytes = general_purpose:: STANDARD
24+ . decode ( & template_graph. authorities . user . mpk_abe )
25+ . unwrap ( ) ;
26+ let user_partial_mpk = PartialMPK :: deserialize_compressed ( & user_mpk_bytes[ ..] ) . unwrap ( ) ;
27+ mpk. add_partial_key ( user_partial_mpk) ;
28+
29+ // Attestation services
30+ for as_service in & template_graph. authorities . attestation_services {
31+ let as_mpk_bytes = general_purpose:: STANDARD
32+ . decode ( & as_service. mpk_abe )
33+ . unwrap ( ) ;
34+ let as_partial_mpk = PartialMPK :: deserialize_compressed ( & as_mpk_bytes[ ..] ) . unwrap ( ) ;
35+ mpk. add_partial_key ( as_partial_mpk) ;
36+ }
37+
38+ // APS
39+ if let Some ( aps_vec) = & template_graph. authorities . aps {
40+ for aps in aps_vec {
41+ let aps_mpk_bytes = general_purpose:: STANDARD . decode ( & aps. mpk_abe ) . unwrap ( ) ;
42+ let aps_partial_mpk = PartialMPK :: deserialize_compressed ( & aps_mpk_bytes[ ..] ) . unwrap ( ) ;
43+ mpk. add_partial_key ( aps_partial_mpk) ;
44+ }
45+ }
46+
47+ mpk
48+ }
49+
550#[ test]
6- fn test_encrypt_decrypt_workflow ( ) {
51+ fn test_encrypt_decrypt_workflow_with_real_mpk ( ) {
752 let yaml_content = r#"
853version: 1
954workflow:
@@ -12,13 +57,13 @@ workflow:
1257authorities:
1358 user:
1459 id: user_42
15- mpk_abe: base64:mpk_abe_user
60+ mpk_abe: ""
1661 attestation-services:
1762 - id: maa
18- mpk_abe: base64:mpk_abe_maa
63+ mpk_abe: ""
1964 aps:
2065 - id: finra
21- mpk_abe: base64:mpk_abe_finra
66+ mpk_abe: ""
2267
2368nodes:
2469- name: fetch_public
@@ -44,12 +89,11 @@ output:
4489 dir: ./tests/out-ciphertexts
4590 "# ;
4691
47- let template_graph = TemplateGraph :: from_yaml ( yaml_content) . unwrap ( ) ;
48- let policies = policy_compiler:: compile_policies ( & template_graph) ;
92+ let mut template_graph = TemplateGraph :: from_yaml ( yaml_content) . unwrap ( ) ;
4993
5094 let mut rng = ark_std:: test_rng ( ) ;
5195
52- // Setup ABE
96+ // Setup ABE and get partial MPKs
5397 let mut auths: HashSet < String > = template_graph
5498 . authorities
5599 . attestation_services
@@ -65,10 +109,38 @@ output:
65109 let auths_str: Vec < & str > = auths. iter ( ) . map ( |s| s. as_str ( ) ) . collect ( ) ;
66110 let ( msk, mpk) = setup ( & mut rng, & auths_str) ;
67111
112+ // Update template graph with real MPKs
113+ for ( auth, partial_mpk) in & mpk. partial_keys {
114+ let mut mpk_bytes = Vec :: new ( ) ;
115+ partial_mpk. serialize_compressed ( & mut mpk_bytes) . unwrap ( ) ;
116+ let mpk_b64 = general_purpose:: STANDARD . encode ( & mpk_bytes) ;
117+
118+ if auth == & template_graph. authorities . user . id {
119+ template_graph. authorities . user . mpk_abe = mpk_b64;
120+ } else if let Some ( as_service) = template_graph
121+ . authorities
122+ . attestation_services
123+ . iter_mut ( )
124+ . find ( |a| & a. id == auth)
125+ {
126+ as_service. mpk_abe = mpk_b64;
127+ } else if let Some ( aps) = template_graph
128+ . authorities
129+ . aps
130+ . as_mut ( )
131+ . and_then ( |aps_vec| aps_vec. iter_mut ( ) . find ( |a| & a. id == auth) )
132+ {
133+ aps. mpk_abe = mpk_b64;
134+ }
135+ }
136+
137+ let policies = policy_compiler:: compile_policies ( & template_graph) ;
138+ let full_mpk = get_full_mpk_from_template_graph ( & template_graph) ;
139+
68140 // Test encryption and decryption for each node
69141 for node in & template_graph. nodes {
70142 if let Some ( policy) = policies. get ( & node. name ) {
71- let ( k_enc, ct) = encrypt ( & mut rng, & mpk , & policy, & Tau :: new ( & policy) ) ;
143+ let ( k_enc, ct) = encrypt ( & mut rng, & full_mpk , & policy, & Tau :: new ( & policy) ) ;
72144
73145 // Simulate a user with the required attributes
74146 let mut user_attrs = Vec :: new ( ) ;
0 commit comments