mtproto 0.0.29 → 0.0.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/ext/factorization/factorization.c +57 -23
- data/lib/mtproto/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 573ffb4ecced33cf4a62f7d2faab6ad88d9903246732dc521f3193c8bba3a0dd
|
|
4
|
+
data.tar.gz: 68f6fb65b7121568e5a1ca7c46daf287430214de1204a7de718d4046d596d4de
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a9e3cd7660f5d50be09e4ec3e68f5d1d84c96e8cfb8a056036b1d41eb1dd4e23fdcc6fb7399d7ea922987f6a18de2be8496c8cabc94bfcd1f1ff020daa5578da
|
|
7
|
+
data.tar.gz: e1fb03e7f7b214788fbc12fdfa859873b809f1483e5e2e008bc07d68e4d751c78b7b4c8a441489f178a809d109efc9ba8273b231ce4450eed2fde6daac85a531
|
|
@@ -1,11 +1,46 @@
|
|
|
1
1
|
#include <ruby.h>
|
|
2
2
|
#include <stdint.h>
|
|
3
|
-
#include <math.h>
|
|
4
3
|
|
|
5
4
|
static VALUE mMTProto;
|
|
6
5
|
static VALUE mCrypto;
|
|
7
6
|
static VALUE mFactorization;
|
|
8
7
|
|
|
8
|
+
static uint64_t
|
|
9
|
+
mulmod(uint64_t a, uint64_t b, uint64_t m)
|
|
10
|
+
{
|
|
11
|
+
return (uint64_t)(((__uint128_t)a * b) % m);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
static uint64_t
|
|
15
|
+
gcd64(uint64_t a, uint64_t b)
|
|
16
|
+
{
|
|
17
|
+
while (b) {
|
|
18
|
+
uint64_t t = a % b;
|
|
19
|
+
a = b;
|
|
20
|
+
b = t;
|
|
21
|
+
}
|
|
22
|
+
return a;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/* Pollard's rho with f(x) = x^2 + c (mod n). Returns a non-trivial factor, or n
|
|
26
|
+
* on failure (cycle with no factor) so the caller can retry with another c. The
|
|
27
|
+
* MTProto pq is a product of two ~31-bit primes, so this finds a factor in a
|
|
28
|
+
* few thousand steps rather than the ~2^31 of trial division. */
|
|
29
|
+
static uint64_t
|
|
30
|
+
pollard_rho(uint64_t n, uint64_t c)
|
|
31
|
+
{
|
|
32
|
+
uint64_t x = 2, y = 2, d = 1;
|
|
33
|
+
while (d == 1) {
|
|
34
|
+
x = (mulmod(x, x, n) + c) % n;
|
|
35
|
+
y = (mulmod(y, y, n) + c) % n;
|
|
36
|
+
y = (mulmod(y, y, n) + c) % n;
|
|
37
|
+
uint64_t diff = x > y ? x - y : y - x;
|
|
38
|
+
if (diff == 0) return n;
|
|
39
|
+
d = gcd64(diff, n);
|
|
40
|
+
}
|
|
41
|
+
return d;
|
|
42
|
+
}
|
|
43
|
+
|
|
9
44
|
static VALUE
|
|
10
45
|
factorize_pq(VALUE self, VALUE pq_bytes)
|
|
11
46
|
{
|
|
@@ -23,33 +58,32 @@ factorize_pq(VALUE self, VALUE pq_bytes)
|
|
|
23
58
|
rb_raise(rb_eArgError, "Number must be > 3");
|
|
24
59
|
}
|
|
25
60
|
|
|
61
|
+
uint64_t p;
|
|
26
62
|
if (n % 2 == 0) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
63
|
+
p = 2;
|
|
64
|
+
} else {
|
|
65
|
+
uint64_t d = n;
|
|
66
|
+
for (uint64_t c = 1; c < 256; c++) {
|
|
67
|
+
d = pollard_rho(n, c);
|
|
68
|
+
if (d != n && d > 1) break;
|
|
69
|
+
}
|
|
70
|
+
if (d == n || d <= 1) {
|
|
71
|
+
rb_raise(rb_eRuntimeError, "No non-trivial factors found (n might be prime)");
|
|
72
|
+
}
|
|
73
|
+
p = d;
|
|
31
74
|
}
|
|
32
75
|
|
|
33
|
-
uint64_t
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
if (p > q) {
|
|
40
|
-
uint64_t tmp = p;
|
|
41
|
-
p = q;
|
|
42
|
-
q = tmp;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
VALUE result = rb_ary_new2(2);
|
|
46
|
-
rb_ary_push(result, ULL2NUM(p));
|
|
47
|
-
rb_ary_push(result, ULL2NUM(q));
|
|
48
|
-
return result;
|
|
49
|
-
}
|
|
76
|
+
uint64_t q = n / p;
|
|
77
|
+
if (p > q) {
|
|
78
|
+
uint64_t tmp = p;
|
|
79
|
+
p = q;
|
|
80
|
+
q = tmp;
|
|
50
81
|
}
|
|
51
82
|
|
|
52
|
-
|
|
83
|
+
VALUE result = rb_ary_new2(2);
|
|
84
|
+
rb_ary_push(result, ULL2NUM(p));
|
|
85
|
+
rb_ary_push(result, ULL2NUM(q));
|
|
86
|
+
return result;
|
|
53
87
|
}
|
|
54
88
|
|
|
55
89
|
void Init_factorization(void)
|
data/lib/mtproto/version.rb
CHANGED