Class: Abst::MPQS

Inherits:
Object
  • Object
show all
Defined in:
lib/include/prime_mpqs.rb

Constant Summary collapse

@@kronecker_table =
nil
@@fixed_factor_base =
[-1, 2, 3, 5, 7, 11, 13].freeze
@@fixed_factor_base_log =
([nil] + @@fixed_factor_base[1..-1].map {|p| Math.log(p)}).freeze
@@mpqs_parameter_map =
[[100,20]] * 9 + [
[100, 20],		# 9 -digits
[100, 21],		# 10
[100, 22],		# 11
[100, 24],		# 12
[100, 26],		# 13
[100, 29],		# 14
[100, 32],		# 15
[200, 35],		# 16
[300, 40],		# 17
[300, 60],		# 18
[300, 80],		# 19
[300, 100],		# 20
[300, 100],		# 21
[300, 120],		# 22
[300, 140],		# 23
[600, 160],		# 24
[900, 180],		# 25
[1000, 200],	# 26
[1000, 220],	# 27
[2000, 240],	# 28
[2000, 260],	# 29
[2000, 325],	# 30
[2000, 355],	# 31
[2000, 375],	# 32
[3000, 400],	# 33
[2000, 425],	# 34
[2000, 550],	# 35
[3000, 650],	# 36
[5000, 750],	# 37
[4000, 850],	# 38
[4000, 950],	# 39
[5000, 1000],	# 40
[14000, 1150],	# 41
[15000, 1300],	# 42
[15000, 1600],	# 43
[15000, 1900],	# 44
[15000, 2200],	# 45
[20000, 2500],	# 46
[25000, 2500],	# 47
[27500, 2700],	# 48
[30000, 2800],	# 49
[35000, 2900],	# 50
[40000, 3000],	# 51
[50000, 3200],	# 52
[50000, 3500]]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(n, thread_num) ⇒ MPQS

Returns a new instance of MPQS.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/include/prime_mpqs.rb', line 76

def initialize(n, thread_num)
#@@proc_time[:init] -= Time.now.to_i + Time.now.usec.to_f / 10 ** 6
	@original_n = n
	@thread_num = [thread_num, 1].max
	@big_prime = {}
	@big_prime_mutex = Mutex.new

	decide_multiplier(n)
	decide_parameter
	select_factor_base
	some_precomputations

	@d = Abst.isqrt(Abst.isqrt(@n >> 1) / @sieve_range)
	@d -= (@d & 3) + 1

	@matrix_left = []
	@matrix_right = []
	@mask = 1
	@check_list = Array.new(@factor_base_size)
#@@proc_time[:init] += Time.now.to_i + Time.now.usec.to_f / 10 ** 6
end

Class Method Details

.kronecker_tableObject

@@proc_time = Hash.new(0) def self.get_times return @@proc_time end



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/include/prime_mpqs.rb', line 62

def self.kronecker_table
	unless @@kronecker_table
		target = [3, 5, 7, 11, 13]
		@@kronecker_table = 4.times.map{Hash.new}
		(17..3583).each_prime do |p|
			k = target.map {|b| Abst.kronecker_symbol(p, b)}
			@@kronecker_table[(p & 6) >> 1][k] ||= p
		end
		@@kronecker_table[0][[1, 1, 1, 1, 1]] = 1
	end

	return @@kronecker_table
end

Instance Method Details

#decide_multiplier(n) ⇒ Object



98
99
100
101
102
# File 'lib/include/prime_mpqs.rb', line 98

def decide_multiplier(n)
	t = [3, 5, 7, 11, 13].map {|p| Abst.kronecker_symbol(n, p)}
	multiplier = self.class.kronecker_table[(n & 6) >> 1][t]
	@n = n * multiplier
end

#decide_parameterObject



104
105
106
107
108
109
110
# File 'lib/include/prime_mpqs.rb', line 104

def decide_parameter
	digit = Math.log(@n, 10).floor
	parameter = @@mpqs_parameter_map[digit] ? @@mpqs_parameter_map[digit].dup : @@mpqs_parameter_map.last.dup
	parameter[0] = (parameter[0] * 2).floor
	@sieve_range, @factor_base_size = parameter
	@sieve_range_2 = @sieve_range << 1
end

