@@ -9,14 +9,16 @@ use crate::allocator::{Allocator, NodePtr, SExp};
99use crate :: cost:: { check_cost, Cost } ;
1010use crate :: err_utils:: err;
1111use crate :: node:: Node ;
12- use crate :: number:: { number_from_u8 , ptr_from_number, Number , Sign } ;
12+ use crate :: number:: { ptr_from_number, Number , Sign } ;
1313use crate :: op_utils:: {
1414 arg_count, atom, check_arg_count, i32_atom, int_atom, two_ints, u32_from_u8,
1515} ;
1616use crate :: reduction:: { Reduction , Response } ;
1717use crate :: serialize:: node_to_bytes;
1818use 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
2123const 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