Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor refactor in to_signed_bytes_be() #155

Open
wants to merge 4 commits into
base: libgmp-transition
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ jobs:
git clone https://github.com/Chia-Network/clvm_tools.git --branch=main --single-branch
python -m pip install ./clvm_tools
python -m pip install colorama
git clone https://github.com/Chia-Network/mpir_gc_x64.git --depth 1
maturin develop --release

- name: Run benchmarks (Windows)
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ jobs:
python -m venv venv
. .\venv\Scripts\Activate.ps1
ln -s venv\Scripts\Activate.ps1 activate
git clone https://github.com/Chia-Network/mpir_gc_x64.git --depth 1
maturin build --no-sdist -i python --release --strip
# this will install into the venv
# it'd be better to use the wheel, but I can't figure out how to do that
Expand Down
44 changes: 11 additions & 33 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@ default = ["extension-module"]
[dependencies]
hex = "=0.4.3"
lazy_static = "=1.4.0"
num-bigint = "=0.4.0"
num-traits = "=0.2.14"
num-integer = "=0.1.44"
bls12_381 = "=0.5.0"

sha2 = "=0.9.5"

[target.'cfg(unix)'.dependencies]
# we just want the GMP bindings, so we disable default features
gmp-mpfr-sys = { version = "=1.4", default-features = false }
openssl = { version = "0.10.35", features = ["vendored"] }

[target.'cfg(target_family="wasm")'.dependencies]
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ Use `maturin` to build the python interface. First, install into current virtual
$ pip install maturin
```

As we need `MPIR` for MSVC builds, prepare this dependency with

```
$ git clone https://github.com/Chia-Network/mpir_gc_x64.git --depth 1
```

Build `clvm_rs` directly into the current virtualenv with

```
Expand Down
7 changes: 7 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn main() {
#[cfg(windows)]
{
println!("cargo:rustc-link-lib=mpir");
println!("cargo:rustc-link-search=mpir_gc_x64");
}
}
4 changes: 1 addition & 3 deletions src/gen/conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,8 +555,6 @@ use crate::serialize::node_to_bytes;
#[cfg(test)]
use hex::FromHex;
#[cfg(test)]
use num_traits::Num;
#[cfg(test)]
use std::collections::HashMap;

