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
use std::{fmt, io};

use byteorder::{BigEndian, WriteBytesExt};
use nom::number::streaming::be_u16;
use nom::{self, Err, InputIter, InputTake};
use num_bigint::BigUint;
use zeroize::{Zeroize, ZeroizeOnDrop};

use crate::errors::{self, Error, IResult};
use crate::ser::Serialize;
use crate::util::{bit_size, strip_leading_zeros, strip_leading_zeros_vec};

/// Number of bits we accept when reading or writing MPIs.
/// The value is the same as gnupgs.
const MAX_EXTERN_MPI_BITS: u32 = 16384;

/// Parse Multi Precision Integers
/// Ref: https://tools.ietf.org/html/rfc4880.html#section-3.2
///
/// # Examples
///
/// ```rust
/// use pgp::types::mpi;
///
/// // Decode the number `1`.
/// assert_eq!(
///     mpi(&[0x00, 0x01, 0x01][..]).unwrap(),
///     (&b""[..], (&[1][..]).into())
/// );
/// ```
///
pub fn mpi(input: &[u8]) -> IResult<&[u8], MpiRef<'_>> {
    let (number, len) = be_u16(input)?;

    let bits = u32::from(len);
    let len_actual = (bits + 7) >> 3;

    if len_actual > MAX_EXTERN_MPI_BITS {
        Err(Err::Error(Error::InvalidInput))
    } else {
        // same as take!
        let cnt = len_actual as usize;
        match number.slice_index(cnt) {
            Err(needed) => Err(nom::Err::Incomplete(needed)),
            Ok(index) => {
                let (rest, n) = number.take_split(index);
                let n_stripped: MpiRef<'_> = strip_leading_zeros(n).into();

                Ok((rest, n_stripped))
            }
        }
    }
}

/// Represents an owned MPI value.
/// The inner value is ready to be serialized, without the need to strip leading zeros.
#[derive(Default, Clone, PartialEq, Eq, Zeroize, ZeroizeOnDrop)]
pub struct Mpi(Vec<u8>);

/// Represents a borrowed MPI value.
/// The inner value is ready to be serialized, without the need to strip leading zeros.
#[derive(Clone, PartialEq, Eq)]
pub struct MpiRef<'a>(&'a [u8]);

impl AsRef<[u8]> for Mpi {
    fn as_ref(&self) -> &[u8] {
        self.0.as_ref()
    }
}

impl Mpi {
    pub fn from_raw(mut v: Vec<u8>) -> Self {
        strip_leading_zeros_vec(&mut v);
        Mpi(v)
    }

    pub fn from_slice(slice: &[u8]) -> Self {
        Mpi(slice.to_vec())
    }

    /// Strips leading zeros.
    pub fn from_raw_slice(raw: &[u8]) -> Self {
        Mpi(strip_leading_zeros(raw).to_vec())
    }

    pub fn as_ref(&self) -> MpiRef<'_> {
        MpiRef(&self.0)
    }

    pub fn as_bytes(&self) -> &[u8] {
        &self.0
    }

    /// Strip trailing zeroes.
    pub fn strip_trailing_zeroes(&mut self) {
        let mut end = self.0.len();
        for byte in self.0.iter().rev() {
            if *byte == 0 {
                end -= 1;
            } else {
                break;
            }
        }
        self.0.truncate(end);
    }

    pub fn pad_right(&mut self, new_len: usize) {
        if new_len <= self.0.len() {
            return;
        }
        self.0.resize(new_len, 0u8);
    }
}

impl std::ops::Deref for Mpi {
    type Target = [u8];

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<'a> std::ops::Deref for MpiRef<'a> {
    type Target = [u8];

    fn deref(&self) -> &Self::Target {
        self.0
    }
}

impl<'a> MpiRef<'a> {
    pub fn from_slice(slice: &'a [u8]) -> Self {
        MpiRef(slice)
    }

    pub fn to_owned(&self) -> Mpi {
        Mpi(self.0.to_owned())
    }

    pub fn parse(slice: &'a [u8]) -> IResult<&'a [u8], MpiRef<'a>> {
        mpi(slice)
    }

