chore: enums for statement and op types (#69)
* Experiment with statement & op enums * Clean-up & fixes * More clean-up * Add argument length checks * More clean-up * Place statement and operation logic in submodules
This commit is contained in:
parent
83a4f8969f
commit
c2d23b0b1b
11 changed files with 891 additions and 606 deletions
|
|
@ -1,25 +1,25 @@
|
|||
//! The middleware includes the type definitions and the traits used to connect the frontend and
|
||||
//! the backend.
|
||||
|
||||
mod operation;
|
||||
mod statement;
|
||||
|
||||
use anyhow::{anyhow, Error, Result};
|
||||
use dyn_clone::DynClone;
|
||||
use hex::{FromHex, FromHexError};
|
||||
pub use operation::*;
|
||||
use plonky2::field::goldilocks_field::GoldilocksField;
|
||||
use plonky2::field::types::{Field, PrimeField64};
|
||||
use plonky2::hash::poseidon::PoseidonHash;
|
||||
use plonky2::plonk::config::{Hasher, PoseidonGoldilocksConfig};
|
||||
pub use statement::*;
|
||||
use std::any::Any;
|
||||
use std::cmp::{Ord, Ordering};
|
||||
use std::collections::HashMap;
|
||||
use std::fmt;
|
||||
use strum_macros::FromRepr;
|
||||
|
||||
pub mod containers;
|
||||
|
||||
pub const KEY_SIGNER: &str = "_signer";
|
||||
pub const KEY_TYPE: &str = "_type";
|
||||
pub const STATEMENT_ARG_F_LEN: usize = 8;
|
||||
|
||||
/// F is the native field we use everywhere. Currently it's Goldilocks from plonky2
|
||||
pub type F = GoldilocksField;
|
||||
/// C is the Plonky2 config used in POD2 to work with Plonky2 recursion.
|
||||
|
|
@ -27,6 +27,22 @@ pub type C = PoseidonGoldilocksConfig;
|
|||
/// D defines the extension degree of the field used in the Plonky2 proofs (quadratic extension).
|
||||
pub const D: usize = 2;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||
/// AnchoredKey is a tuple containing (OriginId: PodId, key: Hash)
|
||||
pub struct AnchoredKey(pub PodId, pub Hash);
|
||||
|
||||
impl AnchoredKey {
|
||||
pub fn origin(&self) -> PodId {
|
||||
self.0
|
||||
}
|
||||
pub fn key(&self) -> Hash {
|
||||
self.1
|
||||
}
|
||||
}
|
||||
|
||||
/// An entry consists of a key-value pair.
|
||||
pub type Entry = (String, Value);
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq)]
|
||||
pub struct Value(pub [F; 4]);
|
||||
|
||||
|
|
@ -231,306 +247,6 @@ impl Default for Params {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, FromRepr, PartialEq, Eq)]
|
||||
pub enum NativeStatement {
|
||||
None = 0,
|
||||
ValueOf = 1,
|
||||
Equal = 2,
|
||||
NotEqual = 3,
|
||||
Gt = 4,
|
||||
Lt = 5,
|
||||
Contains = 6,
|
||||
NotContains = 7,
|
||||
SumOf = 8,
|
||||
ProductOf = 9,
|
||||
MaxOf = 10,
|
||||
}
|
||||
|
||||
impl ToFields for NativeStatement {
|
||||
fn to_fields(self) -> (Vec<F>, usize) {
|
||||
(vec![F::from_canonical_u64(self as u64)], 1)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||
/// AnchoredKey is a tuple containing (OriginId: PodId, key: Hash)
|
||||
pub struct AnchoredKey(pub PodId, pub Hash);
|
||||
|
||||
impl AnchoredKey {
|
||||
pub fn origin(&self) -> PodId {
|
||||
self.0
|
||||
}
|
||||
pub fn key(&self) -> Hash {
|
||||
self.1
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum StatementArg {
|
||||
None,
|
||||
Literal(Value),
|
||||
Key(AnchoredKey),
|
||||
}
|
||||
|
||||
impl fmt::Display for StatementArg {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
StatementArg::None => write!(f, "none"),
|
||||
StatementArg::Literal(v) => write!(f, "{}", v),
|
||||
StatementArg::Key(r) => write!(f, "{}.{}", r.0, r.1),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl StatementArg {
|
||||
pub fn is_none(&self) -> bool {
|
||||
matches!(self, Self::None)
|
||||
}
|
||||
pub fn literal(&self) -> Result<Value> {
|
||||
match self {
|
||||
Self::Literal(value) => Ok(*value),
|
||||
_ => Err(anyhow!("Statement argument {:?} is not a literal.", self)),
|
||||
}
|
||||
}
|
||||
pub fn key(&self) -> Result<AnchoredKey> {
|
||||
match self {
|
||||
Self::Key(ak) => Ok(ak.clone()),
|
||||
_ => Err(anyhow!("Statement argument {:?} is not a key.", self)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ToFields for StatementArg {
|
||||
fn to_fields(self) -> (Vec<F>, usize) {
|
||||
// NOTE: current version returns always the same amount of field elements in the returned
|
||||
// vector, which means that the `None` case is padded with 8 zeroes, and the `Literal` case
|
||||
// is padded with 4 zeroes. Since the returned vector will mostly be hashed (and reproduced
|
||||
// in-circuit), we might be interested into reducing the length of it. If that's the case,
|
||||
// we can check if it makes sense to make it dependant on the concrete StatementArg; that
|
||||
// is, when dealing with a `None` it would be a single field element (zero value), and when
|
||||
// dealing with `Literal` it would be of length 4.
|
||||
let f = match self {
|
||||
StatementArg::None => vec![F::ZERO; STATEMENT_ARG_F_LEN],
|
||||
StatementArg::Literal(v) => {
|
||||
let value_f = v.0.to_vec();
|
||||
[
|
||||
value_f.clone(),
|
||||
vec![F::ZERO; STATEMENT_ARG_F_LEN - value_f.len()],
|
||||
]
|
||||
.concat()
|
||||
}
|
||||
StatementArg::Key(ak) => {
|
||||
let (podid_f, _) = ak.0.to_fields();
|
||||
let (hash_f, _) = ak.1.to_fields();
|
||||
[podid_f, hash_f].concat()
|
||||
}
|
||||
};
|
||||
assert_eq!(f.len(), STATEMENT_ARG_F_LEN); // sanity check
|
||||
(f, STATEMENT_ARG_F_LEN)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Replace this with a more stringly typed enum as in the Devcon implementation.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct Statement(pub NativeStatement, pub Vec<StatementArg>);
|
||||
|
||||
impl fmt::Display for Statement {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{:?} ", self.0)?;
|
||||
for (i, arg) in self.1.iter().enumerate() {
|
||||
if !(!f.alternate() && arg.is_none()) {
|
||||
if i != 0 {
|
||||
write!(f, " ")?;
|
||||
}
|
||||
write!(f, "{}", arg)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Statement {
|
||||
pub fn code(&self) -> NativeStatement {
|
||||
self.0
|
||||
}
|
||||
pub fn args(&self) -> &[StatementArg] {
|
||||
&self.1
|
||||
}
|
||||
pub fn is_none(&self) -> bool {
|
||||
matches!(self.0, NativeStatement::None)
|
||||
}
|
||||
}
|
||||
|
||||
impl ToFields for Statement {
|
||||
fn to_fields(self) -> (Vec<F>, usize) {
|
||||
let (native_statement_f, native_statement_f_len) = self.0.to_fields();
|
||||
let (vec_statementarg_f, vec_statementarg_f_len) = self
|
||||
.1
|
||||
.into_iter()
|
||||
.map(|statement_arg| statement_arg.to_fields())
|
||||
.fold((Vec::new(), 0), |mut acc, (f, l)| {
|
||||
acc.0.extend(f);
|
||||
acc.1 += l;
|
||||
acc
|
||||
});
|
||||
(
|
||||
[native_statement_f, vec_statementarg_f].concat(),
|
||||
native_statement_f_len + vec_statementarg_f_len,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum NativeOperation {
|
||||
None = 0,
|
||||
NewEntry = 1,
|
||||
CopyStatement = 2,
|
||||
EqualFromEntries = 3,
|
||||
NotEqualFromEntries = 4,
|
||||
GtFromEntries = 5,
|
||||
LtFromEntries = 6,
|
||||
TransitiveEqualFromStatements = 7,
|
||||
GtToNotEqual = 8,
|
||||
LtToNotEqual = 9,
|
||||
ContainsFromEntries = 10,
|
||||
NotContainsFromEntries = 11,
|
||||
RenameContainedBy = 12,
|
||||
SumOf = 13,
|
||||
ProductOf = 14,
|
||||
MaxOf = 15,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum OperationArg {
|
||||
None,
|
||||
Statement(Statement),
|
||||
Key(AnchoredKey),
|
||||
}
|
||||
|
||||
impl OperationArg {
|
||||
pub fn is_none(&self) -> bool {
|
||||
matches!(self, Self::None)
|
||||
}
|
||||
pub fn statement(&self) -> Result<Statement> {
|
||||
match self {
|
||||
Self::Statement(statement) => Ok(statement.clone()),
|
||||
_ => Err(anyhow!("Operation argument {:?} is not a statement.", self)),
|
||||
}
|
||||
}
|
||||
pub fn key(&self) -> Result<AnchoredKey> {
|
||||
match self {
|
||||
Self::Key(ak) => Ok(ak.clone()),
|
||||
_ => Err(anyhow!("Operation argument {:?} is not a key.", self)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Replace this with a more stringly typed enum as in the Devcon implementation.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct Operation(pub NativeOperation, pub Vec<OperationArg>);
|
||||
|
||||
impl Operation {
|
||||
pub fn code(&self) -> NativeOperation {
|
||||
self.0
|
||||
}
|
||||
pub fn args(&self) -> &[OperationArg] {
|
||||
&self.1
|
||||
}
|
||||
// TODO: Argument checking.
|
||||
// TODO: Use `Err` for all type mismatches rather than `false`.
|
||||
/// Checks the given operation against a statement.
|
||||
pub fn check(&self, output_statement: Statement) -> Result<bool> {
|
||||
use NativeOperation::*;
|
||||
match self.0 {
|
||||
// Nothing to check.
|
||||
None => Ok(output_statement.code() == NativeStatement::None),
|
||||
// Check that the resulting statement is of type `ValueOf`
|
||||
// and its origin is `SELF`.
|
||||
NewEntry =>
|
||||
Ok(output_statement.code() == NativeStatement::ValueOf && output_statement.args()[0].key()?.origin() == SELF)
|
||||
,
|
||||
// Check that the operation acts on a statement *and* the
|
||||
// output is equal to this statement.
|
||||
CopyStatement => Ok(output_statement == self.args()[0].statement()?)
|
||||
,
|
||||
EqualFromEntries => {
|
||||
let s1 = self.args()[0].statement()?;
|
||||
let (s1_key, s1_value) = (s1.args()[0].key()?, s1.args()[1].literal()?);
|
||||
let s2 = self.args()[1].statement()?;
|
||||
let (s2_key, s2_value) = (s2.args()[0].key()?, s2.args()[1].literal()?);
|
||||
let statements_equal = s1.code() == NativeStatement::ValueOf && s2.code() == NativeStatement::ValueOf && s1_value == s2_value;
|
||||
Ok(statements_equal && output_statement.code() == NativeStatement::Equal && output_statement.args()[0].key()? == s1_key && output_statement.args()[1].key()? == s2_key)}
|
||||
,
|
||||
NotEqualFromEntries => {
|
||||
let s1 = self.args()[0].statement()?;
|
||||
let (s1_key, s1_value) = (s1.args()[0].key()?, s1.args()[1].literal()?);
|
||||
let s2 = self.args()[1].statement()?;
|
||||
let (s2_key, s2_value) = (s2.args()[0].key()?, s2.args()[1].literal()?);
|
||||
let statements_not_equal = s1.code() == NativeStatement::ValueOf && s2.code() == NativeStatement::ValueOf && s1_value != s2_value;
|
||||
Ok(statements_not_equal && output_statement.code() == NativeStatement::NotEqual && output_statement.args()[0].key()? == s1_key && output_statement.args()[1].key()? == s2_key)} ,
|
||||
GtFromEntries => {
|
||||
let s1 = self.args()[0].statement()?;
|
||||
let (s1_key, s1_value) = (s1.args()[0].key()?, s1.args()[1].literal()?);
|
||||
let s2 = self.args()[1].statement()?;
|
||||
let (s2_key, s2_value) = (s2.args()[0].key()?, s2.args()[1].literal()?);
|
||||
let statements_not_equal = s1.code() == NativeStatement::ValueOf && s2.code() == NativeStatement::ValueOf && s1_value > s2_value;
|
||||
Ok(statements_not_equal && output_statement.code() == NativeStatement::Gt && output_statement.args()[0].key()? == s1_key && output_statement.args()[1].key()? == s2_key)},
|
||||
LtFromEntries => {
|
||||
let s1 = self.args()[0].statement()?;
|
||||
let (s1_key, s1_value) = (s1.args()[0].key()?, s1.args()[1].literal()?);
|
||||
let s2 = self.args()[1].statement()?;
|
||||
let (s2_key, s2_value) = (s2.args()[0].key()?, s2.args()[1].literal()?);
|
||||
let statements_not_equal = s1.code() == NativeStatement::ValueOf && s2.code() == NativeStatement::ValueOf && s1_value < s2_value;
|
||||
Ok(statements_not_equal && output_statement.code() == NativeStatement::Lt && output_statement.args()[0].key()? == s1_key && output_statement.args()[1].key()? == s2_key)},
|
||||
TransitiveEqualFromStatements => {
|
||||
let s1 = self.args()[0].statement()?;
|
||||
let s2 = self.args()[1].statement()?;
|
||||
let key1 = s1.args()[0].key()?;
|
||||
let key2 = s1.args()[1].key()?;
|
||||
let key3 = s2.args()[0].key()?;
|
||||
let key4 = s2.args()[1].key()?;
|
||||
let statements_satisfy_transitivity = s1.code() == NativeStatement::Equal && s2.code() == NativeStatement::Equal && key2 == key3;
|
||||
Ok(statements_satisfy_transitivity && output_statement.code() == NativeStatement::Equal && output_statement.args()[0].key()? == key1 && output_statement.args()[1].key()? == key4)
|
||||
},
|
||||
GtToNotEqual => {
|
||||
let s = self.args()[0].statement()?;
|
||||
let arg_is_gt = s.code() == NativeStatement::Gt;
|
||||
Ok(arg_is_gt && output_statement.code() == NativeStatement::NotEqual && output_statement.args() == s.args())
|
||||
},
|
||||
LtToNotEqual => {
|
||||
let s = self.args()[0].statement()?;
|
||||
let arg_is_lt = s.code() == NativeStatement::Lt;
|
||||
Ok(arg_is_lt && output_statement.code() == NativeStatement::NotEqual && output_statement.args() == s.args())
|
||||
},
|
||||
RenameContainedBy => {
|
||||
let s1 = self.args()[0].statement()?;
|
||||
let s2 = self.args()[1].statement()?;
|
||||
let key1 = s1.args()[0].key()?;
|
||||
let key2 = s1.args()[1].key()?;
|
||||
let key3 = s2.args()[0].key()?;
|
||||
let key4 = s2.args()[1].key()?;
|
||||
let args_satisfy_rename = s1.code() == NativeStatement::Contains && s2.code() == NativeStatement::Equal && key1 == key3;
|
||||
Ok(args_satisfy_rename && output_statement.code() == NativeStatement::Contains && output_statement.args()[0].key()? == key4 && output_statement.args()[1].key()? == key2)
|
||||
},
|
||||
SumOf => {
|
||||
let s1 = self.args()[0].statement()?;
|
||||
let s1_key = s1.args()[0].key()?;
|
||||
let s1_value: i64 = s1.args()[1].literal()?.try_into()?;
|
||||
let s2 = self.args()[1].statement()?;
|
||||
let s2_key = s2.args()[0].key()?;
|
||||
let s2_value:i64 = s2.args()[1].literal()?.try_into()?;
|
||||
let s3 = self.args()[2].statement()?;
|
||||
let s3_key = s3.args()[0].key()?;
|
||||
let s3_value: i64 = s3.args()[1].literal()?.try_into()?;
|
||||
let sum_holds = s1.code() == NativeStatement::ValueOf && s2.code() == NativeStatement::ValueOf && s3.code() == NativeStatement::ValueOf && s1_value == s2_value + s3_value;
|
||||
Ok(sum_holds && output_statement.code() == NativeStatement::SumOf && output_statement.args()[0].key()? == s1_key && output_statement.args()[1].key()? == s2_key && output_statement.args()[2].key()? == s3_key)
|
||||
},
|
||||
// TODO: Remaining ops.
|
||||
_ => Ok(true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Pod: fmt::Debug + DynClone {
|
||||
fn verify(&self) -> bool;
|
||||
fn id(&self) -> PodId;
|
||||
|
|
@ -539,11 +255,8 @@ pub trait Pod: fmt::Debug + DynClone {
|
|||
fn kvs(&self) -> HashMap<AnchoredKey, Value> {
|
||||
self.pub_statements()
|
||||
.into_iter()
|
||||
.filter_map(|st| match st.0 {
|
||||
NativeStatement::ValueOf => Some((
|
||||
st.1[0].key().expect("key"),
|
||||
st.1[1].literal().expect("literal"),
|
||||
)),
|
||||
.filter_map(|st| match st {
|
||||
Statement::ValueOf(ak, v) => Some((ak, v)),
|
||||
_ => None,
|
||||
})
|
||||
.collect()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue