Skip to content

Commit

Permalink
style: Happify reviewer
Browse files Browse the repository at this point in the history
 - use `addi` instead of `push` and `add`
 - reuse `entrypoint` in subprocedure name definitions
 - use `use` statements; drop path qualifiers
 - associate const error codes

Co-authored-by: Ferdinand Sauer <[email protected]>
  • Loading branch information
aszepieniec and jan-ferdinand committed Dec 9, 2024
1 parent 26f9568 commit 05d4eb5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
56 changes: 29 additions & 27 deletions tasm-lib/src/arithmetic/u128/shift_right_i128.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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<triton_vm::prelude::LabelledInstruction> {
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<LabelledInstruction> {
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));
Expand All @@ -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


Expand Down Expand Up @@ -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}:

Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions tasm-lib/src/assertion_error_ids.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |

0 comments on commit 05d4eb5

Please sign in to comment.