Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix executeFromVar/executeFromReg #776

Open
SethDusek opened this issue Oct 9, 2024 · 0 comments
Open

Fix executeFromVar/executeFromReg #776

SethDusek opened this issue Oct 9, 2024 · 0 comments
Assignees

Comments

@SethDusek
Copy link
Collaborator

SethDusek commented Oct 9, 2024

executeFromVar and executeFromReg execute Exprs from context vars/registers. This is implemented by replacing instances of DeserializeContext/DeserializeRegister nodes with exprs during deserialization. In sigma-rust however this is done during evaluation:

impl Evaluable for DeserializeContext {
fn eval<'ctx>(
&self,
env: &mut Env<'ctx>,
ctx: &Context<'ctx>,
) -> Result<Value<'ctx>, EvalError> {
match ctx.extension.values.get(&self.id) {
Some(c) => {
let expected_tpe = SType::SColl(SType::SByte.into());
if c.tpe != expected_tpe {
Err(EvalError::UnexpectedExpr(format!(
"DeserializeContext: expected extension value {} with id {} to have type {:?} got {:?}",
c, self.id, expected_tpe, c.tpe
)))
} else {
let bytes = c.v.clone().try_extract_into::<Vec<u8>>()?;
let expr = Expr::sigma_parse_bytes(bytes.as_slice())?;
if expr.tpe() != self.tpe {
return Err(EvalError::UnexpectedExpr(format!("DeserializeContext: expected deserialized expr from extension value {} with id {} to have type {:?}, got {:?}", c, self.id, self.tpe, expr.tpe())));
}
expr.eval(env, ctx)
}
}
None => Err(EvalError::NotFound(format!(
"DeserializeContext: no value with id {} in context extension map {}",
self.id, ctx.extension
))),
}
}
}

Which makes recursion (and stack overflows) possible:

    #[test]
    fn eval_recursive() {
        let expr: Expr = DeserializeContext {
            tpe: SType::SBoolean,
            id: 1,
        }
        .into();
        let ctx_ext = ContextExtension {
            values: [(1u8, expr.sigma_serialize_bytes().unwrap().into())]
                .iter()
                .cloned()
                .collect(),
        };
        assert!(try_eval_out_wo_ctx::<bool>(&expr, &ctx).unwrap());
    }

ExecuteFromVar's implementation should instead match sigmastate-interpreters, Reference. DeserializeContext and DeserializeRegister should also not implement Evaluable

@SethDusek SethDusek self-assigned this Oct 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants