Skip to content

Commit

Permalink
Move Context to ergotree-ir, add Expr::substitute_deserialize, fix ex…
Browse files Browse the repository at this point in the history
…ecuteFromVar/executeFromReg
  • Loading branch information
SethDusek committed Oct 25, 2024
1 parent 002ec13 commit 3e7dd5a
Show file tree
Hide file tree
Showing 66 changed files with 385 additions and 290 deletions.
6 changes: 3 additions & 3 deletions bindings/ergo-lib-c-core/src/context_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use crate::{
util::{const_ptr_as_ref, mut_ptr_as_mut},
Error,
};
use ergo_lib::ergotree_interpreter::sigma_protocol::prover;
use ergo_lib::ergotree_ir::chain::context_extension;

/// User-defined variables to be put into context
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct ContextExtension(pub prover::ContextExtension);
pub struct ContextExtension(pub context_extension::ContextExtension);
pub type ContextExtensionPtr = *mut ContextExtension;
pub type ConstContextExtensionPtr = *const ContextExtension;

Expand All @@ -17,7 +17,7 @@ pub unsafe fn context_extension_empty(
) -> Result<(), Error> {
let context_extension_out = mut_ptr_as_mut(context_extension_out, "context_extension_out")?;
*context_extension_out = Box::into_raw(Box::new(ContextExtension(
prover::ContextExtension::empty(),
context_extension::ContextExtension::empty(),
)));
Ok(())
}
Expand Down
14 changes: 5 additions & 9 deletions bindings/ergo-lib-wasm/src/context_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@ use derive_more::{From, Into};
/// User-defined variables to be put into context
#[wasm_bindgen]
#[derive(PartialEq, Eq, Debug, Clone, From, Into)]
pub struct ContextExtension(
ergo_lib::ergotree_interpreter::sigma_protocol::prover::ContextExtension,
);
pub struct ContextExtension(ergo_lib::ergotree_ir::chain::context_extension::ContextExtension);

