debridge-u256 1.0.1

zkp_u256 crate wrapper for debridge needs
Documentation
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
use std::{
    fmt, io,
    ops::{BitOr, Deref},
};

use borsh::{BorshDeserialize, BorshSerialize};
use num_traits::cast::ToPrimitive;
use serde::{Deserialize, Serialize};
pub use zkp_u256::{Binary, DivRem};

#[derive(Clone, Default, PartialEq, Eq, Deserialize, Serialize, Debug, Hash)]
pub struct U256(zkp_u256::U256);

impl fmt::Display for U256 {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.to_hex())?;
        Ok(())
    }
}

impl BorshSerialize for U256 {
    fn serialize<W: io::Write>(&self, writer: &mut W) -> io::Result<()> {
        <[u8; 32] as BorshSerialize>::serialize(&self.to_bytes(), writer)?;
        Ok(())
    }
}

impl BorshDeserialize for U256 {
    fn deserialize_reader<R: io::Read>(reader: &mut R) -> io::Result<Self> {
        Ok(Self::from(
            &<[u8; 32] as BorshDeserialize>::deserialize_reader(reader)?,
        ))
    }
}

#[cfg(feature = "jsonschema")]
impl schemars::JsonSchema for U256 {
    fn schema_name() -> String {
        "U256".to_owned()
    }

    fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
        let mut schema = <String>::json_schema(gen);
        if let schemars::schema::Schema::Object(obj) = &mut schema {
            obj.string = Some(Box::new(schemars::schema::StringValidation {
                max_length: Some(66),
                min_length: Some(64),
                pattern: Some("^(0x)?[a-fA-F0-9]{64}$".to_owned()),
            }));
        }

        schema
    }
}

impl U256 {
    pub const ZERO: U256 = Self(zkp_u256::U256::ZERO);
    pub const ONE: U256 = Self(zkp_u256::U256::ONE);
    pub const MAX: U256 = Self(zkp_u256::U256::MAX);

    pub const fn from_u64(value: u64) -> U256 {
        U256(zkp_u256::U256::from_limbs([value, 0, 0, 0]))
    }
    pub fn from_decimal_str(input: &str) -> Self {
        U256(zkp_u256::U256::from_decimal_str(input).unwrap())
    }

    #[cfg(feature = "rand")]
    pub fn random<R: rand::Rng>(rng: &mut R) -> Self {
        Self(zkp_u256::U256::from_limbs([
            rng.next_u64(),
            rng.next_u64(),
            rng.next_u64(),
            rng.next_u64(),
        ]))
    }

    #[cfg(feature = "rand")]
    pub fn unique() -> Self {
        Self::random(&mut rand::thread_rng())
    }

    pub fn to_array(&self) -> [u8; 32] {
        self.0.to_bytes_be()
    }

    pub fn to_decimals_string(&self) -> String {
        self.0.to_decimal_string()
    }

    pub fn to_bytes(&self) -> [u8; 32] {
        self.to_array()
    }

    pub fn to_vec(&self) -> Vec<u8> {
        self.to_array().to_vec()
    }

    pub const fn as_limbs(&self) -> &[u64; 4] {
        self.0.as_limbs()
    }

    pub fn to_u64(&self) -> Option<u64> {
        self.0.to_u64()
    }

    pub fn normalize(self, denominator: Denominator) -> u64 {
        self.div_rem(&Self::from_u64(10u64).pow(denominator.get() as u64))
            .and_then(|(part, _fractal)| part.to_u64())
            .expect("Unreachable. `10 ** denominator != zero`")
    }

    pub fn checked_normalize(self, denominator: Denominator) -> Option<u64> {
        self.div_rem(&Self::from_u64(10u64).pow(denominator.get() as u64))
            .and_then(|(part, fractal)| {
                if fractal.is_zero() {
                    part.to_u64()
                } else {
                    None
                }
            })
    }

    pub fn to_hex(&self) -> String {
        self.0.to_hex_string()
    }