    pub fn as_bytes(&self) -> &[u8] {
        self.0
    }

    /// Strip trailing zeroes.
    pub fn strip_trailing_zeroes(&self) -> Self {
        let mut end = self.0.len();
        for byte in self.0.iter().rev() {
            if *byte == 0 {
                end -= 1;
            } else {
                break;
            }
        }
        MpiRef::from_slice(&self.0[..end])
    }
}

impl Serialize for Mpi {
    fn to_writer<W: io::Write>(&self, w: &mut W) -> errors::Result<()> {
        MpiRef(&self.0).to_writer(w)
    }
}

impl<'a> Serialize for MpiRef<'a> {
    fn to_writer<W: io::Write>(&self, w: &mut W) -> errors::Result<()> {
        let bytes = &self.0;
        let size = bit_size(bytes);
        w.write_u16::<BigEndian>(size as u16)?;
        w.write_all(bytes)?;

        Ok(())
    }
}

impl From<&[u8]> for Mpi {
    fn from(other: &[u8]) -> Mpi {
        Mpi::from_slice(other)
    }
}

impl<'a> From<&'a [u8]> for MpiRef<'a> {
    fn from(other: &'a [u8]) -> MpiRef<'a> {
        MpiRef::from_slice(other)
    }
}

impl From<Vec<u8>> for Mpi {
    fn from(other: Vec<u8>) -> Mpi {
        Mpi(other)
    }
}

impl From<BigUint> for Mpi {
    fn from(other: BigUint) -> Self {
        Mpi(other.to_bytes_be())
    }
}

impl From<Mpi> for BigUint {
    fn from(other: Mpi) -> Self {
        BigUint::from_bytes_be(other.as_bytes())
    }
}

impl<'a> From<&'a Mpi> for BigUint {
    fn from(other: &'a Mpi) -> Self {
        BigUint::from_bytes_be(other.as_bytes())
    }
}

impl<'a> From<MpiRef<'a>> for BigUint {
    fn from(other: MpiRef<'a>) -> Self {
        BigUint::from_bytes_be(other.as_bytes())
    }
}

impl<'a, 'b> From<&'b MpiRef<'a>> for BigUint {
    fn from(other: &'b MpiRef<'a>) -> Self {
        BigUint::from_bytes_be(other.as_bytes())
    }
}

impl<'a> From<&'a BigUint> for Mpi {
    fn from(other: &'a BigUint) -> Self {
        Mpi(other.to_bytes_be())
    }
}

impl<'a> fmt::Debug for MpiRef<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Mpi({})", hex::encode(self.0))
    }
}

