Skip to content

Commit

Permalink
Merge pull request #4 from crochethk/various-refactorings
Browse files Browse the repository at this point in the history
Refactor; Remove unused; Add `Sub` overload
  • Loading branch information
crochethk authored Jul 3, 2024
2 parents 156b4b0 + b92da15 commit dcf2e0a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 182 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ version = "0.1.0"
edition = "2021"

[dependencies]
num-traits = "0.2"

[dev-dependencies.pyo3]
version = "0.21.2"
Expand Down
116 changes: 0 additions & 116 deletions src/bit_utils.rs

This file was deleted.

54 changes: 30 additions & 24 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
pub mod bit_utils;
pub mod utils;

pub mod mp_int {
use crate::utils::{
add_with_carry, dec_to_bit_width, div_with_rem, parse_to_digits, ParseError, TrimInPlace,
add_with_carry, dec_to_bit_width, div_with_rem, parse_to_digits, ParseError,
};
use std::{
cmp::Ordering,
Expand Down Expand Up @@ -308,10 +307,11 @@ pub mod mp_int {
result.push(self.sign.into());
}

for d in self.iter().rev() {
result += &crate::bit_utils::int_to_binary_str(*d);
result += " ";
}
const BIN_WIDTH: usize = DIGIT_BITS as usize;
result = self
.iter()
.rev()
.fold(result, |acc, d| acc + &format!("{:0width$b}", d, width = BIN_WIDTH));
result
}

Expand All @@ -329,16 +329,15 @@ pub mod mp_int {
}

pub fn to_hex_string(&self) -> String {
const X_WIDTH: usize = (DIGIT_BITS / 4) as usize;
let mut hex: String = String::new();
if self.is_negative() {
hex.push(self.sign.into());
}
const HEX_WIDTH: usize = (DIGIT_BITS / 4) as usize;
hex = self
.iter()
.rev()
.fold(hex, |acc, d| acc + &format!("{:0width$X} ", d, width = X_WIDTH));
hex.trim_end_in_place();
.fold(hex, |acc, d| acc + &format!("{:0width$X}", d, width = HEX_WIDTH));
hex
}

Expand Down Expand Up @@ -429,6 +428,14 @@ pub mod mp_int {
}
}

impl Sub for MPint {
type Output = Self;
fn sub(mut self, rhs: Self) -> Self::Output {
self -= rhs;
self
}
}