    pub fn pow(&self, exp: u64) -> U256 {
        Self(self.0.pow(exp))
    }

    pub fn from_hex(hex: &str) -> U256 {
        Self(zkp_u256::U256::from_hex_str(hex))
    }

    pub fn bit(&self, i: usize) -> bool {
        self.0.bit(i)
    }
}

impl BitOr for U256 {
    type Output = Self;
    fn bitor(self, rhs: Self) -> Self::Output {
        Self(self.0.bitor(&rhs.0))
    }
}

impl From<&[u8; 32]> for U256 {
    fn from(bytes: &[u8; 32]) -> Self {
        Self(zkp_u256::U256::from_bytes_be(bytes))
    }
}

impl From<[u8; 32]> for U256 {
    fn from(bytes: [u8; 32]) -> Self {
        Self(zkp_u256::U256::from_bytes_be(&bytes))
    }
}

impl From<[u64; 4]> for U256 {
    fn from(bytes: [u64; 4]) -> Self {
        Self(zkp_u256::U256::from_limbs(bytes))
    }
}

impl From<U256> for [u64; 4] {
    fn from(val: U256) -> Self {
        *val.as_limbs()
    }
}

impl From<U256> for [u8; 32] {
    fn from(val: U256) -> Self {
        val.to_bytes()
    }
}

impl<'link> From<&'link U256> for &'link [u64; 4] {
    fn from(val: &'link U256) -> Self {
        val.as_limbs()
    }
}

use std::convert::TryFrom;
impl TryFrom<&[u8]> for U256 {
    type Error = &'static str;
    fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
        Ok(Self::from(
            &<[u8; 32]>::try_from(bytes).map_err(|_| "Wrong size for `U256`")?,
        ))
    }
}

fn bytes_to_limbs(bytes: &[u8]) -> Result<[u64; 4], &'static str> {
    if bytes.len() > 32 {
        return Err("bytes slice too long");
    }
    let mut result = [0u64; 4];
    let mut iter = bytes.iter().rev();
    for i in 0..4 {
        for j in 0..8 {
            if let Some(byte) = iter.next() {
                result[i] |= u64::from(*byte) << (8 * j);
            } else {
                return Ok(result);
            }
        }
    }
    Ok(result)
}

impl TryFrom<&str> for U256 {
    type Error = &'static str;
    fn try_from(number: &str) -> Result<Self, Self::Error> {
        Ok(U256::from(bytes_to_limbs(
            &hex::decode(number).map_err(|_| "Error while decode hex")?,
        )?))
    }
}

use std::ops::Add;
impl Add for U256 {
    type Output = U256;
    fn add(self, rhs: Self) -> Self::Output {
        Self(self.0.add(rhs.0))
    }
}

use std::ops::Mul;

impl Mul<u64> for U256 {
    type Output = U256;
    fn mul(self, rhs: u64) -> Self::Output {
        Self(self.0.mul(rhs))
    }
}
impl Mul<u64> for &U256 {
    type Output = U256;
    fn mul(self, rhs: u64) -> Self::Output {
        U256((&self.0).mul(rhs))
    }
}
impl Mul<&U256> for U256 {
    type Output = U256;
    fn mul(self, rhs: &U256) -> Self::Output {
        Self(self.0.mul(&rhs.0))
    }
}

use num_traits::pow::Pow;
impl Pow<u64> for U256 {
    type Output = U256;
    fn pow(self, rhs: u64) -> Self::Output {
        Self(self.0.pow(rhs))
    }
}
use num_traits::Zero;
impl Zero for U256 {
    fn zero() -> Self {
        Self::ZERO
    }
    fn is_zero(&self) -> bool {
        self.0.is_zero()
    }
}

impl DivRem<u64> for U256 {
    type Quotient = U256;
    type Remainder = u64;
    fn div_rem(&self, rhs: u64) -> Option<(Self::Quotient, Self::Remainder)> {
        self.0.div_rem(rhs).map(|(q, r)| (U256(q), r))
    }
}

