diff --git a/src/isa/exec.rs b/src/isa/exec.rs index 114af11..b28e62d 100644 --- a/src/isa/exec.rs +++ b/src/isa/exec.rs @@ -99,8 +99,7 @@ pub trait InstructionSet: Bytecode + core::fmt::Display + core::fmt::Debug { fn dst_regs(&self) -> BTreeSet; /// Returns computational complexity of the instruction - #[inline] - fn complexity(&self) -> u64 { 1 } + fn complexity(&self) -> u64; /// Executes given instruction taking all registers as input and output. /// @@ -173,6 +172,26 @@ where } } + fn complexity(&self) -> u64 { + match self { + Instr::ControlFlow(instr) => instr.complexity(), + Instr::Put(instr) => instr.complexity(), + Instr::Move(instr) => instr.complexity(), + Instr::Cmp(instr) => instr.complexity(), + Instr::Arithmetic(instr) => instr.complexity(), + Instr::Bitwise(instr) => instr.complexity(), + Instr::Bytes(instr) => instr.complexity(), + Instr::Digest(instr) => instr.complexity(), + #[cfg(feature = "secp256k1")] + Instr::Secp256k1(instr) => instr.complexity(), + #[cfg(feature = "curve25519")] + Instr::Curve25519(instr) => instr.complexity(), + Instr::ExtensionCodes(instr) => instr.complexity(), + Instr::ReservedInstruction(instr) => instr.complexity(), + Instr::Nop => 1, + } + } + #[inline] fn exec(&self, regs: &mut CoreRegs, site: LibSite, ctx: &Self::Context<'_>) -> ExecStep { match self { @@ -413,6 +432,8 @@ impl InstructionSet for MoveOp { } } + fn complexity(&self) -> u64 { 1 } + fn exec(&self, regs: &mut CoreRegs, _: LibSite, _: &()) -> ExecStep { match self { MoveOp::MovA(reg, idx1, idx2) => { @@ -547,6 +568,8 @@ impl InstructionSet for CmpOp { } } + fn complexity(&self) -> u64 { 1 } + fn exec(&self, regs: &mut CoreRegs, _: LibSite, _: &()) -> ExecStep { match self { CmpOp::GtA(sign_flag, reg, idx1, idx2) => { @@ -888,6 +911,8 @@ impl InstructionSet for BitwiseOp { } } + fn complexity(&self) -> u64 { 1 } + fn exec(&self, regs: &mut CoreRegs, _site: LibSite, _: &()) -> ExecStep { fn shl(original: &[u8], shift: usize, n_bytes: usize) -> [u8; 1024] { let mut ret = [0u8; 1024]; @@ -1652,6 +1677,8 @@ impl InstructionSet for ReservedOp { fn dst_regs(&self) -> BTreeSet { bset![] } + fn complexity(&self) -> u64 { u64::MAX } + fn exec(&self, regs: &mut CoreRegs, site: LibSite, ctx: &()) -> ExecStep { ControlFlowOp::Fail.exec(regs, site, ctx) }