#eliminate_big_primes(sieve_rslt) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/include/prime_mpqs.rb', line 263

def eliminate_big_primes(sieve_rslt)
	sieve_rslt_with_big_prime = sieve_rslt.select{|f, re, d, r| 1 != re}
	sieve_rslt.select!{|f, re, d, r| 1 == re}

	temp_f = sieve_rslt.map(&:first)
	temp_r = sieve_rslt.map(&:last)
	temp_big = sieve_rslt.map{|f, re, d, r| d}
	sieve_rslt_with_big_prime.each do |f, re, d, r|
		unless @big_prime[re]
			@big_prime[re] = [f, r, d]
		else
			temp_f << (@big_prime[re][0].zip(f).map{|e1, e2| e1 + e2})
			temp_big << (re * d * @big_prime[re][2])
			temp_r << (r * @big_prime[re][1])
		end
	end

	return temp_f, temp_big, temp_r
end

#find_factorObject



138
139
140
141
142
143
144
145
# File 'lib/include/prime_mpqs.rb', line 138

def find_factor
	if 1 == @thread_num
		find_factor_single_thread
	else
		sieve_thread_num = [@thread_num - 2, 1].max
		find_factor_multi_thread(sieve_thread_num)
	end
end

#find_factor_multi_thread(sieve_thread_num) ⇒ Object



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
# File 'lib/include/prime_mpqs.rb', line 193

def find_factor_multi_thread(sieve_thread_num)
	queue_poly = SizedQueue.new(sieve_thread_num)
	queue_sieve_rslt = SizedQueue.new(sieve_thread_num)

	# Create thread make polynomials
	th_make_poly = Thread.new do
		loop { queue_poly.push next_poly }
	end

	thg_sieve = ThreadGroup.new
	# Create threads for sieve
	sieve_thread_num.times do
		thread = Thread.new do
			loop do
				a, b, c, d = queue_poly.shift

#temp = Time.now.to_i + Time.now.usec.to_f / 10 ** 6
				# Sieve
				rslt = sieve(a, b, c, d)
#@@proc_time[:sieve] += Time.now.to_i + Time.now.usec.to_f / 10 ** 6 - temp

				queue_sieve_rslt.push rslt unless rslt.empty?
			end
		end
		thg_sieve.add thread
	end

	r_list = []
	factorization = []
	big_prime_sup = []
	loop do
		sieve_rslt = queue_sieve_rslt.shift
		next if sieve_rslt.empty?

#temp = Time.now.to_i + Time.now.usec.to_f / 10 ** 6
		f, big, r = eliminate_big_primes(sieve_rslt)
		next if f.empty?

#p [factorization.size, r_list.size, big_prime_sup.size]
		# Gaussian elimination
		factorization.concat f
		r_list.concat r
		big_prime_sup.concat big

		eliminated = gaussian_elimination(f)
#@@proc_time[:gaussian] += Time.now.to_i + Time.now.usec.to_f / 10 ** 6 - temp
		eliminated.each do |row|
			x = y = 1
			f = Array.new(@factor_base_size, 0)
			factorization.size.times do |i|
				next if row[i] == 0
				x = x * r_list[i] % @n
				f = f.zip(factorization[i]).map{|e1, e2| e1 + e2}
				y = y * big_prime_sup[i] % @n
			end

			2.upto(@factor_base_size - 1) do |i|
				y = y * Abst.power(@factor_base[i], f[i] >> 1, @n) % @n
			end
			y = (y << (f[1] >> 1)) % @n

			z = Abst.lehmer_gcd(x - y, @original_n)
			return z if 1 < z and z < @original_n
		end
	end
ensure
	thg_sieve.list.each {|th| th.kill}
	th_make_poly.kill
end

#find_factor_single_threadObject



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
# File 'lib/include/prime_mpqs.rb', line 147

def find_factor_single_thread
	r_list = []
	factorization = []
	big_prime_sup = []

	loop do
		# Create polynomial
		a, b, c, d = next_poly

		# Sieve
#temp = Time.now.to_i + Time.now.usec.to_f / 10 ** 6
		sieve_rslt = sieve(a, b, c, d)