#[cfg(test)]
Expand Down Expand Up @@ -689,7 +687,7 @@ fn parse_list_impl(
(a.new_atom(&buf).unwrap(), v.len() + 1)
} else if input.starts_with("-") || "0123456789".contains(input.get(0..1).unwrap()) {
let v = input.split_once(" ").unwrap().0;
let num = Number::from_str_radix(v, 10).unwrap();
let num = Number::from_str_radix(v, 10);
(ptr_from_number(a, &num).unwrap(), v.len() + 1)
} else {
panic!("atom not supported \"{}\"", input);
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ pub mod f_table;
mod gen;
mod int_to_bytes;
pub mod more_ops;
#[cfg(windows)]
mod mpir_msvc;
pub mod node;
mod number;
mod op_utils;
Expand Down
36 changes: 15 additions & 21 deletions src/more_ops.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use bls12_381::{G1Affine, G1Projective, Scalar};
use num_bigint::{BigUint, Sign};
use num_integer::Integer;
use std::convert::TryFrom;
use std::ops::BitAndAssign;
use std::ops::BitOrAssign;
use std::ops::BitXorAssign;
Expand All @@ -12,7 +9,7 @@ use crate::allocator::{Allocator, NodePtr, SExp};
use crate::cost::{check_cost, Cost};
use crate::err_utils::err;
use crate::node::Node;
use crate::number::{number_from_u8, ptr_from_number, Number};
use crate::number::{number_from_u8, ptr_from_number, Number, Sign};
use crate::op_utils::{
arg_count, atom, check_arg_count, i32_atom, int_atom, two_ints, u32_from_u8,
};
Expand Down Expand Up @@ -354,7 +351,7 @@ pub fn op_sha256(a: &mut Allocator, input: NodePtr, max_cost: Cost) -> Response
pub fn op_add(a: &mut Allocator, input: NodePtr, max_cost: Cost) -> Response {
let mut cost = ARITH_BASE_COST;
let mut byte_count: usize = 0;
let mut total: Number = 0.into();
let mut total = Number::zero();
for arg in Node::new(a, input) {
cost += ARITH_COST_PER_ARG;
check_cost(
Expand All @@ -365,7 +362,7 @@ pub fn op_add(a: &mut Allocator, input: NodePtr, max_cost: Cost) -> Response {
let blob = int_atom(&arg, "+")?;
let v: Number = number_from_u8(blob);
byte_count += blob.len();
total += v;
total += &v;
}
let total = ptr_from_number(a, &total)?;
cost += byte_count as Cost * ARITH_COST_PER_BYTE;
Expand All @@ -375,7 +372,7 @@ pub fn op_add(a: &mut Allocator, input: NodePtr, max_cost: Cost) -> Response {
pub fn op_subtract(a: &mut Allocator, input: NodePtr, max_cost: Cost) -> Response {
let mut cost = ARITH_BASE_COST;
let mut byte_count: usize = 0;
let mut total: Number = 0.into();
let mut total = Number::zero();
let mut is_first = true;
for arg in Node::new(a, input) {
cost += ARITH_COST_PER_ARG;
Expand All @@ -384,9 +381,9 @@ pub fn op_subtract(a: &mut Allocator, input: NodePtr, max_cost: Cost) -> Respons
let v: Number = number_from_u8(blob);
byte_count += blob.len();
if is_first {
total += v;
total += &v;
} else {
total -= v;
total -= &v;
};
is_first = false;
}
Expand Down Expand Up @@ -434,7 +431,7 @@ pub fn op_div(a: &mut Allocator, input: NodePtr, _max_cost: Cost) -> Response {

// this is to preserve a buggy behavior from the initial implementation
// of this operator.
if q == (-1).into() && r != 0.into() {
if q == -1 && r != 0 {
q += 1;
}
let q1 = ptr_from_number(a, &q)?;
Expand Down Expand Up @@ -641,16 +638,14 @@ pub fn op_lsh(a: &mut Allocator, input: NodePtr, _max_cost: Cost) -> Response {
check_arg_count(&args, 2, "lsh")?;
let a0 = args.first()?;
let b0 = int_atom(&a0, "lsh")?;
let i0 = BigUint::from_bytes_be(b0);
let i0 = Number::from_unsigned_bytes_be(b0);
let l0 = b0.len();
let rest = args.rest()?;
let a1 = i32_atom(&rest.first()?, "lsh")?;
if a1 > 65535 || a1 < -65535 {
return args.rest()?.first()?.err("shift too large");
}

let i0: Number = i0.into();

let v: Number = if a1 > 0 { i0 << a1 } else { i0 >> -a1 };

let l1 = limbs_for_int(&v);
Expand Down Expand Up @@ -739,7 +734,7 @@ fn logior_op(a: &mut Number, b: &Number) {
}

pub fn op_logior(a: &mut Allocator, input: NodePtr, max_cost: Cost) -> Response {
let v: Number = (0).into();
let v = Number::zero();
binop_reduction("logior", a, v, input, max_cost, logior_op)
}

Expand All @@ -748,7 +743,7 @@ fn logxor_op(a: &mut Number, b: &Number) {
}

pub fn op_logxor(a: &mut Allocator, input: NodePtr, max_cost: Cost) -> Response {
let v: Number = (0).into();
let v = Number::zero();
binop_reduction("logxor", a, v, input, max_cost, logxor_op)
}

Expand Down Expand Up @@ -804,10 +799,10 @@ pub fn op_softfork(a: &mut Allocator, input: NodePtr, max_cost: Cost) -> Respons
Some((p1, _)) => {
let n: Number = number_from_u8(int_atom(&p1, "softfork")?);
if n.sign() == Sign::Plus {
if n > Number::from(max_cost) {
if n > max_cost {
return err(a.null(), "cost exceeded");
}
let cost: Cost = TryFrom::try_from(&n).unwrap();
let cost: Cost = n.into();
Ok(Reduction(cost, args.null().node))
} else {
args.err("cost must be > 0")
Expand All @@ -824,14 +819,13 @@ lazy_static! {
0xd8, 0x05, 0x53, 0xbd, 0xa4, 0x02, 0xff, 0xfe, 0x5b, 0xfe, 0xff, 0xff, 0xff, 0xff,
0x00, 0x00, 0x00, 0x01,
];
let n = BigUint::from_bytes_be(order_as_bytes);
n.into()
Number::from_unsigned_bytes_be(order_as_bytes)
};
}

fn mod_group_order(n: Number) -> Number {
let order = GROUP_ORDER.clone();
let (_q, mut remainder) = n.div_mod_floor(&order);
let order: &Number = &GROUP_ORDER;
let mut remainder = n.mod_floor(order);
if remainder.sign() == Sign::Minus {
remainder += order;
}
Expand Down
Loading