obfuskey 0.1.0

Cross-language compatible integer obfuscation and bit-packing library
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
406
407
408
409
410
411
412
413
414
use num_bigint::BigUint;
use num_traits::{One, Zero};

use crate::error::ObfuskeyError;

// =============================================================================
// Native u64 primality (used for numbers < 2M, avoids BigUint entirely)
// =============================================================================

/// Trial division using native u64.
fn trial_division_u64(n: u64) -> bool {
    if n <= 1 {
        return false;
    }
    if n <= 3 {
        return true;
    }
    if n % 2 == 0 {
        return false;
    }

    let sqrt_n = (n as f64).sqrt() as u64;
    let mut i = 3;
    while i <= sqrt_n {
        if n % i == 0 {
            return false;
        }
        i += 2;
    }
    true
}

/// GCD using native u64.
#[inline]
fn gcd_u64(mut a: u64, mut b: u64) -> u64 {
    while b != 0 {
        let t = b;
        b = a % b;
        a = t;
    }
    a
}

// =============================================================================
// u128 modular arithmetic (for Miller-Rabin on medium numbers)
// =============================================================================

/// Modular exponentiation: (base^exp) % modulus, using u128 to avoid overflow.
fn mod_pow_u128(mut base: u128, mut exp: u128, modulus: u128) -> u128 {
    if modulus == 1 {
        return 0;
    }
    let mut result: u128 = 1;
    base %= modulus;
    while exp > 0 {
        if exp & 1 == 1 {
            result = mul_mod_u128(result, base, modulus);
        }
        exp >>= 1;
        base = mul_mod_u128(base, base, modulus);
    }
    result
}

/// Multiply two u128 values modulo m without overflow.
/// Uses the fact that if both a, b < m and m < 2^64, then a*b fits in u128.
/// For larger m, uses Russian peasant multiplication.
#[inline]
fn mul_mod_u128(a: u128, b: u128, m: u128) -> u128 {
    if m <= u64::MAX as u128 {
        // Both a, b < m <= 2^64, so a*b <= (2^64-1)^2 < 2^128. Safe.
        (a * b) % m
    } else {
        // Russian peasant multiplication for very large moduli
        let mut result: u128 = 0;
        let mut a = a % m;
        let mut b = b;
        while b > 0 {
            if b & 1 == 1 {
                result = (result + a) % m;
            }
            a = (a + a) % m;
            b >>= 1;
        }
        result
    }
}

/// Factor n-1 = 2^s * d where d is odd. Native u128 version.
fn factor_u128(n: u128) -> (u32, u128) {
    let mut s = 0u32;
    let mut d = n - 1;
    while d & 1 == 0 {
        s += 1;
        d >>= 1;
    }
    (s, d)
}

/// Miller-Rabin strong pseudoprime test using native u128.
fn strong_pseudoprime_u128(n: u128, base: u128) -> bool {
    if n & 1 == 0 || n <= 1 {
        return false;
    }

    let (s, d) = factor_u128(n);
    let n_minus_1 = n - 1;
    let mut x = mod_pow_u128(base, d, n);

    if x == 1 || x == n_minus_1 {
        return true;
    }

    for _ in 0..s - 1 {
        x = mul_mod_u128(x, x, n);
        if x == n_minus_1 {
            return true;
        }
        if x == 1 {
            return false;
        }
    }

    false
}

/// Deterministic Miller-Rabin for numbers that fit in u128.
/// Bases [2, 13, 23, 1662803] are sufficient for n < 2,047,698,921.
fn small_strong_pseudoprime_u128(n: u128) -> bool {
    for base in [2u128, 13, 23, 1662803] {
        if !strong_pseudoprime_u128(n, base) {
            return false;
        }
    }
    true
}

// =============================================================================
// BigUint primality (fallback for numbers > u128::MAX)
// =============================================================================