#@@proc_time[:sieve] += Time.now.to_i + Time.now.usec.to_f / 10 ** 6 - temp
		next if sieve_rslt.empty?
		f, big, r = eliminate_big_primes(sieve_rslt)
		next if f.empty?

		# Gaussian elimination
		factorization += f
		r_list += r
		big_prime_sup += big

#@@proc_time[:gaussian] -= Time.now.to_i + Time.now.usec.to_f / 10 ** 6
		eliminated = gaussian_elimination(f)
#@@proc_time[:gaussian] += Time.now.to_i + Time.now.usec.to_f / 10 ** 6
		eliminated.each do |row|
			x = y = 1
			f = Array.new(@factor_base_size, 0)
			factorization.size.times do |i|
				next if row[i] == 0
				x = x * r_list[i] % @n
				f = f.zip(factorization[i]).map{|e1, e2| e1 + e2}
				y = y * big_prime_sup[i] % @n
			end

			2.upto(@factor_base_size - 1) do |i|
				y = y * Abst.power(@factor_base[i], f[i] >> 1, @n) % @n
			end
			y = (y << (f[1] >> 1)) % @n

			z = Abst.lehmer_gcd(x - y, @original_n)
			return z if 1 < z and z < @original_n
		end
	end
end

#gaussian_elimination(m) ⇒ Object



385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'lib/include/prime_mpqs.rb', line 385

def gaussian_elimination(m)
	elim_start = @matrix_left.size
	temp = Array.new(m.size)
	m.size.times do |i|
		temp[i] = @mask
		@mask <<= 1
	end
	rslt = @matrix_right += temp
	m = @matrix_left.concat(m.map{|row| row.reverse_each.map{|i| i[0]}})

	height = m.size
	width = @factor_base_size

	i = 0
	width.times do |j|
		unless @check_list[j]
			# Find non-zero entry
			row = nil
			elim_start.upto(height - 1) do |i2|
				if 1 == m[i2][j]
					row = i2
					break
				end
			end
			next unless row

			@check_list[j] = row

			# Swap?
			if i < row
				m.insert(i, m.delete_at(row))
				rslt.insert(i, rslt.delete_at(row))
			end

			elim_start += 1
		end

		# Eliminate
		m_i = m[i]
		(row ? (row + 1) : elim_start).upto(height - 1) do |i2|
			next if m[i2][j] == 0

			m_i2 = m[i2]
			(j + 1).upto(width - 1) do |j2|
				m_i2[j2] ^= 1 if 1 == m_i[j2]
			end
			rslt[i2] ^= rslt[i]
		end

		i += 1
	end

	t = height - i
	m.pop(t)
	return rslt.pop(t)
end

#next_dObject



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/include/prime_mpqs.rb', line 298

def next_d
	d = @d + 4
	if d < Abst.primes_list.last
		plist = Abst.primes_list
		(d..plist.last).each_prime do |p|
			return p if p[1] == 1 and Abst.kronecker_symbol(@n, p) == 1
		end
		d += 4
	end

	loop do
		return d if Abst.kronecker_symbol(@n, d) == 1 and Abst.power(@n, d >> 1, d) == 1
		d += 4
	end
end

#next_polyObject

Return

a, b,c



284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/include/prime_mpqs.rb', line 284

def next_poly
#temp = Time.now.to_i + Time.now.usec.to_f / 10 ** 6
	@d = d = next_d
	a = d ** 2
	h1 = Abst.power(@n, (d >> 2) + 1, d)
	h2 = ((@n - h1 ** 2) / d) * Abst.extended_lehmer_gcd(h1 << 1, d)[0] % d
	b = h1 + h2 * d
	b = a - b if b.even?
	c = ((b ** 2 - @n) >> 2) / a

#@@proc_time[:make_poly_2] += Time.now.to_i + Time.now.usec.to_f / 10 ** 6 - temp
	return a, b, c, d
end

#select_factor_baseObject



112
113
114
115
116
117
118
119
120
# File 'lib/include/prime_mpqs.rb', line 112

def select_factor_base
	@factor_base = @@fixed_factor_base.dup
	(17..INFINITY).each_prime do |p|
		if 1 == Abst.kronecker_symbol(@n, p)
			@factor_base.push(p)
			break if @factor_base_size <= @factor_base.size
		end
	end
end

