From 64106c4d3d4ddba8c7bc2af75376e6d3d3d75601 Mon Sep 17 00:00:00 2001
From:
Date: Mon, 29 Jun 2015 20:16:15 +0000
Subject: Update documentation
---
src/openssl/bn/mod.rs.html | 1305 ++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 1305 insertions(+)
create mode 100644 src/openssl/bn/mod.rs.html
(limited to 'src/openssl/bn')
diff --git a/src/openssl/bn/mod.rs.html b/src/openssl/bn/mod.rs.html
new file mode 100644
index 0000000..f57f83e
--- /dev/null
+++ b/src/openssl/bn/mod.rs.html
@@ -0,0 +1,1305 @@
+
+
+
+ 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
+486
+487
+488
+489
+490
+491
+492
+493
+494
+495
+496
+497
+498
+499
+500
+501
+502
+503
+504
+505
+506
+507
+508
+509
+510
+511
+512
+513
+514
+515
+516
+517
+518
+519
+520
+521
+522
+523
+524
+525
+526
+527
+528
+529
+530
+531
+532
+533
+534
+535
+536
+537
+538
+539
+540
+541
+542
+543
+544
+545
+546
+547
+548
+549
+550
+551
+552
+553
+554
+555
+556
+557
+558
+559
+560
+561
+562
+563
+564
+565
+566
+567
+568
+569
+570
+571
+572
+573
+574
+575
+576
+577
+578
+579
+580
+581
+582
+583
+584
+585
+586
+587
+588
+589
+590
+591
+592
+593
+594
+595
+596
+597
+598
+599
+600
+601
+602
+603
+604
+
+use libc::{c_int, c_ulong, c_void};
+use std::ffi::{CStr, CString};
+use std::cmp::Ordering;
+use std::{fmt, ptr};
+
+use ffi;
+use ssl::error::SslError;
+
+pub struct BigNum(*mut ffi::BIGNUM);
+
+#[derive(Copy, Clone)]
+#[repr(C)]
+pub enum RNGProperty {
+ MsbMaybeZero = -1,
+ MsbOne = 0,
+ TwoMsbOne = 1,
+}
+
+macro_rules! with_ctx(
+ ($name:ident, $action:block) => ({
+ let $name = ffi::BN_CTX_new();
+ if ($name).is_null() {
+ Err(SslError::get())
+ } else {
+ let r = $action;
+ ffi::BN_CTX_free($name);
+ r
+ }
+ });
+);
+
+macro_rules! with_bn(
+ ($name:ident, $action:block) => ({
+ let tmp = BigNum::new();
+ match tmp {
+ Ok($name) => {
+ if $action {
+ Ok($name)
+ } else {
+ Err(SslError::get())
+ }
+ },
+ Err(err) => Err(err),
+ }
+ });
+);
+
+macro_rules! with_bn_in_ctx(
+ ($name:ident, $ctx_name:ident, $action:block) => ({
+ let tmp = BigNum::new();
+ match tmp {
+ Ok($name) => {
+ let $ctx_name = ffi::BN_CTX_new();
+ if ($ctx_name).is_null() {
+ Err(SslError::get())
+ } else {
+ let r =
+ if $action {
+ Ok($name)
+ } else {
+ Err(SslError::get())
+ };
+ ffi::BN_CTX_free($ctx_name);
+ r
+ }
+ },
+ Err(err) => Err(err),
+ }
+ });
+);
+
+impl BigNum {
+ pub fn new() -> Result<BigNum, SslError> {
+ unsafe {
+ ffi::init();
+
+ let v = try_ssl_null!(ffi::BN_new());
+ Ok(BigNum(v))
+ }
+ }
+
+ pub fn new_from(n: u64) -> Result<BigNum, SslError> {
+ BigNum::new().and_then(|v| unsafe {
+ try_ssl!(ffi::BN_set_word(v.raw(), n as c_ulong));
+ Ok(v)
+ })
+ }
+
+ pub fn from_dec_str(s: &str) -> Result<BigNum, SslError> {
+ BigNum::new().and_then(|v| unsafe {
+ let c_str = CString::new(s.as_bytes()).unwrap();
+ try_ssl!(ffi::BN_dec2bn(v.raw_ptr(), c_str.as_ptr()));
+ Ok(v)
+ })
+ }
+
+ pub fn from_hex_str(s: &str) -> Result<BigNum, SslError> {
+ BigNum::new().and_then(|v| unsafe {
+ let c_str = CString::new(s.as_bytes()).unwrap();
+ try_ssl!(ffi::BN_hex2bn(v.raw_ptr(), c_str.as_ptr()));
+ Ok(v)
+ })
+ }
+
+ pub fn new_from_slice(n: &[u8]) -> Result<BigNum, SslError> {
+ BigNum::new().and_then(|v| unsafe {
+ try_ssl_null!(ffi::BN_bin2bn(n.as_ptr(), n.len() as c_int, v.raw()));
+ Ok(v)
+ })
+ }
+
+ pub fn checked_sqr(&self) -> Result<BigNum, SslError> {
+ unsafe {
+ with_bn_in_ctx!(r, ctx, { ffi::BN_sqr(r.raw(), self.raw(), ctx) == 1 })
+ }
+ }
+
+ pub fn checked_nnmod(&self, n: &BigNum) -> Result<BigNum, SslError> {
+ unsafe {
+ with_bn_in_ctx!(r, ctx, { ffi::BN_nnmod(r.raw(), self.raw(), n.raw(), ctx) == 1 })
+ }
+ }
+
+ pub fn checked_mod_add(&self, a: &BigNum, n: &BigNum) -> Result<BigNum, SslError> {
+ unsafe {
+ with_bn_in_ctx!(r, ctx, { ffi::BN_mod_add(r.raw(), self.raw(), a.raw(), n.raw(), ctx) == 1 })
+ }
+ }
+
+ pub fn checked_mod_sub(&self, a: &BigNum, n: &BigNum) -> Result<BigNum, SslError> {
+ unsafe {
+ with_bn_in_ctx!(r, ctx, { ffi::BN_mod_sub(r.raw(), self.raw(), a.raw(), n.raw(), ctx) == 1 })
+ }
+ }
+
+ pub fn checked_mod_mul(&self, a: &BigNum, n: &BigNum) -> Result<BigNum, SslError> {
+ unsafe {
+ with_bn_in_ctx!(r, ctx, { ffi::BN_mod_mul(r.raw(), self.raw(), a.raw(), n.raw(), ctx) == 1 })
+ }
+ }
+
+ pub fn checked_mod_sqr(&self, n: &BigNum) -> Result<BigNum, SslError> {
+ unsafe {
+ with_bn_in_ctx!(r, ctx, { ffi::BN_mod_sqr(r.raw(), self.raw(), n.raw(), ctx) == 1 })
+ }
+ }
+
+ pub fn checked_exp(&self, p: &BigNum) -> Result<BigNum, SslError> {
+ unsafe {
+ with_bn_in_ctx!(r, ctx, { ffi::BN_exp(r.raw(), self.raw(), p.raw(), ctx) == 1 })
+ }
+ }
+
+ pub fn checked_mod_exp(&self, p: &BigNum, n: &BigNum) -> Result<BigNum, SslError> {
+ unsafe {
+ with_bn_in_ctx!(r, ctx, { ffi::BN_mod_exp(r.raw(), self.raw(), p.raw(), n.raw(), ctx) == 1 })
+ }
+ }
+
+ pub fn checked_mod_inv(&self, n: &BigNum) -> Result<BigNum, SslError> {
+ unsafe {
+ with_bn_in_ctx!(r, ctx, { !ffi::BN_mod_inverse(r.raw(), self.raw(), n.raw(), ctx).is_null() })
+ }
+ }
+
+ pub fn add_word(&mut self, w: c_ulong) -> Result<(), SslError> {
+ unsafe {
+ if ffi::BN_add_word(self.raw(), w) == 1 {
+ Ok(())
+ } else {
+ Err(SslError::get())
+ }
+ }
+ }
+
+ pub fn sub_word(&mut self, w: c_ulong) -> Result<(), SslError> {
+ unsafe {
+ if ffi::BN_sub_word(self.raw(), w) == 1 {
+ Ok(())
+ } else {
+ Err(SslError::get())
+ }
+ }
+ }
+
+ pub fn mul_word(&mut self, w: c_ulong) -> Result<(), SslError> {
+ unsafe {
+ if ffi::BN_mul_word(self.raw(), w) == 1 {
+ Ok(())
+ } else {
+ Err(SslError::get())
+ }
+ }
+ }
+
+ pub fn div_word(&mut self, w: c_ulong) -> Result<c_ulong, SslError> {
+ unsafe {
+ let result = ffi::BN_div_word(self.raw(), w);
+ if result != !0 as c_ulong {
+ Ok(result)
+ } else {
+ Err(SslError::get())
+ }
+ }
+ }
+
+ pub fn mod_word(&self, w: c_ulong) -> Result<c_ulong, SslError> {
+ unsafe {
+ let result = ffi::BN_mod_word(self.raw(), w);
+ if result != !0 as c_ulong {
+ Ok(result)
+ } else {
+ Err(SslError::get())
+ }
+ }
+ }
+
+ pub fn checked_gcd(&self, a: &BigNum) -> Result<BigNum, SslError> {
+ unsafe {
+ with_bn_in_ctx!(r, ctx, { ffi::BN_gcd(r.raw(), self.raw(), a.raw(), ctx) == 1 })
+ }
+ }
+
+ pub fn checked_generate_prime(bits: i32, safe: bool, add: Option<&BigNum>, rem: Option<&BigNum>) -> Result<BigNum, SslError> {
+ unsafe {
+ with_bn_in_ctx!(r, ctx, {
+ let add_arg = add.map(|a| a.raw()).unwrap_or(ptr::null_mut());
+ let rem_arg = rem.map(|r| r.raw()).unwrap_or(ptr::null_mut());
+
+ ffi::BN_generate_prime_ex(r.raw(), bits as c_int, safe as c_int, add_arg, rem_arg, ptr::null()) == 1
+ })
+ }
+ }
+
+ pub fn is_prime(&self, checks: i32) -> Result<bool, SslError> {
+ unsafe {
+ with_ctx!(ctx, {
+ Ok(ffi::BN_is_prime_ex(self.raw(), checks as c_int, ctx, ptr::null()) == 1)
+ })
+ }
+ }
+
+ pub fn is_prime_fast(&self, checks: i32, do_trial_division: bool) -> Result<bool, SslError> {
+ unsafe {
+ with_ctx!(ctx, {
+ Ok(ffi::BN_is_prime_fasttest_ex(self.raw(), checks as c_int, ctx, do_trial_division as c_int, ptr::null()) == 1)
+ })
+ }
+ }
+
+ pub fn checked_new_random(bits: i32, prop: RNGProperty, odd: bool) -> Result<BigNum, SslError> {
+ unsafe {
+ with_bn_in_ctx!(r, ctx, { ffi::BN_rand(r.raw(), bits as c_int, prop as c_int, odd as c_int) == 1 })
+ }
+ }
+
+ pub fn checked_new_pseudo_random(bits: i32, prop: RNGProperty, odd: bool) -> Result<BigNum, SslError> {
+ unsafe {
+ with_bn_in_ctx!(r, ctx, { ffi::BN_pseudo_rand(r.raw(), bits as c_int, prop as c_int, odd as c_int) == 1 })
+ }
+ }
+
+ pub fn checked_rand_in_range(&self) -> Result<BigNum, SslError> {
+ unsafe {
+ with_bn_in_ctx!(r, ctx, { ffi::BN_rand_range(r.raw(), self.raw()) == 1 })
+ }
+ }
+
+ pub fn checked_pseudo_rand_in_range(&self) -> Result<BigNum, SslError> {
+ unsafe {
+ with_bn_in_ctx!(r, ctx, { ffi::BN_pseudo_rand_range(r.raw(), self.raw()) == 1 })
+ }
+ }
+
+ pub fn set_bit(&mut self, n: i32) -> Result<(), SslError> {
+ unsafe {
+ if ffi::BN_set_bit(self.raw(), n as c_int) == 1 {
+ Ok(())
+ } else {
+ Err(SslError::get())
+ }
+ }
+ }
+
+ pub fn clear_bit(&mut self, n: i32) -> Result<(), SslError> {
+ unsafe {
+ if ffi::BN_clear_bit(self.raw(), n as c_int) == 1 {
+ Ok(())
+ } else {
+ Err(SslError::get())
+ }
+ }
+ }
+
+ pub fn is_bit_set(&self, n: i32) -> bool {
+ unsafe {
+ ffi::BN_is_bit_set(self.raw(), n as c_int) == 1
+ }
+ }
+
+ pub fn mask_bits(&mut self, n: i32) -> Result<(), SslError> {
+ unsafe {
+ if ffi::BN_mask_bits(self.raw(), n as c_int) == 1 {
+ Ok(())
+ } else {
+ Err(SslError::get())
+ }
+ }
+ }
+
+ pub fn checked_shl1(&self) -> Result<BigNum, SslError> {
+ unsafe {
+ with_bn!(r, { ffi::BN_lshift1(r.raw(), self.raw()) == 1 })
+ }
+ }
+
+ pub fn checked_shr1(&self) -> Result<BigNum, SslError> {
+ unsafe {
+ with_bn!(r, { ffi::BN_rshift1(r.raw(), self.raw()) == 1 })
+ }
+ }
+
+ pub fn checked_add(&self, a: &BigNum) -> Result<BigNum, SslError> {
+ unsafe {
+ with_bn!(r, { ffi::BN_add(r.raw(), self.raw(), a.raw()) == 1 })
+ }
+ }
+
+ pub fn checked_sub(&self, a: &BigNum) -> Result<BigNum, SslError> {
+ unsafe {
+ with_bn!(r, { ffi::BN_sub(r.raw(), self.raw(), a.raw()) == 1 })
+ }
+ }
+
+ pub fn checked_mul(&self, a: &BigNum) -> Result<BigNum, SslError> {
+ unsafe {
+ with_bn_in_ctx!(r, ctx, { ffi::BN_mul(r.raw(), self.raw(), a.raw(), ctx) == 1 })
+ }
+ }
+
+ pub fn checked_div(&self, a: &BigNum) -> Result<BigNum, SslError> {
+ unsafe {
+ with_bn_in_ctx!(r, ctx, { ffi::BN_div(r.raw(), ptr::null_mut(), self.raw(), a.raw(), ctx) == 1 })
+ }
+ }
+
+ pub fn checked_mod(&self, a: &BigNum) -> Result<BigNum, SslError> {
+ unsafe {
+ with_bn_in_ctx!(r, ctx, { ffi::BN_div(ptr::null_mut(), r.raw(), self.raw(), a.raw(), ctx) == 1 })
+ }
+ }
+
+ pub fn checked_shl(&self, a: &i32) -> Result<BigNum, SslError> {
+ unsafe {
+ with_bn!(r, { ffi::BN_lshift(r.raw(), self.raw(), *a as c_int) == 1 })
+ }
+ }
+
+ pub fn checked_shr(&self, a: &i32) -> Result<BigNum, SslError> {
+ unsafe {
+ with_bn!(r, { ffi::BN_rshift(r.raw(), self.raw(), *a as c_int) == 1 })
+ }
+ }
+
+ pub fn negate(&mut self) {
+ unsafe {
+ ffi::BN_set_negative(self.raw(), !self.is_negative() as c_int)
+ }
+ }
+
+ pub fn abs_cmp(&self, oth: BigNum) -> Ordering {
+ unsafe {
+ let res = ffi::BN_ucmp(self.raw(), oth.raw()) as i32;
+ if res < 0 {
+ Ordering::Less
+ } else if res > 0 {
+ Ordering::Greater
+ } else {
+ Ordering::Equal
+ }
+ }
+ }
+
+ pub fn is_negative(&self) -> bool {
+ unsafe {
+ (*self.raw()).neg == 1
+ }
+ }
+
+ pub fn num_bits(&self) -> i32 {
+ unsafe {
+ ffi::BN_num_bits(self.raw()) as i32
+ }
+ }
+
+ pub fn num_bytes(&self) -> i32 {
+ (self.num_bits() + 7) / 8
+ }
+
+ unsafe fn raw(&self) -> *mut ffi::BIGNUM {
+ let BigNum(n) = *self;
+ n
+ }
+
+ unsafe fn raw_ptr(&self) -> *const *mut ffi::BIGNUM {
+ let BigNum(ref n) = *self;
+ n
+ }
+
+ pub fn to_vec(&self) -> Vec<u8> {
+ let size = self.num_bytes() as usize;
+ let mut v = Vec::with_capacity(size);
+ unsafe {
+ ffi::BN_bn2bin(self.raw(), v.as_mut_ptr());
+ v.set_len(size);
+ }
+ v
+ }
+
+ pub fn to_dec_str(&self) -> String {
+ unsafe {
+ let buf = ffi::BN_bn2dec(self.raw());
+ assert!(!buf.is_null());
+ let str = String::from_utf8(CStr::from_ptr(buf).to_bytes().to_vec()).unwrap();
+ ffi::CRYPTO_free(buf as *mut c_void);
+ str
+ }
+ }
+
+ pub fn to_hex_str(&self) -> String {
+ unsafe {
+ let buf = ffi::BN_bn2hex(self.raw());
+ assert!(!buf.is_null());
+ let str = String::from_utf8(CStr::from_ptr(buf).to_bytes().to_vec()).unwrap();
+ ffi::CRYPTO_free(buf as *mut c_void);
+ str
+ }
+ }
+}
+
+impl fmt::Debug for BigNum {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write!(f, "{}", self.to_dec_str())
+ }
+}
+
+impl Eq for BigNum { }
+impl PartialEq for BigNum {
+ fn eq(&self, oth: &BigNum) -> bool {
+ unsafe {
+ ffi::BN_cmp(self.raw(), oth.raw()) == 0
+ }
+ }
+}
+
+impl Ord for BigNum {
+ fn cmp(&self, oth: &BigNum) -> Ordering {
+ self.partial_cmp(oth).unwrap()
+ }
+}
+
+impl PartialOrd for BigNum {
+ fn partial_cmp(&self, oth: &BigNum) -> Option<Ordering> {
+ unsafe {
+ let v = ffi::BN_cmp(self.raw(), oth.raw());
+ let ret =
+ if v == 0 {
+ Ordering::Equal
+ } else if v < 0 {
+ Ordering::Less
+ } else {
+ Ordering::Greater
+ };
+ Some(ret)
+ }
+ }
+}
+
+impl Drop for BigNum {
+ fn drop(&mut self) {
+ unsafe {
+ if !self.raw().is_null() {
+ ffi::BN_clear_free(self.raw());
+ }
+ }
+ }
+}
+
+pub mod unchecked {
+ use std::ops::{Add, Div, Mul, Neg, Rem, Shl, Shr, Sub};
+ use ffi;
+ use super::{BigNum};
+
+ impl<'a> Add<&'a BigNum> for &'a BigNum {
+ type Output = BigNum;
+
+ fn add(self, oth: &'a BigNum) -> BigNum {
+ self.checked_add(oth).unwrap()
+ }
+ }
+
+ impl<'a> Sub<&'a BigNum> for &'a BigNum {
+ type Output = BigNum;
+
+ fn sub(self, oth: &'a BigNum) -> BigNum {
+ self.checked_sub(oth).unwrap()
+ }
+ }
+
+ impl<'a> Mul<&'a BigNum> for &'a BigNum {
+ type Output = BigNum;
+
+ fn mul(self, oth: &'a BigNum) -> BigNum {
+ self.checked_mul(oth).unwrap()
+ }
+ }
+
+ impl<'a> Div<&'a BigNum> for &'a BigNum {
+ type Output = BigNum;
+
+ fn div(self, oth: &'a BigNum) -> BigNum {
+ self.checked_div(oth).unwrap()
+ }
+ }
+
+ impl<'a> Rem<&'a BigNum> for &'a BigNum {
+ type Output = BigNum;
+
+ fn rem(self, oth: &'a BigNum) -> BigNum {
+ self.checked_mod(oth).unwrap()
+ }
+ }
+
+ impl<'a> Shl<i32> for &'a BigNum {
+ type Output = BigNum;
+
+ fn shl(self, n: i32) -> BigNum {
+ self.checked_shl(&n).unwrap()
+ }
+ }
+
+ impl<'a> Shr<i32> for &'a BigNum {
+ type Output = BigNum;
+
+ fn shr(self, n: i32) -> BigNum {
+ self.checked_shr(&n).unwrap()
+ }
+ }
+
+ impl Clone for BigNum {
+ fn clone(&self) -> BigNum {
+ unsafe {
+ let r = ffi::BN_dup(self.raw());
+ if r.is_null() {
+ panic!("Unexpected null pointer from BN_dup(..)")
+ } else {
+ BigNum(r)
+ }
+ }
+ }
+ }
+
+ impl Neg for BigNum {
+ type Output = BigNum;
+
+ fn neg(self) -> BigNum {
+ let mut n = self.clone();
+ n.negate();
+ n
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use bn::BigNum;
+
+ #[test]
+ fn test_to_from_slice() {
+ let v0 = BigNum::new_from(10203004_u64).unwrap();
+ let vec = v0.to_vec();
+ let v1 = BigNum::new_from_slice(&vec).unwrap();
+
+ assert!(v0 == v1);
+ }
+
+ #[test]
+ fn test_negation() {
+ let a = BigNum::new_from(909829283_u64).unwrap();
+
+ assert!(!a.is_negative());
+ assert!((-a).is_negative());
+ }
+
+
+ #[test]
+ fn test_prime_numbers() {
+ let a = BigNum::new_from(19029017_u64).unwrap();
+ let p = BigNum::checked_generate_prime(128, true, None, Some(&a)).unwrap();
+
+ assert!(p.is_prime(100).unwrap());
+ assert!(p.is_prime_fast(100, true).unwrap());
+ }
+}
+
+
+