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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f88c032607b33e66a47343f5993bb7884925d17f
4
+ data.tar.gz: 7ca31bdfa0f0401b1e6cbd3c4c0ce8b19f0f6a6a
5
+ SHA512:
6
+ metadata.gz: 3842c52dc96065852cf4d0cf854e0960f165ddb6421cc3efd7b5eae28a1509928949587dd0241caee0a97875cbe823ab07c86070afd5ac33ab4c0464d1cda2da
7
+ data.tar.gz: 0f26cfda18f8aca504cf6a00505fd933326fb03fb4c1262262fb1fc75e02d057d1858cc7a38ba43815d1aa21529265849b0d46b876ece5b7de9e69fbbae7c65b
data/.gitignore ADDED
@@ -0,0 +1,27 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ .settings
24
+ .idea
25
+ .cproject
26
+ .project
27
+ **/.DS_Store
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ttcrypt.gemspec
4
+ gemspec
5
+
data/LICENSE.txt ADDED
@@ -0,0 +1,44 @@
1
+ Copyright (C) 2014 Sergey S. Chernov.
2
+
3
+ This program is free software: you can redistribute it and/or modify
4
+ it under the terms of the GNU General Public License as published by
5
+ the Free Software Foundation, either version 3 of the License, or
6
+ (at your option) any later version.
7
+
8
+ This program is distributed in the hope that it will be useful,
9
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ GNU General Public License for more details.
12
+
13
+ You should have received a copy of the GNU General Public License
14
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
15
+
16
+ ------------------------------------------------------------------------
17
+
18
+ This software includes sha1 implementation by Micael Hildenborg:
19
+
20
+ Copyright (c) 2011, Micael Hildenborg
21
+ All rights reserved.
22
+
23
+ Redistribution and use in source and binary forms, with or without
24
+ modification, are permitted provided that the following conditions are met:
25
+ * Redistributions of source code must retain the above copyright
26
+ notice, this list of conditions and the following disclaimer.
27
+ * Redistributions in binary form must reproduce the above copyright
28
+ notice, this list of conditions and the following disclaimer in the
29
+ documentation and/or other materials provided with the distribution.
30
+ * Neither the name of Micael Hildenborg nor the
31
+ names of its contributors may be used to endorse or promote products
32
+ derived from this software without specific prior written permission.
33
+
34
+ THIS SOFTWARE IS PROVIDED BY Micael Hildenborg ''AS IS'' AND ANY
35
+ EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
36
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37
+ DISCLAIMED. IN NO EVENT SHALL Micael Hildenborg BE LIABLE FOR ANY
38
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
39
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
40
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
41
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
43
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44
+
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # TTCrypt
2
+
3
+ Attention: this version is yet not fully functional.
4
+
5
+ TTCrypt is a fast basic cryptography library written in C++ that implements only string encoded RSA
6
+ variants and othe cryptoprimitives widely used in Thrift projects, namely:
7
+
8
+ * RSAES-OAEP encryption
9
+ * RSASS-PSS signing
10
+ * Pollard 'rho' factorization
11
+ * SHA1 and SHA256 hashes (under development)
12
+ * RJ256/256 (under development)
13
+
14
+ ## Installation
15
+
16
+ Current implementation targeted fro MRI ruby 2.0+.
17
+
18
+ To install your computer should have GMP library installed. Use your target system's packet manager
19
+ (apt, brew, whatever you have) or get it there: https://gmplib.org
20
+
21
+ Then, add this line to your application's Gemfile:
22
+
23
+ gem 'ttcrypt'
24
+
25
+ And then execute:
26
+
27
+ $ bundle
28
+
29
+ Or install it yourself as:
30
+
31
+ $ gem install ttcrypt
32
+
33
+ ## Usage
34
+
35
+ So far you can use rdoc.
36
+
37
+ TODO: Write usage instructions here
38
+
39
+ ## Contributing
40
+
41
+ 1. Fork it ( https://github.com/[my-github-username]/ttcrypt/fork )
42
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
43
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
44
+ 4. Push to the branch (`git push origin my-new-feature`)
45
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/extensiontask"
3
+
4
+ Rake::ExtensionTask.new "ttcrypt" do |ext|
5
+ ext.lib_dir = "lib/ttcrypt"
6
+ ext.source_pattern = "*.{c,cpp}"
7
+ # ext.gem_spec = spec
8
+ end
9
+
10
+
@@ -0,0 +1 @@
1
+ /Makefile
@@ -0,0 +1,59 @@
1
+ //
2
+ // big_integer.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 <stdio.h>
25
+ #include <assert.h>
26
+ #include "big_integer.h"
27
+
28
+ namespace thrift {
29
+
30
+ static byte clear_masks[] = {
31
+ 0, // never used
32
+ 0x7F, // clear only MSB
33
+ 0x3F, 0x1F, 0x0F,
34
+ 0x07, 0x03,
35
+ 0x01 // leave only LSB
36
+ };
37
+
38
+ big_integer big_integer::random_bits(unsigned int n_bits) {
39
+ // big_integer is BIG ENDIAN, e.g. MSB first
40
+ // so we can generate random sequence and mask out high bits
41
+ unsigned n_bytes = (n_bits + 7) / 8;
42
+ unsigned mask_bits = n_bytes*8 - n_bits;
43
+ assert(mask_bits < 8);
44
+ byte_buffer res = byte_buffer::random(n_bytes);
45
+ if( mask_bits != 0 ) {
46
+ res.set(0, res.at(0) & clear_masks[mask_bits]);
47
+ }
48
+ return big_integer(res);
49
+ }
50
+
51
+ big_integer big_integer::random_between(const thrift::big_integer &min, const thrift::big_integer &max) {
52
+ auto delta = max - min;
53
+ auto r = big_integer::random_bits(delta.bit_length());
54
+ if( r > delta )
55
+ r = r % delta;
56
+ return min + r;
57
+ }
58
+
59
+ }
@@ -0,0 +1,321 @@
1
+ //
2
+ // big_integer.h
3
+ // zcoin
4
+ //
5
+ // Created by Sergey Chernov on 03.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_big_integer_h
25
+ #define zcoin_big_integer_h
26
+
27
+ #include <gmp.h>
28
+ #include <string.h>
29
+ #include "byte_buffer.h"
30
+ #include "common_utils.h"
31
+
32
+ using namespace std;
33
+
34
+ namespace thrift {
35
+
36
+
37
+ struct big_divmod_t;
38
+
39
+ class big_integer {
40
+ public:
41
+ big_integer() {
42
+ // log_d("def ctor");
43
+ mpz_init(val);
44
+ }
45
+
46
+ big_integer(long value) {
47
+ // log_d("long ctor %ld",value);
48
+ mpz_init_set_si(val, value);
49
+ }
50
+
51
+ ~big_integer() {
52
+ // It can be moved
53
+ if( *((byte*)&val) != 0 )
54
+ mpz_clear(val);
55
+ }
56
+
57
+ big_integer(const big_integer& x) {
58
+ log_d("copy constructor");
59
+ mpz_init(val);
60
+ mpz_set(val, x.val);
61
+ }
62
+
63
+ big_integer(big_integer&& x) {
64
+ log_d("move constructor");
65
+ memcpy(&val, &x.val, sizeof(val));
66
+ memset((void*)&x.val, 0, sizeof(val));
67
+ }
68
+
69
+ /**
70
+ Import from string representation using a given base
71
+ */
72
+ big_integer(const string& string_value,int base=10) {
73
+ mpz_init_set_str(val, string_value.c_str(), base);
74
+ }
75
+
76
+ /**
77
+ Import from BIG ENDIAN byte array
78
+ */
79
+ big_integer(const byte_buffer& bytes) {
80
+ mpz_init(val);
81
+ // mpz_import (mpz_t rop, size_t count, int order, size_t size, int endian, size_t nails, const void *op)
82
+ mpz_import(val, bytes.size(), 1, 1, 1, 0, bytes.data().get());
83
+ }
84
+
85
+ const big_integer& operator=(const big_integer&& x) noexcept {
86
+ // log_d("move assign");
87
+ memcpy(&val, &x.val, sizeof(val));
88
+ memset((void*)&x.val, 0, sizeof(val));
89
+ return *this;
90
+ }
91
+
92
+ const big_integer& operator=(const big_integer& x) noexcept {
93
+ log_d("copy assign");
94
+ mpz_set(val, x.val);
95
+ return *this;
96
+ }
97
+
98
+ big_integer operator+(const big_integer& b) const noexcept {
99
+ big_integer res;
100
+ mpz_add(res.val, val, b.val);
101
+ return res;
102
+ }
103
+
104
+ // big_integer& operator+=(const big_integer& b) noexcept {
105
+ // mpz_add(val, val, b.val);
106
+ // return *this;
107
+ // }
108
+
109
+ big_integer operator-(const big_integer& b) const noexcept {
110
+ big_integer res;
111
+ mpz_sub(res.val, val, b.val);
112
+ return res;
113
+ }
114
+
115
+ big_integer operator*(const big_integer& b) const noexcept {
116
+ big_integer res;
117
+ mpz_mul(res.val, val, b.val);
118
+ return res;
119
+ }
120
+
121
+ big_integer operator/(const big_integer& b) const noexcept {
122
+ big_integer res;
123
+ mpz_fdiv_q(res.val, val, b.val);
124
+ return res;
125
+ }
126
+
127
+ big_integer operator % (const big_integer& d) const noexcept {
128
+ big_integer res;
129
+ mpz_fdiv_r(res.val, val, d.val);
130
+ return res;
131
+ }
132
+
133
+ big_integer operator <<(unsigned shift) const noexcept {
134
+ big_integer res;
135
+ mpz_mul_2exp(res.val, val, shift);
136
+ return res;
137
+ }
138
+
139
+ bool operator==(const big_integer& other) const noexcept {
140
+ return mpz_cmp(val, other.val) == 0;
141
+ }
142
+
143
+ bool operator==(long other) const noexcept {
144
+ return mpz_cmp_si(val, other) == 0;
145
+ }
146
+
147
+ bool operator<(const big_integer& other) const noexcept {
148
+ return mpz_cmp(val, other.val) < 0;
149
+ }
150
+
151
+ bool operator<(long other) const noexcept {
152
+ return mpz_cmp_si(val, other) < 0;
153
+ }
154
+
155
+ bool is_odd() const noexcept {
156
+ return mpz_odd_p(val);
157
+ }
158
+
159
+ bool is_even() const noexcept {
160
+ return mpz_even_p(val);
161
+ }
162
+
163
+ size_t size_in_base(int base) {
164
+ return mpz_sizeinbase(val, base);
165
+ }
166
+
167
+ big_integer operator-() const noexcept {
168
+ big_integer res;
169
+ mpz_neg(res.val, val);
170
+ return res;
171
+ }
172
+
173
+ unsigned bit_length() const noexcept {
174
+ return (unsigned)mpz_sizeinbase(val, 2);
175
+ }
176
+
177
+ string to_string(int base=10) const noexcept {
178
+ size_t length = mpz_sizeinbase(val, base) + 2;
179
+ char *buffer = new char[length];
180
+ mpz_get_str(buffer, base, val);
181
+ string res = string(buffer);
182
+ delete buffer;
183
+ return res;
184
+ }
185
+
186
+ string hex() const noexcept { return to_string(16); }
187
+
188
+ long to_long() const noexcept {
189
+ return mpz_get_si(val);
190
+ }
191
+
192
+ /**
193
+ Convert to BIE ENDIAN bytes array.
194
+ */
195
+ byte_buffer to_byte_buffer() const noexcept {
196
+ size_t count = (mpz_sizeinbase (val, 2) + 7) / 8;
197
+ byte_buffer res = byte_buffer(count);
198
+ size_t count2 = count;
199
+ // mpz_export (void *rop, size_t *countp, int order, size_t size, int endian, size_t nails, const mpz_t op)
200
+ mpz_export( res.data().get(), &count2, 1, 1, 1, 0, val);
201
+ if( count != count2 && count2 != 0 )
202
+ throw logic_error("mpz export logic failed");
203
+ return res;
204
+ }
205
+
206
+ /**
207
+ Test that *this is probably a prime.
208
+ @param reps number of Rabin-Miller tests. 25 gives an error probability 2e-50 that is usually
209
+ sufficient.
210
+ */
211
+ bool is_prime(int reps=25) const noexcept {
212
+ return mpz_probab_prime_p(val, reps) != 0;
213
+ }
214
+
215
+ /**
216
+ Generate random integer I such as 0 <= I <= 2 ** n_bits. Uses byte_buffer (good) uniform
217
+ random engine.
218
+ */
219
+ static big_integer random_bits(unsigned n_bits);
220
+
221
+ /**
222
+ Generate random integer in the given INCLUSIVE interval
223
+ */
224
+ static big_integer random_between(const big_integer& min,const big_integer& max);
225
+
226
+ friend big_integer operator*(int a,const big_integer& b) noexcept;
227
+ friend big_integer pow(const big_integer& x,unsigned long y) noexcept;
228
+ friend big_integer powmod(const big_integer& x,const big_integer& y,const big_integer& mod) noexcept;
229
+ friend big_integer powmod_sec(const big_integer& x,const big_integer& y,const big_integer& mod) noexcept;
230
+ friend big_integer abs(const big_integer& x) noexcept;
231
+ friend big_integer inverse(const big_integer& u,const big_integer& v) noexcept;
232
+
233
+ friend big_integer lcm(const big_integer& u,const big_integer& v) noexcept;
234
+ friend big_integer gcd(const big_integer& u,const big_integer& v) noexcept;
235
+
236
+ friend big_integer next_prime(const big_integer& i) noexcept;
237
+
238
+ friend big_divmod_t divmod(const big_integer& n, const big_integer& d) noexcept;
239
+
240
+ private:
241
+ mpz_t val;
242
+ };
243
+
244
+ inline big_integer operator "" _b(const char* str) {
245
+ return big_integer(str, 10);
246
+ }
247
+
248
+ inline big_integer operator*(int a,const big_integer& b) noexcept {
249
+ big_integer res;
250
+ mpz_mul_si(res.val,b.val, a);
251
+ return res;
252
+ }
253
+
254
+ inline big_integer pow(const big_integer& x,unsigned long y) noexcept {
255
+ big_integer res;
256
+ mpz_pow_ui(res.val, x.val, y);
257
+ return res;
258
+ }
259
+
260
+ inline big_integer powmod(const big_integer& x,const big_integer& y,const big_integer& mod) noexcept {
261
+ big_integer res;
262
+ mpz_powm(res.val, x.val, y.val, mod.val);
263
+ return res;
264
+ }
265
+
266
+ inline big_integer powmod_sec(const big_integer& x,const big_integer& y,const big_integer& mod) noexcept {
267
+ big_integer res;
268
+ mpz_powm_sec(res.val, x.val, y.val, mod.val);
269
+ return res;
270
+ }
271
+
272
+
273
+ inline string operator+(const string& str, const big_integer& i) {
274
+ return str + i.to_string();
275
+ }
276
+
277
+ inline string string_value(const big_integer &x) noexcept {
278
+ return x.to_string();
279
+ }
280
+
281
+ inline ostream& operator << (ostream& s,const big_integer& value) {
282
+ return s << value.to_string();
283
+ }
284
+
285
+ inline big_integer inverse(const big_integer& u,const big_integer& v) noexcept {
286
+ big_integer res;
287
+ mpz_invert(res.val, u.val, v.val);
288
+ return res;
289
+ }
290
+
291
+ inline big_integer lcm(const big_integer& u,const big_integer& v) noexcept {
292
+ big_integer res;
293
+ mpz_lcm(res.val, u.val, v.val);
294
+ return res;
295
+ }
296
+
297
+ inline big_integer gcd(const big_integer& u,const big_integer& v) noexcept {
298
+ big_integer res;
299
+ mpz_gcd(res.val, u.val, v.val);
300
+ return res;
301
+ }
302
+
303
+ inline big_integer next_prime(const big_integer& i) noexcept {
304
+ big_integer next;
305
+ mpz_nextprime(next.val, i.val);
306
+ return next;
307
+ }
308
+
309
+ struct big_divmod_t {
310
+ big_integer q;
311
+ big_integer r;
312
+ };
313
+
314
+ inline big_divmod_t divmod(const big_integer& n,const big_integer& d) noexcept {
315
+ big_divmod_t result;
316
+ mpz_fdiv_qr(result.q.val, result.r.val, n.val, d.val);
317
+ return result;
318
+ }
319
+ }
320
+
321
+ #endif
@@ -0,0 +1,84 @@
1
+ //
2
+ // byte_buffer.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 <random>
25
+ #include "byte_buffer.h"
26
+ #include "common_utils.h"
27
+ #include "text_utils.h"
28
+
29
+ namespace thrift {
30
+
31
+ char next_non_space_char(const char*& src) {
32
+ char ch;
33
+ while( (ch=*src) != 0 ) {
34
+ src++;
35
+ switch (ch) {
36
+ case ' ': case '\t': case '\n': case '\r':
37
+ break;
38
+ default:
39
+ return ch;
40
+ }
41
+ }
42
+ return 0;
43
+ }
44
+
45
+ int hex_digit_value(char hex_digit) {
46
+ char c = toupper(hex_digit);
47
+ if( c >= 'A' && c <= 'F' )
48
+ return c - 'A' + 10;
49
+ if( c >= '0' && c <= '9' )
50
+ return c - '0';
51
+ throw new invalid_argument(sformat("Invalid hex character %c", c));
52
+ }
53
+
54
+ byte_buffer decode_hex(const string& hex) {
55
+ const char *ptr = hex.c_str();
56
+ byte_buffer res;
57
+ do {
58
+ char c1 = next_non_space_char(ptr);
59
+ if( !c1 ) break;
60
+ char c2 = next_non_space_char(ptr);
61
+ if( !c2 )
62
+ throw invalid_argument("hex has uneven number of digits");
63
+ res.append_byte( (hex_digit_value(c1) << 4) + hex_digit_value(c2) );
64
+ } while(true);
65
+ return res;
66
+ }
67
+
68
+ byte_buffer& byte_buffer::operator += (const byte_buffer& other) noexcept {
69
+ ensure_capacity(length + other.length);
70
+ memcpy(buffer.get() + length, other.buffer.get(), other.length);
71
+ length += other.length;
72
+ return *this;
73
+ }
74
+
75
+ static auto random_engine = std::mt19937_64(std::random_device{}());
76
+
77
+ byte_buffer byte_buffer::random(size_t size) {
78
+ byte_buffer result(size);
79
+ for(size_t i=0; i<size; i++)
80
+ result.set(i, (byte)random_engine());
81
+ return result;
82
+ }
83
+ }
84
+