1
1
use bls12_381:: { G1Affine , G1Projective , Scalar } ;
2
- use num_bigint:: { BigUint , Sign } ;
3
- use num_integer:: Integer ;
4
- use std:: convert:: TryFrom ;
5
2
use std:: ops:: BitAndAssign ;
6
3
use std:: ops:: BitOrAssign ;
7
4
use std:: ops:: BitXorAssign ;
@@ -12,7 +9,7 @@ use crate::allocator::{Allocator, NodePtr, SExp};
12
9
use crate :: cost:: { check_cost, Cost } ;
13
10
use crate :: err_utils:: err;
14
11
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 } ;
16
13
use crate :: op_utils:: {
17
14
arg_count, atom, check_arg_count, i32_atom, int_atom, two_ints, u32_from_u8,
18
15
} ;
@@ -354,7 +351,7 @@ pub fn op_sha256(a: &mut Allocator, input: NodePtr, max_cost: Cost) -> Response
354
351
pub fn op_add ( a : & mut Allocator , input : NodePtr , max_cost : Cost ) -> Response {
355
352
let mut cost = ARITH_BASE_COST ;
356
353
let mut byte_count: usize = 0 ;
357
- let mut total: Number = 0 . into ( ) ;
354
+ let mut total = Number :: zero ( ) ;
358
355
for arg in Node :: new ( a, input) {
359
356
cost += ARITH_COST_PER_ARG ;
360
357
check_cost (
@@ -365,7 +362,7 @@ pub fn op_add(a: &mut Allocator, input: NodePtr, max_cost: Cost) -> Response {
365
362
let blob = int_atom ( & arg, "+" ) ?;
366
363
let v: Number = number_from_u8 ( blob) ;
367
364
byte_count += blob. len ( ) ;
368
- total += v;
365
+ total += & v;
369
366
}
370
367
let total = ptr_from_number ( a, & total) ?;
371
368
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 {
375
372
pub fn op_subtract ( a : & mut Allocator , input : NodePtr , max_cost : Cost ) -> Response {
376
373
let mut cost = ARITH_BASE_COST ;
377
374
let mut byte_count: usize = 0 ;
378
- let mut total: Number = 0 . into ( ) ;
375
+ let mut total = Number :: zero ( ) ;
379
376
let mut is_first = true ;
380
377
for arg in Node :: new ( a, input) {
381
378
cost += ARITH_COST_PER_ARG ;
@@ -384,9 +381,9 @@ pub fn op_subtract(a: &mut Allocator, input: NodePtr, max_cost: Cost) -> Respons
384
381
let v: Number = number_from_u8 ( blob) ;
385
382
byte_count += blob. len ( ) ;
386
383
if is_first {
387
- total += v;
384
+ total += & v;
388
385
} else {
389
- total -= v;
386
+ total -= & v;
390
387
} ;
391
388
is_first = false ;
392
389
}
@@ -434,7 +431,7 @@ pub fn op_div(a: &mut Allocator, input: NodePtr, _max_cost: Cost) -> Response {
434
431
435
432
// this is to preserve a buggy behavior from the initial implementation
436
433
// of this operator.
437
- if q == ( - 1 ) . into ( ) && r != 0 . into ( ) {
434
+ if q == - 1 && r != 0 {
438
435
q += 1 ;
439
436
}
440
437
let q1 = ptr_from_number ( a, & q) ?;
@@ -641,16 +638,14 @@ pub fn op_lsh(a: &mut Allocator, input: NodePtr, _max_cost: Cost) -> Response {
641
638
check_arg_count ( & args, 2 , "lsh" ) ?;
642
639
let a0 = args. first ( ) ?;
643
640
let b0 = int_atom ( & a0, "lsh" ) ?;
644
- let i0 = BigUint :: from_bytes_be ( b0) ;
641
+ let i0 = Number :: from_unsigned_bytes_be ( b0) ;
645
642
let l0 = b0. len ( ) ;
646
643
let rest = args. rest ( ) ?;
647
644
let a1 = i32_atom ( & rest. first ( ) ?, "lsh" ) ?;
648
645
if a1 > 65535 || a1 < -65535 {
649
646
return args. rest ( ) ?. first ( ) ?. err ( "shift too large" ) ;
650
647
}
651
648
652
- let i0: Number = i0. into ( ) ;
653
-
654
649
let v: Number = if a1 > 0 { i0 << a1 } else { i0 >> -a1 } ;
655
650
656
651
let l1 = limbs_for_int ( & v) ;
@@ -739,7 +734,7 @@ fn logior_op(a: &mut Number, b: &Number) {
739
734
}
740
735
741
736
pub fn op_logior ( a : & mut Allocator , input : NodePtr , max_cost : Cost ) -> Response {
742
- let v: Number = ( 0 ) . into ( ) ;
737
+ let v = Number :: zero ( ) ;
743
738
binop_reduction ( "logior" , a, v, input, max_cost, logior_op)
744
739
}
745
740
@@ -748,7 +743,7 @@ fn logxor_op(a: &mut Number, b: &Number) {
748
743
}
749
744
750
745
pub fn op_logxor ( a : & mut Allocator , input : NodePtr , max_cost : Cost ) -> Response {
751
- let v: Number = ( 0 ) . into ( ) ;
746
+ let v = Number :: zero ( ) ;
752
747
binop_reduction ( "logxor" , a, v, input, max_cost, logxor_op)
753
748
}
754
749
@@ -804,10 +799,10 @@ pub fn op_softfork(a: &mut Allocator, input: NodePtr, max_cost: Cost) -> Respons
804
799
Some ( ( p1, _) ) => {
805
800
let n: Number = number_from_u8 ( int_atom ( & p1, "softfork" ) ?) ;
806
801
if n. sign ( ) == Sign :: Plus {
807
- if n > Number :: from ( max_cost) {
802
+ if n > max_cost {
808
803
return err ( a. null ( ) , "cost exceeded" ) ;
809
804
}
810
- let cost: Cost = TryFrom :: try_from ( & n ) . unwrap ( ) ;
805
+ let cost: Cost = n . into ( ) ;
811
806
Ok ( Reduction ( cost, args. null ( ) . node ) )
812
807
} else {
813
808
args. err ( "cost must be > 0" )
@@ -824,14 +819,13 @@ lazy_static! {
824
819
0xd8 , 0x05 , 0x53 , 0xbd , 0xa4 , 0x02 , 0xff , 0xfe , 0x5b , 0xfe , 0xff , 0xff , 0xff , 0xff ,
825
820
0x00 , 0x00 , 0x00 , 0x01 ,
826
821
] ;
827
- let n = BigUint :: from_bytes_be( order_as_bytes) ;
828
- n. into( )
822
+ Number :: from_unsigned_bytes_be( order_as_bytes)
829
823
} ;
830
824
}
831
825
832
826
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) ;
835
829
if remainder. sign ( ) == Sign :: Minus {
836
830
remainder += order;
837
831
}
0 commit comments