Skip to content

Commit 7bc0bd0

Browse files
arnaucubeed255
andauthored
expose some interfaces for external usage (from introduction-pods) (#256)
* expose some interfaces for external usage (from introduction-pods) * add From<MainPod> for OperationArg, add copy op! Co-authored-by: Eduard S. <eduardsanou@posteo.net> --------- Co-authored-by: Eduard S. <eduardsanou@posteo.net>
1 parent 5d13ac3 commit 7bc0bd0

File tree

6 files changed

+54
-22
lines changed

6 files changed

+54
-22
lines changed

book/src/merklestatements.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ all of which are represented as `MerkleTree` on the back end.
3939

4040
The frontend compound types and their implementation as Merkle trees is explained under [POD value types](./values.md#dictionary-array-set). The backend structure of a MerkleTree is explained on [the Merkle tree page](./merkletree.md).
4141

42-
The POD2 interface provides statements for working with Merkle trees and compond types at all layers of the stack:
42+
The POD2 interface provides statements for working with Merkle trees and compound types at all layers of the stack:
4343
- Primitive statements for Merkle trees
4444
- General derived statements for Merkle trees
4545
- Specialized `ContainsKey`, `NotContainsKey`, and `ContainsValue` statements for the three front-end types.

src/backends/plonky2/error.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,31 +63,31 @@ macro_rules! new {
6363
}
6464
use InnerError::*;
6565
impl Error {
66-
pub(crate) fn custom(s: String) -> Self {
66+
pub fn custom(s: String) -> Self {
6767
new!(Custom(s))
6868
}
69-
pub(crate) fn plonky2_proof_fail(e: anyhow::Error) -> Self {
69+
pub fn plonky2_proof_fail(e: anyhow::Error) -> Self {
7070
Self::Plonky2ProofFail(e)
7171
}
72-
pub(crate) fn key_not_found() -> Self {
72+
pub fn key_not_found() -> Self {
7373
new!(KeyNotFound)
7474
}
75-
pub(crate) fn statement_not_check() -> Self {
75+
pub fn statement_not_check() -> Self {
7676
new!(StatementNotCheck)
7777
}
78-
pub(crate) fn repeated_value_of() -> Self {
78+
pub fn repeated_value_of() -> Self {
7979
new!(RepeatedValueOf)
8080
}
81-
pub(crate) fn not_type_statement() -> Self {
81+
pub fn not_type_statement() -> Self {
8282
new!(NotTypeStatement)
8383
}
84-
pub(crate) fn pod_id_invalid() -> Self {
84+
pub fn pod_id_invalid() -> Self {
8585
new!(PodIdInvalid)
8686
}
87-
pub(crate) fn id_not_equal(expected: PodId, found: PodId) -> Self {
87+
pub fn id_not_equal(expected: PodId, found: PodId) -> Self {
8888
new!(IdNotEqual(expected, found))
8989
}
90-
pub(crate) fn type_not_equal(expected: PodType, found: Value) -> Self {
90+
pub fn type_not_equal(expected: PodType, found: Value) -> Self {
9191
new!(TypeNotEqual(expected, found))
9292
}
9393
}

src/backends/plonky2/mainpod/mod.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use crate::{
3939
/// `max_public_statements` only pay for `max_public_statements` by starting the poseidon state
4040
/// with a precomputed constant corresponding to the front-padding part:
4141
/// `id = hash(serialize(reverse(statements || none-statements)))`
42-
pub(crate) fn calculate_id(statements: &[Statement], params: &Params) -> middleware::Hash {
42+
pub fn calculate_id(statements: &[Statement], params: &Params) -> middleware::Hash {
4343
assert!(statements.len() <= params.num_public_statements_id);
4444
assert!(params.max_public_statements <= params.num_public_statements_id);
4545

@@ -409,7 +409,7 @@ impl Prover {
409409
fn _prove(&self, params: &Params, inputs: MainPodInputs) -> Result<MainPod> {
410410
let rec_circuit_data = &*STANDARD_REC_MAIN_POD_CIRCUIT_DATA;
411411
let (main_pod_target, circuit_data) =
412-
RecursiveCircuit::<MainPodVerifyTarget>::circuit_data_padded(
412+
RecursiveCircuit::<MainPodVerifyTarget>::target_and_circuit_data_padded(
413413
params.max_input_recursive_pods,
414414
&rec_circuit_data.common,
415415
params,
@@ -570,11 +570,12 @@ impl MainPod {
570570
let rec_circuit_data = &*STANDARD_REC_MAIN_POD_CIRCUIT_DATA;
571571
// TODO: cache these artefacts
572572
// https://github.com/0xPARC/pod2/issues/247
573-
let (_, circuit_data) = RecursiveCircuit::<MainPodVerifyTarget>::circuit_data_padded(
574-
self.params.max_input_recursive_pods,
575-
&rec_circuit_data.common,
576-
&self.params,
577-
)?;
573+
let (_, circuit_data) =
574+
RecursiveCircuit::<MainPodVerifyTarget>::target_and_circuit_data_padded(
575+
self.params.max_input_recursive_pods,
576+
&rec_circuit_data.common,
577+
&self.params,
578+
)?;
578579
let public_inputs = id
579580
.to_fields(&self.params)
580581
.iter()

src/backends/plonky2/recursion/circuit.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ pub fn new_params_padded<I: InnerCircuit>(
113113
inner_params: &I::Params,
114114
) -> Result<RecursiveParams> {
115115
let (_, circuit_data) =
116-
RecursiveCircuit::<I>::circuit_data_padded(arity, common_data, inner_params)?;
116+
RecursiveCircuit::<I>::target_and_circuit_data_padded(arity, common_data, inner_params)?;
117117
let common_data = circuit_data.common.clone();
118118
let verifier_data = circuit_data.verifier_data();
119119
Ok(RecursiveParams {
@@ -265,7 +265,7 @@ impl<I: InnerCircuit> RecursiveCircuit<I> {
265265
}
266266

267267
/// returns the full-recursive CircuitData padded to share the input `common_data`
268-
pub fn circuit_data_padded(
268+
pub fn target_and_circuit_data_padded(
269269
arity: usize,
270270
common_data: &CommonCircuitData<F, D>,
271271
inner_params: &I::Params,
@@ -688,15 +688,15 @@ mod tests {
688688
let common_data = &circuit_data_3.common;
689689

690690
let (_, circuit_data_1) =
691-
RC::<Circuit1>::circuit_data_padded(arity, &common_data, &inner_params)?;
691+
RC::<Circuit1>::target_and_circuit_data_padded(arity, &common_data, &inner_params)?;
692692
let params_1 = RecursiveParams {
693693
arity,
694694
common_data: circuit_data_1.common.clone(),
695695
verifier_data: circuit_data_1.verifier_data(),
696696
};
697697

698698
let (_, circuit_data_2) =
699-
RC::<Circuit2>::circuit_data_padded(arity, &common_data, &inner_params)?;
699+
RC::<Circuit2>::target_and_circuit_data_padded(arity, &common_data, &inner_params)?;
700700
let params_2 = RecursiveParams {
701701
arity,
702702
common_data: circuit_data_2.common.clone(),

src/frontend/mod.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,22 @@ impl MainPod {
645645
pub fn id(&self) -> PodId {
646646
self.pod.id()
647647
}
648+
649+
/// Returns the value of a ValueOf statement with self id that defines key if it exists.
650+
pub fn get(&self, key: impl Into<Key>) -> Option<Value> {
651+
let key: Key = key.into();
652+
self.public_statements
653+
.iter()
654+
.find_map(|st| match st {
655+
Statement::ValueOf(ak, value)
656+
if ak.pod_id == self.id() && ak.key.hash() == key.hash() =>
657+
{
658+
Some(value)
659+
}
660+
_ => None,
661+
})
662+
.cloned()
663+
}
648664
}
649665

650666
struct MainPodCompilerInputs<'a> {
@@ -759,6 +775,9 @@ pub mod build_utils {
759775
(new_entry, ($key:expr, $value:expr)) => { $crate::frontend::Operation(
760776
$crate::middleware::OperationType::Native($crate::middleware::NativeOperation::NewEntry),
761777
$crate::op_args!(($key, $value)), $crate::middleware::OperationAux::None) };
778+
(copy, $($arg:expr),+) => { $crate::frontend::Operation(
779+
$crate::middleware::OperationType::Native($crate::middleware::NativeOperation::CopyStatement),
780+
$crate::op_args!($($arg),*), $crate::middleware::OperationAux::None) };
762781
(eq, $($arg:expr),+) => { $crate::frontend::Operation(
763782
$crate::middleware::OperationType::Native($crate::middleware::NativeOperation::EqualFromEntries),
764783
$crate::op_args!($($arg),*), $crate::middleware::OperationAux::None) };

src/frontend/operation.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::fmt;
22

33
use crate::{
4-
frontend::SignedPod,
4+
frontend::{MainPod, SignedPod},
55
middleware::{AnchoredKey, OperationAux, OperationType, Statement, Value},
66
};
77

@@ -78,6 +78,18 @@ impl From<(&SignedPod, &str)> for OperationArg {
7878
))
7979
}
8080
}
81+
impl From<(&MainPod, &str)> for OperationArg {
82+
fn from((pod, key): (&MainPod, &str)) -> Self {
83+
// TODO: TryFrom.
84+
let value = pod
85+
.get(key)
86+
.unwrap_or_else(|| panic!("Key {} is not present in POD: {}", key, pod));
87+
Self::Statement(Statement::ValueOf(
88+
AnchoredKey::from((pod.id(), key)),
89+
value,
90+
))
91+
}
92+
}
8193

8294
impl From<Statement> for OperationArg {
8395
fn from(s: Statement) -> Self {

0 commit comments

Comments
 (0)