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
|
# coding: us-ascii
# frozen_string_literal: false
require_relative 'utils'
require "prime"
if defined?(OpenSSL::TestUtils)
class OpenSSL::TestBN < OpenSSL::TestCase
def setup
super
@e1 = OpenSSL::BN.new(999.to_s(16), 16) # OpenSSL::BN.new(str, 16) must be most stable
@e2 = OpenSSL::BN.new("-" + 999.to_s(16), 16)
@e3 = OpenSSL::BN.new((2**107-1).to_s(16), 16)
@e4 = OpenSSL::BN.new("-" + (2**107-1).to_s(16), 16)
end
def test_new
assert_equal(@e1, OpenSSL::BN.new("999"))
assert_equal(@e1, OpenSSL::BN.new("999", 10))
assert_equal(@e1, OpenSSL::BN.new("\x03\xE7", 2))
assert_equal(@e1, OpenSSL::BN.new("\x00\x00\x00\x02\x03\xE7", 0))
assert_equal(@e2, OpenSSL::BN.new("-999"))
assert_equal(@e2, OpenSSL::BN.new("-999", 10))
assert_equal(@e2, OpenSSL::BN.new("\x00\x00\x00\x02\x83\xE7", 0))
assert_equal(@e3, OpenSSL::BN.new((2**107-1).to_s))
assert_equal(@e3, OpenSSL::BN.new((2**107-1).to_s, 10))
assert_equal(@e3, OpenSSL::BN.new("\a\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 2))
assert_equal(@e3, OpenSSL::BN.new("\x00\x00\x00\x0E\a\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 0))
assert_equal(@e4, OpenSSL::BN.new("-" + (2**107-1).to_s))
assert_equal(@e4, OpenSSL::BN.new("-" + (2**107-1).to_s, 10))
assert_equal(@e4, OpenSSL::BN.new("\x00\x00\x00\x0E\x87\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 0))
e1copy = OpenSSL::BN.new(@e1)
assert_equal(@e1, e1copy)
e1copy.clear_bit!(0) #=> 998
assert_not_equal(@e1, e1copy)
assert_equal(@e1, OpenSSL::BN.new(999))
assert_equal(@e2, OpenSSL::BN.new(-999))
assert_equal(@e3, OpenSSL::BN.new(2**107-1))
assert_equal(@e4, OpenSSL::BN.new(-(2**107-1)))
assert_equal(@e1, 999.to_bn)
assert_equal(@e2, -999.to_bn)
assert_equal(@e3, (2**107-1).to_bn)
assert_equal(@e4, (-(2**107-1)).to_bn)
end
def test_to_str
assert_equal("999", @e1.to_s(10))
assert_equal("-999", @e2.to_s(10))
assert_equal((2**107-1).to_s, @e3.to_s(10))
assert_equal((-(2**107-1)).to_s, @e4.to_s(10))
assert_equal("999", @e1.to_s)
assert_equal("03E7", @e1.to_s(16))
assert_equal("-03E7", @e2.to_s(16))
assert_equal("07FFFFFFFFFFFFFFFFFFFFFFFFFF", @e3.to_s(16))
assert_equal("-07FFFFFFFFFFFFFFFFFFFFFFFFFF", @e4.to_s(16))
assert_equal("\x03\xe7", @e1.to_s(2))
assert_equal("\x03\xe7", @e2.to_s(2))
assert_equal("\x07\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff", @e3.to_s(2))
assert_equal("\x07\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff", @e4.to_s(2))
assert_equal("\x00\x00\x00\x02\x03\xe7", @e1.to_s(0))
assert_equal("\x00\x00\x00\x02\x83\xe7", @e2.to_s(0))
assert_equal("\x00\x00\x00\x0e\x07\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff", @e3.to_s(0))
assert_equal("\x00\x00\x00\x0e\x87\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff", @e4.to_s(0))
end
def test_to_int
assert_equal(999, @e1.to_i)
assert_equal(-999, @e2.to_i)
assert_equal(2**107-1, @e3.to_i)
assert_equal(-(2**107-1), @e4.to_i)
assert_equal(999, @e1.to_int)
end
def test_coerce
assert_equal(["", "-999"], @e2.coerce(""))
assert_equal([1000, -999], @e2.coerce(1000))
assert_raise(TypeError) { @e2.coerce(Class.new.new) }
end
def test_zero_p
assert_equal(true, 0.to_bn.zero?)
assert_equal(false, 1.to_bn.zero?)
end
def test_one_p
assert_equal(true, 1.to_bn.one?)
assert_equal(false, 2.to_bn.one?)
end
def test_odd_p
assert_equal(true, 1.to_bn.odd?)
assert_equal(false, 2.to_bn.odd?)
end
def test_negative_p
assert_equal(false, 0.to_bn.negative?)
assert_equal(false, @e1.negative?)
assert_equal(true, @e2.negative?)
end
def test_sqr
assert_equal(1, 1.to_bn.sqr)
assert_equal(100, 10.to_bn.sqr)
end
def test_four_ops
assert_equal(3, 1.to_bn + 2)
assert_equal(-1, 1.to_bn + -2)
assert_equal(-1, 1.to_bn - 2)
assert_equal(3, 1.to_bn - -2)
assert_equal(2, 1.to_bn * 2)
assert_equal(-2, 1.to_bn * -2)
assert_equal([0, 1], 1.to_bn / 2)
assert_equal([2, 0], 2.to_bn / 1)
assert_raise(OpenSSL::BNError) { 1.to_bn / 0 }
end
def test_unary_plus_minus
assert_equal(999, +@e1)
assert_equal(-999, +@e2)
assert_equal(-999, -@e1)
assert_equal(+999, -@e2)
end
def test_mod
assert_equal(1, 1.to_bn % 2)
assert_equal(0, 2.to_bn % 1)
assert_equal(-2, -2.to_bn % 7)
end
def test_exp
assert_equal(1, 1.to_bn ** 5)
assert_equal(32, 2.to_bn ** 5)
end
def test_gcd
assert_equal(1, 7.to_bn.gcd(5))
assert_equal(8, 24.to_bn.gcd(16))
end
def test_mod_sqr
assert_equal(4, 3.to_bn.mod_sqr(5))
assert_equal(0, 59.to_bn.mod_sqr(59))
end
def test_mod_inverse
assert_equal(2, 3.to_bn.mod_inverse(5))
assert_raise(OpenSSL::BNError) { 3.to_bn.mod_inverse(6) }
end
def test_mod_add
assert_equal(1, 3.to_bn.mod_add(5, 7))
assert_equal(2, 3.to_bn.mod_add(5, 3))
assert_equal(5, 3.to_bn.mod_add(-5, 7))
end
def test_mod_sub
assert_equal(1, 11.to_bn.mod_sub(3, 7))
assert_equal(2, 11.to_bn.mod_sub(3, 3))
assert_equal(5, 3.to_bn.mod_sub(5, 7))
end
def test_mod_mul
assert_equal(1, 2.to_bn.mod_mul(4, 7))
assert_equal(5, 2.to_bn.mod_mul(-1, 7))
end
def test_mod_exp
assert_equal(1, 3.to_bn.mod_exp(2, 8))
assert_equal(4, 2.to_bn.mod_exp(5, 7))
end
def test_bit_operations
e = 0b10010010.to_bn
assert_equal(0b10010011, e.set_bit!(0))
assert_equal(0b10010011, e.set_bit!(1))
assert_equal(0b1010010011, e.set_bit!(9))
e = 0b10010010.to_bn
assert_equal(0b10010010, e.clear_bit!(0))
assert_equal(0b10010000, e.clear_bit!(1))
e = 0b10010010.to_bn
assert_equal(0b10010010, e.mask_bits!(8))
assert_equal(0b10, e.mask_bits!(3))
e = 0b10010010.to_bn
assert_equal(false, e.bit_set?(0))
assert_equal(true, e.bit_set?(1))
assert_equal(false, e.bit_set?(1000))
e = 0b10010010.to_bn
assert_equal(0b1001001000, e << 2)
assert_equal(0b10010010, e)
assert_equal(0b1001001000, e.lshift!(2))
assert_equal(0b1001001000, e)
e = 0b10010010.to_bn
assert_equal(0b100100, e >> 2)
assert_equal(0b10010010, e)
assert_equal(0b100100, e.rshift!(2))
assert_equal(0b100100, e)
end
def test_random
10.times {
r1 = OpenSSL::BN.rand(8)
assert_include(128..255, r1)
r2 = OpenSSL::BN.rand(8, -1)
assert_include(0..255, r2)
r3 = OpenSSL::BN.rand(8, 1)
assert_include(192..255, r3)
r4 = OpenSSL::BN.rand(8, 1, true)
assert_include(192..255, r4)
assert_equal(true, r4.odd?)
r5 = OpenSSL::BN.rand_range(256)
assert_include(0..255, r5)
}
end
def test_prime
p1 = OpenSSL::BN.generate_prime(32)
assert_include(0...2**32, p1)
assert_equal(true, Prime.prime?(p1.to_i))
p2 = OpenSSL::BN.generate_prime(32, true)
assert_equal(true, Prime.prime?((p2.to_i - 1) / 2))
p3 = OpenSSL::BN.generate_prime(32, false, 4)
assert_equal(1, p3 % 4)
p4 = OpenSSL::BN.generate_prime(32, false, 4, 3)
assert_equal(3, p4 % 4)
assert_equal(true, p1.prime?)
assert_equal(true, p2.prime?)
assert_equal(true, p3.prime?)
assert_equal(true, p4.prime?)
assert_equal(true, @e3.prime?)
assert_equal(true, @e3.prime_fasttest?)
end
def test_num_bits_bytes
assert_equal(10, @e1.num_bits)
assert_equal(2, @e1.num_bytes)
assert_equal(107, @e3.num_bits)
assert_equal(14, @e3.num_bytes)
assert_equal(0, 0.to_bn.num_bits)
assert_equal(0, 0.to_bn.num_bytes)
assert_equal(9, -256.to_bn.num_bits)
assert_equal(2, -256.to_bn.num_bytes)
end
def test_comparison
assert_equal(false, @e1 == nil)
assert_equal(false, @e1 == -999)
assert_equal(true, @e1 == 999)
assert_equal(true, @e1 == 999.to_bn)
assert_equal(false, @e1.eql?(nil))
assert_equal(false, @e1.eql?(999))
assert_equal(true, @e1.eql?(999.to_bn))
assert_equal(@e1.hash, 999.to_bn.hash)
assert_not_equal(@e1.hash, @e3.hash)
assert_equal(0, @e1.cmp(999))
assert_equal(1, @e1.cmp(-999))
assert_equal(0, @e1.ucmp(999))
assert_equal(0, @e1.ucmp(-999))
end
end
end
|