Add some top-level examples (#303)

The examples show:
- Building a Signed Pod with different types of values
- Building a MainPod
- Input SignedPod to MainPod
- Input MainPod to MainPod
- Using MainPod or MockMainPod
- Using custom predicates
This commit is contained in:
Eduard S. 2025-06-19 19:31:54 +02:00 committed by GitHub
parent 6249406cb2
commit b7ac54d972
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 227 additions and 44 deletions

View file

@ -155,7 +155,7 @@ impl EthDosHelper {
let mut pod = MainPodBuilder::new(&self.params, &self.vd_set);
pod.add_signed_pod(int_attestation);
pod.add_main_pod(eth_dos_src_to_int_pod.clone());
pod.add_recursive_pod(eth_dos_src_to_int_pod.clone());
let eth_dos_int_to_dst = eth_dos_src_to_int_pod
.pod

View file

@ -120,7 +120,7 @@ pub struct MainPodBuilder {
pub params: Params,
pub vd_set: VDSet,
pub input_signed_pods: Vec<SignedPod>,
pub input_main_pods: Vec<MainPod>,
pub input_recursive_pods: Vec<MainPod>,
pub statements: Vec<Statement>,
pub operations: Vec<Operation>,
pub public_statements: Vec<Statement>,
@ -139,7 +139,7 @@ impl fmt::Display for MainPodBuilder {
writeln!(f, " - {}", in_pod.id())?;
}
writeln!(f, " input_main_pods:")?;
for in_pod in &self.input_main_pods {
for in_pod in &self.input_recursive_pods {
writeln!(f, " - {}", in_pod.id())?;
}
writeln!(f, " statements:")?;
@ -158,7 +158,7 @@ impl MainPodBuilder {
params: params.clone(),
vd_set: vd_set.clone(),
input_signed_pods: Vec::new(),
input_main_pods: Vec::new(),
input_recursive_pods: Vec::new(),
statements: Vec::new(),
operations: Vec::new(),
public_statements: Vec::new(),
@ -169,8 +169,8 @@ impl MainPodBuilder {
pub fn add_signed_pod(&mut self, pod: &SignedPod) {
self.input_signed_pods.push(pod.clone());
}
pub fn add_main_pod(&mut self, pod: MainPod) {
self.input_main_pods.push(pod);
pub fn add_recursive_pod(&mut self, pod: MainPod) {
self.input_recursive_pods.push(pod);
}
pub fn insert(&mut self, public: bool, st_op: (Statement, Operation)) {
// TODO: Do error handling instead of panic
@ -538,7 +538,7 @@ impl MainPodBuilder {
self.public_statements.push(st.clone());
}
pub fn prove<P: PodProver>(&self, prover: &mut P, params: &Params) -> Result<MainPod> {
pub fn prove(&self, prover: &dyn PodProver, params: &Params) -> Result<MainPod> {
let compiler = MainPodCompiler::new(&self.params);
let inputs = MainPodCompilerInputs {
// signed_pods: &self.input_signed_pods,
@ -557,7 +557,7 @@ impl MainPodBuilder {
.map(|p| p.pod.as_ref())
.collect_vec(),
recursive_pods: &self
.input_main_pods
.input_recursive_pods
.iter()
.map(|p| p.pod.as_ref())
.collect_vec(),

View file

@ -45,9 +45,9 @@ impl fmt::Display for OperationArg {
}
}
impl From<Value> for OperationArg {
fn from(v: Value) -> Self {
Self::Literal(v)
impl<V: Into<Value>> From<V> for OperationArg {
fn from(value: V) -> Self {
Self::Literal(value.into())
}
}
@ -57,24 +57,6 @@ impl From<&Value> for OperationArg {
}
}
impl From<&str> for OperationArg {
fn from(s: &str) -> Self {
Self::Literal(Value::from(s))
}
}
impl From<i64> for OperationArg {
fn from(v: i64) -> Self {
Self::Literal(Value::from(v))
}
}
impl From<bool> for OperationArg {
fn from(b: bool) -> Self {
Self::Literal(Value::from(b))
}
}
impl From<(&SignedPod, &str)> for OperationArg {
fn from((pod, key): (&SignedPod, &str)) -> Self {
// TODO: TryFrom.

View file

@ -121,16 +121,7 @@ impl From<Hash> for RawValue {
impl fmt::Display for RawValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.0[2].is_zero() && self.0[3].is_zero() {
// Assume this is an integer
let (l0, l1) = (self.0[0].to_canonical_u64(), self.0[1].to_canonical_u64());
assert!(l0 < (1 << 32));
assert!(l1 < (1 << 32));
write!(f, "{}", l0 + l1 * (1 << 32))
} else {
// Assume this is a hash
Hash(self.0).fmt(f)
}
Hash(self.0).fmt(f)
}
}
@ -206,14 +197,19 @@ impl Ord for Hash {
}
}
// TODO: In alternate mode, don't shorten the hash
impl fmt::Display for Hash {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let v0 = self.0[0].to_canonical_u64();
for i in 0..HASH_SIZE {
write!(f, "{:02x}", (v0 >> (i * 8)) & 0xff)?;
if f.alternate() {
write!(f, "0x{}", self.encode_hex::<String>())
} else {
// display first hex digit in big endian
write!(f, "0x")?;
let v3 = self.0[3].to_canonical_u64();
for i in 0..4 {
write!(f, "{:02x}", (v3 >> ((7 - i) * 8)) & 0xff)?;
}
write!(f, "")
}
write!(f, "")
}
}