fn factor_big(n: &BigUint) -> (u64, BigUint) {
    let mut s: u64 = 0;
    let mut d = n - BigUint::one();
    let two = BigUint::from(2u32);

    while (&d % &two).is_zero() {
        s += 1;
        d /= &two;
    }

    (s, d)
}

fn strong_pseudoprime_big(n: &BigUint, base: u64) -> bool {
    let (s, d) = factor_big(n);
    let n_minus_1 = n - BigUint::one();
    let mut x = BigUint::from(base).modpow(&d, n);

    if x == BigUint::one() || x == n_minus_1 {
        return true;
    }

    let two = BigUint::from(2u32);
    for _ in 0..s - 1 {
        x = x.modpow(&two, n);
        if x == n_minus_1 {
            return true;
        }
        if x == BigUint::one() {
            return false;
        }
    }

    false
}

// =============================================================================
// Public primality API
// =============================================================================

/// Determines if an integer is prime.
pub fn is_prime(n: &BigUint) -> bool {
    // Fast path: if it fits in u64, use native arithmetic
    if let Ok(n_u64) = TryInto::<u64>::try_into(n) {
        return is_prime_u64(n_u64);
    }

    // For large numbers, use BigUint Miller-Rabin
    if let Ok(n_u128) = TryInto::<u128>::try_into(n) {
        return is_prime_u128(n_u128);
    }

    // Very large: BigUint path
    let two = BigUint::from(2u32);
    if (n % &two).is_zero() {
        return false;
    }
    for base in [2u64, 13, 23, 1662803] {
        if !strong_pseudoprime_big(n, base) {
            return false;
        }
    }
    true
}

#[inline]
fn is_prime_u64(n: u64) -> bool {
    if n == 2 {
        return true;
    }
    if n < 2 || n % 2 == 0 {
        return false;
    }

    // gcd(n, 510510) > 1 check — 510510 = 2*3*5*7*11*13*17
    if gcd_u64(n, 510510) > 1 {
        return matches!(n, 3 | 5 | 7 | 11 | 13 | 17);
    }

    if n < 2_000_000 {
        return trial_division_u64(n);
    }

    small_strong_pseudoprime_u128(n as u128)
}

fn is_prime_u128(n: u128) -> bool {
    if n == 2 {
        return true;
    }
    if n < 2 || n % 2 == 0 {
        return false;
    }

    // gcd check (small factor sieve)
    if n <= u64::MAX as u128 {
        return is_prime_u64(n as u64);
    }

    // Large u128: Miller-Rabin
    small_strong_pseudoprime_u128(n)
}

// =============================================================================
// Next prime
// =============================================================================

const GAP: [u64; 30] = [
    1, 6, 5, 4, 3, 2, 1, 4, 3, 2, 1, 2, 1, 4, 3, 2, 1, 2, 1, 4, 3, 2, 1, 6, 5, 4, 3, 2, 1, 2,
];

/// Find the next prime strictly greater than n, using native u128 arithmetic.
fn next_prime_u128(n: u128) -> u128 {
    if n < 2 {
        return 2;
    }
    if n < 5 {
        return match n {
            2 => 3,
            3 | 4 => 5,
            _ => unreachable!(),
        };
    }

    let mut candidate = n + 1 + (n & 1);

    if candidate % 3 == 0 || candidate % 5 == 0 {
        candidate += GAP[(candidate % 30) as usize] as u128;
    }

    while !is_prime_u128(candidate) {
        candidate += GAP[(candidate % 30) as usize] as u128;
    }

    candidate
}

