Allow literals in statement templates (#287)
This PR is a continuation of the work done in #276 - Fix PodType in MainPod (we were using `MockMain` instead of `Main`) - Update anchored keys in statement template arguments to only support wildcards in the origin and literal keys as the key. - Update the pest grammar accordingly - Update the parser accordingly - Rewrite the eth_dos example in a recursive manner so that we use one recursive pod for every distance increment of 1. - I've also used the podlang to define the eth_dos custom predicates. Currently all predicates are in a single batch (previously `eth_friend` was in a different batch). With #286 we could define `eth_friend` in a different batch again. - I was feeling a bit creative and used a format macro to pass `Value`s from rust to the podlang code. - The eth_dos is now written using literals. This resolves https://github.com/0xPARC/pod2/issues/255 - Remove `StatementArg::WildcardValue` in favor of `StatementArg::Literal`. The `WildcardValue` was just a way to have some kind of typing for values that would be used as arguments in custom predicates. Now that we can have literals in any statement this value can be anything, so I just removed the `WildcardValue` and use `Literal` instead. On the backend it was already the case that both cases were treated the same way (after all, `WildcardValue` and `Literal` were 4 fields in the backend). - Added a new type for Value: `PodId` so that we can use it for custom predicates that take a pod id to be used in a wildcard - Add a mock vd_set that is empty for tests that don't use plonky2; this allows running those tests individually without paying for the expensive work of calculating the vd for various circuits. - rename StatementTmplArg::WildcardValue to StatementTmplArg::Wildcard
This commit is contained in:
parent
7d0d3ad769
commit
3c6930dfe6
20 changed files with 659 additions and 1111 deletions
|
|
@ -315,7 +315,12 @@ pub(crate) fn layout_statements(
|
|||
}
|
||||
|
||||
// Input statements
|
||||
assert!(inputs.statements.len() <= params.max_priv_statements());
|
||||
assert!(
|
||||
inputs.statements.len() <= params.max_priv_statements(),
|
||||
"inputs.statements.len={} > params.max_priv_statements={}",
|
||||
inputs.statements.len(),
|
||||
params.max_priv_statements()
|
||||
);
|
||||
for i in 0..params.max_priv_statements() {
|
||||
let mut st = inputs
|
||||
.statements
|
||||
|
|
@ -329,9 +334,14 @@ pub(crate) fn layout_statements(
|
|||
|
||||
// Public statements
|
||||
assert!(inputs.public_statements.len() < params.max_public_statements);
|
||||
let pod_type = if mock {
|
||||
PodType::MockMain
|
||||
} else {
|
||||
PodType::Main
|
||||
};
|
||||
let mut type_st = middleware::Statement::Equal(
|
||||
AnchoredKey::from((SELF, KEY_TYPE)).into(),
|
||||
middleware::Value::from(PodType::MockMain).into(),
|
||||
middleware::Value::from(pod_type).into(),
|
||||
)
|
||||
.into();
|
||||
pad_statement(params, &mut type_st);
|
||||
|
|
@ -709,16 +719,13 @@ pub mod tests {
|
|||
primitives::ec::schnorr::SecretKey,
|
||||
signedpod::Signer,
|
||||
},
|
||||
examples::{
|
||||
eth_dos_pod_builder, eth_friend_signed_pod_builder, zu_kyc_pod_builder,
|
||||
zu_kyc_sign_pod_builders,
|
||||
},
|
||||
examples::{attest_eth_friend, zu_kyc_pod_builder, zu_kyc_sign_pod_builders, EthDosHelper},
|
||||
frontend::{
|
||||
key, literal, CustomPredicateBatchBuilder, MainPodBuilder, StatementTmplBuilder as STB,
|
||||
literal, CustomPredicateBatchBuilder, MainPodBuilder, StatementTmplBuilder as STB,
|
||||
{self},
|
||||
},
|
||||
middleware,
|
||||
middleware::{CustomPredicateRef, NativePredicate as NP, DEFAULT_VD_SET},
|
||||
middleware::{CustomPredicateRef, NativePredicate as NP, Value, DEFAULT_VD_SET},
|
||||
op,
|
||||
};
|
||||
|
||||
|
|
@ -847,52 +854,31 @@ pub mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_main_ethdos() -> frontend::Result<()> {
|
||||
let params = Params {
|
||||
max_input_signed_pods: 2,
|
||||
max_input_recursive_pods: 1,
|
||||
max_statements: 26,
|
||||
max_public_statements: 5,
|
||||
max_signed_pod_values: 8,
|
||||
max_operation_args: 5,
|
||||
max_custom_predicate_wildcards: 6,
|
||||
max_custom_predicate_verifications: 8,
|
||||
..Default::default()
|
||||
};
|
||||
let params = Params::default();
|
||||
println!("{:#?}", params);
|
||||
let vd_set = &*DEFAULT_VD_SET;
|
||||
|
||||
let mut alice = Signer(SecretKey(1u32.into()));
|
||||
let bob = Signer(SecretKey(2u32.into()));
|
||||
let mut charlie = Signer(SecretKey(3u32.into()));
|
||||
let mut bob = Signer(SecretKey(2u32.into()));
|
||||
let charlie = Signer(SecretKey(3u32.into()));
|
||||
|
||||
// Alice attests that she is ETH friends with Charlie and Charlie
|
||||
// attests that he is ETH friends with Bob.
|
||||
// Alice attests that she is ETH friends with Bob and Bob
|
||||
// attests that he is ETH friends with Charlie.
|
||||
let alice_attestation =
|
||||
eth_friend_signed_pod_builder(¶ms, charlie.public_key().into()).sign(&mut alice)?;
|
||||
let charlie_attestation =
|
||||
eth_friend_signed_pod_builder(¶ms, bob.public_key().into()).sign(&mut charlie)?;
|
||||
|
||||
let alice_bob_ethdos_builder = eth_dos_pod_builder(
|
||||
¶ms,
|
||||
&vd_set,
|
||||
false,
|
||||
&alice_attestation,
|
||||
&charlie_attestation,
|
||||
bob.public_key().into(),
|
||||
)?;
|
||||
|
||||
let mut prover = MockProver {};
|
||||
let pod = alice_bob_ethdos_builder.prove(&mut prover, ¶ms)?;
|
||||
assert!(pod.pod.verify().is_ok());
|
||||
attest_eth_friend(¶ms, &mut alice, Value::from(bob.public_key()));
|
||||
let bob_attestation =
|
||||
attest_eth_friend(¶ms, &mut bob, Value::from(charlie.public_key()));
|
||||
|
||||
let helper = EthDosHelper::new(¶ms, vd_set, false, Value::from(alice.public_key()))?;
|
||||
let mut prover = Prover {};
|
||||
let alice_bob_ethdos = alice_bob_ethdos_builder.prove(&mut prover, ¶ms)?;
|
||||
crate::measure_gates_print!();
|
||||
let pod = (alice_bob_ethdos.pod as Box<dyn Any>)
|
||||
.downcast::<MainPod>()
|
||||
.unwrap();
|
||||
|
||||
Ok(pod.verify()?)
|
||||
let dist_1 = helper
|
||||
.dist_1(&alice_attestation)?
|
||||
.prove(&mut prover, ¶ms)?;
|
||||
dist_1.pod.verify()?;
|
||||
let dist_2 = helper
|
||||
.dist_n_plus_1(&dist_1, &bob_attestation)?
|
||||
.prove(&mut prover, ¶ms)?;
|
||||
Ok(dist_2.pod.verify()?)
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -914,19 +900,17 @@ pub mod tests {
|
|||
let vd_set = &*DEFAULT_VD_SET;
|
||||
|
||||
let mut cpb_builder = CustomPredicateBatchBuilder::new(params.clone(), "cpb".into());
|
||||
let stb0 = STB::new(NP::Equal)
|
||||
.arg(("id", key("score")))
|
||||
.arg(literal(42));
|
||||
let stb0 = STB::new(NP::Equal).arg(("id", "score")).arg(literal(42));
|
||||
let stb1 = STB::new(NP::Equal)
|
||||
.arg(("id", "secret_key"))
|
||||
.arg(("id", key("score")));
|
||||
.arg(("secret_id", "key"))
|
||||
.arg(("id", "score"));
|
||||
let _ = cpb_builder.predicate_and(
|
||||
"pred_and",
|
||||
&["id"],
|
||||
&["secret_key"],
|
||||
&["secret_id"],
|
||||
&[stb0.clone(), stb1.clone()],
|
||||
)?;
|
||||
let _ = cpb_builder.predicate_or("pred_or", &["id"], &["secret_key"], &[stb0, stb1])?;
|
||||
let _ = cpb_builder.predicate_or("pred_or", &["id"], &["secret_id"], &[stb0, stb1])?;
|
||||
let cpb = cpb_builder.finish();
|
||||
|
||||
let cpb_and = CustomPredicateRef::new(cpb.clone(), 0);
|
||||
|
|
@ -934,8 +918,8 @@ pub mod tests {
|
|||
|
||||
let mut pod_builder = MainPodBuilder::new(¶ms, &vd_set);
|
||||
|
||||
let st0 = pod_builder.priv_op(op!(new_entry, ("score", 42)))?;
|
||||
let st1 = pod_builder.priv_op(op!(new_entry, ("foo", 42)))?;
|
||||
let st0 = pod_builder.priv_op(op!(new_entry, "score", 42))?;
|
||||
let st1 = pod_builder.priv_op(op!(new_entry, "key", 42))?;
|
||||
let st2 = pod_builder.priv_op(op!(eq, st1.clone(), st0.clone()))?;
|
||||
|
||||
let _st3 = pod_builder.priv_op(op!(custom, cpb_and.clone(), st0, st2))?;
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
|
|||
|
||||
use crate::{
|
||||
backends::plonky2::error::{Error, Result},
|
||||
middleware::{self, NativePredicate, Params, Predicate, StatementArg, ToFields, WildcardValue},
|
||||
middleware::{self, NativePredicate, Params, Predicate, StatementArg, ToFields, Value},
|
||||
};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
|
|
@ -74,11 +74,11 @@ impl TryFrom<Statement> for middleware::Statement {
|
|||
)))?,
|
||||
},
|
||||
Predicate::Custom(cpr) => {
|
||||
let vs: Vec<WildcardValue> = proper_args
|
||||
let vs: Vec<Value> = proper_args
|
||||
.into_iter()
|
||||
.filter_map(|arg| match arg {
|
||||
SA::None => None,
|
||||
SA::WildcardLiteral(v) => Some(v),
|
||||
SA::Literal(v) => Some(v),
|
||||
_ => unreachable!(),
|
||||
})
|
||||
.collect();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue