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:
Eduard S. 2025-06-16 16:38:38 +02:00 committed by GitHub
parent 7d0d3ad769
commit 3c6930dfe6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
20 changed files with 659 additions and 1111 deletions

View file

@ -7,9 +7,9 @@ use serde::{Deserialize, Serialize};
use crate::{
backends::plonky2::primitives::merkletree::MerkleProof,
middleware::{
custom::KeyOrWildcard, hash_values, AnchoredKey, CustomPredicate, CustomPredicateRef,
Error, NativePredicate, Params, Predicate, Result, SelfOrWildcard, Statement, StatementArg,
StatementTmplArg, ToFields, Value, ValueRef, Wildcard, WildcardValue, F, SELF,
hash_values, AnchoredKey, CustomPredicate, CustomPredicateRef, Error, NativePredicate,
Params, Predicate, Result, Statement, StatementArg, StatementTmplArg, ToFields, Value,
ValueRef, Wildcard, F, SELF,
},
};
@ -369,14 +369,10 @@ pub fn check_st_tmpl(
st_tmpl_arg: &StatementTmplArg,
st_arg: &StatementArg,
// Map from wildcards to values that we have seen so far.
wildcard_map: &mut [Option<WildcardValue>],
wildcard_map: &mut [Option<Value>],
) -> bool {
// Check that the value `v` at wildcard `wc` exists in the map or set it.
fn check_or_set(
v: WildcardValue,
wc: &Wildcard,
wildcard_map: &mut [Option<WildcardValue>],
) -> bool {
fn check_or_set(v: Value, wc: &Wildcard, wildcard_map: &mut [Option<Value>]) -> bool {
if let Some(prev) = &wildcard_map[wc.index] {
if *prev != v {
// TODO: Return nice error
@ -392,27 +388,19 @@ pub fn check_st_tmpl(
(StatementTmplArg::None, StatementArg::None) => true,
(StatementTmplArg::Literal(lhs), StatementArg::Literal(rhs)) if lhs == rhs => true,
(
StatementTmplArg::AnchoredKey(self_or_wc, key_or_wc),
StatementTmplArg::AnchoredKey(pod_id_wc, key_tmpl),
StatementArg::Key(AnchoredKey { pod_id, key }),
) => {
let pod_id_ok = match self_or_wc {
SelfOrWildcard::SELF => SELF == *pod_id,
SelfOrWildcard::Wildcard(pod_id_wc) => {
check_or_set(WildcardValue::PodId(*pod_id), pod_id_wc, wildcard_map)
}
};
let key_ok = match key_or_wc {
KeyOrWildcard::Key(tmpl_key) => tmpl_key == key,
KeyOrWildcard::Wildcard(key_wc) => {
check_or_set(WildcardValue::Key(key.clone()), key_wc, wildcard_map)
}
};
pod_id_ok && key_ok
let pod_id_ok = check_or_set(Value::from(*pod_id), pod_id_wc, wildcard_map);
pod_id_ok && (key_tmpl == key)
}
(StatementTmplArg::WildcardLiteral(wc), StatementArg::WildcardLiteral(v)) => {
(StatementTmplArg::Wildcard(wc), StatementArg::Literal(v)) => {
check_or_set(v.clone(), wc, wildcard_map)
}
_ => false,
_ => {
println!("DBG {:?} {:?}", st_tmpl_arg, st_arg);
false
}
}
}
@ -420,7 +408,7 @@ pub fn resolve_wildcard_values(
params: &Params,
pred: &CustomPredicate,
args: &[Statement],
) -> Option<Vec<WildcardValue>> {
) -> Option<Vec<Value>> {
// Check that all wildcard have consistent values as assigned in the statements while storing a
// map of their values.
// NOTE: We assume the statements have the same order as defined in the custom predicate. For
@ -444,7 +432,7 @@ pub fn resolve_wildcard_values(
Some(
wildcard_map
.into_iter()
.map(|opt| opt.unwrap_or(WildcardValue::None))
.map(|opt| opt.unwrap_or(Value::from(0)))
.collect(),
)
}
@ -453,7 +441,7 @@ fn check_custom_pred(
params: &Params,
custom_pred_ref: &CustomPredicateRef,
args: &[Statement],
s_args: &[WildcardValue],
s_args: &[Value],
) -> Result<bool> {
let pred = custom_pred_ref.predicate();
if pred.statements.len() != args.len() {