From 7f8b41dedb656eb5ba7fae40d79cf28ac0a2daaa Mon Sep 17 00:00:00 2001 From: clint Date: Fri, 13 Mar 2026 15:58:32 +0800 Subject: [PATCH] fix(vm): optimize constant folding for multiplication by zero --- psy_vm/src/dpn/ops/exec_context.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/psy_vm/src/dpn/ops/exec_context.rs b/psy_vm/src/dpn/ops/exec_context.rs index 382072164..d13009981 100644 --- a/psy_vm/src/dpn/ops/exec_context.rs +++ b/psy_vm/src/dpn/ops/exec_context.rs @@ -228,13 +228,17 @@ impl QExecContext { let b_val = b.get_constant_value(); return self.op_const(op_type.eval_binary_constant(a_val, b_val)); } + let is_const_zero = |v: SymFeltRef| { + matches!( + v.get_op_type(), + DPNOpType::Constant | DPNOpType::ConstantTrue | DPNOpType::ConstantFalse + ) && v.get_constant_value() == 0 + }; if (op_type == DPNOpType::Add || op_type == DPNOpType::Sub) && b_type == DPNOpType::Constant && b.get_constant_value() == 0 { return a; } - if op_type == DPNOpType::Mul - && (a_type == DPNOpType::Constant && a.get_constant_value() == 0 || b_type == DPNOpType::Constant && b.get_constant_value() == 0) - { - return a; + if op_type == DPNOpType::Mul && (is_const_zero(a) || is_const_zero(b)) { + return self.op_const(0); } let value = SymFeltRefValue { op_type,