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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
use std::{cmp::Ordering, fmt, ops, ptr, str};

use cstr_argument::CStrArgument;
use ffi;
use libc::c_uint;

use crate::{buffer::Buffer, error::return_err, rand::Level, Error, NonNull, Result};

#[repr(usize)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum Format {
    Standard = ffi::GCRYMPI_FMT_STD as usize,
    Unsigned = ffi::GCRYMPI_FMT_USG as usize,
    Pgp = ffi::GCRYMPI_FMT_PGP as usize,
    Ssh = ffi::GCRYMPI_FMT_SSH as usize,
    Hex = ffi::GCRYMPI_FMT_HEX as usize,
}

pub struct Integer(NonNull<ffi::gcry_mpi_t>);

impl Drop for Integer {
    #[inline]
    fn drop(&mut self) {
        unsafe {
            ffi::gcry_mpi_release(self.as_raw());
        }
    }
}

impl Clone for Integer {
    #[inline]
    fn clone(&self) -> Integer {
        unsafe { Integer::from_raw(ffi::gcry_mpi_copy(self.as_raw())) }
    }

    #[inline]
    fn clone_from(&mut self, source: &Integer) {
        unsafe {
            ffi::gcry_mpi_set(self.as_raw(), source.as_raw());
        }
    }
}

impl Integer {
    impl_wrapper!(Integer: ffi::gcry_mpi_t);

    #[inline]
    pub fn zero() -> Integer {
        Integer::new(0)
    }

    #[inline]
    pub fn one() -> Integer {
        Integer::from_uint(1)
    }

    #[inline]
    pub fn new(nbits: u32) -> Integer {
        let _ = crate::init_default();
        unsafe { Integer::from_raw(ffi::gcry_mpi_new(nbits.into())) }
    }

    #[inline]
    pub fn new_secure(nbits: u32) -> Integer {
        let _ = crate::init_default();
        unsafe { Integer::from_raw(ffi::gcry_mpi_snew(nbits.into())) }
    }

    #[inline]
    pub fn from_uint(n: u32) -> Integer {
        let _ = crate::init_default();
        unsafe { Integer::from_raw(ffi::gcry_mpi_set_ui(ptr::null_mut(), n.into())) }
    }

    #[inline]
    pub fn from_bytes(format: Format, bytes: impl AsRef<[u8]>) -> Result<Integer> {
        let bytes = bytes.as_ref();
        let _ = crate::init_default();
        unsafe {
            let mut raw = ptr::null_mut();
            let len = if format != Format::Hex {
                bytes.len()
            } else if bytes.contains(&0) {
                0
            } else {
                return Err(Error::INV_ARG);
            };
            return_err!(ffi::gcry_mpi_scan(
                &mut raw,
                format as ffi::gcry_mpi_format,
                bytes.as_ptr().cast(),
                len,
                ptr::null_mut()
            ));
            Ok(Integer::from_raw(raw))
        }
    }

    #[inline]
    pub fn from_str(s: impl CStrArgument) -> Result<Integer> {
        let s = s.into_cstr();
        Integer::from_bytes(Format::Hex, s.as_ref().to_bytes_with_nul())
    }

    #[inline]
    pub fn to_bytes(&self, format: Format) -> Result<Buffer> {
        unsafe {
            let mut buffer = ptr::null_mut();
            let mut len = 0;
            return_err!(ffi::gcry_mpi_aprint(
                format as ffi::gcry_mpi_format,
                &mut buffer,
                &mut len,
                self.as_raw()
            ));
            Ok(Buffer::from_raw(buffer.cast(), len))
        }
    }

    #[inline]
    pub fn len_encoded(&self, format: Format) -> Result<usize> {
        unsafe {
            let mut len = 0;
            return_err!(ffi::gcry_mpi_print(
                format as ffi::gcry_mpi_format,
                ptr::null_mut(),
                0,
                &mut len,
                self.as_raw()
            ));
            Ok(len)
        }
    }

    #[inline]
    pub fn encode(&self, format: Format, buf: &mut [u8]) -> Result<usize> {
        unsafe {
            let mut written = 0;
            return_err!(ffi::gcry_mpi_print(
                format as ffi::gcry_mpi_format,
                buf.as_mut_ptr().cast(),
                buf.len(),
                &mut written,
                self.as_raw()
            ));
            Ok(written)
        }
    }

    #[inline]
    pub fn set(&mut self, n: u32) {
        unsafe {
            ffi::gcry_mpi_set_ui(self.as_raw(), n.into());
        }
    }

