diff --git a/tasm-lib/src/arithmetic/u128/shift_right_i128.rs b/tasm-lib/src/arithmetic/u128/shift_right_i128.rs index 31eb1626..b6d42482 100644 --- a/tasm-lib/src/arithmetic/u128/shift_right_i128.rs +++ b/tasm-lib/src/arithmetic/u128/shift_right_i128.rs @@ -1,8 +1,11 @@ use triton_vm::isa::triton_asm; +use triton_vm::prelude::LabelledInstruction; use crate::arithmetic::u32::isu32::Isu32; use crate::arithmetic::u32::shiftleft::Shiftleft; use crate::arithmetic::u32::shiftright::Shiftright; +use crate::data_type::DataType; +use crate::library::Library; use crate::prelude::BasicSnippet; /// Sign-preserving right-shift for `u128`s. @@ -29,35 +32,34 @@ use crate::prelude::BasicSnippet; /// - If preconditions are not met. pub struct ShiftRightI128; -pub const ARGUMENT_LIMB_3_NOT_U32_ERROR: i128 = 13; -pub const ARGUMENT_LIMB_2_NOT_U32_ERROR: i128 = 12; -pub const ARGUMENT_LIMB_1_NOT_U32_ERROR: i128 = 11; -pub const ARGUMENT_LIMB_0_NOT_U32_ERROR: i128 = 10; -pub const SHAMT_NOT_U32_ERROR: i128 = 14; +impl ShiftRightI128 { + pub const ARGUMENT_LIMB_3_NOT_U32_ERROR_ID: i128 = 323; + pub const ARGUMENT_LIMB_2_NOT_U32_ERROR_ID: i128 = 322; + pub const ARGUMENT_LIMB_1_NOT_U32_ERROR_ID: i128 = 321; + pub const ARGUMENT_LIMB_0_NOT_U32_ERROR_ID: i128 = 320; + pub const SHAMT_NOT_U32_ERROR_ID: i128 = 324; +} impl BasicSnippet for ShiftRightI128 { - fn inputs(&self) -> Vec<(crate::data_type::DataType, String)> { + fn inputs(&self) -> Vec<(DataType, String)> { vec![ - (crate::data_type::DataType::U128, "arg".to_string()), - (crate::data_type::DataType::U32, "shamt".to_string()), + (DataType::U128, "arg".to_string()), + (DataType::U32, "shamt".to_string()), ] } - fn outputs(&self) -> Vec<(crate::data_type::DataType, String)> { - vec![(crate::data_type::DataType::U128, "res".to_string())] + fn outputs(&self) -> Vec<(DataType, String)> { + vec![(DataType::U128, "res".to_string())] } fn entrypoint(&self) -> String { "tasmlib_arithmetic_u128_shift_right_i128".to_string() } - fn code( - &self, - library: &mut crate::prelude::Library, - ) -> Vec { - let shr_i128_by_32n = "tasmlib_arithmetic_u128_shift_right_i128_by_32n".to_string(); - let clean_up_for_early_return = - "tasmlib_arithmetic_u128_shift_right_i128_early_return".to_string(); + fn code(&self, library: &mut Library) -> Vec { + let entrypoint = self.entrypoint(); + let shr_i128_by_32n = format!("{entrypoint}_by_32n"); + let clean_up_for_early_return = format!("{entrypoint}_early_return"); let entrypoint = self.entrypoint(); let is_u32 = library.import(Box::new(Isu32)); @@ -80,13 +82,13 @@ impl BasicSnippet for ShiftRightI128 { lt // _ arg3 arg2 arg1 arg0 shamt arg3 arg2 arg1 arg0 (shamt < 128) - assert error_id {SHAMT_NOT_U32_ERROR} + assert error_id {Self::SHAMT_NOT_U32_ERROR_ID} // _ arg3 arg2 arg1 arg0 shamt arg3 arg2 arg1 arg0 - call {is_u32} assert error_id {ARGUMENT_LIMB_0_NOT_U32_ERROR} - call {is_u32} assert error_id {ARGUMENT_LIMB_1_NOT_U32_ERROR} - call {is_u32} assert error_id {ARGUMENT_LIMB_2_NOT_U32_ERROR} - call {is_u32} assert error_id {ARGUMENT_LIMB_3_NOT_U32_ERROR} + call {is_u32} assert error_id {Self::ARGUMENT_LIMB_0_NOT_U32_ERROR_ID} + call {is_u32} assert error_id {Self::ARGUMENT_LIMB_1_NOT_U32_ERROR_ID} + call {is_u32} assert error_id {Self::ARGUMENT_LIMB_2_NOT_U32_ERROR_ID} + call {is_u32} assert error_id {Self::ARGUMENT_LIMB_3_NOT_U32_ERROR_ID} // _ arg3 arg2 arg1 arg0 shamt @@ -197,8 +199,8 @@ impl BasicSnippet for ShiftRightI128 { return - // BEFORE: _ arg3 arg2 arg1 arg0 shamt top - // AFTER: _ arg3' arg2' arg1' arg0' shamt' top + // BEFORE: _ arg3 arg2 arg1 arg0 shamt top + // AFTER: _ arg3' arg2' arg1' arg0' shamt' top // where `arg >> shamt == arg' >> shamt'` and `shamt' < 32` {shr_i128_by_32n}: @@ -222,13 +224,13 @@ impl BasicSnippet for ShiftRightI128 { pick 2 pop 1 // _ top_limb arg3 arg2 arg1 shamt top - pick 1 push {-32} add place 1 + pick 1 addi -32 place 1 // _ top_limb arg3 arg2 arg1 (shamt-32) top recurse - // BEFORE _ arg3' arg2' arg1' arg0' shamt' top b - // AFTER _ arg3' arg2' arg1' arg0' b + // BEFORE: _ arg3' arg2' arg1' arg0' shamt' top b + // AFTER: _ arg3' arg2' arg1' arg0' b {clean_up_for_early_return}: place 2 pop 2 diff --git a/tasm-lib/src/assertion_error_ids.md b/tasm-lib/src/assertion_error_ids.md index a714e960..edb46fda 100644 --- a/tasm-lib/src/assertion_error_ids.md +++ b/tasm-lib/src/assertion_error_ids.md @@ -41,3 +41,4 @@ often. | 230..300 | [`StarkVerify`](verifier/stark_verify.rs) | | 300..310 | [`vm_proof_iter::New`](verifier/vm_proof_iter/new.rs) | | 310..320 | [`AddU64`](arithmetic/u64/add_u64.rs) | +| 320..330 | [`ShiftRightI128`](arithmetic/u128/shift_right_i128.rs) |