Skip to content

Commit ea0e742

Browse files
committed
wip
1 parent d097896 commit ea0e742

File tree

11 files changed

+880
-756
lines changed

11 files changed

+880
-756
lines changed

.github/workflows/build-crate-and-npm.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ jobs:
5858
run: cargo install wasm-pack
5959

6060
- name: wasm-pack build and pack
61-
run: wasm-pack build && wasm-pack pack
61+
run: wasm-pack build --features=num-bigint && wasm-pack pack
6262

6363
- name: Upload npm pkg artifacts
6464
uses: actions/upload-artifact@v2

Cargo.lock

Lines changed: 33 additions & 0 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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ default = ["extension-module", "libgmp"]
2727
[dependencies]
2828
hex = "=0.4.3"
2929
lazy_static = "=1.4.0"
30+
num-bigint = { version = "0.4.0", optional = true }
31+
num-traits = "=0.2.14"
32+
num-integer = "=0.1.44"
3033
bls12_381 = "=0.5.0"
3134
sha2 = "=0.9.5"
3235
openssl = { version = "0.10.35", features = ["vendored"], optional = true }

src/gen/conditions.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,9 @@ fn test_coin_id(parent_id: &[u8], puzzle_hash: &[u8], amount: u64) -> [u8; 32] {
692692
// (1 (2 (3 ) means: (1 . (2 . (3 . ())))
693693
// and:
694694

695+
#[cfg(test)]
696+
use crate::number_traits::TestNumberTraits;
697+
695698
#[cfg(test)]
696699
fn parse_list_impl(
697700
a: &mut Allocator,

src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,17 @@ pub mod cost;
55
mod dialect;
66
mod err_utils;
77
mod gen;
8-
mod gmp_ffi;
98
pub mod more_ops;
109
pub mod node;
10+
11+
#[cfg(not(feature = "num-bigint"))]
12+
mod gmp_ffi;
13+
#[cfg(not(feature = "num-bigint"))]
14+
mod number_gmp;
15+
1116
mod number;
17+
mod number_traits;
18+
1219
mod op_utils;
1320
#[cfg(not(any(test, target_family = "wasm")))]
1421
mod py;

src/more_ops.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@ use crate::allocator::{Allocator, NodePtr, SExp};
99
use crate::cost::{check_cost, Cost};
1010
use crate::err_utils::err;
1111
use crate::node::Node;
12-
use crate::number::{number_from_u8, ptr_from_number, Number, Sign};
12+
use crate::number::{ptr_from_number, Number, Sign};
1313
use crate::op_utils::{
1414
arg_count, atom, check_arg_count, i32_atom, int_atom, two_ints, u32_from_u8,
1515
};
1616
use crate::reduction::{Reduction, Response};
1717
use crate::serialize::node_to_bytes;
1818
use crate::sha2::Sha256;
1919

20+
use crate::number_traits::NumberTraits;
21+
2022
// We ascribe some additional cost per byte for operations that allocate new atoms
2123
const MALLOC_COST_PER_BYTE: Cost = 10;
2224

@@ -360,7 +362,7 @@ pub fn op_add(a: &mut Allocator, input: NodePtr, max_cost: Cost) -> Response {
360362
max_cost,
361363
)?;
362364
let blob = int_atom(&arg, "+")?;
363-
let v: Number = number_from_u8(blob);
365+
let v: Number = Number::from_u8(blob);
364366
byte_count += blob.len();
365367
total += &v;
366368
}
@@ -378,7 +380,7 @@ pub fn op_subtract(a: &mut Allocator, input: NodePtr, max_cost: Cost) -> Respons
378380
cost += ARITH_COST_PER_ARG;
379381
check_cost(a, cost + byte_count as Cost * ARITH_COST_PER_BYTE, max_cost)?;
380382
let blob = int_atom(&arg, "-")?;
381-
let v: Number = number_from_u8(blob);
383+
let v: Number = Number::from_u8(blob);
382384
byte_count += blob.len();
383385
if is_first {
384386
total += &v;
@@ -402,13 +404,13 @@ pub fn op_multiply(a: &mut Allocator, input: NodePtr, max_cost: Cost) -> Respons
402404
let blob = int_atom(&arg, "*")?;
403405
if first_iter {
404406
l0 = blob.len();
405-
total = number_from_u8(blob);
407+
total = Number::from_u8(blob);
406408
first_iter = false;
407409
continue;
408410
}
409411
let l1 = blob.len();
410412

411-
total *= number_from_u8(blob);
413+
total *= Number::from_u8(blob);
412414
cost += MUL_COST_PER_OP;
413415

414416
cost += (l0 + l1) as Cost * MUL_LINEAR_COST_PER_BYTE;
@@ -477,7 +479,7 @@ pub fn op_gr(a: &mut Allocator, input: NodePtr, _max_cost: Cost) -> Response {
477479
let cost = GR_BASE_COST + (v0.len() + v1.len()) as Cost * GR_COST_PER_BYTE;
478480
Ok(Reduction(
479481
cost,
480-
if number_from_u8(v0) > number_from_u8(v1) {
482+
if Number::from_u8(v0) > Number::from_u8(v1) {
481483
a.one()
482484
} else {
483485
a.null()
@@ -567,7 +569,7 @@ pub fn op_ash(a: &mut Allocator, input: NodePtr, _max_cost: Cost) -> Response {
567569
check_arg_count(&args, 2, "ash")?;
568570
let a0 = args.first()?;
569571
let b0 = int_atom(&a0, "ash")?;
570-
let i0 = number_from_u8(b0);
572+
let i0 = Number::from_u8(b0);
571573
let l0 = b0.len();
572574
let rest = args.rest()?;
573575
let a1 = i32_atom(&rest.first()?, "ash")?;
@@ -715,7 +717,7 @@ fn binop_reduction(
715717
let mut cost = LOG_BASE_COST;
716718
for arg in Node::new(a, input) {
717719
let blob = int_atom(&arg, op_name)?;
718-
let n0 = number_from_u8(blob);
720+
let n0 = Number::from_u8(blob);
719721
op_f(&mut total, &n0);
720722
arg_size += blob.len();
721723
cost += LOG_COST_PER_ARG;
@@ -758,7 +760,7 @@ pub fn op_lognot(a: &mut Allocator, input: NodePtr, _max_cost: Cost) -> Response
758760
check_arg_count(&args, 1, "lognot")?;
759761
let a0 = args.first()?;
760762
let v0 = int_atom(&a0, "lognot")?;
761-
let mut n: Number = number_from_u8(v0);
763+
let mut n: Number = Number::from_u8(v0);
762764
n = !n;
763765
let cost = LOGNOT_BASE_COST + ((v0.len() as Cost) * LOGNOT_COST_PER_BYTE);
764766
let r = ptr_from_number(a, &n)?;
@@ -803,12 +805,12 @@ pub fn op_softfork(a: &mut Allocator, input: NodePtr, max_cost: Cost) -> Respons
803805
let args = Node::new(a, input);
804806
match args.pair() {
805807
Some((p1, _)) => {
806-
let n: Number = number_from_u8(int_atom(&p1, "softfork")?);
808+
let n: Number = Number::from_u8(int_atom(&p1, "softfork")?);
807809
if n.sign() == Sign::Plus {
808810
if n > max_cost {
809811
return err(a.null(), "cost exceeded");
810812
}
811-
let cost: Cost = n.into();
813+
let cost: Cost = n.to_u64();
812814
Ok(Reduction(cost, args.null().node))
813815
} else {
814816
args.err("cost must be > 0")
@@ -856,7 +858,7 @@ pub fn op_pubkey_for_exp(a: &mut Allocator, input: NodePtr, _max_cost: Cost) ->
856858
let a0 = args.first()?;
857859

858860
let v0 = int_atom(&a0, "pubkey_for_exp")?;
859-
let exp: Number = mod_group_order(number_from_u8(v0));
861+
let exp: Number = mod_group_order(Number::from_u8(v0));
860862
let cost = PUBKEY_BASE_COST + (v0.len() as Cost) * PUBKEY_COST_PER_BYTE;
861863
let exp: Scalar = number_to_scalar(exp);
862864
let point: G1Projective = G1Affine::generator() * exp;

0 commit comments

Comments
 (0)