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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0a50a3572643e17c2435e3b310979e9a9ddbbf498bf8febb5819d96a73443899
4
- data.tar.gz: 622d75782346d1cf190e3daa6097305afc3520b5fe5834b727a79bf4c0e86b36
3
+ metadata.gz: 573ffb4ecced33cf4a62f7d2faab6ad88d9903246732dc521f3193c8bba3a0dd
4
+ data.tar.gz: 68f6fb65b7121568e5a1ca7c46daf287430214de1204a7de718d4046d596d4de
5
5
  SHA512:
6
- metadata.gz: 7ec7ee0beac3ee1143720d8e4972dba6a6f7db379069b0cc2a3274b5c81becb8e1353f448654b4c6c9d0385472d4985725292da56c040a50476ec94d2f980a93
7
- data.tar.gz: c0951ce0991d6c960f6963647849156333bf5bebe4d59b0857af6703707430249c54bc4805be7b8e2527df84141f68dce9c43e6668ca5200eb4f4b7f3d146d4c
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
- VALUE result = rb_ary_new2(2);
28
- rb_ary_push(result, ULL2NUM(2));
29
- rb_ary_push(result, ULL2NUM(n / 2));
30
- return result;
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 limit = (uint64_t)sqrt((double)n) + 1;
34
- for (uint64_t i = 3; i < limit; i += 2) {
35
- if (n % i == 0) {
36
- uint64_t p = i;
37
- uint64_t q = n / i;
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
- rb_raise(rb_eRuntimeError, "No non-trivial factors found (n might be prime)");
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)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MTProto
4
- VERSION = '0.0.29'
4
+ VERSION = '0.0.30'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mtproto
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.29
4
+ version: 0.0.30
5
5
  platform: ruby
6
6
  authors:
7
7
  - Artem Levenkov