-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathflow.rs
391 lines (365 loc) · 13.4 KB
/
flow.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
use blsful::inner_types::*;
use credx::blind::BlindCredentialRequest;
use credx::claim::{
ClaimType, ClaimValidator, HashedClaim, NumberClaim, RevocationClaim, ScalarClaim,
};
use credx::credential::{ClaimSchema, CredentialSchema};
use credx::error::Error;
use credx::issuer::Issuer;
use credx::knox::bbs::BbsScheme;
use credx::prelude::{
MembershipClaim, MembershipCredential, MembershipRegistry, MembershipSigningKey,
MembershipStatement, MembershipVerificationKey,
};
use credx::presentation::{Presentation, PresentationSchema};
use credx::statement::{
CommitmentStatement, RangeStatement, RevocationStatement, SignatureStatement,
VerifiableEncryptionStatement,
};
use credx::{random_string, CredxResult};
use indexmap::indexmap;
use maplit::{btreemap, btreeset};
use rand::thread_rng;
use rand_core::RngCore;
use regex::Regex;
use sha2;
use std::time::Instant;
fn setup() {
let _ = env_logger::builder().is_test(true).try_init();
}
#[test]
fn presentation_1_credential_works() {
setup();
let res = test_presentation_1_credential_works();
assert!(res.is_ok(), "{:?}", res);
}
fn test_presentation_1_credential_works() -> CredxResult<()> {
const LABEL: &str = "Test Schema";
const DESCRIPTION: &str = "This is a test presentation schema";
const CRED_ID: &str = "91742856-6eda-45fb-a709-d22ebb5ec8a5";
let schema_claims = [
ClaimSchema {
claim_type: ClaimType::Revocation,
label: "identifier".to_string(),
print_friendly: false,
validators: vec![],
},
ClaimSchema {
claim_type: ClaimType::Hashed,
label: "name".to_string(),
print_friendly: true,
validators: vec![ClaimValidator::Length {
min: Some(3),
max: Some(u8::MAX as usize),
}],
},
ClaimSchema {
claim_type: ClaimType::Hashed,
label: "address".to_string(),
print_friendly: true,
validators: vec![ClaimValidator::Length {
min: None,
max: Some(u8::MAX as usize),
}],
},
ClaimSchema {
claim_type: ClaimType::Number,
label: "age".to_string(),
print_friendly: true,
validators: vec![ClaimValidator::Range {
min: Some(0),
max: Some(u16::MAX as isize),
}],
},
];
let cred_schema = CredentialSchema::new(Some(LABEL), Some(DESCRIPTION), &[], &schema_claims)?;
let before = Instant::now();
let (issuer_public, mut issuer) = Issuer::<BbsScheme>::new(&cred_schema);
println!("key generation time = {:?}", before.elapsed());
let before = std::time::Instant::now();
let credential = issuer.sign_credential(&[
RevocationClaim::from(CRED_ID).into(),
HashedClaim::from("John Doe").into(),
HashedClaim::from("P Sherman 42 Wallaby Way Sydney").into(),
NumberClaim::from(30303).into(),
])?;
println!("sign credential {:?}", before.elapsed());
let dummy_sk = MembershipSigningKey::new(None);
let dummy_vk = MembershipVerificationKey::from(&dummy_sk);
let dummy_registry = MembershipRegistry::random(thread_rng());
let dummy_membership_credential = MembershipCredential::new(
MembershipClaim::from(&credential.credential.claims[2]).0,
dummy_registry,
&dummy_sk,
);
let sig_st = SignatureStatement {
disclosed: btreeset! {"name".to_string()},
id: random_string(16, rand::thread_rng()),
issuer: issuer_public.clone(),
};
let acc_st = RevocationStatement {
id: random_string(16, rand::thread_rng()),
reference_id: sig_st.id.clone(),
accumulator: issuer_public.revocation_registry,
verification_key: issuer_public.revocation_verifying_key,
claim: 0,
};
let comm_st = CommitmentStatement {
id: random_string(16, rand::thread_rng()),
reference_id: sig_st.id.clone(),
message_generator: G1Projective::hash::<ExpandMsgXmd<sha2::Sha256>>(
b"message generator",
b"BLS12381G1_XMD:SHA-256_SSWU_RO_",
),
blinder_generator: G1Projective::hash::<ExpandMsgXmd<sha2::Sha256>>(
b"blinder generator",
b"BLS12381G1_XMD:SHA-256_SSWU_RO_",
),
claim: 3,
};
let verenc_st = VerifiableEncryptionStatement {
message_generator: G1Projective::GENERATOR,
encryption_key: issuer_public.verifiable_encryption_key,
id: random_string(16, rand::thread_rng()),
reference_id: sig_st.id.clone(),
claim: 0,
};
let range_st = RangeStatement {
id: random_string(16, thread_rng()),
reference_id: comm_st.id.clone(),
signature_id: sig_st.id.clone(),
claim: 3,
lower: Some(0),
upper: Some(44829),
};
let mem_st = MembershipStatement {
id: random_string(16, thread_rng()),
reference_id: sig_st.id.clone(),
accumulator: dummy_registry,
verification_key: dummy_vk,
claim: 2,
};
let mut nonce = [0u8; 16];
thread_rng().fill_bytes(&mut nonce);
let credentials = indexmap! { sig_st.id.clone() => credential.credential.into(), mem_st.id.clone() => dummy_membership_credential.into() };
let presentation_schema = PresentationSchema::new(&[
sig_st.into(),
acc_st.into(),
comm_st.into(),
verenc_st.into(),
range_st.into(),
mem_st.into(),
]);
// println!("{}", serde_json::to_string(&presentation_schema).unwrap());
let before = Instant::now();
let presentation = Presentation::create(&credentials, &presentation_schema, &nonce)?;
println!("proof generation: {:?}", before.elapsed());
presentation.verify(&presentation_schema, &nonce)?;
let before = Instant::now();
println!("proof verification: {:?}", before.elapsed());
let proof_data = serde_bare::to_vec(&presentation).unwrap();
let presentation: Presentation<BbsScheme> = serde_bare::from_slice(&proof_data).unwrap();
println!("proof size = {}", proof_data.len());
presentation.verify(&presentation_schema, &nonce)
}
#[ignore]
#[test]
fn presentation_1_credential_alter_revealed_message_fails() {
let res = test_presentation_1_credential_alter_revealed_message_fails();
assert!(res.is_ok(), "{:?}", res);
}
fn test_presentation_1_credential_alter_revealed_message_fails() -> CredxResult<()> {
const LABEL: &str = "Test Schema";
const DESCRIPTION: &str = "This is a test presentation schema";
const CRED_ID: &str = "91742856-6eda-45fb-a709-d22ebb5ec8a5";
let schema_claims = [
ClaimSchema {
claim_type: ClaimType::Revocation,
label: "identifier".to_string(),
print_friendly: false,
validators: vec![],
},
ClaimSchema {
claim_type: ClaimType::Hashed,
label: "name".to_string(),
print_friendly: true,
validators: vec![ClaimValidator::Length {
min: Some(3),
max: Some(u8::MAX as usize),
}],
},
ClaimSchema {
claim_type: ClaimType::Hashed,
label: "address".to_string(),
print_friendly: true,
validators: vec![
ClaimValidator::Length {
min: None,
max: Some(u8::MAX as usize),
},
ClaimValidator::Regex(Regex::new(r#"[\w\s]+"#).unwrap()),
ClaimValidator::AnyOne(vec![
NumberClaim::from(0).into(),
NumberClaim::from(2).into(),
NumberClaim::from(4).into(),
NumberClaim::from(6).into(),
NumberClaim::from(8).into(),
NumberClaim::from(10).into(),
]),
],
},
ClaimSchema {
claim_type: ClaimType::Number,
label: "age".to_string(),
print_friendly: true,
validators: vec![ClaimValidator::Range {
min: Some(0),
max: Some(u32::MAX as isize),
}],
},
];
let cred_schema = CredentialSchema::new(Some(LABEL), Some(DESCRIPTION), &[], &schema_claims)?;
println!("{}", serde_json::to_string(&cred_schema).unwrap());
let (issuer_public, mut issuer) = Issuer::<BbsScheme>::new(&cred_schema);
let credential = issuer.sign_credential(&[
RevocationClaim::from(CRED_ID).into(),
HashedClaim::from("John Doe").into(),
HashedClaim::from("P Sherman 42 Wallaby Way Sydney").into(),
NumberClaim::from(30303).into(),
])?;
let sig_st_id = random_string(16, rand::thread_rng());
let sig_st = SignatureStatement {
disclosed: btreeset! {"name".to_string()},
id: sig_st_id.clone(),
issuer: issuer_public.clone(),
};
let acc_st = RevocationStatement {
id: random_string(16, rand::thread_rng()),
reference_id: sig_st.id.clone(),
accumulator: issuer_public.revocation_registry,
verification_key: issuer_public.revocation_verifying_key,
claim: 0,
};
let comm_st = CommitmentStatement {
id: random_string(16, rand::thread_rng()),
reference_id: sig_st.id.clone(),
message_generator: G1Projective::hash::<ExpandMsgXmd<sha2::Sha256>>(
b"message generator",
b"BLS12381G1_XMD:SHA-256_SSWU_RO_",
),
blinder_generator: G1Projective::hash::<ExpandMsgXmd<sha2::Sha256>>(
b"blinder generator",
b"BLS12381G1_XMD:SHA-256_SSWU_RO_",
),
claim: 3,
};
let verenc_st = VerifiableEncryptionStatement {
message_generator: G1Projective::GENERATOR,
encryption_key: issuer_public.verifiable_encryption_key,
id: random_string(16, rand::thread_rng()),
reference_id: sig_st.id.clone(),
claim: 0,
};
let range_st = RangeStatement {
id: random_string(16, rand::thread_rng()),
reference_id: comm_st.id.clone(),
signature_id: sig_st.id.clone(),
claim: 3,
lower: Some(0),
upper: Some(44829),
};
let mut nonce = [0u8; 16];
rand::thread_rng().fill_bytes(&mut nonce);
let credentials = indexmap! { sig_st.id.clone() => credential.credential.into() };
let presentation_schema = PresentationSchema::new(&[
sig_st.into(),
acc_st.into(),
comm_st.into(),
verenc_st.into(),
range_st.into(),
]);
let mut presentation = Presentation::create(&credentials, &presentation_schema, &nonce)?;
let disclosed_claim = presentation
.disclosed_messages
.get_mut(&sig_st_id)
.unwrap()
.get_mut("name")
.unwrap();
*disclosed_claim = HashedClaim::from("Jane Doe").into();
match presentation.verify(&presentation_schema, &nonce) {
Err(_) => Ok(()),
Ok(_) => Err(Error::InvalidPresentationData),
}
}
#[test]
fn blind_sign_request() {
setup();
let res = test_blind_sign_request();
assert!(res.is_ok(), "{:?}", res);
}
fn test_blind_sign_request() -> CredxResult<()> {
const LABEL: &str = "Test Schema";
const DESCRIPTION: &str = "This is a test presentation schema";
const CRED_ID: &str = "91742856-6eda-45fb-a709-d22ebb5ec8a5";
let schema_claims = [
ClaimSchema {
claim_type: ClaimType::Revocation,
label: "identifier".to_string(),
print_friendly: false,
validators: vec![],
},
ClaimSchema {
claim_type: ClaimType::Scalar,
label: "link_secret".to_string(),
print_friendly: false,
validators: vec![],
},
ClaimSchema {
claim_type: ClaimType::Hashed,
label: "name".to_string(),
print_friendly: true,
validators: vec![ClaimValidator::Length {
min: Some(3),
max: Some(u8::MAX as usize),
}],
},
ClaimSchema {
claim_type: ClaimType::Hashed,
label: "address".to_string(),
print_friendly: true,
validators: vec![ClaimValidator::Length {
min: None,
max: Some(u8::MAX as usize),
}],
},
ClaimSchema {
claim_type: ClaimType::Number,
label: "age".to_string(),
print_friendly: true,
validators: vec![ClaimValidator::Range {
min: Some(0),
max: Some(u16::MAX as isize),
}],
},
];
let cred_schema = CredentialSchema::new(
Some(LABEL),
Some(DESCRIPTION),
&["link_secret"],
&schema_claims,
)?;
let (issuer_public, mut issuer) = Issuer::<BbsScheme>::new(&cred_schema);
let blind_claims = btreemap! { "link_secret".to_string() => ScalarClaim::from(Scalar::random(rand_core::OsRng)).into() };
let (request, blinder) = BlindCredentialRequest::new(&issuer_public, &blind_claims)?;
let blind_bundle = issuer.blind_sign_credential(
&request,
&btreemap! {
"identifier".to_string() => RevocationClaim::from(CRED_ID).into(),
"name".to_string() => HashedClaim::from("John Doe").into(),
"address".to_string() => HashedClaim::from("P Sherman 42 Wallaby Way Sydney").into(),
"age".to_string() => NumberClaim::from(30303).into(),
},
)?;
let _ = blind_bundle.to_unblinded(&blind_claims, blinder)?;
Ok(())
}