impl DivRem<&U256> for U256 {
    type Quotient = U256;
    type Remainder = U256;
    fn div_rem(&self, rhs: &U256) -> Option<(Self::Quotient, Self::Remainder)> {
        self.0.div_rem(&rhs.0).map(|(q, r)| (U256(q), U256(r)))
    }
}

use std::ops::Sub;

impl Sub for U256 {
    type Output = U256;
    fn sub(self, rhs: Self) -> Self::Output {
        Self(self.0.sub(rhs.0))
    }
}

impl From<u128> for U256 {
    fn from(value: u128) -> Self {
        U256(zkp_u256::U256::from(value))
    }
}

impl From<u64> for U256 {
    fn from(value: u64) -> Self {
        U256(zkp_u256::U256::from(value))
    }
}

/// Denormalize solana amounts to use in debridge protocol
pub trait Denormalize {
    fn denormalize(self, denominator: Denominator) -> U256;
}

impl Denormalize for U256 {
    fn denormalize(self, denominator: Denominator) -> U256 {
        self.mul(&U256::from_u64(10).pow(denominator.get() as u64))
    }
}

impl Denormalize for u64 {
    fn denormalize(self, denominator: Denominator) -> U256 {
        U256::from_u64(self).denormalize(denominator)
    }
}

#[cfg(test)]
mod u256_tests {
    use super::*;

    #[test]
    fn denominate() {
        assert_eq!(
            U256::from(25600000000_u64),
            256.denormalize(Denominator::new(8).unwrap())
        );
    }

    #[test]
    fn zero_denominate() {
        assert_eq!(
            U256::from(256_u64),
            256.denormalize(Denominator::new(0).unwrap())
        );
    }
}

/// Offset of decimals
/// Due to different types for solana and evm networks, you have to shift decimals
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, BorshSerialize, BorshDeserialize)]
pub struct Denominator {
    value: u8,
}

const DENOMINATOR_LIMIT: u8 = 76;

impl Denominator {
    pub const ZERO: Denominator = Denominator { value: 0 };

    pub const DENOMINATION_DELTA: u8 = 8;

    /// Calculate decimals for Solana wrapped token based on common decimals value for EVM networks.
    ///
    /// This is necessary for compatibility with the general protocol. In Solana the amount of tokens
    /// in wallet is stored in the u64 type, and in EVM networks in u256 type.
    /// By agreement, if the token decimal from the EVM network is less than 8, then it remains unchanged in Solana.
    /// If it is more than 8, then the decimals of the wrapped token created in Solana are equal 8.
    pub fn get_wrapper_decimals(decimals: u8) -> Self {
        Self {
            value: decimals.min(Self::DENOMINATION_DELTA),
        }
    }

    /// Calculate the denominator for the solana wrapped token relative to the original token from the EVM network.
    ///
    /// This is necessary for compatibility with the general protocol. In Solana the amount of tokens
    /// in wallet is stored in the u64 type, and in EVM networks in u256 type.
    /// By agreement, the minimum value of the wrapped token in the Solana network will be 10^-8.
    /// Any less token value is rounded up to 0. If in the native network the smallest token value
    /// is more than 10-8, then in Solana the smallest value of the wrapped token
    /// will be the same as in the native network.
    pub fn from_decimals(decimals: u8) -> Option<Denominator> {
        Denominator::new(decimals.saturating_sub(Self::DENOMINATION_DELTA))
    }

    pub fn new(value: u8) -> Option<Self> {
        if value < DENOMINATOR_LIMIT {
            Some(Self { value })
        } else {
            None
        }
    }

    /// Create new `Denominator`
    ///
    /// # Safety
    /// `value` need to be less then `DENOMINATOR_LIMIT`
    pub unsafe fn new_unchecked(value: u8) -> Self {
        Self { value }
    }
    pub fn get(&self) -> u8 {
        self.value
    }
}

impl Deref for Denominator {
    type Target = u8;
    fn deref(&self) -> &Self::Target {
        &self.value
    }
}