Skip to content

Commit

Permalink
use libgmp for bignum instead of num-bigint
Browse files Browse the repository at this point in the history
  • Loading branch information
arvidn committed Nov 15, 2021
1 parent 2441c43 commit b485f4c
Show file tree
Hide file tree
Showing 6 changed files with 535 additions and 103 deletions.
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: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ 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"
openssl = { version = "0.10.35", features = ["vendored"], optional = true }

# we just want the GMP bindings, so we disable default features
gmp-mpfr-sys = { version = "=1.4", default-features = false }

[target.'cfg(target_family="wasm")'.dependencies]
wasm-bindgen = "=0.2.75"
wasm-bindgen-test = "=0.3.25"
Expand Down
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
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 mut remainder = n.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

0 comments on commit b485f4c

Please sign in to comment.