impl fmt::Debug for Mpi {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.as_ref().fmt(f)
    }
}

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used)]

    use super::*;

    #[test]
    fn test_mpi() {
        // Decode the number `511` (`0x1FF` in hex).
        assert_eq!(
            mpi(&[0x00, 0x09, 0x01, 0xFF][..]).unwrap(),
            (&b""[..], (&[0x01, 0xFF][..]).into())
        );

        // Decode the number `2^255 + 7`.
        assert_eq!(
            mpi(&[
                0x01, 0, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0x07
            ][..])
            .unwrap(),
            (
                &b""[..],
                (&[
                    0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                    0, 0, 0, 0, 0, 0, 0x07
                ][..])
                    .into()
            )
        );
    }

    #[test]
    fn test_bignum_mpi() {
        let fixtures = [
            ("b4a71b058ac8aa1ddc453ab2663331c38f7645542815ac189a9af56d0e07a615469d3e08849650e03026d49259423cf00d089931cd700fd3a6e940bf83c81406e142a4b0a86f00738c7e1a9ff1b709f6bccc6cf900d0113a8e62e53d63be0a05105755b9efc6a4098c362c73fb422d40187d8e2382e88624d72caffceb13cec8fa0079c7d17883a46a1336471ab5be8cbb555c5d330d7fadb43318fa73b584edac312fa3302886bb5d04a05da3be2676c1fb94b3cf5c19d598659c3a7728ebab95f71721b662ac46aa9910726fe576d438f789c5ce2448f54546f254da814bcae1c35ee44b171e870ffa6403167a10e68573bdf155549274b431ff8e2418b627", "0800b4a71b058ac8aa1ddc453ab2663331c38f7645542815ac189a9af56d0e07a615469d3e08849650e03026d49259423cf00d089931cd700fd3a6e940bf83c81406e142a4b0a86f00738c7e1a9ff1b709f6bccc6cf900d0113a8e62e53d63be0a05105755b9efc6a4098c362c73fb422d40187d8e2382e88624d72caffceb13cec8fa0079c7d17883a46a1336471ab5be8cbb555c5d330d7fadb43318fa73b584edac312fa3302886bb5d04a05da3be2676c1fb94b3cf5c19d598659c3a7728ebab95f71721b662ac46aa9910726fe576d438f789c5ce2448f54546f254da814bcae1c35ee44b171e870ffa6403167a10e68573bdf155549274b431ff8e2418b627"),
            ("00e57192fa7bd6abd7d01331f0411eebff4651290af1329369cc3bb3b8ccbd7ba6e352400c3f64f637967e24524921ee04f1e0a79168781f0bec9029e34c8a1fb1c328a4b8d74c31429616a6ff4707bb56b71ab66643243087c8ff0d0c4883b3473c56deece9a83dbd06eef09fac3558003ae45f8898b8a9490aa79672eebdd7d985d051d62698f2da7eee33ba740e30fc5a93c3f16ca1490dfd62b84ba016c9da7c087a28a4e97d8af79c6b638bc22f20a8b5953bb83caa3dddaaf1d0dc15a3f7ed47870174af74e5308b856138771a10019fe4374389eb89d2280776e33fa2dd3526cec35cd86a9cf6c94253fe00c4b8a87a36451745116456833bb1a237", "07f0e57192fa7bd6abd7d01331f0411eebff4651290af1329369cc3bb3b8ccbd7ba6e352400c3f64f637967e24524921ee04f1e0a79168781f0bec9029e34c8a1fb1c328a4b8d74c31429616a6ff4707bb56b71ab66643243087c8ff0d0c4883b3473c56deece9a83dbd06eef09fac3558003ae45f8898b8a9490aa79672eebdd7d985d051d62698f2da7eee33ba740e30fc5a93c3f16ca1490dfd62b84ba016c9da7c087a28a4e97d8af79c6b638bc22f20a8b5953bb83caa3dddaaf1d0dc15a3f7ed47870174af74e5308b856138771a10019fe4374389eb89d2280776e33fa2dd3526cec35cd86a9cf6c94253fe00c4b8a87a36451745116456833bb1a237"),
        ];

        for (i, (raw, encoded)) in fixtures.iter().enumerate() {
            println!("fixture {i}");
            let n = hex::decode(raw).unwrap();

            let n_big = BigUint::from_bytes_be(&n);
            let n_mpi: Mpi = n_big.clone().into();
            let mut n_encoded = Vec::new();
            n_mpi.to_writer(&mut n_encoded).unwrap();

            assert_eq!(&n_encoded, &hex::decode(encoded).unwrap());

            let (rest, n_big2) = mpi(&n_encoded).unwrap();
            assert_eq!(rest.len(), 0);
            assert_eq!(n_big, n_big2.into());
        }
    }

    #[test]
    fn test_strip_trailing_zeroes() {
        let bytes = [1, 2, 3, 4, 0];
        assert_eq!(
            MpiRef::from_slice(&bytes)
                .strip_trailing_zeroes()
                .as_bytes(),
            &[1, 2, 3, 4],
        );

        let bytes = [0, 1, 2, 3, 4];
        assert_eq!(
            MpiRef::from_slice(&bytes)
                .strip_trailing_zeroes()
                .as_bytes(),
            &[0, 1, 2, 3, 4],
        );

        let bytes = [1, 2, 0, 3, 4];
        assert_eq!(
            MpiRef::from_slice(&bytes)
                .strip_trailing_zeroes()
                .as_bytes(),
            &[1, 2, 0, 3, 4],
        );
    }
}