Skip to content

Commit 3465fc8

Browse files
committed
Place statement and operation logic in submodules
1 parent a1128a3 commit 3465fc8

File tree

9 files changed

+737
-688
lines changed

9 files changed

+737
-688
lines changed

src/backends/mock_main.rs

Lines changed: 6 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1+
mod operation;
2+
mod statement;
3+
14
use crate::middleware::{
25
self, hash_str, AnchoredKey, Hash, MainPodInputs, NativeOperation, NativeStatement, NonePod,
36
Params, Pod, PodId, PodProver, StatementArg, ToFields, KEY_TYPE, SELF,
47
};
5-
use anyhow::{anyhow, Result};
8+
use anyhow::Result;
69
use itertools::Itertools;
10+
pub use operation::*;
711
use plonky2::hash::poseidon::PoseidonHash;
812
use plonky2::plonk::config::Hasher;
13+
pub use statement::*;
914
use std::any::Any;
1015
use std::fmt;
1116

@@ -19,168 +24,6 @@ impl PodProver for MockProver {
1924
}
2025
}
2126

22-
#[derive(Clone, Debug, PartialEq, Eq)]
23-
enum OperationArg {
24-
None,
25-
Index(usize),
26-
}
27-
28-
impl OperationArg {
29-
fn is_none(&self) -> bool {
30-
matches!(self, OperationArg::None)
31-
}
32-
}
33-
34-
#[derive(Clone, Debug, PartialEq, Eq)]
35-
enum OperationArgError {
36-
KeyNotFound,
37-
StatementNotFound,
38-
}
39-
40-
impl std::fmt::Display for OperationArgError {
41-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42-
match self {
43-
OperationArgError::KeyNotFound => write!(f, "Key not found"),
44-
OperationArgError::StatementNotFound => write!(f, "Statement not found"),
45-
}
46-
}
47-
}
48-
49-
impl std::error::Error for OperationArgError {}
50-
51-
#[derive(Clone, Debug, PartialEq, Eq)]
52-
struct Operation(pub NativeOperation, pub Vec<OperationArg>);
53-
54-
impl Operation {
55-
pub fn deref(&self, statements: &[Statement]) -> Result<crate::middleware::Operation> {
56-
let deref_args = self
57-
.1
58-
.iter()
59-
.flat_map(|arg| match arg {
60-
OperationArg::None => None,
61-
OperationArg::Index(i) => Some(statements[*i].clone().try_into()),
62-
})
63-
.collect::<Result<Vec<crate::middleware::Statement>>>()?;
64-
middleware::Operation::op(self.0, &deref_args)
65-
}
66-
}
67-
68-
#[derive(Clone, Debug, PartialEq, Eq)]
69-
pub struct Statement(pub NativeStatement, pub Vec<StatementArg>);
70-
71-
impl Statement {
72-
pub fn is_none(&self) -> bool {
73-
self.0 == NativeStatement::None
74-
}
75-
/// Argument method. Trailing Nones are filtered out.
76-
pub fn args(&self) -> Vec<StatementArg> {
77-
let maybe_last_arg_index = (0..self.1.len()).rev().find(|i| !self.1[*i].is_none());
78-
match maybe_last_arg_index {
79-
None => vec![],
80-
Some(i) => self.1[0..i + 1].to_vec(),
81-
}
82-
}
83-
}
84-
85-
impl ToFields for Statement {
86-
fn to_fields(self) -> (Vec<middleware::F>, usize) {
87-
let (native_statement_f, native_statement_f_len) = self.0.to_fields();
88-
let (vec_statementarg_f, vec_statementarg_f_len) = self
89-
.1
90-
.into_iter()
91-
.map(|statement_arg| statement_arg.to_fields())
92-
.fold((Vec::new(), 0), |mut acc, (f, l)| {
93-
acc.0.extend(f);
94-
acc.1 += l;
95-
acc
96-
});
97-
(
98-
[native_statement_f, vec_statementarg_f].concat(),
99-
native_statement_f_len + vec_statementarg_f_len,
100-
)
101-
}
102-
}
103-
104-
impl TryFrom<Statement> for middleware::Statement {
105-
type Error = anyhow::Error;
106-
fn try_from(s: Statement) -> Result<Self> {
107-
type S = middleware::Statement;
108-
type NS = NativeStatement;
109-
type SA = StatementArg;
110-
let proper_args = s.args();
111-
let args = (
112-
proper_args.get(0).cloned(),
113-
proper_args.get(1).cloned(),
114-
proper_args.get(2).cloned(),
115-
);
116-
Ok(match (s.0, args, proper_args.len()) {
117-
(NS::None, _, 0) => S::None,
118-
(NS::ValueOf, (Some(SA::Key(ak)), Some(SA::Literal(v)), None), 2) => S::ValueOf(ak, v),
119-
(NS::Equal, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), None), 2) => S::Equal(ak1, ak2),
120-
(NS::NotEqual, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), None), 2) => {
121-
S::NotEqual(ak1, ak2)
122-
}
123-
(NS::Gt, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), None), 2) => S::Gt(ak1, ak2),
124-
(NS::Lt, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), None), 2) => S::Lt(ak1, ak2),
125-
(NS::Contains, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), None), 2) => {
126-
S::Contains(ak1, ak2)
127-
}
128-
(NS::NotContains, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), None), 2) => {
129-
S::NotContains(ak1, ak2)
130-
}
131-
(NS::SumOf, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), Some(SA::Key(ak3))), 3) => {
132-
S::SumOf(ak1, ak2, ak3)
133-
}
134-
(NS::ProductOf, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), Some(SA::Key(ak3))), 3) => {
135-
S::ProductOf(ak1, ak2, ak3)
136-
}
137-
(NS::MaxOf, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), Some(SA::Key(ak3))), 3) => {
138-
S::MaxOf(ak1, ak2, ak3)
139-
}
140-
_ => Err(anyhow!("Ill-formed statement expression {:?}", s))?,
141-
})
142-
}
143-
}
144-
145-
impl From<middleware::Statement> for Statement {
146-
fn from(s: middleware::Statement) -> Self {
147-
Statement(s.code(), s.args().into_iter().map(|arg| arg).collect())
148-
}
149-
}
150-
151-
impl fmt::Display for Statement {
152-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
153-
write!(f, "{:?} ", self.0)?;
154-
for (i, arg) in self.1.iter().enumerate() {
155-
if !(!f.alternate() && arg.is_none()) {
156-
if i != 0 {
157-
write!(f, " ")?;
158-
}
159-
write!(f, "{}", arg)?;
160-
}
161-
}
162-
Ok(())
163-
}
164-
}
165-
166-
impl fmt::Display for Operation {
167-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
168-
write!(f, "{:?} ", self.0)?;
169-
for (i, arg) in self.1.iter().enumerate() {
170-
if !(!f.alternate() && arg.is_none()) {
171-
if i != 0 {
172-
write!(f, " ")?;
173-
}
174-
match arg {
175-
OperationArg::None => write!(f, "none")?,
176-
OperationArg::Index(i) => write!(f, "{:02}", i)?,
177-
}
178-
}
179-
}
180-
Ok(())
181-
}
182-
}
183-
18427
#[derive(Clone, Debug)]
18528
pub struct MockMainPod {
18629
params: Params,
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
use std::fmt;
2+
3+
use anyhow::Result;
4+
5+
use crate::middleware::{self, NativeOperation};
6+
7+
use super::Statement;
8+
9+
#[derive(Clone, Debug, PartialEq, Eq)]
10+
pub enum OperationArg {
11+
None,
12+
Index(usize),
13+
}
14+
15+
impl OperationArg {
16+
pub fn is_none(&self) -> bool {
17+
matches!(self, OperationArg::None)
18+
}
19+
}
20+
21+
#[derive(Clone, Debug, PartialEq, Eq)]
22+
pub enum OperationArgError {
23+
KeyNotFound,
24+
StatementNotFound,
25+
}
26+
27+
impl std::fmt::Display for OperationArgError {
28+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
29+
match self {
30+
OperationArgError::KeyNotFound => write!(f, "Key not found"),
31+
OperationArgError::StatementNotFound => write!(f, "Statement not found"),
32+
}
33+
}
34+
}
35+
36+
impl std::error::Error for OperationArgError {}
37+
38+
#[derive(Clone, Debug, PartialEq, Eq)]
39+
pub struct Operation(pub NativeOperation, pub Vec<OperationArg>);
40+
41+
impl Operation {
42+
pub fn deref(&self, statements: &[Statement]) -> Result<crate::middleware::Operation> {
43+
let deref_args = self
44+
.1
45+
.iter()
46+
.flat_map(|arg| match arg {
47+
OperationArg::None => None,
48+
OperationArg::Index(i) => Some(statements[*i].clone().try_into()),
49+
})
50+
.collect::<Result<Vec<crate::middleware::Statement>>>()?;
51+
middleware::Operation::op(self.0, &deref_args)
52+
}
53+
}
54+
55+
impl fmt::Display for Operation {
56+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
57+
write!(f, "{:?} ", self.0)?;
58+
for (i, arg) in self.1.iter().enumerate() {
59+
if !(!f.alternate() && arg.is_none()) {
60+
if i != 0 {
61+
write!(f, " ")?;
62+
}
63+
match arg {
64+
OperationArg::None => write!(f, "none")?,
65+
OperationArg::Index(i) => write!(f, "{:02}", i)?,
66+
}
67+
}
68+
}
69+
Ok(())
70+
}
71+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
use std::fmt;
2+
3+
use anyhow::{anyhow, Result};
4+
5+
use crate::middleware::{self, NativeStatement, StatementArg, ToFields};
6+
7+
#[derive(Clone, Debug, PartialEq, Eq)]
8+
pub struct Statement(pub NativeStatement, pub Vec<StatementArg>);
9+
10+
impl Statement {
11+
pub fn is_none(&self) -> bool {
12+
self.0 == NativeStatement::None
13+
}
14+
/// Argument method. Trailing Nones are filtered out.
15+
pub fn args(&self) -> Vec<StatementArg> {
16+
let maybe_last_arg_index = (0..self.1.len()).rev().find(|i| !self.1[*i].is_none());
17+
match maybe_last_arg_index {
18+
None => vec![],
19+
Some(i) => self.1[0..i + 1].to_vec(),
20+
}
21+
}
22+
}
23+
24+
impl ToFields for Statement {
25+
fn to_fields(self) -> (Vec<middleware::F>, usize) {
26+
let (native_statement_f, native_statement_f_len) = self.0.to_fields();
27+
let (vec_statementarg_f, vec_statementarg_f_len) = self
28+
.1
29+
.into_iter()
30+
.map(|statement_arg| statement_arg.to_fields())
31+
.fold((Vec::new(), 0), |mut acc, (f, l)| {
32+
acc.0.extend(f);
33+
acc.1 += l;
34+
acc
35+
});
36+
(
37+
[native_statement_f, vec_statementarg_f].concat(),
38+
native_statement_f_len + vec_statementarg_f_len,
39+
)
40+
}
41+
}
42+
43+
impl TryFrom<Statement> for middleware::Statement {
44+
type Error = anyhow::Error;
45+
fn try_from(s: Statement) -> Result<Self> {
46+
type S = middleware::Statement;
47+
type NS = NativeStatement;
48+
type SA = StatementArg;
49+
let proper_args = s.args();
50+
let args = (
51+
proper_args.get(0).cloned(),
52+
proper_args.get(1).cloned(),
53+
proper_args.get(2).cloned(),
54+
);
55+
Ok(match (s.0, args, proper_args.len()) {
56+
(NS::None, _, 0) => S::None,
57+
(NS::ValueOf, (Some(SA::Key(ak)), Some(SA::Literal(v)), None), 2) => S::ValueOf(ak, v),
58+
(NS::Equal, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), None), 2) => S::Equal(ak1, ak2),
59+
(NS::NotEqual, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), None), 2) => {
60+
S::NotEqual(ak1, ak2)
61+
}
62+
(NS::Gt, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), None), 2) => S::Gt(ak1, ak2),
63+
(NS::Lt, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), None), 2) => S::Lt(ak1, ak2),
64+
(NS::Contains, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), None), 2) => {
65+
S::Contains(ak1, ak2)
66+
}
67+
(NS::NotContains, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), None), 2) => {
68+
S::NotContains(ak1, ak2)
69+
}
70+
(NS::SumOf, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), Some(SA::Key(ak3))), 3) => {
71+
S::SumOf(ak1, ak2, ak3)
72+
}
73+
(NS::ProductOf, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), Some(SA::Key(ak3))), 3) => {
74+
S::ProductOf(ak1, ak2, ak3)
75+
}
76+
(NS::MaxOf, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), Some(SA::Key(ak3))), 3) => {
77+
S::MaxOf(ak1, ak2, ak3)
78+
}
79+
_ => Err(anyhow!("Ill-formed statement expression {:?}", s))?,
80+
})
81+
}
82+
}
83+
84+
impl From<middleware::Statement> for Statement {
85+
fn from(s: middleware::Statement) -> Self {
86+
Statement(s.code(), s.args().into_iter().map(|arg| arg).collect())
87+
}
88+
}
89+
90+
impl fmt::Display for Statement {
91+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
92+
write!(f, "{:?} ", self.0)?;
93+
for (i, arg) in self.1.iter().enumerate() {
94+
if !(!f.alternate() && arg.is_none()) {
95+
if i != 0 {
96+
write!(f, " ")?;
97+
}
98+
write!(f, "{}", arg)?;
99+
}
100+
}
101+
Ok(())
102+
}
103+
}

0 commit comments

Comments
 (0)