Skip to content

Commit fb4db22

Browse files
committed
use libgmp for bignum instead of num-bigint
1 parent 446b387 commit fb4db22

File tree

6 files changed

+535
-104
lines changed

6 files changed

+535
-104
lines changed

Cargo.lock

Lines changed: 11 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,12 @@ default = ["extension-module"]
2323
[dependencies]
2424
hex = "=0.4.3"
2525
lazy_static = "=1.4.0"
26-
num-bigint = "=0.4.0"
27-
num-traits = "=0.2.14"
28-
num-integer = "=0.1.44"
2926
bls12_381 = "=0.5.0"
30-
3127
sha2 = "=0.9.5"
3228

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

src/gen/conditions.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -555,8 +555,6 @@ use crate::serialize::node_to_bytes;
555555
#[cfg(test)]
556556
use hex::FromHex;
557557
#[cfg(test)]
558-
use num_traits::Num;
559-
#[cfg(test)]
560558
use std::collections::HashMap;
561559

562560
#[cfg(test)]
@@ -689,7 +687,7 @@ fn parse_list_impl(
689687
(a.new_atom(&buf).unwrap(), v.len() + 1)
690688
} else if input.starts_with("-") || "0123456789".contains(input.get(0..1).unwrap()) {
691689
let v = input.split_once(" ").unwrap().0;
692-
let num = Number::from_str_radix(v, 10).unwrap();
690+
let num = Number::from_str_radix(v, 10);
693691
(ptr_from_number(a, &num).unwrap(), v.len() + 1)
694692
} else {
695693
panic!("atom not supported \"{}\"", input);

src/more_ops.rs

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
use bls12_381::{G1Affine, G1Projective, Scalar};
2-
use num_bigint::{BigUint, Sign};
3-
use num_integer::Integer;
4-
use std::convert::TryFrom;
52
use std::ops::BitAndAssign;
63
use std::ops::BitOrAssign;
74
use std::ops::BitXorAssign;
@@ -12,7 +9,7 @@ use crate::allocator::{Allocator, NodePtr, SExp};
129
use crate::cost::{check_cost, Cost};
1310
use crate::err_utils::err;
1411
use crate::node::Node;
15-
use crate::number::{number_from_u8, ptr_from_number, Number};
12+
use crate::number::{number_from_u8, ptr_from_number, Number, Sign};
1613
use crate::op_utils::{
1714
arg_count, atom, check_arg_count, i32_atom, int_atom, two_ints, u32_from_u8,
1815
};
@@ -354,7 +351,7 @@ pub fn op_sha256(a: &mut Allocator, input: NodePtr, max_cost: Cost) -> Response
354351
pub fn op_add(a: &mut Allocator, input: NodePtr, max_cost: Cost) -> Response {
355352
let mut cost = ARITH_BASE_COST;
356353
let mut byte_count: usize = 0;
357-
let mut total: Number = 0.into();
354+
let mut total = Number::zero();
358355
for arg in Node::new(a, input) {
359356
cost += ARITH_COST_PER_ARG;
360357
check_cost(
@@ -365,7 +362,7 @@ pub fn op_add(a: &mut Allocator, input: NodePtr, max_cost: Cost) -> Response {
365362
let blob = int_atom(&arg, "+")?;
366363
let v: Number = number_from_u8(blob);
367364
byte_count += blob.len();
368-
total += v;
365+
total += &v;
369366
}
370367
let total = ptr_from_number(a, &total)?;
371368
cost += byte_count as Cost * ARITH_COST_PER_BYTE;
@@ -375,7 +372,7 @@ pub fn op_add(a: &mut Allocator, input: NodePtr, max_cost: Cost) -> Response {
375372
pub fn op_subtract(a: &mut Allocator, input: NodePtr, max_cost: Cost) -> Response {
376373
let mut cost = ARITH_BASE_COST;
377374
let mut byte_count: usize = 0;
378-
let mut total: Number = 0.into();
375+
let mut total = Number::zero();
379376
let mut is_first = true;
380377
for arg in Node::new(a, input) {
381378
cost += ARITH_COST_PER_ARG;
@@ -384,9 +381,9 @@ pub fn op_subtract(a: &mut Allocator, input: NodePtr, max_cost: Cost) -> Respons
384381
let v: Number = number_from_u8(blob);
385382
byte_count += blob.len();
386383
if is_first {
387-
total += v;
384+
total += &v;
388385
} else {
389-
total -= v;
386+
total -= &v;
390387
};
391388
is_first = false;
392389
}
@@ -434,7 +431,7 @@ pub fn op_div(a: &mut Allocator, input: NodePtr, _max_cost: Cost) -> Response {
434431

435432
// this is to preserve a buggy behavior from the initial implementation
436433
// of this operator.
437-
if q == (-1).into() && r != 0.into() {
434+
if q == -1 && r != 0 {
438435
q += 1;
439436
}
440437
let q1 = ptr_from_number(a, &q)?;
@@ -641,16 +638,14 @@ pub fn op_lsh(a: &mut Allocator, input: NodePtr, _max_cost: Cost) -> Response {
641638
check_arg_count(&args, 2, "lsh")?;
642639
let a0 = args.first()?;
643640
let b0 = int_atom(&a0, "lsh")?;
644-
let i0 = BigUint::from_bytes_be(b0);
641+
let i0 = Number::from_unsigned_bytes_be(b0);
645642
let l0 = b0.len();
646643
let rest = args.rest()?;
647644
let a1 = i32_atom(&rest.first()?, "lsh")?;
648645
if a1 > 65535 || a1 < -65535 {
649646
return args.rest()?.first()?.err("shift too large");
650647
}
651648

652-
let i0: Number = i0.into();
653-
654649
let v: Number = if a1 > 0 { i0 << a1 } else { i0 >> -a1 };
655650

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

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

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

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

@@ -804,10 +799,10 @@ pub fn op_softfork(a: &mut Allocator, input: NodePtr, max_cost: Cost) -> Respons
804799
Some((p1, _)) => {
805800
let n: Number = number_from_u8(int_atom(&p1, "softfork")?);
806801
if n.sign() == Sign::Plus {
807-
if n > Number::from(max_cost) {
802+
if n > max_cost {
808803
return err(a.null(), "cost exceeded");
809804
}
810-
let cost: Cost = TryFrom::try_from(&n).unwrap();
805+
let cost: Cost = n.into();
811806
Ok(Reduction(cost, args.null().node))
812807
} else {
813808
args.err("cost must be > 0")
@@ -824,14 +819,13 @@ lazy_static! {
824819
0xd8, 0x05, 0x53, 0xbd, 0xa4, 0x02, 0xff, 0xfe, 0x5b, 0xfe, 0xff, 0xff, 0xff, 0xff,
825820
0x00, 0x00, 0x00, 0x01,
826821
];
827-
let n = BigUint::from_bytes_be(order_as_bytes);
828-
n.into()
822+
Number::from_unsigned_bytes_be(order_as_bytes)
829823
};
830824
}
831825

832826
fn mod_group_order(n: Number) -> Number {
833-
let order = GROUP_ORDER.clone();
834-
let (_q, mut remainder) = n.div_mod_floor(&order);
827+
let order: &Number = &GROUP_ORDER;
828+
let mut remainder = n.mod_floor(order);
835829
if remainder.sign() == Sign::Minus {
836830
remainder += order;
837831
}

0 commit comments

Comments
 (0)