Skip to content

Commit cb0c123

Browse files
committed
Invoke split predicates by name, passing in full argument list
1 parent f6ff84e commit cb0c123

File tree

6 files changed

+875
-60
lines changed

6 files changed

+875
-60
lines changed

src/frontend/error.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ impl From<crate::lang::LangError> for Error {
7171
}
7272
}
7373

74+
impl From<crate::lang::MultiOperationError> for Error {
75+
fn from(value: crate::lang::MultiOperationError) -> Self {
76+
Error::custom(value.to_string())
77+
}
78+
}
79+
7480
impl Debug for Error {
7581
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
7682
std::fmt::Display::fmt(self, f)

src/frontend/mod.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,4 +1406,94 @@ pub mod tests {
14061406

14071407
Ok(())
14081408
}
1409+
1410+
#[test]
1411+
fn test_apply_predicate_e2e() -> Result<()> {
1412+
// End-to-end test of apply_predicate with MockProver
1413+
// Tests a split predicate being applied through the full pipeline
1414+
let params = Params::default();
1415+
let vd_set = &*MOCK_VD_SET;
1416+
1417+
// Create a predicate that will split (6 Equal statements)
1418+
// The predicate checks that values at different keys are equal to specific literals
1419+
let input = r#"
1420+
large_pred(A) = AND(
1421+
Equal(A["a"], 1)
1422+
Equal(A["b"], 2)
1423+
Equal(A["c"], 3)
1424+
Equal(A["d"], 4)
1425+
Equal(A["e"], 5)
1426+
Equal(A["f"], 6)
1427+
)
1428+
"#;
1429+
1430+
// Parse and batch the predicate (this handles splitting internally)
1431+
let parsed = parse(input, &params, &[])?;
1432+
let batches = &parsed.custom_batches;
1433+
1434+
// Verify it was split
1435+
assert!(batches.split_chain("large_pred").is_some());
1436+
let chain_info = batches.split_chain("large_pred").unwrap();
1437+
assert_eq!(chain_info.chain_pieces.len(), 2);
1438+
assert_eq!(chain_info.total_real_statements, 6);
1439+
1440+
// Create a signed dict with the required entries
1441+
let mut signed_builder = SignedDictBuilder::new(&params);
1442+
signed_builder.insert("a", 1);
1443+
signed_builder.insert("b", 2);
1444+
signed_builder.insert("c", 3);
1445+
signed_builder.insert("d", 4);
1446+
signed_builder.insert("e", 5);
1447+
signed_builder.insert("f", 6);
1448+
let signer = Signer(SecretKey(1u32.into()));
1449+
let signed_dict = signed_builder.sign(&signer)?;
1450+
1451+
// Build the main pod
1452+
let mut builder = MainPodBuilder::new(&params, vd_set);
1453+
builder.pub_op(Operation::dict_signed_by(&signed_dict))?;
1454+
1455+
// Create 6 Equal statements (one for each predicate constraint) in original order
1456+
// Each proves that signed_dict["x"] = n, matching the Equal(A["x"], n) template
1457+
let st_a = builder.priv_op(Operation::eq((&signed_dict, "a"), 1))?;
1458+
let st_b = builder.priv_op(Operation::eq((&signed_dict, "b"), 2))?;
1459+
let st_c = builder.priv_op(Operation::eq((&signed_dict, "c"), 3))?;
1460+
let st_d = builder.priv_op(Operation::eq((&signed_dict, "d"), 4))?;
1461+
let st_e = builder.priv_op(Operation::eq((&signed_dict, "e"), 5))?;
1462+
let st_f = builder.priv_op(Operation::eq((&signed_dict, "f"), 6))?;
1463+
1464+
// Pass statements in original declaration order
1465+
let statements = vec![st_a, st_b, st_c, st_d, st_e, st_f];
1466+
1467+
// Use apply_predicate to automatically wire the split chain
1468+
let result = batches.apply_predicate(
1469+
"large_pred",
1470+
statements,
1471+
true, // public
1472+
|public, op| {
1473+
if public {
1474+
builder.pub_op(op)
1475+
} else {
1476+
builder.priv_op(op)
1477+
}
1478+
},
1479+
)?;
1480+
1481+
// The result should be a valid statement
1482+
let predicate = batches.predicate_ref_by_name("large_pred")?;
1483+
match &result {
1484+
Statement::Custom(pred_ref, _) => {
1485+
assert_eq!(pred_ref, &predicate);
1486+
}
1487+
_ => panic!("Expected Statement::Custom, got {:?}", result),
1488+
}
1489+
1490+
// Prove with MockProver
1491+
let prover = MockProver {};
1492+
let pod = builder.prove(&prover)?;
1493+
1494+
// Verify the pod
1495+
pod.pod.verify()?;
1496+
1497+
Ok(())
1498+
}
14091499
}

0 commit comments

Comments
 (0)