cairo_lang_utils/
bigint.rs

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
#[cfg(test)]
#[path = "bigint_tests/mod.rs"]
mod test;

#[cfg(all(not(feature = "std"), feature = "serde"))]
use alloc::{format, string::String};

#[cfg(feature = "serde")]
use num_bigint::ToBigInt;
use num_bigint::{BigInt, BigUint};
#[cfg(feature = "serde")]
use num_traits::{Num, Signed};

/// A wrapper for BigUint that serializes as hex.
#[derive(Clone, Default, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize), serde(transparent))]
pub struct BigUintAsHex {
    /// A field element that encodes the signature of the called function.
    #[cfg_attr(
        feature = "serde",
        serde(serialize_with = "serialize_big_uint", deserialize_with = "deserialize_big_uint")
    )]
    pub value: BigUint,
}

#[cfg(feature = "serde")]
fn deserialize_from_str<'a, D>(s: &str) -> Result<BigUint, D::Error>
where
    D: serde::Deserializer<'a>,
{
    match s.strip_prefix("0x") {
        Some(num_no_prefix) => BigUint::from_str_radix(num_no_prefix, 16)
            .map_err(|error| serde::de::Error::custom(format!("{error}"))),
        None => Err(serde::de::Error::custom(format!("{s} does not start with `0x` is missing."))),
    }
}

#[cfg(feature = "serde")]
pub fn serialize_big_uint<S>(num: &BigUint, serializer: S) -> Result<S::Ok, S::Error>
where
    S: serde::Serializer,
{
    serializer.serialize_str(&format!("{num:#x}"))
}

#[cfg(feature = "serde")]
pub fn deserialize_big_uint<'a, D>(deserializer: D) -> Result<BigUint, D::Error>
where
    D: serde::Deserializer<'a>,
{
    let s = &<String as serde::Deserialize>::deserialize(deserializer)?;
    deserialize_from_str::<D>(s)
}

// A wrapper for BigInt that serializes as hex.
#[derive(Default, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize), serde(transparent))]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct BigIntAsHex {
    /// A field element that encodes the signature of the called function.
    #[cfg_attr(
        feature = "serde",
        serde(serialize_with = "serialize_big_int", deserialize_with = "deserialize_big_int")
    )]
    #[cfg_attr(feature = "schemars", schemars(schema_with = "big_int_schema"))]
    pub value: BigInt,
}

// BigInt doesn't implement JsonSchema, so we need to manually define it.
#[cfg(feature = "schemars")]
fn big_int_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
    use schemars::JsonSchema;

    #[allow(dead_code)]
    #[allow(clippy::enum_variant_names)]
    #[derive(JsonSchema)]
    pub enum Sign {
        Minus,
        NoSign,
        Plus,
    }

    #[allow(dead_code)]
    #[derive(JsonSchema)]
    pub struct BigUint {
        data: Vec<u64>, // BigDigit is u64 or u32.
    }

    #[allow(dead_code)]
    #[derive(JsonSchema)]
    struct BigInt {
        sign: Sign,
        data: BigUint,
    }

    gen.subschema_for::<BigInt>()
}

impl<T: Into<BigInt>> From<T> for BigIntAsHex {
    fn from(x: T) -> Self {
        Self { value: x.into() }
    }
}

#[cfg(feature = "serde")]
pub fn serialize_big_int<S>(num: &BigInt, serializer: S) -> Result<S::Ok, S::Error>
where
    S: serde::ser::Serializer,
{
    serializer.serialize_str(&format!(
        "{}{:#x}",
        if num.is_negative() { "-" } else { "" },
        num.magnitude()
    ))
}

#[cfg(feature = "serde")]
pub fn deserialize_big_int<'a, D>(deserializer: D) -> Result<BigInt, D::Error>
where
    D: serde::de::Deserializer<'a>,
{
    use core::ops::Neg;

    let s = &<String as serde::Deserialize>::deserialize(deserializer)?;
    match s.strip_prefix('-') {
        Some(abs_value) => Ok(deserialize_from_str::<D>(abs_value)?.to_bigint().unwrap().neg()),
        None => Ok(deserialize_from_str::<D>(s)?.to_bigint().unwrap()),
    }
}

#[cfg(feature = "parity-scale-codec")]
mod impl_parity_scale_codec {
    #[cfg(not(feature = "std"))]
    use alloc::vec;

    use parity_scale_codec::{Decode, Encode};

    use super::*;

    impl Encode for BigIntAsHex {
        fn size_hint(&self) -> usize {
            // sign + len packed in the same byte, it allows numbers of byte size up to 63 (2**504),
            // data.
            let bits = self.value.bits() as usize;
            core::mem::size_of::<u8>() + bits / 8 + if bits % 8 != 0 { 1 } else { 0 }
        }

        /// /!\ Warning this function panics if the number encoded is too big (>= 2**504)
        fn encode_to<T: parity_scale_codec::Output + ?Sized>(&self, dest: &mut T) {
            let (sign, data) = self.value.to_bytes_le();
            assert!(data.len() <= 63, "Can't encode numbers longer than 63 bytes");
            // Pack sign + number byte size.
            ((match sign {
                num_bigint::Sign::Minus => 0u8,
                num_bigint::Sign::NoSign => 1u8,
                num_bigint::Sign::Plus => 2u8,
            } << 6)
                + data.len() as u8)
                .encode_to(dest);
            dest.write(&data);
        }
    }

    impl Decode for BigIntAsHex {
        fn decode<I: parity_scale_codec::Input>(
            input: &mut I,
        ) -> Result<Self, parity_scale_codec::Error> {
            let sign_and_len = input.read_byte()?;
            let sign = match sign_and_len >> 6 {
                0u8 => num_bigint::Sign::Minus,
                1u8 => num_bigint::Sign::NoSign,
                2u8 => num_bigint::Sign::Plus,
                _ => {
                    return Err(parity_scale_codec::Error::from("Bad sign encoding."));
                }
            };
            let len = sign_and_len & 0b00111111;
            let mut buffer = vec![0; len as usize];
            input.read(&mut buffer)?;
            Ok(Self { value: BigInt::from_bytes_le(sign, buffer.as_slice()) })
        }
    }
}