ttcrypt 0.0.2
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 +7 -0
- data/.gitignore +27 -0
- data/.rspec +2 -0
- data/Gemfile +5 -0
- data/LICENSE.txt +44 -0
- data/README.md +45 -0
- data/Rakefile +10 -0
- data/ext/ttcrypt/.gitignore +1 -0
- data/ext/ttcrypt/big_integer.cpp +59 -0
- data/ext/ttcrypt/big_integer.h +321 -0
- data/ext/ttcrypt/byte_buffer.cpp +84 -0
- data/ext/ttcrypt/byte_buffer.h +352 -0
- data/ext/ttcrypt/common_utils.cpp +24 -0
- data/ext/ttcrypt/common_utils.h +61 -0
- data/ext/ttcrypt/extconf.rb +62 -0
- data/ext/ttcrypt/pollard_rho.cpp +83 -0
- data/ext/ttcrypt/pollard_rho.h +55 -0
- data/ext/ttcrypt/rsa_key.cpp +373 -0
- data/ext/ttcrypt/rsa_key.h +248 -0
- data/ext/ttcrypt/ruby_cpp_tools.cpp +66 -0
- data/ext/ttcrypt/ruby_cpp_tools.h +77 -0
- data/ext/ttcrypt/sha1.cpp +185 -0
- data/ext/ttcrypt/sha1.h +49 -0
- data/ext/ttcrypt/sha256.c +379 -0
- data/ext/ttcrypt/sha256.h +67 -0
- data/ext/ttcrypt/text_utils.cpp +63 -0
- data/ext/ttcrypt/text_utils.h +64 -0
- data/ext/ttcrypt/ttcrypt.cpp +51 -0
- data/ext/ttcrypt/ttcrypt.h +45 -0
- data/ext/ttcrypt/ttcrypt_ruby.cpp +234 -0
- data/lib/ttcrypt/version.rb +5 -0
- data/lib/ttcrypt.rb +142 -0
- data/spec/spec_helper.rb +17 -0
- data/spec/ttcrypt_spec.rb +318 -0
- data/ttcrypt.gemspec +47 -0
- metadata +139 -0
@@ -0,0 +1,352 @@
|
|
1
|
+
//
|
2
|
+
// byte_buffer.h
|
3
|
+
// zcoin
|
4
|
+
//
|
5
|
+
// Created by Sergey Chernov on 02.06.14.
|
6
|
+
// Copyright (c) 2014 thrift. All rights reserved.
|
7
|
+
//
|
8
|
+
|
9
|
+
/*
|
10
|
+
This program is free software: you can redistribute it and/or modify
|
11
|
+
it under the terms of the GNU General Public License as published by
|
12
|
+
the Free Software Foundation, either version 3 of the License, or
|
13
|
+
(at your option) any later version.
|
14
|
+
|
15
|
+
This program is distributed in the hope that it will be useful,
|
16
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18
|
+
GNU General Public License for more details.
|
19
|
+
|
20
|
+
You should have received a copy of the GNU General Public License
|
21
|
+
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
22
|
+
*/
|
23
|
+
|
24
|
+
#ifndef __zcoin__byte_buffer__
|
25
|
+
#define __zcoin__byte_buffer__
|
26
|
+
|
27
|
+
#include <iostream>
|
28
|
+
#include <string.h>
|
29
|
+
#include "text_utils.h"
|
30
|
+
|
31
|
+
using namespace std;
|
32
|
+
namespace thrift {
|
33
|
+
|
34
|
+
typedef unsigned char byte;
|
35
|
+
|
36
|
+
/**
|
37
|
+
Writable memory-effective autorsizing byte buffer. Implements copy-on-write policy,
|
38
|
+
so all instances are independent but share data as long as possible. Actual copying
|
39
|
+
takes place only when some instance starts updating. It is safe and effective to pass
|
40
|
+
it by value and return as value. Is NOT thread safe!
|
41
|
+
*/
|
42
|
+
class byte_buffer {
|
43
|
+
|
44
|
+
private:
|
45
|
+
std::shared_ptr<byte> buffer;
|
46
|
+
size_t length;
|
47
|
+
size_t _capacity;
|
48
|
+
|
49
|
+
public:
|
50
|
+
/** Creates byte buffer from size first bytes from a given memory area.
|
51
|
+
Zero bytes are ok.
|
52
|
+
*/
|
53
|
+
byte_buffer(const void* src, size_t size) : length(size), _capacity(size) {
|
54
|
+
byte *dst = new byte[length];
|
55
|
+
buffer = std::shared_ptr<byte>(dst);
|
56
|
+
memcpy(dst, src, length);
|
57
|
+
}
|
58
|
+
|
59
|
+
byte_buffer(const char* str) : length(strlen(str)) {
|
60
|
+
_capacity = length;
|
61
|
+
byte *dst = new byte[length];
|
62
|
+
buffer = std::shared_ptr<byte>(dst);
|
63
|
+
memcpy(dst, str, length);
|
64
|
+
}
|
65
|
+
|
66
|
+
byte_buffer(const string& str) : length(str.length()) {
|
67
|
+
_capacity = length;
|
68
|
+
byte *dst = new byte[length];
|
69
|
+
buffer = std::shared_ptr<byte>(dst);
|
70
|
+
memcpy(dst, str.c_str(), length);
|
71
|
+
}
|
72
|
+
|
73
|
+
/**
|
74
|
+
Empty constructor
|
75
|
+
*/
|
76
|
+
byte_buffer(size_t size=0) :
|
77
|
+
buffer(new byte[size]),
|
78
|
+
length(size), _capacity(size)
|
79
|
+
{
|
80
|
+
}
|
81
|
+
|
82
|
+
/** Range constructor. Copies a range of the other buffer. range is inclusive
|
83
|
+
e,g. (x,0,1) will extract TWO bytes. Negative indexes mean from the end of the
|
84
|
+
source, e.g. (x, -2, -1) returns 2 last elements. Constructs empty buffer if requested
|
85
|
+
range is empty or invalid.
|
86
|
+
@param source
|
87
|
+
@param from_index index of the first extracted element
|
88
|
+
@param to_index index of the last extracred element
|
89
|
+
*/
|
90
|
+
template <class I1,class I2>
|
91
|
+
byte_buffer(const byte_buffer& source, I1 from_index, I2 to_index) {
|
92
|
+
int from = (int) from_index;
|
93
|
+
int to = (int) to_index;
|
94
|
+
if( from < 0 ) from = (int) source.size() + from;
|
95
|
+
if( to < 0 ) to = (int) source.size() + to;
|
96
|
+
if( from < 0 ) from = 0;
|
97
|
+
if( to < 0 ) to = 0;
|
98
|
+
|
99
|
+
if( from >= to ) {
|
100
|
+
// Empty buffer
|
101
|
+
_capacity = 32;
|
102
|
+
length = 0;
|
103
|
+
buffer.reset( new byte[_capacity] );
|
104
|
+
}
|
105
|
+
else {
|
106
|
+
length = to - from + 1;
|
107
|
+
_capacity = length;
|
108
|
+
buffer.reset(new byte[_capacity]);
|
109
|
+
memcpy(buffer.get(), source.buffer.get()+from, length);
|
110
|
+
}
|
111
|
+
}
|
112
|
+
|
113
|
+
|
114
|
+
/**
|
115
|
+
Fill constructor. Create buffer with specified size and fill value
|
116
|
+
*/
|
117
|
+
byte_buffer(byte fill, size_t size)
|
118
|
+
: buffer(new byte[size]), length(size), _capacity(size) {
|
119
|
+
memset( buffer.get(), fill, size);
|
120
|
+
}
|
121
|
+
|
122
|
+
shared_ptr<byte> data() const noexcept {
|
123
|
+
return buffer;
|
124
|
+
}
|
125
|
+
|
126
|
+
size_t size() const { return length; }
|
127
|
+
|
128
|
+
size_t capacity() const { return _capacity; }
|
129
|
+
|
130
|
+
long use_count() const { return buffer.use_count(); }
|
131
|
+
|
132
|
+
/**
|
133
|
+
Concatenate buffers
|
134
|
+
*/
|
135
|
+
byte_buffer operator + (const byte_buffer& other) const {
|
136
|
+
byte_buffer res(length + other.length);
|
137
|
+
memcpy(res.buffer.get(), buffer.get(), length);
|
138
|
+
memcpy(res.buffer.get() + length, other.buffer.get(), other.length);
|
139
|
+
return res;
|
140
|
+
}
|
141
|
+
|
142
|
+
/**
|
143
|
+
byte access (readonly), same as get(int)
|
144
|
+
*/
|
145
|
+
template<class T>
|
146
|
+
byte operator[] (T index) const {
|
147
|
+
return at(index);
|
148
|
+
}
|
149
|
+
|
150
|
+
/**
|
151
|
+
byte acces (readonly)
|
152
|
+
*/
|
153
|
+
template<class T>
|
154
|
+
byte at(T _index) const {
|
155
|
+
int index = (int)_index;
|
156
|
+
prepare_index(index);
|
157
|
+
return buffer.get()[index];
|
158
|
+
}
|
159
|
+
|
160
|
+
/**
|
161
|
+
byte update. negative indexes (if used) mean offset from the end, e.g. -1 is the
|
162
|
+
last item.
|
163
|
+
*/
|
164
|
+
template<class T> byte set(T _index, byte value) {
|
165
|
+
int index = (int) _index;
|
166
|
+
prepare_index(index);
|
167
|
+
ensure_unique_owner();
|
168
|
+
return (buffer.get()[index] = value);
|
169
|
+
}
|
170
|
+
|
171
|
+
/**
|
172
|
+
Convert to an ASCII string. Non-ASCII characters are substituted by dots.
|
173
|
+
*/
|
174
|
+
string to_string() const noexcept {
|
175
|
+
string res;
|
176
|
+
for( size_t i=0; i<length; i++) {
|
177
|
+
byte b = buffer.get()[i];
|
178
|
+
if( b < ' ' || b > 'z') b = '.';
|
179
|
+
res.append(1,(char)b);
|
180
|
+
}
|
181
|
+
return res;
|
182
|
+
}
|
183
|
+
|
184
|
+
/**
|
185
|
+
XOR this and other byte_buffer which should have the same size().
|
186
|
+
|
187
|
+
@return new byte_buffer which is XOR of this and other byte_buffer's.
|
188
|
+
*/
|
189
|
+
byte_buffer operator^(const byte_buffer& other) const {
|
190
|
+
if( length != other.length)
|
191
|
+
throw length_error(sformat("byte buffers should have same size (my:%d != other:%d)", length, other.length));
|
192
|
+
byte_buffer res(length);
|
193
|
+
|
194
|
+
const byte* src1 = buffer.get();
|
195
|
+
const byte* src2 = other.buffer.get();
|
196
|
+
byte* dst = res.buffer.get();
|
197
|
+
|
198
|
+
for(size_t i=length; i-- > 0;)
|
199
|
+
*dst++ = *src1++ ^ *src2++;
|
200
|
+
|
201
|
+
return res;
|
202
|
+
}
|
203
|
+
|
204
|
+
/**
|
205
|
+
Find first occurency of a given value starting from a given point
|
206
|
+
@param value to find
|
207
|
+
@param start_from starting index, -1 means the last byte
|
208
|
+
@return index of value or -1 if not found
|
209
|
+
*/
|
210
|
+
int index_of(byte value,int start_from=0) {
|
211
|
+
int i = start_from;
|
212
|
+
prepare_index(i);
|
213
|
+
while( i < length ) {
|
214
|
+
if( buffer.get()[i] == value )
|
215
|
+
return i;
|
216
|
+
i++;
|
217
|
+
}
|
218
|
+
return -1;
|
219
|
+
}
|
220
|
+
|
221
|
+
string hex(unsigned per_string=24) const {
|
222
|
+
string hex;
|
223
|
+
size_t n = 0;
|
224
|
+
for( unsigned i=0; i < length; i++ ) {
|
225
|
+
if( i > 0 ) {
|
226
|
+
hex.append(1,' ');
|
227
|
+
if( ++n % per_string == 0 )
|
228
|
+
hex.append("\n");
|
229
|
+
}
|
230
|
+
hex.append(sformat("%02x", buffer.get()[i]));
|
231
|
+
}
|
232
|
+
return hex;
|
233
|
+
}
|
234
|
+
|
235
|
+
/**
|
236
|
+
@return true is buffers have the same size and contents/
|
237
|
+
*/
|
238
|
+
bool operator == (const byte_buffer& other) const noexcept {
|
239
|
+
if( other.length != length )
|
240
|
+
return false;
|
241
|
+
return memcmp(buffer.get(), other.buffer.get(), length) == 0;
|
242
|
+
}
|
243
|
+
|
244
|
+
/**
|
245
|
+
Append one byte to the end of the buffer. Buffer capacity extends as need.
|
246
|
+
*/
|
247
|
+
byte_buffer& append_byte(byte byte_value) {
|
248
|
+
ensure_capacity(length + 1);
|
249
|
+
buffer.get()[length++] = (byte)byte_value;
|
250
|
+
return *this;
|
251
|
+
}
|
252
|
+
|
253
|
+
/** Append another byte_buffer to this
|
254
|
+
*/
|
255
|
+
byte_buffer& operator += (const byte_buffer& other) noexcept;
|
256
|
+
|
257
|
+
class iterator {
|
258
|
+
public:
|
259
|
+
iterator(const byte_buffer& buffer, size_t index=0) : _buffer(buffer), _index(index) {}
|
260
|
+
|
261
|
+
byte operator*() const { return (byte)_buffer[_index]; }
|
262
|
+
iterator& operator ++() { _index++; return *this; }
|
263
|
+
bool operator!=(const byte_buffer::iterator& x) const { return x._index != _index; }
|
264
|
+
|
265
|
+
private:
|
266
|
+
const byte_buffer& _buffer;
|
267
|
+
size_t _index;
|
268
|
+
};
|
269
|
+
|
270
|
+
byte_buffer::iterator begin() const { return byte_buffer::iterator(*this, 0); }
|
271
|
+
byte_buffer::iterator end() const { return byte_buffer::iterator(*this, size()); }
|
272
|
+
|
273
|
+
/**
|
274
|
+
Extract subbufer [from..to] inclusive. Negative indexes means offset
|
275
|
+
from the end, e.g.
|
276
|
+
|
277
|
+
x.sub(0, -1) == x
|
278
|
+
*/
|
279
|
+
byte_buffer sub(int from,int to) const {
|
280
|
+
prepare_index(from);
|
281
|
+
prepare_index(to);
|
282
|
+
return byte_buffer(*this, from, to);
|
283
|
+
}
|
284
|
+
|
285
|
+
static byte_buffer random(size_t length);
|
286
|
+
|
287
|
+
protected:
|
288
|
+
shared_ptr<byte> clone_data(size_t new_capacity) const {
|
289
|
+
shared_ptr<byte> cloned(new byte[new_capacity]);
|
290
|
+
memcpy(cloned.get(), buffer.get(), length);
|
291
|
+
return cloned;
|
292
|
+
}
|
293
|
+
|
294
|
+
void ensure_unique_owner() noexcept {
|
295
|
+
if( buffer.use_count() > 1 ) {
|
296
|
+
buffer = clone_data(_capacity);
|
297
|
+
}
|
298
|
+
}
|
299
|
+
|
300
|
+
void ensure_capacity(size_t min_capacity) {
|
301
|
+
if( _capacity <= min_capacity ) {
|
302
|
+
auto new_capacity = ((min_capacity + 63) >> 6) << 6;
|
303
|
+
if( new_capacity < 64 )
|
304
|
+
new_capacity = 64;
|
305
|
+
buffer = clone_data(new_capacity);
|
306
|
+
_capacity = new_capacity;
|
307
|
+
}
|
308
|
+
else {
|
309
|
+
ensure_unique_owner();
|
310
|
+
}
|
311
|
+
}
|
312
|
+
|
313
|
+
int prepare_index(int& index) const noexcept {
|
314
|
+
if( index < 0 ) index = (int)length + index;
|
315
|
+
if( index < 0 || index > length ) throw length_error("wrong index");
|
316
|
+
return index;
|
317
|
+
}
|
318
|
+
};
|
319
|
+
|
320
|
+
/**
|
321
|
+
Advance to the next non-space character, unless end of string is found.
|
322
|
+
@param src reference to pointer, after this call points to next
|
323
|
+
character after first non-space character, if found, otherwise
|
324
|
+
points to the end of line.
|
325
|
+
@return character or 0;
|
326
|
+
*/
|
327
|
+
char next_non_space_char(const char*& src);
|
328
|
+
|
329
|
+
/**
|
330
|
+
@return weight of the hexadecimal digit 0-1A-F (case insensitive).
|
331
|
+
@throw invalid_argument if the character is not a valid digit
|
332
|
+
*/
|
333
|
+
int hex_digit_value(char hex_digit);
|
334
|
+
|
335
|
+
/**
|
336
|
+
Convert hexadecimal string into a byte_buffer buffer. All space characters are ignored. Note this
|
337
|
+
IS NOT an Intel Hex decoder, just hex notation converter, e.g. 01 A0 -> 0x01 0xa0.
|
338
|
+
|
339
|
+
@param hex string with even number of hexadecimal digit, case insensitive.
|
340
|
+
@return decoded byte_buffer buffer
|
341
|
+
@throws invalid_argument if the given string is not a hex string.
|
342
|
+
*/
|
343
|
+
byte_buffer decode_hex(const string& hex);
|
344
|
+
|
345
|
+
inline bool operator==(const string& str, const byte_buffer& bb) {
|
346
|
+
return bb.size() == str.length() && memcmp(bb.data().get(), str.c_str(), bb.size()) == 0;
|
347
|
+
}
|
348
|
+
}
|
349
|
+
|
350
|
+
|
351
|
+
|
352
|
+
#endif /* defined(__zcoin__byte_buffer__) */
|
@@ -0,0 +1,24 @@
|
|
1
|
+
//
|
2
|
+
// common_utils.cpp
|
3
|
+
// zcoin
|
4
|
+
//
|
5
|
+
// Created by Sergey Chernov on 02.06.14.
|
6
|
+
// Copyright (c) 2014 thrift. All rights reserved.
|
7
|
+
//
|
8
|
+
|
9
|
+
/*
|
10
|
+
This program is free software: you can redistribute it and/or modify
|
11
|
+
it under the terms of the GNU General Public License as published by
|
12
|
+
the Free Software Foundation, either version 3 of the License, or
|
13
|
+
(at your option) any later version.
|
14
|
+
|
15
|
+
This program is distributed in the hope that it will be useful,
|
16
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18
|
+
GNU General Public License for more details.
|
19
|
+
|
20
|
+
You should have received a copy of the GNU General Public License
|
21
|
+
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
22
|
+
*/
|
23
|
+
|
24
|
+
#include "common_utils.h"
|
@@ -0,0 +1,61 @@
|
|
1
|
+
//
|
2
|
+
// common_utils.h
|
3
|
+
// zcoin
|
4
|
+
//
|
5
|
+
// Created by Sergey Chernov on 02.06.14.
|
6
|
+
// Copyright (c) 2014 thrift. All rights reserved.
|
7
|
+
//
|
8
|
+
|
9
|
+
/*
|
10
|
+
This program is free software: you can redistribute it and/or modify
|
11
|
+
it under the terms of the GNU General Public License as published by
|
12
|
+
the Free Software Foundation, either version 3 of the License, or
|
13
|
+
(at your option) any later version.
|
14
|
+
|
15
|
+
This program is distributed in the hope that it will be useful,
|
16
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18
|
+
GNU General Public License for more details.
|
19
|
+
|
20
|
+
You should have received a copy of the GNU General Public License
|
21
|
+
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
22
|
+
*/
|
23
|
+
|
24
|
+
#ifndef zcoin_common_utils_h
|
25
|
+
#define zcoin_common_utils_h
|
26
|
+
|
27
|
+
typedef unsigned char byte;
|
28
|
+
|
29
|
+
template<class valueT, class countT>
|
30
|
+
valueT clear_left_bits(valueT value,countT count) {
|
31
|
+
byte bitmask = 0x80;
|
32
|
+
for( unsigned bit_no=0; bit_no < count; bit_no++ ) {
|
33
|
+
value &= ~bitmask;
|
34
|
+
bitmask >>= 1;
|
35
|
+
}
|
36
|
+
return value;
|
37
|
+
}
|
38
|
+
|
39
|
+
|
40
|
+
template <class A,class B>
|
41
|
+
bool operator !=(const A& a,const B& b) {
|
42
|
+
return !(a == b);
|
43
|
+
}
|
44
|
+
|
45
|
+
template <class A,class B>
|
46
|
+
bool operator >(const A& a,const B& b) {
|
47
|
+
return !(a < b) && !(a == b);
|
48
|
+
}
|
49
|
+
|
50
|
+
template <class A,class B>
|
51
|
+
bool operator <=(const A& a,const B& b) {
|
52
|
+
return a < b || a == b;
|
53
|
+
}
|
54
|
+
|
55
|
+
template <class A,class B>
|
56
|
+
bool operator >=(const A& a,const B& b) {
|
57
|
+
return !(a < b);
|
58
|
+
}
|
59
|
+
|
60
|
+
|
61
|
+
#endif
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# TTCrypt thrift cryptoutuls package
|
2
|
+
# Copyright (C) 2014 by Thrift.
|
3
|
+
#
|
4
|
+
# This program is free software: you can redistribute it and/or modify
|
5
|
+
# it under the terms of the GNU General Public License as published by
|
6
|
+
# the Free Software Foundation, either version 3 of the License, or
|
7
|
+
# (at your option) any later version.
|
8
|
+
#
|
9
|
+
# This program is distributed in the hope that it will be useful,
|
10
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
# GNU General Public License for more details.
|
13
|
+
#
|
14
|
+
# You should have received a copy of the GNU General Public License
|
15
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
16
|
+
|
17
|
+
require "mkmf"
|
18
|
+
|
19
|
+
|
20
|
+
abort 'missing malloc()' unless have_func 'malloc'
|
21
|
+
abort 'missing free()' unless have_func 'free'
|
22
|
+
|
23
|
+
# Give it a name
|
24
|
+
extension_name = 'ttcrypt'
|
25
|
+
|
26
|
+
|
27
|
+
dir_config(extension_name)
|
28
|
+
ok = true
|
29
|
+
|
30
|
+
unless have_header('gmp.h')
|
31
|
+
$stderr.puts "can't find gmp.h, try --with-gmp-include=<path>"
|
32
|
+
ok = false
|
33
|
+
end
|
34
|
+
|
35
|
+
unless have_library('gmp', '__gmpz_init')
|
36
|
+
$stderr.puts "can't find -lgmp, try --with-gmp-lib=<path>"
|
37
|
+
ok = false
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
# This test is actually due to a Clang 3.3 shortcoming, included in OS X 10.9,
|
42
|
+
# fixed in Clang 3.4:
|
43
|
+
# http://llvm.org/releases/3.4/tools/clang/docs/ReleaseNotes.html#new-compiler-flags
|
44
|
+
if try_compile('', '-O6')
|
45
|
+
$CFLAGS += ' -Wall -W -O6 -g'
|
46
|
+
else
|
47
|
+
$CFLAGS += ' -Wall -W -O3 -g'
|
48
|
+
end
|
49
|
+
|
50
|
+
CONFIG['CXXFLAGS'] += " -std=c++11"
|
51
|
+
|
52
|
+
if ok
|
53
|
+
create_makefile(extension_name)
|
54
|
+
else
|
55
|
+
$stderr.puts "*********************************************************************"
|
56
|
+
$stderr.puts "Your compiler was unable to link to GMP while installing the gmp gem."
|
57
|
+
$stderr.puts "Please install GMP before installing the gmp gem."
|
58
|
+
$stderr.puts "*********************************************************************"
|
59
|
+
|
60
|
+
raise "Unable to build, correct above errors and rerun"
|
61
|
+
end
|
62
|
+
|
@@ -0,0 +1,83 @@
|
|
1
|
+
//
|
2
|
+
// pollard_rho.cpp
|
3
|
+
// zcoin
|
4
|
+
//
|
5
|
+
// Created by Sergey Chernov on 10.06.14.
|
6
|
+
// Copyright (c) 2014 thrift. All rights reserved.
|
7
|
+
//
|
8
|
+
|
9
|
+
/*
|
10
|
+
This program is free software: you can redistribute it and/or modify
|
11
|
+
it under the terms of the GNU General Public License as published by
|
12
|
+
the Free Software Foundation, either version 3 of the License, or
|
13
|
+
(at your option) any later version.
|
14
|
+
|
15
|
+
This program is distributed in the hope that it will be useful,
|
16
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18
|
+
GNU General Public License for more details.
|
19
|
+
|
20
|
+
You should have received a copy of the GNU General Public License
|
21
|
+
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
22
|
+
*/
|
23
|
+
|
24
|
+
#include "pollard_rho.h"
|
25
|
+
|
26
|
+
using namespace thrift;
|
27
|
+
using namespace std;
|
28
|
+
|
29
|
+
inline big_integer rho(const big_integer& n) {
|
30
|
+
/*
|
31
|
+
BigInteger divisor;
|
32
|
+
BigInteger c = new BigInteger(N.bitLength(), random);
|
33
|
+
BigInteger x = new BigInteger(N.bitLength(), random);
|
34
|
+
BigInteger xx = x;
|
35
|
+
|
36
|
+
// check divisibility by 2
|
37
|
+
if (N.mod(two).compareTo(BigInteger.ZERO) == 0) return two;
|
38
|
+
|
39
|
+
do {
|
40
|
+
x = x.multiply(x).mod(N).add(c).mod(N);
|
41
|
+
xx = xx.multiply(xx).mod(N).add(c).mod(N);
|
42
|
+
xx = xx.multiply(xx).mod(N).add(c).mod(N);
|
43
|
+
divisor = x.subtract(xx).gcd(N);
|
44
|
+
} while ((divisor.compareTo(BigInteger.ONE)) == 0);
|
45
|
+
|
46
|
+
return divisor;
|
47
|
+
}
|
48
|
+
*/
|
49
|
+
big_integer divisor;
|
50
|
+
auto c = big_integer::random_bits(n.bit_length());
|
51
|
+
auto x = big_integer::random_bits(n.bit_length());
|
52
|
+
auto xx = x;
|
53
|
+
|
54
|
+
if( n.is_even() )
|
55
|
+
return 2;
|
56
|
+
do {
|
57
|
+
x = (((x*x) % n) + c) % n;
|
58
|
+
xx = (((xx*xx) % n) + c) % n;
|
59
|
+
xx = (((xx*xx) % n) + c) % n;
|
60
|
+
divisor = gcd(x - xx, n);
|
61
|
+
}
|
62
|
+
while( divisor == 1 );
|
63
|
+
|
64
|
+
return divisor;
|
65
|
+
}
|
66
|
+
|
67
|
+
void pollard_rho::factor(const big_integer &n) {
|
68
|
+
if( n == 1 )
|
69
|
+
return;
|
70
|
+
if( n.is_prime(repetitions) )
|
71
|
+
factors.push_back(n);
|
72
|
+
else {
|
73
|
+
auto divisor = rho(n);
|
74
|
+
factor(divisor);
|
75
|
+
factor(n/divisor);
|
76
|
+
}
|
77
|
+
}
|
78
|
+
|
79
|
+
vector<big_integer> pollard_rho::factorize(const big_integer &number, int repetitions) {
|
80
|
+
pollard_rho pr(number, repetitions);
|
81
|
+
return pr.factors;
|
82
|
+
}
|
83
|
+
|
@@ -0,0 +1,55 @@
|
|
1
|
+
//
|
2
|
+
// pollard_rho.h
|
3
|
+
// zcoin
|
4
|
+
//
|
5
|
+
// Created by Sergey Chernov on 10.06.14.
|
6
|
+
// Copyright (c) 2014 thrift. All rights reserved.
|
7
|
+
//
|
8
|
+
|
9
|
+
/*
|
10
|
+
This program is free software: you can redistribute it and/or modify
|
11
|
+
it under the terms of the GNU General Public License as published by
|
12
|
+
the Free Software Foundation, either version 3 of the License, or
|
13
|
+
(at your option) any later version.
|
14
|
+
|
15
|
+
This program is distributed in the hope that it will be useful,
|
16
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18
|
+
GNU General Public License for more details.
|
19
|
+
|
20
|
+
You should have received a copy of the GNU General Public License
|
21
|
+
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
22
|
+
*/
|
23
|
+
|
24
|
+
#ifndef __zcoin__pollard_rho__
|
25
|
+
#define __zcoin__pollard_rho__
|
26
|
+
|
27
|
+
#include <iostream>
|
28
|
+
#include <vector>
|
29
|
+
#include "big_integer.h"
|
30
|
+
|
31
|
+
using namespace std;
|
32
|
+
namespace thrift {
|
33
|
+
|
34
|
+
/**
|
35
|
+
Factorize some number
|
36
|
+
*/
|
37
|
+
class pollard_rho {
|
38
|
+
public:
|
39
|
+
|
40
|
+
static vector<big_integer> factorize(const big_integer& number, int repetitions = 25);
|
41
|
+
|
42
|
+
private:
|
43
|
+
pollard_rho(const big_integer& number,int repetitions) : repetitions(repetitions) {
|
44
|
+
factor(number);
|
45
|
+
}
|
46
|
+
|
47
|
+
void factor(const big_integer& n);
|
48
|
+
|
49
|
+
int repetitions;
|
50
|
+
std::vector<big_integer> factors;
|
51
|
+
};
|
52
|
+
|
53
|
+
}
|
54
|
+
|
55
|
+
#endif /* defined(__zcoin__pollard_rho__) */
|