1
+ extern crate alloc;
2
+
3
+ use alloc:: { string:: String , vec:: Vec } ;
1
4
use ark_ff:: { BigInt , BigInteger as ark_BigInteger, BigInteger256 } ;
2
- use ark_serialize:: { CanonicalDeserialize , CanonicalSerialize , Write } ;
5
+ use ark_serialize:: { CanonicalDeserialize , CanonicalSerialize } ;
6
+
3
7
use core:: {
4
8
cmp:: Ordering :: { Equal , Greater , Less } ,
5
9
convert:: TryInto ,
@@ -25,7 +29,7 @@ pub struct WasmBigInteger256(pub BigInteger256);
25
29
26
30
impl wasm_bindgen:: describe:: WasmDescribe for WasmBigInteger256 {
27
31
fn describe ( ) {
28
- <Vec < u8 > as wasm_bindgen:: describe:: WasmDescribe >:: describe ( )
32
+ <Vec < u8 > as wasm_bindgen:: describe:: WasmDescribe >:: describe ( ) ;
29
33
}
30
34
}
31
35
@@ -40,15 +44,14 @@ impl FromWasmAbi for WasmBigInteger256 {
40
44
41
45
impl IntoWasmAbi for WasmBigInteger256 {
42
46
type Abi = <Vec < u8 > as FromWasmAbi >:: Abi ;
47
+
43
48
fn into_abi ( self ) -> Self :: Abi {
44
- let mut bytes: Vec < u8 > = vec ! [ ] ;
45
- bytes. write_all ( self . 0 . to_bytes_le ( ) . as_slice ( ) ) . unwrap ( ) ;
46
- bytes. into_abi ( )
49
+ self . 0 . to_bytes_le ( ) . into_abi ( )
47
50
}
48
51
}
49
52
50
53
pub fn to_biguint ( x : & BigInteger256 ) -> BigUint {
51
- let x_ = x. 0 . as_ptr ( ) as * const u8 ;
54
+ let x_ = x. 0 . as_ptr ( ) . cast :: < u8 > ( ) ;
52
55
let x_ = unsafe { core:: slice:: from_raw_parts ( x_, BIGINT256_NUM_BYTES ) } ;
53
56
num_bigint:: BigUint :: from_bytes_le ( x_)
54
57
}
@@ -57,7 +60,7 @@ pub fn of_biguint(x: &BigUint) -> BigInteger256 {
57
60
let mut bytes = x. to_bytes_le ( ) ;
58
61
bytes. resize ( BIGINT256_NUM_BYTES , 0 ) ;
59
62
let limbs = bytes. as_ptr ( ) ;
60
- let limbs = limbs as * const [ u64 ; BIGINT256_NUM_LIMBS as usize ] ;
63
+ let limbs = limbs. cast :: < [ u64 ; BIGINT256_NUM_LIMBS as usize ] > ( ) ;
61
64
let limbs = unsafe { & ( * limbs) } ;
62
65
BigInt ( * limbs)
63
66
}
@@ -103,12 +106,16 @@ pub fn caml_bigint_256_compare(x: WasmBigInteger256, y: WasmBigInteger256) -> i8
103
106
}
104
107
}
105
108
106
- #[ wasm_bindgen]
109
+ // I don't think this is used anywhere
110
+ #[ cfg_attr( feature = "std" , wasm_bindgen) ]
111
+ #[ cfg( feature = "std" ) ]
107
112
pub fn caml_bigint_256_print ( x : WasmBigInteger256 ) {
108
113
println ! ( "{}" , to_biguint( & x. 0 ) ) ;
109
114
}
110
115
111
- #[ wasm_bindgen]
116
+ // I don't think this is used anywhere
117
+ #[ cfg_attr( feature = "std" , wasm_bindgen) ]
118
+ #[ cfg( feature = "std" ) ]
112
119
pub fn caml_bigint_256_to_string ( x : WasmBigInteger256 ) -> String {
113
120
to_biguint ( & x. 0 ) . to_string ( )
114
121
}
@@ -123,7 +130,9 @@ pub fn caml_bigint_256_test_bit(x: WasmBigInteger256, i: i32) -> bool {
123
130
124
131
#[ wasm_bindgen]
125
132
pub fn caml_bigint_256_to_bytes ( x : WasmBigInteger256 ) -> Vec < u8 > {
126
- let mut serialized_bytes = vec ! [ ] ;
133
+ // This is the size of the implementation of BigInteger256 at the time of writing
134
+ // but even if it changes, we will be no worse off than the original code `vec![]`
135
+ let mut serialized_bytes = Vec :: with_capacity ( core:: mem:: size_of :: < WasmBigInteger256 > ( ) ) ;
127
136
x. 0 . serialize_compressed ( & mut serialized_bytes)
128
137
. expect ( "serialize failed" ) ;
129
138
serialized_bytes
@@ -132,9 +141,7 @@ pub fn caml_bigint_256_to_bytes(x: WasmBigInteger256) -> Vec<u8> {
132
141
#[ wasm_bindgen]
133
142
pub fn caml_bigint_256_of_bytes ( x : & [ u8 ] ) -> WasmBigInteger256 {
134
143
let len = core:: mem:: size_of :: < WasmBigInteger256 > ( ) ;
135
- if x. len ( ) != len {
136
- panic ! ( "caml_bigint_256_of_bytes" ) ;
137
- } ;
144
+ assert ! ( x. len( ) == len, "caml_bigint_256_of_bytes" ) ;
138
145
// TODO this used FromBytes before arkworks 0.4.2, check serialization is consistent after update
139
146
WasmBigInteger256 (
140
147
BigInteger256 :: deserialize_compressed ( & mut & x[ ..] ) . expect ( "deserialization error" ) ,
0 commit comments