#sieve(a, b, c, d) ⇒ Object



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/include/prime_mpqs.rb', line 314

def sieve(a, b, c, d)
	a2 = a << 1
	lo = -(b / a2) - @sieve_range + 1

	sieve = Array.new(@sieve_range_2, 0)

#temp = Time.now.to_i + Time.now.usec.to_f / 10 ** 6
	# Sieve by 2
	#		0.upto(@sieve_range_2 - 1) do |i|
	#			count = 1
	#			count += 1 while sieve[i][2][count] == 0
	#			sieve[i][1] += @factor_base_log[1] * count
	#		end

	# Sieve by 3, 5, 7, 11, ...
	#		2.upto(@factor_base_size - 1) do |i|
	4.upto(@factor_base_size - 1) do |i|
		p = @factor_base[i]
		a_inverse = Abst.extended_lehmer_gcd(a2, p ** @power_limit[i])[0]
		pe = 1
		e = 1

		power_limit_i = @power_limit[i]
		factor_base_log_i = @factor_base_log[i]
		mod_sqrt_cache_i = @mod_sqrt_cache[i]
		while e <= power_limit_i
			pe *= p
			sqrt = mod_sqrt_cache_i[e]

			t = sqrt
			s = ((t - b) * a_inverse - lo) % pe
			s.step(@sieve_range_2 - 1, pe) do |j|
				sieve[j] += factor_base_log_i
			end

			t = pe - sqrt
			s = ((t - b) * a_inverse - lo) % pe
			s.step(@sieve_range_2 - 1, pe) do |j|
				sieve[j] += factor_base_log_i
			end

			e += 1
		end
	end
#@@proc_time[:sieve_a] += Time.now.to_i + Time.now.usec.to_f / 10 ** 6 - temp

#temp = Time.now.to_i + Time.now.usec.to_f / 10 ** 6
	# select trial division target
	td_target = []
	sieve.each.with_index do |sum_of_log, idx|
		if @closenuf < sum_of_log
			x = idx + lo
			t = a * x
			td_target.push([(t << 1) + b, (t + b) * x + c])
		end
	end
#@@proc_time[:sieve_slct] += Time.now.to_i + Time.now.usec.to_f / 10 ** 6 - temp

	# trial division on factor base
	rslt = []
#temp = Time.now.to_i + Time.now.usec.to_f / 10 ** 6
	td_target.each do |r, s|
		f, re = trial_division_on_factor_base(s, @factor_base)
		f[1] += 2
		rslt.push [f, re, d, r]
	end
#@@proc_time[:sieve_td] += Time.now.to_i + Time.now.usec.to_f / 10 ** 6 - temp

	return rslt
end

#some_precomputationsObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/include/prime_mpqs.rb', line 122

def some_precomputations
	size  = @@fixed_factor_base_log.size
	@factor_base_log = @@fixed_factor_base_log + @factor_base[size..-1].map {|p| Math.log(p)}

	@power_limit = Array.new(@factor_base_size)
	@mod_sqrt_cache = Array.new(@factor_base_size)
	2.upto(@factor_base_size - 1) do |i|
		p = @factor_base[i]
		@power_limit[i] = (@factor_base_log.last / @factor_base_log[i]).floor
		@mod_sqrt_cache[i] = [nil] + Abst.mod_sqrt(@n, p, @power_limit[i], true)
	end

	target = Math.log(@n) / 2 + Math.log(@sieve_range) - 1
	@closenuf = target - 1.8 * Math.log(@factor_base.last)
end

#trial_division_on_factor_base(n, factor_base) ⇒ Object



442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
# File 'lib/include/prime_mpqs.rb', line 442

def trial_division_on_factor_base(n, factor_base)
	factor = Array.new(@factor_base_size, 0)
	if n < 0
		factor[0] = 1
		n = -n
	end

	div_count = 1
	div_count += 1 while n[div_count] == 0
	factor[1] = div_count
	n >>= div_count

	i = 2
	while i < @factor_base_size
		d = factor_base[i]
		q, r = n.divmod(d)
		if 0 == r
			n = q
			div_count = 1
			loop do
				q, r = n.divmod(d)
				break unless 0 == r

				n = q
				div_count += 1
			end

			factor[i] = div_count
		end

		i += 1
	end

	return factor, n
end