impl Sub for &MPint {
type Output = MPint;
fn sub(self, rhs: Self) -> Self::Output {
Expand Down Expand Up @@ -764,7 +771,6 @@ pub mod mp_int {
use pyo3::types::PyList;

use super::*;
use crate::utils::Op;

const D_MAX: DigitT = DigitT::MAX;

Expand All @@ -773,7 +779,7 @@ pub mod mp_int {
/// # Rules
/// - `create_op_correctness_tester($fn_name, $op)`
/// - `$fn_name` - The created function's name.
/// - `$op` - An operator token (e.g. `+`). Must have a corresponding `utils::Op`.
/// - `$op` - An operator token. Currently supports: `+`, `-`, `*`.
/// # Examples
/// ```rust
/// create_op_correctness_tester!(test_addition_correctness, +);
Expand All @@ -783,7 +789,7 @@ pub mod mp_int {
fn $fn_name(a: MPint, b: MPint) {
let result = &a $op &b;
let test_result = verify_arithmetic_result(
&a, stringify!($op).try_into().unwrap(), &b, &result);
&a, stringify!($op), &b, &result);
println!("{:?}", test_result);
assert!(test_result.0, "{}", test_result.1);
}
Expand Down Expand Up @@ -888,23 +894,23 @@ pub mod mp_int {
{
let a = mpint![0, D_MAX, 2, 3];
let expected = concat!(
"0000000000000003 ",
"0000000000000002 ",
"FFFFFFFFFFFFFFFF ",
"0000000000000003",
"0000000000000002",
"FFFFFFFFFFFFFFFF",
"0000000000000000"
);
assert_eq!(a.to_hex_string(), expected);
}
{
let a = mpint![42, 1 << 13, (1 as DigitT).rotate_right(1)];
let expected =
concat!("8000000000000000 ", "0000000000002000 ", "000000000000002A",);
concat!("8000000000000000", "0000000000002000", "000000000000002A",);
assert_eq!(a.to_hex_string(), expected);
}
{
let a = mpint![D_MAX, D_MAX, D_MAX];
let expected =
concat!("FFFFFFFFFFFFFFFF ", "FFFFFFFFFFFFFFFF ", "FFFFFFFFFFFFFFFF",);
concat!("FFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFF",);
assert_eq!(a.to_hex_string(), expected);
}
}
Expand All @@ -915,23 +921,23 @@ pub mod mp_int {
let a = -mpint![0, D_MAX, 2, 3];
let expected = concat!(
"-",
"0000000000000003 ",
"0000000000000002 ",
"FFFFFFFFFFFFFFFF ",
"0000000000000003",
"0000000000000002",
"FFFFFFFFFFFFFFFF",
"0000000000000000"
);
assert_eq!(a.to_hex_string(), expected);
}
{
let a = -mpint![42, 1 << 13, (1 as DigitT).rotate_right(1)];
let expected =
concat!("-", "8000000000000000 ", "0000000000002000 ", "000000000000002A",);
concat!("-", "8000000000000000", "0000000000002000", "000000000000002A",);
assert_eq!(a.to_hex_string(), expected);
}
{
let a = -mpint![D_MAX, D_MAX, D_MAX];
let expected =
concat!("-", "FFFFFFFFFFFFFFFF ", "FFFFFFFFFFFFFFFF ", "FFFFFFFFFFFFFFFF",);
concat!("-", "FFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFF",);
assert_eq!(a.to_hex_string(), expected);
}
}
Expand Down Expand Up @@ -1316,7 +1322,7 @@ pub mod mp_int {
/// - `res_to_verify` - The result to verify against python's calculations.
fn verify_arithmetic_result(
lhs: &MPint,
op: Op,
op: &str,
rhs: &MPint,
res_to_verify: &MPint,
) -> (bool, String) {
Expand All @@ -1340,7 +1346,7 @@ pub mod mp_int {
let fn_name = "test_operation_result";
let args: (String, &str, String, String, i32) = (
lhs.to_hex_string(),
op.into(),
op,
rhs.to_hex_string(),
res_to_verify.to_hex_string(),
16, //base of the number strings
Expand Down
41 changes: 0 additions & 41 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,6 @@ use std::f64::consts::{LOG10_2, LOG2_10};
use std::fmt::Display;
use std::ops::{Div, Rem};

pub enum Op {
PLUS,
MINUS,
MULT,
DIV,
}

impl From<Op> for &'static str {
fn from(value: Op) -> Self {
match value {
Op::PLUS => "+",
Op::MINUS => "-",
Op::MULT => "*",
Op::DIV => "/",
}
}
}

impl TryFrom<&'static str> for Op {
type Error = ParseError;
fn try_from(value: &'static str) -> Result<Self, Self::Error> {
match value {
"+" => Ok(Op::PLUS),
"-" => Ok(Op::MINUS),
"*" => Ok(Op::MULT),
"/" => Ok(Op::DIV),
_ => Err("unknown operator".into()),
}
}
}

/// Basically a full adder for `u64`
///
/// # Explanation
Expand Down Expand Up @@ -126,16 +95,6 @@ fn digit_char_to_value(ch: u8) -> Option<u8> {
}
}

pub trait TrimInPlace {
fn trim_end_in_place(&mut self);
}
impl TrimInPlace for String {
fn trim_end_in_place(&mut self) {
let new_end = self.trim_end().len();
self.truncate(new_end);
}
}

#[derive(Debug, Clone, PartialEq)]
pub struct ParseError {
msg: &'static str,
Expand Down

0 comments on commit dcf2e0a

Please sign in to comment.