1
1
use core:: str:: FromStr ;
2
2
3
- use super :: {
4
- hint_utils:: get_relocatable_from_var_name,
5
- secp:: { bigint_utils:: BigInt3 , secp_utils:: SECP_P } ,
6
- } ;
3
+ use super :: { hint_utils:: get_relocatable_from_var_name, secp:: bigint_utils:: BigInt3 } ;
7
4
use crate :: {
8
5
hint_processor:: hint_processor_definition:: HintReference ,
9
6
serde:: deserialize_program:: ApTracking ,
10
7
types:: relocatable:: MaybeRelocatable ,
8
+ utils:: CAIRO_PRIME ,
11
9
vm:: { errors:: hint_errors:: HintError , vm_core:: VirtualMachine } ,
12
10
Felt252 ,
13
11
} ;
14
12
use crate :: {
15
- stdlib:: { collections:: HashMap , ops :: Deref , prelude:: * } ,
13
+ stdlib:: { collections:: HashMap , prelude:: * } ,
16
14
types:: exec_scope:: ExecutionScopes ,
17
15
} ;
18
16
use lazy_static:: lazy_static;
19
- use num_bigint:: BigInt ;
17
+ use num_bigint:: { BigInt , BigUint } ;
20
18
use num_integer:: Integer ;
21
19
use num_traits:: FromPrimitive ;
22
- use num_traits:: Zero ;
20
+ use num_traits:: Signed ;
23
21
24
22
lazy_static ! {
25
23
static ref BLS_BASE : BigInt = BigInt :: from_u64( 2 ) . unwrap( ) . pow( 86 ) ;
@@ -50,21 +48,21 @@ pub fn write_div_mod_segment(
50
48
) -> Result < ( ) , HintError > {
51
49
let a = bls_pack (
52
50
& BigInt3 :: from_var_name ( "a" , vm, ids_data, ap_tracking) ?,
53
- & SECP_P ,
51
+ & CAIRO_PRIME ,
54
52
) ;
55
53
let b = bls_pack (
56
54
& BigInt3 :: from_var_name ( "b" , vm, ids_data, ap_tracking) ?,
57
- & SECP_P ,
55
+ & CAIRO_PRIME ,
58
56
) ;
59
57
let ( q, r) = ( a * b) . div_mod_floor ( & BLS_PRIME ) ;
60
58
let q_reloc = get_relocatable_from_var_name ( "q" , vm, ids_data, ap_tracking) ?;
61
59
let res_reloc = get_relocatable_from_var_name ( "res" , vm, ids_data, ap_tracking) ?;
62
60
63
- let q_arg: Vec < MaybeRelocatable > = bls_split ( q)
61
+ let q_arg: Vec < MaybeRelocatable > = bls_split ( q) ?
64
62
. into_iter ( )
65
63
. map ( |ref n| Felt252 :: from ( n) . into ( ) )
66
64
. collect :: < Vec < MaybeRelocatable > > ( ) ;
67
- let res_arg: Vec < MaybeRelocatable > = bls_split ( r)
65
+ let res_arg: Vec < MaybeRelocatable > = bls_split ( r) ?
68
66
. into_iter ( )
69
67
. map ( |ref n| Felt252 :: from ( n) . into ( ) )
70
68
. collect :: < Vec < MaybeRelocatable > > ( ) ;
@@ -74,35 +72,36 @@ pub fn write_div_mod_segment(
74
72
Ok ( ( ) )
75
73
}
76
74
77
- fn bls_split ( mut num : BigInt ) -> Vec < BigInt > {
78
- use num_traits:: Signed ;
79
- let mut a = Vec :: new ( ) ;
75
+ fn bls_split ( mut num : BigInt ) -> Result < Vec < BigInt > , HintError > {
76
+ let mut canonical = Vec :: new ( ) ;
80
77
for _ in 0 ..2 {
81
- let residue = & num % BLS_BASE . deref ( ) ;
82
- num /= BLS_BASE . deref ( ) ;
83
- a. push ( residue) ;
78
+ let ( new_num, residue) = num. div_rem ( & BLS_BASE ) ;
79
+ num = new_num;
80
+ canonical. push ( residue) ;
81
+ }
82
+
83
+ if num. abs ( ) >= BigInt :: from ( 1u128 << 127 ) {
84
+ return Err ( HintError :: BlsSplitError ( Box :: new ( num) ) ) ;
84
85
}
85
- assert ! ( num . abs ( ) < BigInt :: from_u128 ( 1 << 127 ) . unwrap ( ) ) ;
86
- a . push ( num) ;
87
- a
86
+
87
+ canonical . push ( num) ;
88
+ Ok ( canonical )
88
89
}
89
90
90
- fn as_int ( value : BigInt , prime : & BigInt ) -> BigInt {
91
- let half_prime = prime / 2u32 ;
92
- if value > half_prime {
93
- value - prime
94
- } else {
91
+ fn as_int ( value : BigInt , prime : & BigUint ) -> BigInt {
92
+ let half_prime: BigInt = ( prime / 2u32 ) . into ( ) ;
93
+ let prime: BigInt = prime. clone ( ) . into ( ) ;
94
+ if value < half_prime {
95
95
value
96
+ } else {
97
+ value - prime
96
98
}
97
99
}
98
100
99
- fn bls_pack ( z : & BigInt3 , prime : & BigInt ) -> BigInt {
100
- let limbs = & z. limbs ;
101
- limbs
101
+ fn bls_pack ( z : & BigInt3 , prime : & BigUint ) -> BigInt {
102
+ z. limbs
102
103
. iter ( )
103
104
. enumerate ( )
104
- . fold ( BigInt :: zero ( ) , |acc, ( i, limb) | {
105
- let limb_as_int = as_int ( limb. to_bigint ( ) , prime) ;
106
- acc + limb_as_int * & BLS_BASE . pow ( i as u32 )
107
- } )
105
+ . map ( |( i, limb) | as_int ( limb. to_bigint ( ) , prime) * & BLS_BASE . pow ( i as u32 ) )
106
+ . sum ( )
108
107
}
0 commit comments