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()
|
||||
|
|
|
|||
181
src/middleware/operation.rs
Normal file
181
src/middleware/operation.rs
Normal file
|
|
@ -0,0 +1,181 @@
|
|||
use crate::middleware::{AnchoredKey, SELF};
|
||||
use anyhow::{anyhow, Result};
|
||||
|
||||
use super::Statement;
|
||||
|
||||
#[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,
|
||||
}
|
||||
|
||||
// TODO: Refine this enum.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum Operation {
|
||||
None,
|
||||
NewEntry,
|
||||
CopyStatement(Statement),
|
||||
EqualFromEntries(Statement, Statement),
|
||||
NotEqualFromEntries(Statement, Statement),
|
||||
GtFromEntries(Statement, Statement),
|
||||
LtFromEntries(Statement, Statement),
|
||||
TransitiveEqualFromStatements(Statement, Statement),
|
||||
GtToNotEqual(Statement),
|
||||
LtToNotEqual(Statement),
|
||||
ContainsFromEntries(Statement, Statement),
|
||||
NotContainsFromEntries(Statement, Statement),
|
||||
RenameContainedBy(Statement, Statement),
|
||||
SumOf(Statement, Statement, Statement),
|
||||
ProductOf(Statement, Statement, Statement),
|
||||
MaxOf(Statement, Statement, Statement),
|
||||
}
|
||||
|
||||
impl Operation {
|
||||
pub fn code(&self) -> NativeOperation {
|
||||
use NativeOperation::*;
|
||||
match self {
|
||||
Self::None => None,
|
||||
Self::NewEntry => NewEntry,
|
||||
Self::CopyStatement(_) => CopyStatement,
|
||||
Self::EqualFromEntries(_, _) => EqualFromEntries,
|
||||
Self::NotEqualFromEntries(_, _) => NotEqualFromEntries,
|
||||
Self::GtFromEntries(_, _) => GtFromEntries,
|
||||
Self::LtFromEntries(_, _) => LtFromEntries,
|
||||
Self::TransitiveEqualFromStatements(_, _) => TransitiveEqualFromStatements,
|
||||
Self::GtToNotEqual(_) => GtToNotEqual,
|
||||
Self::LtToNotEqual(_) => LtToNotEqual,
|
||||
Self::ContainsFromEntries(_, _) => ContainsFromEntries,
|
||||
Self::NotContainsFromEntries(_, _) => NotContainsFromEntries,
|
||||
Self::RenameContainedBy(_, _) => RenameContainedBy,
|
||||
Self::SumOf(_, _, _) => SumOf,
|
||||
Self::ProductOf(_, _, _) => ProductOf,
|
||||
Self::MaxOf(_, _, _) => MaxOf,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn args(&self) -> Vec<Statement> {
|
||||
match self.clone() {
|
||||
Self::None => vec![],
|
||||
Self::NewEntry => vec![],
|
||||
Self::CopyStatement(s) => vec![s],
|
||||
Self::EqualFromEntries(s1, s2) => vec![s1, s2],
|
||||
Self::NotEqualFromEntries(s1, s2) => vec![s1, s2],
|
||||
Self::GtFromEntries(s1, s2) => vec![s1, s2],
|
||||
Self::LtFromEntries(s1, s2) => vec![s1, s2],
|
||||
Self::TransitiveEqualFromStatements(s1, s2) => vec![s1, s2],
|
||||
Self::GtToNotEqual(s) => vec![s],
|
||||
Self::LtToNotEqual(s) => vec![s],
|
||||
Self::ContainsFromEntries(s1, s2) => vec![s1, s2],
|
||||
Self::NotContainsFromEntries(s1, s2) => vec![s1, s2],
|
||||
Self::RenameContainedBy(s1, s2) => vec![s1, s2],
|
||||
Self::SumOf(s1, s2, s3) => vec![s1, s2, s3],
|
||||
Self::ProductOf(s1, s2, s3) => vec![s1, s2, s3],
|
||||
Self::MaxOf(s1, s2, s3) => vec![s1, s2, s3],
|
||||
}
|
||||
}
|
||||
/// Forms operation from op-code and arguments.
|
||||
pub fn op(op_code: NativeOperation, args: &[Statement]) -> Result<Self> {
|
||||
type NO = NativeOperation;
|
||||
let arg_tup = (
|
||||
args.get(0).cloned(),
|
||||
args.get(1).cloned(),
|
||||
args.get(2).cloned(),
|
||||
);
|
||||
Ok(match (op_code, arg_tup, args.len()) {
|
||||
(NO::None, (None, None, None), 0) => Self::None,
|
||||
(NO::NewEntry, (None, None, None), 0) => Self::NewEntry,
|
||||
(NO::CopyStatement, (Some(s), None, None), 1) => Self::CopyStatement(s),
|
||||
(NO::EqualFromEntries, (Some(s1), Some(s2), None), 2) => Self::EqualFromEntries(s1, s2),
|
||||
(NO::NotEqualFromEntries, (Some(s1), Some(s2), None), 2) => {
|
||||
Self::NotEqualFromEntries(s1, s2)
|
||||
}
|
||||
(NO::GtFromEntries, (Some(s1), Some(s2), None), 2) => Self::GtFromEntries(s1, s2),
|
||||
(NO::LtFromEntries, (Some(s1), Some(s2), None), 2) => Self::LtFromEntries(s1, s2),
|
||||
(NO::ContainsFromEntries, (Some(s1), Some(s2), None), 2) => {
|
||||
Self::ContainsFromEntries(s1, s2)
|
||||
}
|
||||
(NO::NotContainsFromEntries, (Some(s1), Some(s2), None), 2) => {
|
||||
Self::NotContainsFromEntries(s1, s2)
|
||||
}
|
||||
(NO::RenameContainedBy, (Some(s1), Some(s2), None), 2) => {
|
||||
Self::RenameContainedBy(s1, s2)
|
||||
}
|
||||
(NO::SumOf, (Some(s1), Some(s2), Some(s3)), 3) => Self::SumOf(s1, s2, s3),
|
||||
(NO::ProductOf, (Some(s1), Some(s2), Some(s3)), 3) => Self::ProductOf(s1, s2, s3),
|
||||
(NO::MaxOf, (Some(s1), Some(s2), Some(s3)), 3) => Self::MaxOf(s1, s2, s3),
|
||||
_ => Err(anyhow!(
|
||||
"Ill-formed operation {:?} with arguments {:?}.",
|
||||
op_code,
|
||||
args
|
||||
))?,
|
||||
})
|
||||
}
|
||||
/// Checks the given operation against a statement.
|
||||
pub fn check(&self, output_statement: &Statement) -> Result<bool> {
|
||||
use Statement::*;
|
||||
match (self, output_statement) {
|
||||
(Self::None, None) => Ok(true),
|
||||
(Self::NewEntry, ValueOf(AnchoredKey(pod_id, _), _)) => Ok(pod_id == &SELF),
|
||||
(Self::CopyStatement(s1), s2) => Ok(s1 == s2),
|
||||
(Self::EqualFromEntries(ValueOf(ak1, v1), ValueOf(ak2, v2)), Equal(ak3, ak4)) => {
|
||||
Ok(v1 == v2 && ak3 == ak1 && ak4 == ak2)
|
||||
}
|
||||
(Self::NotEqualFromEntries(ValueOf(ak1, v1), ValueOf(ak2, v2)), NotEqual(ak3, ak4)) => {
|
||||
Ok(v1 != v2 && ak3 == ak1 && ak4 == ak2)
|
||||
}
|
||||
(Self::GtFromEntries(ValueOf(ak1, v1), ValueOf(ak2, v2)), Gt(ak3, ak4)) => {
|
||||
Ok(v1 > v2 && ak3 == ak1 && ak4 == ak2)
|
||||
}
|
||||
(Self::LtFromEntries(ValueOf(ak1, v1), ValueOf(ak2, v2)), Lt(ak3, ak4)) => {
|
||||
Ok(v1 < v2 && ak3 == ak1 && ak4 == ak2)
|
||||
}
|
||||
(Self::ContainsFromEntries(_, _), Contains(_, _)) =>
|
||||
/* TODO */
|
||||
{
|
||||
Ok(true)
|
||||
}
|
||||
(Self::NotContainsFromEntries(_, _), NotContains(_, _)) =>
|
||||
/* TODO */
|
||||
{
|
||||
Ok(true)
|
||||
}
|
||||
(
|
||||
Self::TransitiveEqualFromStatements(Equal(ak1, ak2), Equal(ak3, ak4)),
|
||||
Equal(ak5, ak6),
|
||||
) => Ok(ak2 == ak3 && ak5 == ak1 && ak6 == ak4),
|
||||
(Self::GtToNotEqual(Gt(ak1, ak2)), NotEqual(ak3, ak4)) => Ok(ak1 == ak3 && ak2 == ak4),
|
||||
(Self::LtToNotEqual(Lt(ak1, ak2)), NotEqual(ak3, ak4)) => Ok(ak1 == ak3 && ak2 == ak4),
|
||||
(Self::RenameContainedBy(Contains(ak1, ak2), Equal(ak3, ak4)), Contains(ak5, ak6)) => {
|
||||
Ok(ak1 == ak3 && ak4 == ak5 && ak2 == ak6)
|
||||
}
|
||||
(
|
||||
Self::SumOf(ValueOf(ak1, v1), ValueOf(ak2, v2), ValueOf(ak3, v3)),
|
||||
SumOf(ak4, ak5, ak6),
|
||||
) => {
|
||||
let v1: i64 = v1.clone().try_into()?;
|
||||
let v2: i64 = v2.clone().try_into()?;
|
||||
let v3: i64 = v3.clone().try_into()?;
|
||||
Ok((v1 == v2 + v3) && ak4 == ak1 && ak5 == ak2 && ak6 == ak3)
|
||||
}
|
||||
_ => Err(anyhow!(
|
||||
"Invalid deduction: {:?} ⇏ {:#}",
|
||||
self,
|
||||
output_statement
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
183
src/middleware/statement.rs
Normal file
183
src/middleware/statement.rs
Normal file
|
|
@ -0,0 +1,183 @@
|
|||
use anyhow::{anyhow, Result};
|
||||
use plonky2::field::types::Field;
|
||||
use std::fmt;
|
||||
use strum_macros::FromRepr;
|
||||
|
||||
use super::{AnchoredKey, ToFields, Value, F};
|
||||
|
||||
pub const KEY_SIGNER: &str = "_signer";
|
||||
pub const KEY_TYPE: &str = "_type";
|
||||
pub const STATEMENT_ARG_F_LEN: usize = 8;
|
||||
|
||||
#[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)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Incorporate custom statements into this enum.
|
||||
/// Type encapsulating statements with their associated arguments.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||
pub enum Statement {
|
||||
None,
|
||||
ValueOf(AnchoredKey, Value),
|
||||
Equal(AnchoredKey, AnchoredKey),
|
||||
NotEqual(AnchoredKey, AnchoredKey),
|
||||
Gt(AnchoredKey, AnchoredKey),
|
||||
Lt(AnchoredKey, AnchoredKey),
|
||||
Contains(AnchoredKey, AnchoredKey),
|
||||
NotContains(AnchoredKey, AnchoredKey),
|
||||
SumOf(AnchoredKey, AnchoredKey, AnchoredKey),
|
||||
ProductOf(AnchoredKey, AnchoredKey, AnchoredKey),
|
||||
MaxOf(AnchoredKey, AnchoredKey, AnchoredKey),
|
||||
}
|
||||
|
||||
impl Statement {
|
||||
pub fn is_none(&self) -> bool {
|
||||
self == &Self::None
|
||||
}
|
||||
pub fn code(&self) -> NativeStatement {
|
||||
match self {
|
||||
Self::None => NativeStatement::None,
|
||||
Self::ValueOf(_, _) => NativeStatement::ValueOf,
|
||||
Self::Equal(_, _) => NativeStatement::Equal,
|
||||
Self::NotEqual(_, _) => NativeStatement::NotEqual,
|
||||
Self::Gt(_, _) => NativeStatement::Gt,
|
||||
Self::Lt(_, _) => NativeStatement::Lt,
|
||||
Self::Contains(_, _) => NativeStatement::Contains,
|
||||
Self::NotContains(_, _) => NativeStatement::NotContains,
|
||||
Self::SumOf(_, _, _) => NativeStatement::SumOf,
|
||||
Self::ProductOf(_, _, _) => NativeStatement::ProductOf,
|
||||
Self::MaxOf(_, _, _) => NativeStatement::MaxOf,
|
||||
}
|
||||
}
|
||||
pub fn args(&self) -> Vec<StatementArg> {
|
||||
use StatementArg::*;
|
||||
match self.clone() {
|
||||
Self::None => vec![],
|
||||
Self::ValueOf(ak, v) => vec![Key(ak), Literal(v)],
|
||||
Self::Equal(ak1, ak2) => vec![Key(ak1), Key(ak2)],
|
||||
Self::NotEqual(ak1, ak2) => vec![Key(ak1), Key(ak2)],
|
||||
Self::Gt(ak1, ak2) => vec![Key(ak1), Key(ak2)],
|
||||
Self::Lt(ak1, ak2) => vec![Key(ak1), Key(ak2)],
|
||||
Self::Contains(ak1, ak2) => vec![Key(ak1), Key(ak2)],
|
||||
Self::NotContains(ak1, ak2) => vec![Key(ak1), Key(ak2)],
|
||||
Self::SumOf(ak1, ak2, ak3) => vec![Key(ak1), Key(ak2), Key(ak3)],
|
||||
Self::ProductOf(ak1, ak2, ak3) => vec![Key(ak1), Key(ak2), Key(ak3)],
|
||||
Self::MaxOf(ak1, ak2, ak3) => vec![Key(ak1), Key(ak2), Key(ak3)],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ToFields for Statement {
|
||||
fn to_fields(self) -> (Vec<F>, usize) {
|
||||
let (native_statement_f, native_statement_f_len) = self.code().to_fields();
|
||||
let (vec_statementarg_f, vec_statementarg_f_len) = self
|
||||
.args()
|
||||
.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,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Statement {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{:?} ", self.code())?;
|
||||
for (i, arg) in self.args().iter().enumerate() {
|
||||
if i != 0 {
|
||||
write!(f, " ")?;
|
||||
}
|
||||
write!(f, "{}", arg)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Statement argument type. Useful for statement decompositions.
|
||||
#[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)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue