Skip to content

Commit

Permalink
add EvalError::WrappedWithEnvError with evaluation env attached;
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat committed Jul 18, 2023
1 parent 6fb1949 commit 90c0059
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 20 deletions.
7 changes: 6 additions & 1 deletion ergo-lib/src/chain/transaction/reduced.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ impl SigmaSerializable for ReducedTransaction {
let cost = r.get_u64()?;
let extension = input.spending_proof.extension;
let reduced_input = ReducedInput {
reduction_result: ReductionResult { sigma_prop, cost },
reduction_result: ReductionResult {
sigma_prop,
cost,
env: Env::empty(),
},
extension: extension.clone(),
};
let unsigned_input = UnsignedInput {
Expand Down Expand Up @@ -167,6 +171,7 @@ pub mod arbitrary {
reduction_result: ReductionResult {
sigma_prop: sb.clone(),
cost: 0,
env: Env::empty(),
},
extension: unsigned_input.extension,
}),
Expand Down
26 changes: 15 additions & 11 deletions ergotree-interpreter/src/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,9 @@ mod tests {
let encoder = AddressEncoder::new(NetworkPrefix::Mainnet);
let addr = encoder.parse_address_from_str(p2s_addr_str).unwrap();
assert!(addr.script().unwrap().proposition().is_ok());
let _script = addr.script().unwrap().proposition().unwrap();
// let script = addr.script().unwrap().proposition().unwrap();
// dbg!(&script);
// let res: bool = eval_out_wo_ctx::<SigmaProp>(script.as_ref())
// .try_into()
// .unwrap();
// let res: bool = eval_out_wo_ctx::<SigmaProp>(&script).try_into().unwrap();
// assert!(!res);
}

Expand All @@ -227,12 +225,9 @@ mod tests {
let p2s_addr_str = "cLPHJ3MHuKAHoCUwGhcEFw5sWJqvPwFyKxTRj1aUoMwgAz78Fg3zLXRhBup9Te1WLau1gZXNmXvUmeXGCd7QLeqB7ArrT3v5cg26piEtqymM6j2SkgYVCobgoAGKeTf6nMLxv1uVrLdjt1GnPxG1MuWj7Es7Dfumotbx9YEaxwqtTUC5SKsJc9LCpAmNWRAQbU6tVVEvmfwWivrGoZ3L5C4DMisxN3U";
let encoder = AddressEncoder::new(NetworkPrefix::Mainnet);
let addr = encoder.parse_address_from_str(p2s_addr_str).unwrap();
let _script = addr.script().unwrap().proposition().unwrap();
// dbg!(&script);
// let res: bool = eval_out_wo_ctx::<SigmaProp>(script.as_ref())
// .try_into()
// .unwrap();
// assert!(!res);
let _ = addr.script().unwrap().proposition().unwrap();
// let ctx = Rc::new(force_any_val::<Context>());
// let _ = reduce_to_crypto(&script, &Env::empty(), ctx).unwrap();
}

#[test]
Expand Down Expand Up @@ -284,6 +279,15 @@ mod tests {

let encoder = AddressEncoder::new(NetworkPrefix::Mainnet);
let addr = encoder.parse_address_from_str(p2s_str).unwrap();
let _script = addr.script().unwrap().proposition().unwrap();
let _ = addr.script().unwrap().proposition().unwrap();
// let ctx = Rc::new(force_any_val::<Context>());
// let res = reduce_to_crypto(&script, &Env::empty(), ctx).unwrap();
// match res.sigma_prop {
// SigmaBoolean::TrivialProp(b) => assert!(b),
// SigmaBoolean::ProofOfKnowledge(_) => {
// todo!()
// }
// SigmaBoolean::SigmaConjecture(_) => todo!(),
// }
}
}
22 changes: 19 additions & 3 deletions ergotree-interpreter/src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,14 @@ pub enum EvalError {
/// Scorex serialization parsing error
#[error("Serialization parsing error: {0}")]
ScorexParsingError(#[from] ScorexParsingError),
/// Wrapped eval error with environment after evaluation
#[error("eval error: {error}, env: {env:?}")]
WrappedWithEnvError {
/// eval error
error: Box<EvalError>,
/// environment after evaluation
env: Env,
},
}

/// Result of expression reduction procedure (see `reduce_to_crypto`).
Expand All @@ -164,31 +172,39 @@ pub struct ReductionResult {
pub sigma_prop: SigmaBoolean,
/// estimated cost of expression evaluation
pub cost: u64,
// pub env: Env,
/// environment after the evaluation
pub env: Env,
}

/// Evaluate the given expression by reducing it to SigmaBoolean value.
pub fn reduce_to_crypto(
expr: &Expr,
env: &mut Env,
env: &Env,
ctx: Rc<Context>,
) -> Result<ReductionResult, EvalError> {
let cost_accum = CostAccumulator::new(0, None);
let mut ectx = EvalContext::new(ctx, cost_accum);
expr.eval(env, &mut ectx)
let mut env_mut = env.clone();
expr.eval(&mut env_mut, &mut ectx)
.and_then(|v| -> Result<ReductionResult, EvalError> {
match v {
Value::Boolean(b) => Ok(ReductionResult {
sigma_prop: SigmaBoolean::TrivialProp(b),
cost: 0,
env: env_mut.clone(),
}),
Value::SigmaProp(sp) => Ok(ReductionResult {
sigma_prop: sp.value().clone(),
cost: 0,
env: env_mut.clone(),
}),
_ => Err(EvalError::InvalidResultType),
}
})
.map_err(|e| EvalError::WrappedWithEnvError {
error: Box::new(e),
env: env_mut,
})
}

/// Expects SigmaProp constant value and returns it's value. Otherwise, returns an error.
Expand Down
3 changes: 1 addition & 2 deletions ergotree-interpreter/src/sigma_protocol/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,8 @@ pub trait Prover {
) -> Result<ProverResult, ProverError> {
let expr = tree.proposition()?;
let ctx_ext = ctx.extension.clone();
let mut env_mut = env.clone();
let reduction_result =
reduce_to_crypto(&expr, &mut env_mut, ctx).map_err(ProverError::EvalError)?;
reduce_to_crypto(&expr, env, ctx).map_err(ProverError::EvalError)?;

self.generate_proof(reduction_result.sigma_prop, message, hints_bag)
.map(|p| ProverResult {
Expand Down
3 changes: 1 addition & 2 deletions ergotree-interpreter/src/sigma_protocol/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ pub trait Verifier {
message: &[u8],
) -> Result<VerificationResult, VerifierError> {
let expr = tree.proposition()?;
let mut env_mut = env.clone();
let cprop = reduce_to_crypto(&expr, &mut env_mut, ctx)?.sigma_prop;
let cprop = reduce_to_crypto(&expr, env, ctx)?.sigma_prop;
let res: bool = match cprop {
SigmaBoolean::TrivialProp(b) => b,
sb => {
Expand Down
3 changes: 2 additions & 1 deletion ergotree-ir/src/mir/val_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ use crate::types::stype::SType;
use super::expr::Expr;

extern crate derive_more;
use derive_more::Display;
use derive_more::From;

use crate::has_opcode::HasStaticOpCode;
#[cfg(feature = "arbitrary")]
use proptest_derive::Arbitrary;

/// Variable id
#[derive(PartialEq, Eq, Hash, Debug, Clone, Copy, From)]
#[derive(PartialEq, Eq, Hash, Debug, Clone, Copy, From, Display)]
#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
pub struct ValId(pub u32);

Expand Down

0 comments on commit 90c0059

Please sign in to comment.