Wrap up ETHDoS example (#121)

This commit is contained in:
Ahmad Afuni 2025-03-11 00:03:39 +10:00 committed by GitHub
parent ef3bf26533
commit aa4d0a2670
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 124 additions and 35 deletions

View file

@ -82,8 +82,11 @@ pub fn eth_dos_pod_builder(
bob_pubkey: &Value, bob_pubkey: &Value,
) -> Result<MainPodBuilder> { ) -> Result<MainPodBuilder> {
// Will need ETH friend and ETH DoS custom predicate batches. // Will need ETH friend and ETH DoS custom predicate batches.
let eth_friend_batch = eth_friend_batch(params)?; let eth_friend = CustomPredicateRef(eth_friend_batch(params)?, 0);
let eth_dos_batch = eth_dos_batch(params)?; let eth_dos_batch = eth_dos_batch(params)?;
let eth_dos_base = CustomPredicateRef(eth_dos_batch.clone(), 0);
let eth_dos_ind = CustomPredicateRef(eth_dos_batch.clone(), 1);
let eth_dos = CustomPredicateRef(eth_dos_batch.clone(), 2);
// ETHDoS POD builder // ETHDoS POD builder
let mut alice_bob_ethdos = MainPodBuilder::new(params); let mut alice_bob_ethdos = MainPodBuilder::new(params);
@ -100,7 +103,8 @@ pub fn eth_dos_pod_builder(
.get(&KEY_SIGNER.into()) .get(&KEY_SIGNER.into())
.ok_or(anyhow!("Could not find Charlie's public key!"))?; .ok_or(anyhow!("Could not find Charlie's public key!"))?;
// Include Alice and Bob's keys as public statements. // Include Alice and Bob's keys as public statements. We don't
// want to reveal the middleman.
let alice_pubkey_copy = alice_bob_ethdos.pub_op(Operation( let alice_pubkey_copy = alice_bob_ethdos.pub_op(Operation(
OperationType::Native(NativeOperation::NewEntry), OperationType::Native(NativeOperation::NewEntry),
vec![OperationArg::Entry( vec![OperationArg::Entry(
@ -108,7 +112,7 @@ pub fn eth_dos_pod_builder(
alice_pubkey.into(), alice_pubkey.into(),
)], )],
))?; ))?;
let _bob_pubkey_copy = alice_bob_ethdos.pub_op(Operation( let bob_pubkey_copy = alice_bob_ethdos.pub_op(Operation(
OperationType::Native(NativeOperation::NewEntry), OperationType::Native(NativeOperation::NewEntry),
vec![OperationArg::Entry("Bob".to_string(), bob_pubkey.clone())], vec![OperationArg::Entry("Bob".to_string(), bob_pubkey.clone())],
))?; ))?;
@ -132,13 +136,17 @@ pub fn eth_dos_pod_builder(
OperationArg::Statement(alice_pubkey_copy.clone()), OperationArg::Statement(alice_pubkey_copy.clone()),
], ],
))?; ))?;
let _ethdos_alice_alice_is_zero = alice_bob_ethdos.priv_op(Operation( let ethdos_alice_alice_is_zero_base = alice_bob_ethdos.priv_op(Operation(
OperationType::Custom(CustomPredicateRef(eth_dos_batch, 0)), OperationType::Custom(eth_dos_base.clone()),
vec![ vec![
OperationArg::Statement(alice_equals_alice), OperationArg::Statement(alice_equals_alice),
OperationArg::Statement(zero.clone()), OperationArg::Statement(zero.clone()),
], ],
))?; ))?;
let ethdos_alice_alice_is_zero = alice_bob_ethdos.priv_op(Operation(
OperationType::Custom(eth_dos.clone()),
vec![OperationArg::Statement(ethdos_alice_alice_is_zero_base)],
))?;
// Alice and Charlie are ETH friends. // Alice and Charlie are ETH friends.
let attestation_is_signed_pod = Statement::from((alice_attestation, KEY_TYPE)); let attestation_is_signed_pod = Statement::from((alice_attestation, KEY_TYPE));
@ -153,11 +161,11 @@ pub fn eth_dos_pod_builder(
OperationType::Native(NativeOperation::EqualFromEntries), OperationType::Native(NativeOperation::EqualFromEntries),
vec![ vec![
OperationArg::from((alice_attestation, "attestation")), OperationArg::from((alice_attestation, "attestation")),
OperationArg::Statement(charlie_pubkey), OperationArg::Statement(charlie_pubkey.clone()),
], ],
))?; ))?;
let _ethfriends_alice_charlie = alice_bob_ethdos.priv_op(Operation( let ethfriends_alice_charlie = alice_bob_ethdos.priv_op(Operation(
OperationType::Custom(CustomPredicateRef(eth_friend_batch, 0)), OperationType::Custom(eth_friend.clone()),
vec![ vec![
OperationArg::Statement(attestation_is_signed_pod), OperationArg::Statement(attestation_is_signed_pod),
OperationArg::Statement(attestation_signed_by_alice), OperationArg::Statement(attestation_signed_by_alice),
@ -165,23 +173,88 @@ pub fn eth_dos_pod_builder(
], ],
))?; ))?;
// ...and so are Chuck and Bob.
let attestation_is_signed_pod = Statement::from((charlie_attestation, KEY_TYPE));
let attestation_signed_by_charlie = alice_bob_ethdos.priv_op(Operation(
OperationType::Native(NativeOperation::EqualFromEntries),
vec![
OperationArg::from((charlie_attestation, KEY_SIGNER)),
OperationArg::Statement(charlie_pubkey),
],
))?;
let charlie_attests_to_bob = alice_bob_ethdos.priv_op(Operation(
OperationType::Native(NativeOperation::EqualFromEntries),
vec![
OperationArg::from((charlie_attestation, "attestation")),
OperationArg::Statement(bob_pubkey_copy),
],
))?;
let ethfriends_charlie_bob = alice_bob_ethdos.priv_op(Operation(
OperationType::Custom(eth_friend.clone()),
vec![
OperationArg::Statement(attestation_is_signed_pod),
OperationArg::Statement(attestation_signed_by_charlie),
OperationArg::Statement(charlie_attests_to_bob),
],
))?;
// The ETHDoS distance from Alice to Charlie is 1. // The ETHDoS distance from Alice to Charlie is 1.
let _one = alice_bob_ethdos.priv_op(Operation( let one = alice_bob_ethdos.priv_op(Operation(
OperationType::Native(NativeOperation::NewEntry), OperationType::Native(NativeOperation::NewEntry),
vec![OperationArg::Entry("ZERO".to_string(), Value::from(0i64))], vec![OperationArg::Entry("ONE".to_string(), Value::from(1i64))],
))?; ))?;
// 1 = 0 + 1 // 1 = 0 + 1
// let ethdos_sum = alice_bob_ethdos.priv_op( let ethdos_sum = alice_bob_ethdos.priv_op(Operation(
// Operation( OperationType::Native(NativeOperation::SumOf),
// OperationType::Native(NativeOperation::SumOf vec![
// ), OperationArg::Statement(one.clone()),
// vec![ OperationArg::Statement(zero.clone()),
// OperationArg::Statement(_one.clone()), OperationArg::Statement(one.clone()),
// OperationArg::Statement(zero.clone()), ],
// OperationArg::Statement(zero.clone()) ))?;
// ] let ethdos_alice_charlie_is_one_ind = alice_bob_ethdos.priv_op(Operation(
// ) OperationType::Custom(eth_dos_ind.clone()),
// ); vec![
OperationArg::Statement(ethdos_alice_alice_is_zero),
OperationArg::Statement(one.clone()),
OperationArg::Statement(ethdos_sum),
OperationArg::Statement(ethfriends_alice_charlie),
],
))?;
let ethdos_alice_charlie_is_one = alice_bob_ethdos.priv_op(Operation(
OperationType::Custom(eth_dos.clone()),
vec![OperationArg::Statement(ethdos_alice_charlie_is_one_ind)],
))?;
// The ETHDoS distance from Alice to Bob is 2.
// The constant "TWO" and the final statement are both to be
// public.
let two = alice_bob_ethdos.pub_op(Operation(
OperationType::Native(NativeOperation::NewEntry),
vec![OperationArg::Entry("TWO".to_string(), Value::from(2i64))],
))?;
// 2 = 1 + 1
let ethdos_sum = alice_bob_ethdos.priv_op(Operation(
OperationType::Native(NativeOperation::SumOf),
vec![
OperationArg::Statement(two.clone()),
OperationArg::Statement(one.clone()),
OperationArg::Statement(one.clone()),
],
))?;
let ethdos_alice_bob_is_two_ind = alice_bob_ethdos.priv_op(Operation(
OperationType::Custom(eth_dos_ind.clone()),
vec![
OperationArg::Statement(ethdos_alice_charlie_is_one),
OperationArg::Statement(one.clone()),
OperationArg::Statement(ethdos_sum),
OperationArg::Statement(ethfriends_charlie_bob),
],
))?;
let _ethdos_alice_bob_is_two = alice_bob_ethdos.pub_op(Operation(
OperationType::Custom(eth_dos.clone()),
vec![OperationArg::Statement(ethdos_alice_bob_is_two_ind)],
))?;
Ok(alice_bob_ethdos) Ok(alice_bob_ethdos)
} }

