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

@ -6,67 +6,31 @@ use schemars::JsonSchema;
use crate::{
frontend::{AnchoredKey, Error, Result, Statement, StatementArg},
middleware::{
self, hash_str, CustomPredicate, CustomPredicateBatch, Key, KeyOrWildcard, NativePredicate,
Params, PodId, Predicate, SelfOrWildcard, StatementTmpl, StatementTmplArg, ToFields, Value,
Wildcard,
self, hash_str, CustomPredicate, CustomPredicateBatch, Key, NativePredicate, Params, PodId,
Predicate, StatementTmpl, StatementTmplArg, ToFields, Value, Wildcard,
},
};
#[derive(Clone, Debug, PartialEq, Eq)]
/// Argument to a statement template
pub enum KeyOrWildcardStr {
Key(String), // represents a literal key
Wildcard(String),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum SelfOrWildcardStr {
SELF,
Wildcard(String),
}
/// helper to build a literal KeyOrWildcardStr::Key from the given str
pub fn key(s: &str) -> KeyOrWildcardStr {
KeyOrWildcardStr::Key(s.to_string())
}
/// Builder Argument for the StatementTmplBuilder
#[derive(Clone, Debug)]
pub enum BuilderArg {
Literal(Value),
/// Key: (origin, key), where origin is SELF or Wildcard and key is Key or Wildcard
Key(SelfOrWildcardStr, KeyOrWildcardStr),
/// Key: (origin, key), where origin is Wildcard and key is Key
Key(String, String),
WildcardLiteral(String),
}
impl From<&str> for SelfOrWildcardStr {
fn from(origin: &str) -> Self {
if origin == "SELF" {
SelfOrWildcardStr::SELF
} else {
SelfOrWildcardStr::Wildcard(origin.into())
}
}
}
/// When defining a `BuilderArg`, it can be done from 3 different inputs:
/// i. (&str, literal): this is to set a POD and a field, ie. (POD, literal("field"))
/// ii. (&str, &str): this is to define a origin-key wildcard pair, ie. (src_origin, src_dest)
/// iii. &str: this is to define a WildcardValue wildcard, ie. "src_or"
/// i. (&str, &str): this is to define a origin-key pair, ie. ?attestation_pod["attestation"])
/// ii. &str: this is to define a Value wildcard, ie. ?distance
///
/// case i.
impl From<(&str, KeyOrWildcardStr)> for BuilderArg {
fn from((origin, lit): (&str, KeyOrWildcardStr)) -> Self {
Self::Key(origin.into(), lit)
impl From<(&str, &str)> for BuilderArg {
fn from((origin, field): (&str, &str)) -> Self {
Self::Key(origin.to_string(), field.to_string())
}
}
/// case ii.
impl From<(&str, &str)> for BuilderArg {
fn from((origin, field): (&str, &str)) -> Self {
Self::Key(origin.into(), KeyOrWildcardStr::Wildcard(field.to_string()))
}
}
/// case iii.
impl From<&str> for BuilderArg {
fn from(wc: &str) -> Self {
Self::WildcardLiteral(wc.to_string())
@ -216,12 +180,12 @@ impl CustomPredicateBatchBuilder {
.iter()
.map(|a| match a {
BuilderArg::Literal(v) => StatementTmplArg::Literal(v.clone()),
BuilderArg::Key(pod_id, key) => StatementTmplArg::AnchoredKey(
resolve_self_or_wildcard(args, priv_args, pod_id),
resolve_key_or_wildcard(args, priv_args, key),
BuilderArg::Key(pod_id_wc, key_str) => StatementTmplArg::AnchoredKey(
resolve_wildcard(args, priv_args, pod_id_wc),
Key::from(key_str),
),
BuilderArg::WildcardLiteral(v) => {
StatementTmplArg::WildcardLiteral(resolve_wildcard(args, priv_args, v))
StatementTmplArg::Wildcard(resolve_wildcard(args, priv_args, v))
}
})
.collect();
@ -251,32 +215,6 @@ impl CustomPredicateBatchBuilder {
}
}
fn resolve_self_or_wildcard(
args: &[&str],
priv_args: &[&str],
v: &SelfOrWildcardStr,
) -> SelfOrWildcard {
match v {
SelfOrWildcardStr::SELF => SelfOrWildcard::SELF,
SelfOrWildcardStr::Wildcard(s) => {
SelfOrWildcard::Wildcard(resolve_wildcard(args, priv_args, s))
}
}
}
fn resolve_key_or_wildcard(
args: &[&str],
priv_args: &[&str],
v: &KeyOrWildcardStr,
) -> KeyOrWildcard {
match v {
KeyOrWildcardStr::Key(k) => KeyOrWildcard::Key(Key::from(k)),
KeyOrWildcardStr::Wildcard(s) => {
KeyOrWildcard::Wildcard(resolve_wildcard(args, priv_args, s))
}
}
}
fn resolve_wildcard(args: &[&str], priv_args: &[&str], s: &str) -> Wildcard {
args.iter()
.chain(priv_args.iter())
@ -292,7 +230,7 @@ mod tests {
use super::*;
use crate::{
backends::plonky2::mock::mainpod::MockProver,
examples::custom::{eth_dos_batch, eth_friend_batch},
examples::{custom::eth_dos_batch, MOCK_VD_SET},
frontend::MainPodBuilder,
middleware::{self, containers::Set, CustomPredicateRef, Params, PodType, DEFAULT_VD_SET},
op,
@ -311,12 +249,11 @@ mod tests {
params.print_serialized_sizes();
// ETH friend custom predicate batch
let eth_friend = eth_friend_batch(&params, true)?;
let eth_dos_batch = eth_dos_batch(&params, true)?;
// This batch only has 1 predicate, so we pick it already for convenience
let eth_friend = Predicate::Custom(CustomPredicateRef::new(eth_friend, 0));
let eth_friend = eth_dos_batch.predicate_ref_by_name("eth_friend").unwrap();
let eth_dos_batch = eth_dos_batch(&params, true)?;
let eth_dos_batch_mw: middleware::CustomPredicateBatch =
Arc::unwrap_or_clone(eth_dos_batch);
let fields = eth_dos_batch_mw.to_fields(&params);
@ -328,7 +265,7 @@ mod tests {
#[test]
fn test_desugared_gt_custom_pred() -> Result<()> {
let params = Params::default();
let vd_set = &*DEFAULT_VD_SET;
let vd_set = &*MOCK_VD_SET;
let mut builder = CustomPredicateBatchBuilder::new(params.clone(), "gt_custom_pred".into());
let gt_stb = StatementTmplBuilder::new(NativePredicate::Gt)
@ -337,7 +274,7 @@ mod tests {
builder.predicate_and(
"gt_custom_pred",
&["s1_origin", "s1_key", "s2_origin", "s2_key"],
&["s1_origin", "s2_origin"],
&[],
&[gt_stb],
)?;
@ -348,8 +285,8 @@ mod tests {
let mut mp_builder = MainPodBuilder::new(&params, &vd_set);
// 2 > 1
let s1 = mp_builder.literal(true, Value::from(2))?;
let s2 = mp_builder.literal(true, Value::from(1))?;
let s1 = mp_builder.priv_op(op!(new_entry, "s1_key", Value::from(2)))?;
let s2 = mp_builder.priv_op(op!(new_entry, "s2_key", Value::from(1)))?;
// Adding a gt operation will produce a desugared lt operation
let desugared_gt = mp_builder.pub_op(op!(gt, s1, s2))?;
@ -377,7 +314,7 @@ mod tests {
#[test]
fn test_desugared_set_contains_custom_pred() -> Result<()> {
let params = Params::default();
let vd_set = &*DEFAULT_VD_SET;
let vd_set = &*MOCK_VD_SET;
let mut builder =
CustomPredicateBatchBuilder::new(params.clone(), "set_contains_custom_pred".into());
@ -387,7 +324,7 @@ mod tests {
builder.predicate_and(
"set_contains_custom_pred",
&["s1_origin", "s1_key", "s2_origin", "s2_key"],
&["s1_origin", "s2_origin"],
&[],
&[set_contains_stb],
)?;
@ -397,11 +334,12 @@ mod tests {
let mut mp_builder = MainPodBuilder::new(&params, &vd_set);
let set_values: HashSet<Value> = [1, 2, 3].iter().map(|i| Value::from(*i)).collect();
let s1 = mp_builder.literal(
true,
Value::from(Set::new(params.max_depth_mt_containers, set_values)?),
)?;
let s2 = mp_builder.literal(true, Value::from(1))?;
let s1 = mp_builder.priv_op(op!(
new_entry,
"s1_key",
Value::from(Set::new(params.max_depth_mt_containers, set_values)?)
))?;
let s2 = mp_builder.priv_op(op!(new_entry, "s2_key", Value::from(1)))?;
let set_contains = mp_builder.pub_op(op!(set_contains, s1, s2))?;
assert_eq!(