/// Finds the next prime number strictly greater than n.
pub fn next_prime(n: &BigUint) -> Result<BigUint, ObfuskeyError> {
    if n.bits() > 512 {
        return Err(ObfuskeyError::MaximumValueError(
            "For integers larger than 512-bit, prime generation is not supported.".to_string(),
        ));
    }

    // Fast path: if it fits in u128
    if let Ok(n_u128) = TryInto::<u128>::try_into(n) {
        return Ok(BigUint::from(next_prime_u128(n_u128)));
    }

    // BigUint fallback for very large numbers
    let two = BigUint::from(2u32);
    if *n < two {
        return Ok(two);
    }
    if *n < BigUint::from(5u32) {
        let result = match TryInto::<u64>::try_into(n).unwrap() {
            2 => 3u64,
            3 | 4 => 5,
            _ => unreachable!(),
        };
        return Ok(BigUint::from(result));
    }

    let mut candidate = n.clone() + BigUint::one() + (n & BigUint::one());

    let thirty = BigUint::from(30u32);
    let three = BigUint::from(3u32);
    let five = BigUint::from(5u32);

    if (&candidate % &three).is_zero() || (&candidate % &five).is_zero() {
        let idx: usize = (&candidate % &thirty).try_into().unwrap();
        candidate += BigUint::from(GAP[idx]);
    }

    while !is_prime(&candidate) {
        let idx: usize = (&candidate % &thirty).try_into().unwrap();
        candidate += BigUint::from(GAP[idx]);
    }

    Ok(candidate)
}

// =============================================================================
// Modular inverse
// =============================================================================

/// Modular inverse using native u128. Returns x such that (base * x) % modulus == 1.
pub fn mod_inv_u128(base: u128, modulus: u128) -> Result<u128, ObfuskeyError> {
    let base = (base % modulus) as i128;
    let modulus_i = modulus as i128;

    let mut r_prev = modulus_i;
    let mut r_curr = base;
    let mut t_prev: i128 = 0;
    let mut t_curr: i128 = 1;

    while r_curr != 0 {
        let q = r_prev / r_curr;

        let temp_r = r_prev - q * r_curr;
        r_prev = r_curr;
        r_curr = temp_r;

        let temp_t = t_prev - q * t_curr;
        t_prev = t_curr;
        t_curr = temp_t;
    }

    if r_prev != 1 {
        return Err(ObfuskeyError::ValueError(
            "Modular inverse does not exist (values are not coprime).".to_string(),
        ));
    }

    Ok(((t_prev % modulus_i + modulus_i) % modulus_i) as u128)
}

/// Modular inverse using BigUint. Returns x such that (base * x) % modulus == 1.
pub fn mod_inv_big(base: &BigUint, modulus: &BigUint) -> Result<BigUint, ObfuskeyError> {
    use num_bigint::BigInt;

    let mod_int = BigInt::from(modulus.clone());
    let base_int = BigInt::from(base % modulus);

    let mut r_prev = mod_int.clone();
    let mut r_curr = base_int;
    let mut t_prev = BigInt::zero();
    let mut t_curr = BigInt::one();

    while !r_curr.is_zero() {
        let q = &r_prev / &r_curr;

        let temp_r = &r_prev - &q * &r_curr;
        r_prev = r_curr;
        r_curr = temp_r;

        let temp_t = &t_prev - &q * &t_curr;
        t_prev = t_curr;
        t_curr = temp_t;
    }

    if r_prev != BigInt::one() {
        return Err(ObfuskeyError::ValueError(
            "Modular inverse does not exist (values are not coprime).".to_string(),
        ));
    }

    let result = ((t_prev % &mod_int) + &mod_int) % &mod_int;
    Ok(result.to_biguint().unwrap())
}

// =============================================================================
// Prime generation
// =============================================================================

/// Generates a prime multiplier for obfuscation.
/// Uses fixed-point arithmetic (scale = 10^18) to match Python/JS precision.
pub fn generate_prime(
    base: usize,
    key_length: u32,
    prime_multiplier: f64,
) -> Result<BigUint, ObfuskeyError> {
    let base_big = BigUint::from(base);
    let max_value = base_big.pow(key_length) - BigUint::one();

    let scale: u64 = 1_000_000_000_000_000_000; // 10^18
    let multiplier_scaled = (prime_multiplier * scale as f64) as u64;

    let target = (&max_value * BigUint::from(multiplier_scaled)) / BigUint::from(scale);

    next_prime(&target)
}