View file

@ -384,7 +384,7 @@ impl MainPodBuilder {
CopyStatement => match &args[0] { CopyStatement => match &args[0] {
OperationArg::Statement(s) => s.1.clone(), OperationArg::Statement(s) => s.1.clone(),
_ => { _ => {
return Err(anyhow!("Invalid arguments to operation: {}", op)); return Err(anyhow!("Invalid arguments to copy operation: {}", op));
} }
}, },
EqualFromEntries => self.op_args_entries(public, args)?, EqualFromEntries => self.op_args_entries(public, args)?,
@ -409,11 +409,15 @@ impl MainPodBuilder {
if st0_args[1] == st1_args[0] { if st0_args[1] == st1_args[0] {
vec![st0_args[0].clone(), st1_args[1].clone()] vec![st0_args[0].clone(), st1_args[1].clone()]
} else { } else {
return Err(anyhow!("Invalid arguments to operation")); return Err(anyhow!(
"Invalid arguments to transitive equality operation"
));
} }
} }
_ => { _ => {
return Err(anyhow!("Invalid arguments to operation")); return Err(anyhow!(
"Invalid arguments to transitive equality operation"
));
} }
} }
} }
@ -425,7 +429,7 @@ impl MainPodBuilder {
vec![st_args[0].clone()] vec![st_args[0].clone()]
} }
_ => { _ => {
return Err(anyhow!("Invalid arguments to operation")); return Err(anyhow!("Invalid arguments to gt-to-neq operation"));
} }
}, },
LtToNotEqual => match args[0].clone() { LtToNotEqual => match args[0].clone() {
@ -436,7 +440,7 @@ impl MainPodBuilder {
vec![st_args[0].clone()] vec![st_args[0].clone()]
} }
_ => { _ => {
return Err(anyhow!("Invalid arguments to operation")); return Err(anyhow!("Invalid arguments to lt-to-neq operation"));
} }
}, },
ContainsFromEntries => self.op_args_entries(public, args)?, ContainsFromEntries => self.op_args_entries(public, args)?,
@ -476,17 +480,17 @@ impl MainPodBuilder {
st2_args[0].clone(), st2_args[0].clone(),
] ]
} else { } else {
return Err(anyhow!("Invalid arguments to operation")); return Err(anyhow!("Invalid arguments to sum-of operation"));
} }
} }
_ => { _ => {
return Err(anyhow!("Invalid arguments to operation")); return Err(anyhow!("Invalid arguments to sum-of operation"));
} }
}; };
st_args st_args
} }
_ => { _ => {
return Err(anyhow!("Invalid arguments to operation")); return Err(anyhow!("Invalid arguments to sum-of operation"));
} }
}, },
ProductOf => match (args[0].clone(), args[1].clone(), args[2].clone()) { ProductOf => match (args[0].clone(), args[1].clone(), args[2].clone()) {
@ -524,17 +528,19 @@ impl MainPodBuilder {
st2_args[0].clone(), st2_args[0].clone(),
] ]
} else { } else {
return Err(anyhow!("Invalid arguments to operation")); return Err(anyhow!(
"Invalid arguments to product-of operation"
));
} }
} }
_ => { _ => {
return Err(anyhow!("Invalid arguments to operation")); return Err(anyhow!("Invalid arguments to product-of operation"));
} }
}; };
st_args st_args
} }
_ => { _ => {
return Err(anyhow!("Invalid arguments to operation")); return Err(anyhow!("Invalid arguments to product-of operation"));
} }
}, },
MaxOf => match (args[0].clone(), args[1].clone(), args[2].clone()) { MaxOf => match (args[0].clone(), args[1].clone(), args[2].clone()) {
@ -572,11 +578,11 @@ impl MainPodBuilder {
st2_args[0].clone(), st2_args[0].clone(),
] ]
} else { } else {
return Err(anyhow!("Invalid arguments to operation")); return Err(anyhow!("Invalid arguments to max-of operation"));
} }
} }
_ => { _ => {
return Err(anyhow!("Invalid arguments to operation")); return Err(anyhow!("Invalid arguments to max-of operation"));
} }
}; };
st_args st_args
@ -977,7 +983,17 @@ pub mod tests {
#[test] #[test]
fn test_ethdos() -> Result<()> { fn test_ethdos() -> Result<()> {
let params = Params::default(); let params = Params {
max_input_signed_pods: 3,
max_input_main_pods: 3,
max_statements: 31,
max_signed_pod_values: 8,
max_public_statements: 10,
max_statement_args: 5,
max_operation_args: 5,
max_custom_predicate_arity: 5,
max_custom_batch_size: 5,
};
let mut alice = MockSigner { pk: "Alice".into() }; let mut alice = MockSigner { pk: "Alice".into() };
let bob = MockSigner { pk: "Bob".into() }; let bob = MockSigner { pk: "Bob".into() };