#[wasm_bindgen]
impl ContextExtension {
/// Create new ContextExtension instance
#[wasm_bindgen(constructor)]
pub fn new() -> Self {
Self(ergo_lib::ergotree_interpreter::sigma_protocol::prover::ContextExtension::empty())
Self(ergo_lib::ergotree_ir::chain::context_extension::ContextExtension::empty())
}

/// Set the supplied pair in the ContextExtension
Expand All @@ -29,13 +27,11 @@ impl ContextExtension {

/// Returns the number of elements in the collection
pub fn len(&self) -> usize {
let wrapped: ergo_lib::ergotree_interpreter::sigma_protocol::prover::ContextExtension =
self.0.clone();
wrapped.values.len()
self.0.values.len()
}
/// get from map or fail if key is missing
pub fn get(&self, key: u8) -> Result<Constant, JsValue> {
let wrapped: ergo_lib::ergotree_interpreter::sigma_protocol::prover::ContextExtension =
let wrapped: ergo_lib::ergotree_ir::chain::context_extension::ContextExtension =
self.0.clone();
Ok(wrapped
.values
Expand All @@ -47,7 +43,7 @@ impl ContextExtension {

/// Returns all keys in the map
pub fn keys(&self) -> Vec<u8> {
let wrapped: ergo_lib::ergotree_interpreter::sigma_protocol::prover::ContextExtension =
let wrapped: ergo_lib::ergotree_ir::chain::context_extension::ContextExtension =
self.0.clone();
wrapped.values.keys().cloned().collect()
}
Expand Down
6 changes: 2 additions & 4 deletions ergo-chain-generation/src/chain_generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use std::convert::TryFrom;

use ergo_lib::ergotree_ir::chain::context_extension::ContextExtension;
use ergo_lib::{
chain::{
ergo_box::box_builder::ErgoBoxCandidateBuilder,
Expand All @@ -12,10 +13,7 @@ use ergo_lib::{
};
use ergo_lib::{
ergo_chain_types::ADDigest,
ergotree_interpreter::sigma_protocol::{
private_input::DlogProverInput,
prover::{ContextExtension, ProofBytes},
},
ergotree_interpreter::sigma_protocol::{private_input::DlogProverInput, prover::ProofBytes},
};
use ergo_lib::{
ergo_chain_types::{blake2b256_hash, AutolykosSolution, Header, Votes},
Expand Down
2 changes: 1 addition & 1 deletion ergo-lib/src/chain/json/context_extension.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ergotree_interpreter::sigma_protocol::prover::ContextExtension;
use ergotree_ir::chain::context_extension::ContextExtension;
use ergotree_ir::{mir::constant::Constant, serialization::SigmaSerializable};
use indexmap::IndexMap;
use serde::{ser::SerializeMap, Deserialize, Serialize};
Expand Down
4 changes: 2 additions & 2 deletions ergo-lib/src/chain/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ pub mod unsigned;

use bounded_vec::BoundedVec;
use ergo_chain_types::blake2b256_hash;
use ergotree_interpreter::eval::context::Context;
pub use ergotree_interpreter::eval::context::TxIoVec;
use ergotree_interpreter::eval::env::Env;
use ergotree_interpreter::eval::extract_sigma_boolean;
use ergotree_interpreter::eval::EvalError;
Expand All @@ -20,6 +18,8 @@ use ergotree_interpreter::sigma_protocol::verifier::TestVerifier;
use ergotree_interpreter::sigma_protocol::verifier::VerificationResult;
use ergotree_interpreter::sigma_protocol::verifier::Verifier;
use ergotree_interpreter::sigma_protocol::verifier::VerifierError;
use ergotree_ir::chain::context::Context;
pub use ergotree_ir::chain::context::TxIoVec;
use ergotree_ir::chain::ergo_box::BoxId;
use ergotree_ir::chain::ergo_box::ErgoBox;
use ergotree_ir::chain::ergo_box::ErgoBoxCandidate;
Expand Down
6 changes: 2 additions & 4 deletions ergo-lib/src/chain/transaction/ergo_transaction.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//! Exposes common properties for signed and unsigned transactions
use ergotree_interpreter::sigma_protocol::{
prover::ContextExtension,
verifier::{VerificationResult, VerifierError},
};
use ergotree_interpreter::sigma_protocol::verifier::{VerificationResult, VerifierError};
use ergotree_ir::chain::context_extension::ContextExtension;
use ergotree_ir::{
chain::{
ergo_box::{box_value::BoxValue, BoxId, ErgoBox},
Expand Down
2 changes: 1 addition & 1 deletion ergo-lib/src/chain/transaction/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
pub mod prover_result;

use ergotree_interpreter::sigma_protocol::prover::ContextExtension;
use ergotree_interpreter::sigma_protocol::prover::ProofBytes;
use ergotree_ir::chain::context_extension::ContextExtension;
use ergotree_ir::chain::ergo_box::BoxId;
use ergotree_ir::serialization::sigma_byte_reader::SigmaByteRead;
use ergotree_ir::serialization::sigma_byte_writer::SigmaByteWrite;
Expand Down
2 changes: 1 addition & 1 deletion ergo-lib/src/chain/transaction/input/prover_result.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Proof generated by the prover in Sigma protocol
use ergotree_interpreter::sigma_protocol::prover::ContextExtension;
use ergotree_interpreter::sigma_protocol::prover::ProofBytes;
use ergotree_ir::chain::context_extension::ContextExtension;
use ergotree_ir::serialization::sigma_byte_reader::SigmaByteRead;
use ergotree_ir::serialization::sigma_byte_writer::SigmaByteWrite;
use ergotree_ir::serialization::SigmaParsingError;
Expand Down
2 changes: 1 addition & 1 deletion ergo-lib/src/chain/transaction/input/prover_result/json.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::str::FromStr;

use crate::chain::json::context_extension::ContextExtensionSerde;
use ergotree_interpreter::sigma_protocol::prover::ContextExtension;
use ergotree_ir::chain::context_extension::ContextExtension;
use serde::ser::SerializeStruct;
use serde::Serialize;

Expand Down
9 changes: 2 additions & 7 deletions ergo-lib/src/chain/transaction/reduced.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
//! is augmented with ReducedInput which contains a script reduction result.
use ergotree_interpreter::eval::reduce_to_crypto;
use ergotree_interpreter::sigma_protocol::prover::ContextExtension;
use ergotree_interpreter::sigma_protocol::prover::ProverError;
use ergotree_ir::chain::context_extension::ContextExtension;
use ergotree_ir::serialization::sigma_byte_reader::SigmaByteRead;
use ergotree_ir::serialization::sigma_byte_writer::SigmaByteWrite;
use ergotree_ir::serialization::SigmaParsingError;
Expand Down Expand Up @@ -78,12 +78,7 @@ pub fn reduce_tx(
let input_box = tx_context
.get_input_box(&input.box_id)
.ok_or(TransactionContextError::InputBoxNotFound(idx))?;
let expr = input_box
.ergo_tree
.proposition()
.map_err(ProverError::ErgoTreeError)
.map_err(|e| TxSigningError::ProverError(e, idx))?;
let reduction_result = reduce_to_crypto(&expr, &ctx)
let reduction_result = reduce_to_crypto(&input_box.ergo_tree, &ctx)
.map_err(ProverError::EvalError)
.map_err(|e| TxSigningError::ProverError(e, idx))?;
Ok(ReducedInput {
Expand Down
3 changes: 2 additions & 1 deletion ergo-lib/src/chain/transaction/storage_rent.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use ergotree_interpreter::{eval::context::Context, sigma_protocol::prover::ProofBytes};
use ergotree_interpreter::sigma_protocol::prover::ProofBytes;
use ergotree_ir::chain::context::Context;
use ergotree_ir::chain::ergo_box::ErgoBox;
use ergotree_ir::{
chain::ergo_box::RegisterId, mir::constant::TryExtractInto, serialization::SigmaSerializable,
Expand Down
31 changes: 10 additions & 21 deletions ergo-lib/src/wallet/multi_sig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,7 @@ pub fn generate_commitments(
let input_box = tx_context
.get_input_box(&input.box_id)
.ok_or(TransactionContextError::InputBoxNotFound(i))?;
let tree = input_box.ergo_tree.clone();
let exp = tree
.proposition()
.map_err(ProverError::ErgoTreeError)
.map_err(|e| TxSigningError::ProverError(e, i))?;
let reduction_result = reduce_to_crypto(&exp, &ctx)
let reduction_result = reduce_to_crypto(&input_box.ergo_tree, &ctx)
.map_err(ProverError::EvalError)
.map_err(|e| TxSigningError::ProverError(e, i))?;

Expand All @@ -280,12 +275,7 @@ pub fn extract_hints(
let input_box = tx_context
.get_input_box(&input.box_id)
.ok_or(TransactionContextError::InputBoxNotFound(i))?;
let tree = input_box.ergo_tree.clone();
let exp = tree
.proposition()
.map_err(ProverError::ErgoTreeError)
.map_err(|e| TxSigningError::ProverError(e, i))?;
let reduction_result = reduce_to_crypto(&exp, &ctx)
let reduction_result = reduce_to_crypto(&input_box.ergo_tree, &ctx)
.map_err(ProverError::EvalError)
.map_err(|e| TxSigningError::ProverError(e, i))?;
let sigma_tree = reduction_result.sigma_prop;
Expand Down Expand Up @@ -389,7 +379,6 @@ pub fn generate_commitments_for(
mod tests {
use super::*;
use crate::chain::transaction::Transaction;
use crate::ergotree_interpreter::eval::context::Context;
use crate::ergotree_interpreter::eval::reduce_to_crypto;
use crate::ergotree_interpreter::sigma_protocol::private_input::{
DlogProverInput, PrivateInput,
Expand All @@ -406,6 +395,7 @@ mod tests {
use ergo_chain_types::Base16DecodedBytes;
use ergotree_interpreter::sigma_protocol::private_input::DhTupleProverInput;
use ergotree_interpreter::sigma_protocol::wscalar::Wscalar;
use ergotree_ir::chain::context::Context;
use ergotree_ir::mir::atleast::Atleast;
use ergotree_ir::mir::constant::{Constant, Literal};
use ergotree_ir::mir::sigma_or::SigmaOr;
Expand Down Expand Up @@ -466,8 +456,7 @@ mod tests {
let tree_m: ErgoTree = ErgoTree::sigma_parse_bytes(&bytes_m.0).unwrap();

let contx = force_any_val::<Context>();
let exp = tree_m.proposition().unwrap();
let reduction_result = reduce_to_crypto(&exp, &contx).unwrap();
let reduction_result = reduce_to_crypto(&tree_m, &contx).unwrap();
let sigma_tree = reduction_result.sigma_prop;
let stx: Transaction = serde_json::from_str(signed_tx).unwrap();
let test: ProofBytes = stx.inputs.first().clone().spending_proof.proof;
Expand Down Expand Up @@ -675,7 +664,7 @@ mod tests {
.into();
let tree_and = ErgoTree::try_from(expr.clone()).unwrap();

let cand = reduce_to_crypto(&expr, &ctx).unwrap().sigma_prop;
let cand = reduce_to_crypto(&tree_and, &ctx).unwrap().sigma_prop;
let generate_for: Vec<SigmaBoolean> = vec![SigmaBoolean::ProofOfKnowledge(
SigmaProofOfKnowledgeTree::ProveDlog(pk2),
)];
Expand Down Expand Up @@ -747,7 +736,7 @@ mod tests {

let tree_expr = ErgoTree::try_from(expr.clone()).unwrap();

let expr_reduced = reduce_to_crypto(&expr, &ctx).unwrap().sigma_prop;
let expr_reduced = reduce_to_crypto(&tree_expr, &ctx).unwrap().sigma_prop;
let mut generate_for: Vec<SigmaBoolean> = vec![SigmaBoolean::ProofOfKnowledge(
SigmaProofOfKnowledgeTree::ProveDlog(pk2),
)];
Expand Down Expand Up @@ -882,7 +871,7 @@ mod tests {
.into();
let exp: Expr = SigmaAnd::new(vec![first_expr, second_expr]).unwrap().into();
let tree = ErgoTree::try_from(exp.clone()).unwrap();
let ctree = reduce_to_crypto(&exp, &ctx).unwrap().sigma_prop;
let ctree = reduce_to_crypto(&tree, &ctx).unwrap().sigma_prop;
let mut generate_for: Vec<SigmaBoolean> = vec![SigmaBoolean::ProofOfKnowledge(
SigmaProofOfKnowledgeTree::ProveDlog(pk_alice.clone()),
)];
Expand Down Expand Up @@ -993,7 +982,7 @@ mod tests {

let tree_expr = ErgoTree::try_from(expr.clone()).unwrap();

let expr_reduced = reduce_to_crypto(&expr, &ctx).unwrap().sigma_prop;
let expr_reduced = reduce_to_crypto(&tree_expr, &ctx).unwrap().sigma_prop;
let message = vec![0u8; 100];

let hints_from_bob: HintsBag = generate_commitments_for(&expr_reduced, &[bob_pk.into()]);
Expand Down Expand Up @@ -1089,7 +1078,7 @@ mod tests {

let tree_expr = ErgoTree::try_from(expr.clone()).unwrap();

let expr_reduced = reduce_to_crypto(&expr, &ctx).unwrap().sigma_prop;
let expr_reduced = reduce_to_crypto(&tree_expr, &ctx).unwrap().sigma_prop;
let message = vec![0u8; 100];

let bob_hints: HintsBag = generate_commitments_for(&expr_reduced, &[bob_pk.into()]);
Expand Down Expand Up @@ -1262,7 +1251,7 @@ mod tests {
.into();
let expr: Expr = Atleast::new(bound, input).unwrap().into();
let tree_expr = ErgoTree::try_from(expr.clone()).unwrap();
let expr_reduced = reduce_to_crypto(&expr, &ctx).unwrap().sigma_prop;
let expr_reduced = reduce_to_crypto(&tree_expr, &ctx).unwrap().sigma_prop;
let message = vec![0u8; 100];

// only actors 1, 2, 3, 4, 5, 6, 7 are signing, others are simulated (see bag_one below)
Expand Down
6 changes: 3 additions & 3 deletions ergo-lib/src/wallet/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ use ergotree_ir::sigma_protocol::sigma_boolean::SigmaBoolean;

use crate::chain::transaction::storage_rent::check_storage_rent_conditions;
use crate::wallet::multi_sig::TransactionHintsBag;
use ergotree_interpreter::eval::context::Context;
use ergotree_interpreter::sigma_protocol::prover::ProofBytes;
use ergotree_interpreter::sigma_protocol::prover::Prover;
use ergotree_interpreter::sigma_protocol::prover::ProverError;
use ergotree_interpreter::sigma_protocol::prover::ProverResult;
use ergotree_ir::chain::context::Context;
use thiserror::Error;

pub use super::tx_context::TransactionContext;
Expand Down Expand Up @@ -260,17 +260,17 @@ pub fn sign_tx_input<'ctx>(
#[allow(clippy::unwrap_used, clippy::panic)]
mod tests {
use super::*;
use ergotree_interpreter::eval::context::TxIoVec;
use ergotree_interpreter::sigma_protocol::private_input::DlogProverInput;
use ergotree_interpreter::sigma_protocol::private_input::PrivateInput;
use ergotree_interpreter::sigma_protocol::prover::ContextExtension;
use ergotree_interpreter::sigma_protocol::prover::TestProver;
use ergotree_interpreter::sigma_protocol::verifier::verify_signature;
use ergotree_interpreter::sigma_protocol::verifier::TestVerifier;
use ergotree_interpreter::sigma_protocol::verifier::Verifier;
use ergotree_interpreter::sigma_protocol::verifier::VerifierError;
use ergotree_ir::chain::address::AddressEncoder;
use ergotree_ir::chain::address::NetworkPrefix;
use ergotree_ir::chain::context::TxIoVec;
use ergotree_ir::chain::context_extension::ContextExtension;
use ergotree_ir::chain::ergo_box::box_value::BoxValue;
use ergotree_ir::chain::ergo_box::ErgoBox;
use ergotree_ir::chain::ergo_box::NonMandatoryRegisters;
Expand Down
4 changes: 2 additions & 2 deletions ergo-lib/src/wallet/tx_builder.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Builder for an UnsignedTransaction
use ergotree_interpreter::eval::context::TxIoVec;
use ergotree_interpreter::sigma_protocol::prover::ContextExtension;
use ergotree_ir::chain::context::TxIoVec;
use ergotree_ir::chain::context_extension::ContextExtension;
use ergotree_ir::chain::token::TokenAmount;
use ergotree_ir::chain::token::TokenAmountError;
use ergotree_ir::ergo_tree::ErgoTree;
Expand Down
7 changes: 4 additions & 3 deletions ergo-lib/src/wallet/tx_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use crate::chain::ergo_state_context::ErgoStateContext;
use crate::chain::transaction::ergo_transaction::{ErgoTransaction, TxValidationError};
use crate::chain::transaction::{verify_tx_input_proof, Transaction, TransactionError};
use crate::ergotree_ir::chain::ergo_box::BoxId;
use ergotree_interpreter::eval::context::TxIoVec;
use ergotree_interpreter::sigma_protocol::verifier::VerificationResult;
use ergotree_ir::chain::context::TxIoVec;
use ergotree_ir::chain::ergo_box::box_value::BoxValue;
use ergotree_ir::chain::ergo_box::{BoxTokens, ErgoBox};
use ergotree_ir::chain::token::{TokenAmount, TokenId};
Expand Down Expand Up @@ -288,8 +288,9 @@ pub enum TransactionContextError {
mod test {
use std::collections::HashMap;

use ergotree_interpreter::eval::context::TxIoVec;
use ergotree_interpreter::sigma_protocol::prover::{ContextExtension, ProofBytes};
use ergotree_interpreter::sigma_protocol::prover::ProofBytes;
use ergotree_ir::chain::context::TxIoVec;
use ergotree_ir::chain::context_extension::ContextExtension;
use ergotree_ir::chain::ergo_box::arbitrary::ArbBoxParameters;
use ergotree_ir::chain::ergo_box::box_value::BoxValue;
use ergotree_ir::chain::ergo_box::{
Expand Down
2 changes: 2 additions & 0 deletions ergo-p2p/src/message/handshake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use crate::PeerSpec;
/// peerSpec - general (declared) information about peer
/// time - handshake time
pub struct Handshake {
/// Peer specification
pub peer_spec: PeerSpec,
/// Handshake time
pub time: SystemTime,
}
Loading

0 comments on commit 3e7dd5a

Please sign in to comment.