Skip to content

Commit aa4d0a2

Browse files
authored
Wrap up ETHDoS example (#121)
1 parent ef3bf26 commit aa4d0a2

File tree

2 files changed

+124
-35
lines changed

2 files changed

+124
-35
lines changed

src/examples.rs

Lines changed: 94 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,11 @@ pub fn eth_dos_pod_builder(
8282
bob_pubkey: &Value,
8383
) -> Result<MainPodBuilder> {
8484
// Will need ETH friend and ETH DoS custom predicate batches.
85-
let eth_friend_batch = eth_friend_batch(params)?;
85+
let eth_friend = CustomPredicateRef(eth_friend_batch(params)?, 0);
8686
let eth_dos_batch = eth_dos_batch(params)?;
87+
let eth_dos_base = CustomPredicateRef(eth_dos_batch.clone(), 0);
88+
let eth_dos_ind = CustomPredicateRef(eth_dos_batch.clone(), 1);
89+
let eth_dos = CustomPredicateRef(eth_dos_batch.clone(), 2);
8790

8891
// ETHDoS POD builder
8992
let mut alice_bob_ethdos = MainPodBuilder::new(params);
@@ -100,15 +103,16 @@ pub fn eth_dos_pod_builder(
100103
.get(&KEY_SIGNER.into())
101104
.ok_or(anyhow!("Could not find Charlie's public key!"))?;
102105

103-
// Include Alice and Bob's keys as public statements.
106+
// Include Alice and Bob's keys as public statements. We don't
107+
// want to reveal the middleman.
104108
let alice_pubkey_copy = alice_bob_ethdos.pub_op(Operation(
105109
OperationType::Native(NativeOperation::NewEntry),
106110
vec![OperationArg::Entry(
107111
"Alice".to_string(),
108112
alice_pubkey.into(),
109113
)],
110114
))?;
111-
let _bob_pubkey_copy = alice_bob_ethdos.pub_op(Operation(
115+
let bob_pubkey_copy = alice_bob_ethdos.pub_op(Operation(
112116
OperationType::Native(NativeOperation::NewEntry),
113117
vec![OperationArg::Entry("Bob".to_string(), bob_pubkey.clone())],
114118
))?;
@@ -132,13 +136,17 @@ pub fn eth_dos_pod_builder(
132136
OperationArg::Statement(alice_pubkey_copy.clone()),
133137
],
134138
))?;
135-
let _ethdos_alice_alice_is_zero = alice_bob_ethdos.priv_op(Operation(
136-
OperationType::Custom(CustomPredicateRef(eth_dos_batch, 0)),
139+
let ethdos_alice_alice_is_zero_base = alice_bob_ethdos.priv_op(Operation(
140+
OperationType::Custom(eth_dos_base.clone()),
137141
vec![
138142
OperationArg::Statement(alice_equals_alice),
139143
OperationArg::Statement(zero.clone()),
140144
],
141145
))?;
146+
let ethdos_alice_alice_is_zero = alice_bob_ethdos.priv_op(Operation(
147+
OperationType::Custom(eth_dos.clone()),
148+
vec![OperationArg::Statement(ethdos_alice_alice_is_zero_base)],
149+
))?;
142150

143151
// Alice and Charlie are ETH friends.
144152
let attestation_is_signed_pod = Statement::from((alice_attestation, KEY_TYPE));
@@ -153,35 +161,100 @@ pub fn eth_dos_pod_builder(
153161
OperationType::Native(NativeOperation::EqualFromEntries),
154162
vec![
155163
OperationArg::from((alice_attestation, "attestation")),
156-
OperationArg::Statement(charlie_pubkey),
164+
OperationArg::Statement(charlie_pubkey.clone()),
157165
],
158166
))?;
159-
let _ethfriends_alice_charlie = alice_bob_ethdos.priv_op(Operation(
160-
OperationType::Custom(CustomPredicateRef(eth_friend_batch, 0)),
167+
let ethfriends_alice_charlie = alice_bob_ethdos.priv_op(Operation(
168+
OperationType::Custom(eth_friend.clone()),
161169
vec![
162170
OperationArg::Statement(attestation_is_signed_pod),
163171
OperationArg::Statement(attestation_signed_by_alice),
164172
OperationArg::Statement(alice_attests_to_charlie),
165173
],
166174
))?;
167175

176+
// ...and so are Chuck and Bob.
177+
let attestation_is_signed_pod = Statement::from((charlie_attestation, KEY_TYPE));
178+
let attestation_signed_by_charlie = alice_bob_ethdos.priv_op(Operation(
179+
OperationType::Native(NativeOperation::EqualFromEntries),
180+
vec![
181+
OperationArg::from((charlie_attestation, KEY_SIGNER)),
182+
OperationArg::Statement(charlie_pubkey),
183+
],
184+
))?;
185+
let charlie_attests_to_bob = alice_bob_ethdos.priv_op(Operation(
186+
OperationType::Native(NativeOperation::EqualFromEntries),
187+
vec![
188+
OperationArg::from((charlie_attestation, "attestation")),
189+
OperationArg::Statement(bob_pubkey_copy),
190+
],
191+
))?;
192+
let ethfriends_charlie_bob = alice_bob_ethdos.priv_op(Operation(
193+
OperationType::Custom(eth_friend.clone()),
194+
vec![
195+
OperationArg::Statement(attestation_is_signed_pod),
196+
OperationArg::Statement(attestation_signed_by_charlie),
197+
OperationArg::Statement(charlie_attests_to_bob),
198+
],
199+
))?;
200+
168201
// The ETHDoS distance from Alice to Charlie is 1.
169-
let _one = alice_bob_ethdos.priv_op(Operation(
202+
let one = alice_bob_ethdos.priv_op(Operation(
170203
OperationType::Native(NativeOperation::NewEntry),
171-
vec![OperationArg::Entry("ZERO".to_string(), Value::from(0i64))],
204+
vec![OperationArg::Entry("ONE".to_string(), Value::from(1i64))],
172205
))?;
173206
// 1 = 0 + 1
174-
// let ethdos_sum = alice_bob_ethdos.priv_op(
175-
// Operation(
176-
// OperationType::Native(NativeOperation::SumOf
177-
// ),
178-
// vec![
179-
// OperationArg::Statement(_one.clone()),
180-
// OperationArg::Statement(zero.clone()),
181-
// OperationArg::Statement(zero.clone())
182-
// ]
183-
// )
184-
// );
207+
let ethdos_sum = alice_bob_ethdos.priv_op(Operation(
208+
OperationType::Native(NativeOperation::SumOf),
209+
vec![
210+
OperationArg::Statement(one.clone()),
211+
OperationArg::Statement(zero.clone()),
212+
OperationArg::Statement(one.clone()),
213+
],
214+
))?;
215+
let ethdos_alice_charlie_is_one_ind = alice_bob_ethdos.priv_op(Operation(
216+
OperationType::Custom(eth_dos_ind.clone()),
217+
vec![
218+
OperationArg::Statement(ethdos_alice_alice_is_zero),
219+
OperationArg::Statement(one.clone()),
220+
OperationArg::Statement(ethdos_sum),
221+
OperationArg::Statement(ethfriends_alice_charlie),
222+
],
223+
))?;
224+
let ethdos_alice_charlie_is_one = alice_bob_ethdos.priv_op(Operation(
225+
OperationType::Custom(eth_dos.clone()),
226+
vec![OperationArg::Statement(ethdos_alice_charlie_is_one_ind)],
227+
))?;
228+
229+
// The ETHDoS distance from Alice to Bob is 2.
230+
// The constant "TWO" and the final statement are both to be
231+
// public.
232+
let two = alice_bob_ethdos.pub_op(Operation(
233+
OperationType::Native(NativeOperation::NewEntry),
234+
vec![OperationArg::Entry("TWO".to_string(), Value::from(2i64))],
235+
))?;
236+
// 2 = 1 + 1
237+
let ethdos_sum = alice_bob_ethdos.priv_op(Operation(
238+
OperationType::Native(NativeOperation::SumOf),
239+
vec![
240+
OperationArg::Statement(two.clone()),
241+
OperationArg::Statement(one.clone()),
242+
OperationArg::Statement(one.clone()),
243+
],
244+
))?;
245+
let ethdos_alice_bob_is_two_ind = alice_bob_ethdos.priv_op(Operation(
246+
OperationType::Custom(eth_dos_ind.clone()),
247+
vec![
248+
OperationArg::Statement(ethdos_alice_charlie_is_one),
249+
OperationArg::Statement(one.clone()),
250+
OperationArg::Statement(ethdos_sum),
251+
OperationArg::Statement(ethfriends_charlie_bob),
252+
],
253+
))?;
254+
let _ethdos_alice_bob_is_two = alice_bob_ethdos.pub_op(Operation(
255+
OperationType::Custom(eth_dos.clone()),
256+
vec![OperationArg::Statement(ethdos_alice_bob_is_two_ind)],
257+
))?;
185258

186259
Ok(alice_bob_ethdos)
187260
}

src/frontend/mod.rs

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ impl MainPodBuilder {
384384
CopyStatement => match &args[0] {
385385
OperationArg::Statement(s) => s.1.clone(),
386386
_ => {
387-
return Err(anyhow!("Invalid arguments to operation: {}", op));
387+
return Err(anyhow!("Invalid arguments to copy operation: {}", op));
388388
}
389389
},
390390
EqualFromEntries => self.op_args_entries(public, args)?,
@@ -409,11 +409,15 @@ impl MainPodBuilder {
409409
if st0_args[1] == st1_args[0] {
410410
vec![st0_args[0].clone(), st1_args[1].clone()]
411411
} else {
412-
return Err(anyhow!("Invalid arguments to operation"));
412+
return Err(anyhow!(
413+
"Invalid arguments to transitive equality operation"
414+
));
413415
}
414416
}
415417
_ => {
416-
return Err(anyhow!("Invalid arguments to operation"));
418+
return Err(anyhow!(
419+
"Invalid arguments to transitive equality operation"
420+
));
417421
}
418422
}
419423
}
@@ -425,7 +429,7 @@ impl MainPodBuilder {
425429
vec![st_args[0].clone()]
426430
}
427431
_ => {
428-
return Err(anyhow!("Invalid arguments to operation"));
432+
return Err(anyhow!("Invalid arguments to gt-to-neq operation"));
429433
}
430434
},
431435
LtToNotEqual => match args[0].clone() {
@@ -436,7 +440,7 @@ impl MainPodBuilder {
436440
vec![st_args[0].clone()]
437441
}
438442
_ => {
439-
return Err(anyhow!("Invalid arguments to operation"));
443+
return Err(anyhow!("Invalid arguments to lt-to-neq operation"));
440444
}
441445
},
442446
ContainsFromEntries => self.op_args_entries(public, args)?,
@@ -476,17 +480,17 @@ impl MainPodBuilder {
476480
st2_args[0].clone(),
477481
]
478482
} else {
479-
return Err(anyhow!("Invalid arguments to operation"));
483+
return Err(anyhow!("Invalid arguments to sum-of operation"));
480484
}
481485
}
482486
_ => {
483-
return Err(anyhow!("Invalid arguments to operation"));
487+
return Err(anyhow!("Invalid arguments to sum-of operation"));
484488
}
485489
};
486490
st_args
487491
}
488492
_ => {
489-
return Err(anyhow!("Invalid arguments to operation"));
493+
return Err(anyhow!("Invalid arguments to sum-of operation"));
490494
}
491495
},
492496
ProductOf => match (args[0].clone(), args[1].clone(), args[2].clone()) {
@@ -524,17 +528,19 @@ impl MainPodBuilder {
524528
st2_args[0].clone(),
525529
]
526530
} else {
527-
return Err(anyhow!("Invalid arguments to operation"));
531+
return Err(anyhow!(
532+
"Invalid arguments to product-of operation"
533+
));
528534
}
529535
}
530536
_ => {
531-
return Err(anyhow!("Invalid arguments to operation"));
537+
return Err(anyhow!("Invalid arguments to product-of operation"));
532538
}
533539
};
534540
st_args
535541
}
536542
_ => {
537-
return Err(anyhow!("Invalid arguments to operation"));
543+
return Err(anyhow!("Invalid arguments to product-of operation"));
538544
}
539545
},
540546
MaxOf => match (args[0].clone(), args[1].clone(), args[2].clone()) {
@@ -572,11 +578,11 @@ impl MainPodBuilder {
572578
st2_args[0].clone(),
573579
]
574580
} else {
575-
return Err(anyhow!("Invalid arguments to operation"));
581+
return Err(anyhow!("Invalid arguments to max-of operation"));
576582
}
577583
}
578584
_ => {
579-
return Err(anyhow!("Invalid arguments to operation"));
585+
return Err(anyhow!("Invalid arguments to max-of operation"));
580586
}
581587
};
582588
st_args
@@ -977,7 +983,17 @@ pub mod tests {
977983

978984
#[test]
979985
fn test_ethdos() -> Result<()> {
980-
let params = Params::default();
986+
let params = Params {
987+
max_input_signed_pods: 3,
988+
max_input_main_pods: 3,
989+
max_statements: 31,
990+
max_signed_pod_values: 8,
991+
max_public_statements: 10,
992+
max_statement_args: 5,
993+
max_operation_args: 5,
994+
max_custom_predicate_arity: 5,
995+
max_custom_batch_size: 5,
996+
};
981997

982998
let mut alice = MockSigner { pk: "Alice".into() };
983999
let bob = MockSigner { pk: "Bob".into() };

0 commit comments

Comments
 (0)