Skip to content

Commit 530cbc2

Browse files
committed
[template-graph] Parse Partial MPK From The WTG
1 parent a4d526b commit 530cbc2

File tree

4 files changed

+86
-10
lines changed

4 files changed

+86
-10
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

accless/libs/abe4/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
mod curve;
22
mod hashing;
33
pub mod policy;
4-
mod scheme;
4+
pub mod scheme;
55

66
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
77
use base64::engine::{Engine as _, general_purpose};

template-graph/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ serde_yaml = { workspace = true }
1717

1818
[dev-dependencies]
1919
ark-std = { workspace = true }
20+
base64 = { workspace = true }
21+
ark-serialize = { workspace = true }

template-graph/tests/api_tests.rs

Lines changed: 81 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,54 @@
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};
211
use std::collections::HashSet;
312
use 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#"
853
version: 1
954
workflow:
@@ -12,13 +57,13 @@ workflow:
1257
authorities:
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
2368
nodes:
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

Comments
 (0)