    #[inline]
    pub fn randomize(&mut self, nbits: u32, level: Level) {
        unsafe {
            ffi::gcry_mpi_randomize(self.as_raw(), nbits.into(), level.raw());
        }
    }

    #[inline]
    pub fn num_bits(&self) -> usize {
        unsafe { ffi::gcry_mpi_get_nbits(self.as_raw()) as usize }
    }

    #[inline]
    pub fn is_prime(&self) -> bool {
        unsafe { ffi::gcry_prime_check(self.as_raw(), 0) == 0 }
    }

    #[inline]
    pub fn is_positive(&self) -> bool {
        unsafe { ffi::gcry_mpi_cmp_ui(self.as_raw(), 0) > 0 }
    }

    #[inline]
    pub fn is_negative(&self) -> bool {
        unsafe { ffi::gcry_mpi_cmp_ui(self.as_raw(), 0) < 0 }
    }

    #[inline]
    pub fn abs(self) -> Integer {
        unsafe {
            ffi::gcry_mpi_abs(self.as_raw());
        }
        self
    }

    #[inline]
    pub fn add_mod(self, other: &Integer, m: &Integer) -> Integer {
        unsafe {
            ffi::gcry_mpi_addm(self.as_raw(), self.as_raw(), other.as_raw(), m.as_raw());
        }
        self
    }

    #[inline]
    pub fn sub_mod(self, other: &Integer, m: &Integer) -> Integer {
        unsafe {
            ffi::gcry_mpi_subm(self.as_raw(), self.as_raw(), other.as_raw(), m.as_raw());
        }
        self
    }

    #[inline]
    pub fn mul_mod(self, other: &Integer, m: &Integer) -> Integer {
        unsafe {
            ffi::gcry_mpi_mulm(self.as_raw(), self.as_raw(), other.as_raw(), m.as_raw());
        }
        self
    }

    #[inline]
    pub fn inv_mod(self, m: &Integer) -> Option<Integer> {
        let result = unsafe { ffi::gcry_mpi_invm(self.as_raw(), self.as_raw(), m.as_raw()) };
        if result != 0 {
            Some(self)
        } else {
            None
        }
    }

    #[inline]
    pub fn div_floor(self, other: &Integer) -> Integer {
        unsafe {
            ffi::gcry_mpi_div(
                self.as_raw(),
                ptr::null_mut(),
                self.as_raw(),
                other.as_raw(),
                -1,
            );
        }
        self
    }

    #[inline]
    pub fn mod_floor(self, other: &Integer) -> Integer {
        unsafe {
            ffi::gcry_mpi_div(
                ptr::null_mut(),
                self.as_raw(),
                self.as_raw(),
                other.as_raw(),
                -1,
            );
        }
        self
    }

    #[inline]
    pub fn div_rem(self, other: &Integer) -> (Integer, Integer) {
        let rem = Integer::zero();
        unsafe {
            ffi::gcry_mpi_div(
                self.as_raw(),
                rem.as_raw(),
                self.as_raw(),
                other.as_raw(),
                0,
            );
        }
        (self, rem)
    }

    #[inline]
    pub fn div_mod_floor(self, other: &Integer) -> (Integer, Integer) {
        let rem = Integer::zero();
        unsafe {
            ffi::gcry_mpi_div(
                self.as_raw(),
                rem.as_raw(),
                self.as_raw(),
                other.as_raw(),
                -1,
            );
        }
        (self, rem)
    }

    #[inline]
    pub fn ldexp(self, e: u32) -> Integer {
        unsafe {
            ffi::gcry_mpi_mul_2exp(self.as_raw(), self.as_raw(), e.into());
        }
        self
    }

    #[inline]
    pub fn pow_mod(self, e: &Integer, m: &Integer) -> Integer {
        unsafe {
            ffi::gcry_mpi_powm(self.as_raw(), self.as_raw(), e.as_raw(), m.as_raw());
        }
        self
    }

    #[inline]
    pub fn gcd(self, other: &Integer) -> Integer {
        unsafe {
            ffi::gcry_mpi_gcd(self.as_raw(), self.as_raw(), other.as_raw());
        }
        self
    }

    #[inline]
    pub fn lcm(self, other: &Integer) -> Integer {
        (self.clone() * other) / self.gcd(other)
    }
}

impl Default for Integer {
    #[inline]
    fn default() -> Self {
        Self::zero()
    }
}

