1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
use super::{ManagedBuffer, ManagedDefault, ManagedFrom, ManagedType};
use crate::{
api::{Handle, ManagedTypeApi},
types::BoxedBytes,
};
use alloc::string::String;
use elrond_codec::{
DecodeError, EncodeError, NestedDecode, NestedDecodeInput, NestedEncode, NestedEncodeOutput,
TopDecode, TopDecodeInput, TopEncode, TopEncodeOutput, TryStaticCast,
};
#[derive(Debug)]
pub struct BigUint<M: ManagedTypeApi> {
pub(crate) handle: Handle,
pub(crate) api: M,
}
impl<M: ManagedTypeApi> ManagedType<M> for BigUint<M> {
#[doc(hidden)]
fn from_raw_handle(api: M, raw_handle: Handle) -> Self {
BigUint {
handle: raw_handle,
api,
}
}
#[doc(hidden)]
fn get_raw_handle(&self) -> Handle {
self.handle
}
#[inline]
fn type_manager(&self) -> M {
self.api.clone()
}
}
impl<M: ManagedTypeApi> From<&ManagedBuffer<M>> for BigUint<M> {
fn from(item: &ManagedBuffer<M>) -> Self {
BigUint::from_bytes_be_buffer(item)
}
}
impl<M: ManagedTypeApi> From<ManagedBuffer<M>> for BigUint<M> {
fn from(item: ManagedBuffer<M>) -> Self {
BigUint::from_bytes_be_buffer(&item)
}
}
impl<M> ManagedFrom<M, u64> for BigUint<M>
where
M: ManagedTypeApi,
{
fn managed_from(api: M, value: u64) -> Self {
BigUint {
handle: api.bi_new(value as i64),
api,
}
}
}
impl<M> ManagedFrom<M, u32> for BigUint<M>
where
M: ManagedTypeApi,
{
fn managed_from(api: M, value: u32) -> Self {
BigUint {
handle: api.bi_new(value as i64),
api,
}
}
}
impl<M: ManagedTypeApi> ManagedDefault<M> for BigUint<M> {
#[inline]
fn managed_default(api: M) -> Self {
Self::zero(api)
}
}
impl<M: ManagedTypeApi> BigUint<M> {
#[inline]
pub fn zero(api: M) -> Self {
BigUint {
handle: api.bi_new_zero(),
api,
}
}
#[inline]
pub fn to_u64(&self) -> Option<u64> {
self.api.bi_to_i64(self.handle).map(|bi| bi as u64)
}
#[inline]
pub fn from_bytes_be(api: M, bytes: &[u8]) -> Self {
let handle = api.bi_new(0);
api.bi_set_unsigned_bytes(handle, bytes);
BigUint { handle, api }
}
#[inline]
pub fn to_bytes_be(&self) -> BoxedBytes {
self.api.bi_get_unsigned_bytes(self.handle)
}
#[inline]
pub fn from_bytes_be_buffer(managed_buffer: &ManagedBuffer<M>) -> Self {
BigUint {
handle: managed_buffer
.api
.mb_to_big_int_unsigned(managed_buffer.handle),
api: managed_buffer.api.clone(),
}
}
#[inline]
pub fn to_bytes_be_buffer(&self) -> ManagedBuffer<M> {
ManagedBuffer {
handle: self.api.mb_from_big_int_unsigned(self.handle),
api: self.api.clone(),
}
}
pub fn copy_to_array_big_endian_pad_right(&self, target: &mut [u8; 32]) {
let mb_handle = self.api.mb_from_big_int_unsigned(self.handle);
self.api
.mb_copy_to_slice_pad_right(mb_handle, &mut target[..]);
}
}
impl<M: ManagedTypeApi> BigUint<M> {
#[inline]
pub fn sqrt(&self) -> Self {
let handle = self.api.bi_new_zero();
self.api.bi_sqrt(handle, self.handle);
BigUint {
handle,
api: self.api.clone(),
}
}
pub fn pow(&self, exp: u32) -> Self {
let handle = self.api.bi_new_zero();
let exp_handle = self.api.bi_new(exp as i64);
self.api.bi_pow(handle, self.handle, exp_handle);
BigUint {
handle,
api: self.api.clone(),
}
}
#[inline]
pub fn log2(&self) -> u32 {
self.api.bi_log2(self.handle)
}
}
impl<M: ManagedTypeApi> Clone for BigUint<M> {
fn clone(&self) -> Self {
let clone_handle = self.api.bi_new_zero();
self.api.bi_add(clone_handle, clone_handle, self.handle);
BigUint {
handle: clone_handle,
api: self.api.clone(),
}
}
}
impl<M: ManagedTypeApi> TryStaticCast for BigUint<M> {}
impl<M: ManagedTypeApi> TopEncode for BigUint<M> {
#[inline]
fn top_encode<O: TopEncodeOutput>(&self, output: O) -> Result<(), EncodeError> {
output.set_specialized(self, |else_output| {
else_output.set_slice_u8(self.to_bytes_be().as_slice());
Ok(())
})
}
}
impl<M: ManagedTypeApi> NestedEncode for BigUint<M> {
fn dep_encode<O: NestedEncodeOutput>(&self, dest: &mut O) -> Result<(), EncodeError> {
dest.push_specialized((), self, |else_output| {
self.to_bytes_be().as_slice().dep_encode(else_output)
})
}
}
impl<M: ManagedTypeApi> NestedDecode for BigUint<M> {
fn dep_decode<I: NestedDecodeInput>(input: &mut I) -> Result<Self, DecodeError> {
input.read_specialized((), |_| Err(DecodeError::UNSUPPORTED_OPERATION))
}
fn dep_decode_or_exit<I: NestedDecodeInput, ExitCtx: Clone>(
input: &mut I,
c: ExitCtx,
exit: fn(ExitCtx, DecodeError) -> !,
) -> Self {
input.read_specialized_or_exit((), c, exit, |_, c| {
exit(c, DecodeError::UNSUPPORTED_OPERATION)
})
}
}
impl<M: ManagedTypeApi> TopDecode for BigUint<M> {
fn top_decode<I: TopDecodeInput>(input: I) -> Result<Self, DecodeError> {
input.into_specialized(|_| Err(DecodeError::UNSUPPORTED_OPERATION))
}
}
impl<M: ManagedTypeApi> crate::abi::TypeAbi for BigUint<M> {
fn type_name() -> String {
String::from("BigUint")
}
}