From 899878be69cff16a745b87848f50ec13eb03d776 Mon Sep 17 00:00:00 2001 From: Denys Zadorozhnyi Date: Mon, 27 Jul 2020 14:45:58 +0300 Subject: [PATCH] fix build; add Prover test for true prop; --- sigma-tree/src/chain.rs | 1 + sigma-tree/src/chain/digest32.rs | 2 ++ sigma-tree/src/ergo_tree.rs | 22 ++++++--------- sigma-tree/src/eval.rs | 18 ++++++++++-- sigma-tree/src/eval/cost_accum.rs | 10 +++---- sigma-tree/src/eval/costs.rs | 4 +++ sigma-tree/src/sigma_protocol.rs | 13 ++++++++- sigma-tree/src/sigma_protocol/prover.rs | 34 ++++++++++++++++++++--- sigma-tree/src/sigma_protocol/verifier.rs | 1 + 9 files changed, 79 insertions(+), 26 deletions(-) diff --git a/sigma-tree/src/chain.rs b/sigma-tree/src/chain.rs index d1d3ad46c..02a0f426f 100644 --- a/sigma-tree/src/chain.rs +++ b/sigma-tree/src/chain.rs @@ -22,6 +22,7 @@ pub use box_id::*; pub use context_extension::*; pub use contract::*; pub use digest32::*; +pub use ergo_box::*; pub use input::*; pub use prover_result::*; pub use prover_result::*; diff --git a/sigma-tree/src/chain/digest32.rs b/sigma-tree/src/chain/digest32.rs index 5c8b67bcc..3ee354612 100644 --- a/sigma-tree/src/chain/digest32.rs +++ b/sigma-tree/src/chain/digest32.rs @@ -34,6 +34,7 @@ impl Digest32 { } } +/// Blake2b256 hash (256 bit) pub fn blake2b256_hash(bytes: &[u8]) -> Digest32 { // unwrap is safe 32 bytes is a valid hash size (<= 512 && 32 % 8 == 0) let mut hasher = VarBlake2b::new(Digest32::SIZE).unwrap(); @@ -82,6 +83,7 @@ impl SigmaSerializable for Digest32 { } } +/// Invalie byte array size #[derive(Error, Debug)] #[error("Invalid byte array size ({0})")] pub struct Digest32Error(std::array::TryFromSliceError); diff --git a/sigma-tree/src/ergo_tree.rs b/sigma-tree/src/ergo_tree.rs index b0ec6ba91..65d656693 100644 --- a/sigma-tree/src/ergo_tree.rs +++ b/sigma-tree/src/ergo_tree.rs @@ -1,8 +1,5 @@ //! ErgoTree -use crate::{ - ast::{Constant, Expr}, - types::SType, -}; +use crate::ast::{Constant, Expr}; use io::{Cursor, Read}; use sigma_ser::serializer::SerializationError; use sigma_ser::serializer::SigmaSerializable; @@ -69,15 +66,12 @@ impl ErgoTree { impl From> for ErgoTree { fn from(expr: Rc) -> Self { - match &*expr { - Expr::Const(c) if c.tpe == SType::SSigmaProp => ErgoTree { - header: ErgoTree::DEFAULT_HEADER, - tree: Ok(ParsedTree { - constants: Vec::new(), - root: Ok(expr), - }), - }, - _ => panic!("not yet supported"), + ErgoTree { + header: ErgoTree::DEFAULT_HEADER, + tree: Ok(ParsedTree { + constants: Vec::new(), + root: Ok(expr), + }), } } } @@ -178,7 +172,7 @@ impl SigmaSerializable for ErgoTree { #[cfg(test)] mod tests { use super::*; - use crate::{ast::ConstantVal, sigma_protocol::SigmaProp}; + use crate::{ast::ConstantVal, sigma_protocol::SigmaProp, types::SType}; use proptest::prelude::*; use sigma_ser::test_helpers::*; diff --git a/sigma-tree/src/eval.rs b/sigma-tree/src/eval.rs index e7596a565..61f7f77dc 100644 --- a/sigma-tree/src/eval.rs +++ b/sigma-tree/src/eval.rs @@ -1,6 +1,7 @@ use crate::{ - ast::{ops::BinOp, ops::NumOp, Expr}, + ast::{ops::BinOp, ops::NumOp, Constant, ConstantVal, Expr}, sigma_protocol::SigmaBoolean, + types::SType, }; use cost_accum::CostAccumulator; @@ -12,8 +13,17 @@ mod value; pub struct Env(); +impl Env { + pub fn empty() -> Env { + Env() + } +} + +#[derive(PartialEq, Eq, Debug, Clone)] pub enum EvalError { InvalidResultType, + // TODO: store unexpected expr + UnexpectedExpr, } pub trait Evaluator { @@ -31,7 +41,10 @@ pub trait Evaluator { #[allow(unconditional_recursion)] fn eval(expr: &Expr, env: &Env, ca: &mut CostAccumulator) -> Result { match expr { - Expr::Const(_) => todo!(), //Ok(EvalResult(*v)), + Expr::Const(Constant { + tpe: SType::SBoolean, + v: ConstantVal::Boolean(b), + }) => Ok(Value::Boolean(*b)), //Ok(EvalResult(*v)), Expr::Coll { .. } => todo!(), Expr::Tup { .. } => todo!(), Expr::PredefFunc(_) => todo!(), @@ -49,6 +62,7 @@ fn eval(expr: &Expr, env: &Env, ca: &mut CostAccumulator) -> Result Err(EvalError::UnexpectedExpr), } } diff --git a/sigma-tree/src/eval/cost_accum.rs b/sigma-tree/src/eval/cost_accum.rs index f901fad0b..391ccb1bd 100644 --- a/sigma-tree/src/eval/cost_accum.rs +++ b/sigma-tree/src/eval/cost_accum.rs @@ -6,8 +6,10 @@ pub struct CostAccumulator { } impl CostAccumulator { - pub fn new(initial_cost: u64, cost_limit: Option) -> CostAccumulator { - todo!() + pub fn new(_initial_cost: u64, _cost_limit: Option) -> CostAccumulator { + CostAccumulator { + costs: Costs::DEFAULT, + } } pub fn add_cost_of(&mut self, expr: &Expr) { @@ -15,7 +17,5 @@ impl CostAccumulator { self.add(cost); } - pub fn add(&self, _: Cost) { - todo!(); - } + pub fn add(&self, _: Cost) {} } diff --git a/sigma-tree/src/eval/costs.rs b/sigma-tree/src/eval/costs.rs index 984ccadfc..b116c97af 100644 --- a/sigma-tree/src/eval/costs.rs +++ b/sigma-tree/src/eval/costs.rs @@ -4,6 +4,10 @@ pub struct Cost(u32); pub struct Costs {} +impl Costs { + pub const DEFAULT: Costs = Costs {}; +} + impl Costs { pub fn cost_of(&self, expr: &Expr) -> Cost { match expr { diff --git a/sigma-tree/src/sigma_protocol.rs b/sigma-tree/src/sigma_protocol.rs index 320c4dae7..f57aeea7d 100644 --- a/sigma-tree/src/sigma_protocol.rs +++ b/sigma-tree/src/sigma_protocol.rs @@ -132,22 +132,33 @@ impl SigmaProp { } } +/// Proof tree pub enum ProofTree { + /// Unchecked tree UncheckedTree(UncheckedTree), + /// Unproven tree UnprovenTree(UnprovenTree), } +/// Unproven tree pub enum UnprovenTree {} +/// Unchecked sigma tree pub enum UncheckedSigmaTree {} +/// Unchecked tree pub enum UncheckedTree { + /// No proof needed NoProof, + /// Unchecked sigma tree UncheckedSigmaTree(UncheckedSigmaTree), } fn serialize_sig(tree: UncheckedTree) -> Vec { - todo!() + match tree { + UncheckedTree::NoProof => vec![], + UncheckedTree::UncheckedSigmaTree(_) => todo!(), + } } #[cfg(test)] diff --git a/sigma-tree/src/sigma_protocol/prover.rs b/sigma-tree/src/sigma_protocol/prover.rs index e51c69138..8204b0749 100644 --- a/sigma-tree/src/sigma_protocol/prover.rs +++ b/sigma-tree/src/sigma_protocol/prover.rs @@ -2,6 +2,7 @@ #![allow(dead_code)] #![allow(unused_variables)] +#![allow(missing_docs)] use super::{serialize_sig, SigmaBoolean, UncheckedSigmaTree, UncheckedTree, UnprovenTree}; use crate::{ @@ -15,6 +16,7 @@ pub struct TestProver {} impl Evaluator for TestProver {} impl Prover for TestProver {} +#[derive(PartialEq, Eq, Debug, Clone)] pub enum ProverError { ErgoTreeError(ErgoTreeParsingError), EvalError(EvalError), @@ -27,10 +29,10 @@ impl From for ProverError { } } -pub struct ReductionResult { - sigma_prop: SigmaBoolean, - cost: u64, -} +// pub struct ReductionResult { +// sigma_prop: SigmaBoolean, +// cost: u64, +// } pub trait Prover: Evaluator { fn prove( @@ -70,3 +72,27 @@ pub trait Prover: Evaluator { todo!() } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::{ + ast::{Constant, ConstantVal, Expr}, + types::SType, + }; + use std::rc::Rc; + + #[test] + fn test_prove_true_prop() { + let bool_true_tree = ErgoTree::from(Rc::new(Expr::Const(Constant { + tpe: SType::SBoolean, + v: ConstantVal::Boolean(true), + }))); + let message = vec![0u8; 100]; + + let prover = TestProver {}; + let res = prover.prove(&bool_true_tree, &Env::empty(), message.as_slice()); + assert!(res.is_ok()); + assert!(res.unwrap().proof.is_empty()); + } +} diff --git a/sigma-tree/src/sigma_protocol/verifier.rs b/sigma-tree/src/sigma_protocol/verifier.rs index 0177734a6..e69240e0a 100644 --- a/sigma-tree/src/sigma_protocol/verifier.rs +++ b/sigma-tree/src/sigma_protocol/verifier.rs @@ -2,6 +2,7 @@ #![allow(dead_code)] #![allow(unused_variables)] +#![allow(missing_docs)] use super::SigmaBoolean; use crate::{