impl From<u32> for Integer {
    #[inline]
    fn from(x: u32) -> Self {
        Self::from_uint(x)
    }
}

impl fmt::Debug for Integer {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut s = f.debug_struct("Integer");
        s.field("raw", &self.0);
        if let Ok(bytes) = self.to_bytes(Format::Hex) {
            s.field("hex", &str::from_utf8(&bytes[..(bytes.len() - 1)]).unwrap());
        }
        s.finish()
    }
}

impl PartialEq<u32> for Integer {
    #[inline]
    fn eq(&self, other: &u32) -> bool {
        self.partial_cmp(other).unwrap() == Ordering::Equal
    }
}

impl PartialEq<Integer> for u32 {
    #[inline]
    fn eq(&self, other: &Integer) -> bool {
        self.partial_cmp(other).unwrap() == Ordering::Equal
    }
}

impl PartialOrd<u32> for Integer {
    #[inline]
    fn partial_cmp(&self, other: &u32) -> Option<Ordering> {
        let result = unsafe { ffi::gcry_mpi_cmp_ui(self.as_raw(), (*other).into()) };
        match result {
            x if x < 0 => Some(Ordering::Less),
            x if x > 0 => Some(Ordering::Greater),
            _ => Some(Ordering::Equal),
        }
    }
}

impl PartialOrd<Integer> for u32 {
    #[inline]
    fn partial_cmp(&self, other: &Integer) -> Option<Ordering> {
        other.partial_cmp(self).map(Ordering::reverse)
    }
}

impl PartialEq for Integer {
    #[inline]
    fn eq(&self, other: &Integer) -> bool {
        self.cmp(other) == Ordering::Equal
    }
}
impl Eq for Integer {}

impl PartialOrd for Integer {
    #[inline]
    fn partial_cmp(&self, other: &Integer) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}
impl Ord for Integer {
    #[inline]
    fn cmp(&self, other: &Integer) -> Ordering {
        let result = unsafe { ffi::gcry_mpi_cmp(self.as_raw(), other.as_raw()) };
        match result {
            x if x < 0 => Ordering::Less,
            x if x > 0 => Ordering::Greater,
            _ => Ordering::Equal,
        }
    }
}

impl ops::Neg for Integer {
    type Output = Integer;

    #[inline]
    fn neg(self) -> Integer {
        unsafe {
            ffi::gcry_mpi_neg(self.as_raw(), self.as_raw());
        }
        self
    }
}

impl ops::Neg for &'_ Integer {
    type Output = Integer;

    #[inline]
    fn neg(self) -> Integer {
        self.clone().neg()
    }
}

macro_rules! impl_binary_op {
    ($imp:ident, $method:ident, $body:expr) => {
        impl ops::$imp for Integer {
            type Output = Integer;

            #[inline]
            fn $method(self, other: Integer) -> Integer {
                self.$method(&other)
            }
        }
        impl<'a> ops::$imp<Integer> for &'a Integer {
            type Output = Integer;

            #[inline]
            fn $method(self, other: Integer) -> Integer {
                self.clone().$method(&other)
            }
        }

        impl<'a> ops::$imp<&'a Integer> for Integer {
            type Output = Integer;

            #[inline]
            fn $method(self, other: &'a Integer) -> Integer {
                unsafe {
                    $body(self.as_raw(), other.as_raw());
                }
                self
            }
        }
    };
}
impl_binary_op!(Add, add, |x, y| ffi::gcry_mpi_add(x, x, y));
impl_binary_op!(Sub, sub, |x, y| ffi::gcry_mpi_sub(x, x, y));
impl_binary_op!(Mul, mul, |x, y| ffi::gcry_mpi_mul(x, x, y));
impl_binary_op!(Div, div, |x, y| ffi::gcry_mpi_div(
    x,
    ptr::null_mut(),
    x,
    y,
    0
));
impl_binary_op!(Rem, rem, |x, y| ffi::gcry_mpi_mod(x, x, y));

impl ops::Shl<usize> for Integer {
    type Output = Integer;

    #[inline]
    fn shl(self, other: usize) -> Integer {
        unsafe {
            ffi::gcry_mpi_lshift(self.as_raw(), self.as_raw(), other as c_uint);
        }
        self
    }
}

impl ops::Shr<usize> for Integer {
    type Output = Integer;

    #[inline]
    fn shr(self, other: usize) -> Integer {
        unsafe {
            ffi::gcry_mpi_rshift(self.as_raw(), self.as_raw(), other as c_uint);
        }
        self
    }
}