ergoplatform / sigma-rust

Rust implementation of ErgoTree interpreter and wallet-related features
Creative Commons Zero v1.0 Universal
69 stars 48 forks source link

Reuse make_context in signing/validation #738

Closed SethDusek closed 4 months ago

SethDusek commented 7 months ago

Currently tx_input_proof/verify_tx_input_proof remake make_context for each input:

pub fn sign_tx_input(
    prover: &dyn Prover,
    tx_context: &TransactionContext<UnsignedTransaction>,
    state_context: &ErgoStateContext,
    tx_hints: Option<&TransactionHintsBag>,
    input_idx: usize,
    message_to_sign: &[u8],
) -> Result<Input, TxSigningError> {
    let unsigned_input = tx_context
        .spending_tx
        .inputs
        .get(input_idx)
        .ok_or(TransactionContextError::InputBoxNotFound(input_idx))?;
    let input_box = tx_context
        .get_input_box(&unsigned_input.box_id)
        .ok_or(TransactionContextError::InputBoxNotFound(input_idx))?;
    let ctx = Rc::new(make_context(state_context, tx_context, input_idx)?);

make_context ends up being very expensive for transactions with multiple inputs. Instead pass a reference to sign_tx_input/verify_tx_input_proof, and only modify the self box/contextextension for each input instead of recreating context