Skip to content

Commit

Permalink
fix build;
Browse files Browse the repository at this point in the history
add Prover test for true prop;
  • Loading branch information
greenhat committed Jul 27, 2020
1 parent ebbce1d commit 899878b
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 26 deletions.
1 change: 1 addition & 0 deletions sigma-tree/src/chain.rs
Expand Up @@ -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::*;
Expand Down
2 changes: 2 additions & 0 deletions sigma-tree/src/chain/digest32.rs
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
22 changes: 8 additions & 14 deletions 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;
Expand Down Expand Up @@ -69,15 +66,12 @@ impl ErgoTree {

impl From<Rc<Expr>> for ErgoTree {
fn from(expr: Rc<Expr>) -> 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),
}),
}
}
}
Expand Down Expand Up @@ -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::*;

Expand Down
18 changes: 16 additions & 2 deletions 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;
Expand All @@ -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 {
Expand All @@ -31,7 +41,10 @@ pub trait Evaluator {
#[allow(unconditional_recursion)]
fn eval(expr: &Expr, env: &Env, ca: &mut CostAccumulator) -> Result<Value, EvalError> {
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!(),
Expand All @@ -49,6 +62,7 @@ fn eval(expr: &Expr, env: &Env, ca: &mut CostAccumulator) -> Result<Value, EvalE
},
})
}
_ => Err(EvalError::UnexpectedExpr),
}
}

10 changes: 5 additions & 5 deletions sigma-tree/src/eval/cost_accum.rs
Expand Up @@ -6,16 +6,16 @@ pub struct CostAccumulator {
}

impl CostAccumulator {
pub fn new(initial_cost: u64, cost_limit: Option<u64>) -> CostAccumulator {
todo!()
pub fn new(_initial_cost: u64, _cost_limit: Option<u64>) -> CostAccumulator {
CostAccumulator {
costs: Costs::DEFAULT,
}
}

pub fn add_cost_of(&mut self, expr: &Expr) {
let cost = self.costs.cost_of(expr);
self.add(cost);
}

pub fn add(&self, _: Cost) {
todo!();
}
pub fn add(&self, _: Cost) {}
}
4 changes: 4 additions & 0 deletions sigma-tree/src/eval/costs.rs
Expand Up @@ -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 {
Expand Down
13 changes: 12 additions & 1 deletion sigma-tree/src/sigma_protocol.rs
Expand Up @@ -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<u8> {
todo!()
match tree {
UncheckedTree::NoProof => vec![],
UncheckedTree::UncheckedSigmaTree(_) => todo!(),
}
}

#[cfg(test)]
Expand Down
34 changes: 30 additions & 4 deletions sigma-tree/src/sigma_protocol/prover.rs
Expand Up @@ -2,6 +2,7 @@

#![allow(dead_code)]
#![allow(unused_variables)]
#![allow(missing_docs)]

use super::{serialize_sig, SigmaBoolean, UncheckedSigmaTree, UncheckedTree, UnprovenTree};
use crate::{
Expand All @@ -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),
Expand All @@ -27,10 +29,10 @@ impl From<ErgoTreeParsingError> for ProverError {
}
}

pub struct ReductionResult {
sigma_prop: SigmaBoolean,
cost: u64,
}
// pub struct ReductionResult {
// sigma_prop: SigmaBoolean,
// cost: u64,
// }

pub trait Prover: Evaluator {
fn prove(
Expand Down Expand Up @@ -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());
}
}
1 change: 1 addition & 0 deletions sigma-tree/src/sigma_protocol/verifier.rs
Expand Up @@ -2,6 +2,7 @@

#![allow(dead_code)]
#![allow(unused_variables)]
#![allow(missing_docs)]

use super::SigmaBoolean;
use crate::{
Expand Down

0 comments on commit 899878b

Please sign in to comment.