/*
Copyright (C) 2022 Stephen Hewitt, cambridgeclarion.org
This file is part of a programme to research fast Fermat factorisation algorithms.
This is free software: you can redistribute it and/or modify
it under the terms of version 3 of the GNU General Public License as
published by the Free Software Foundation.
It is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public Licence
along with this programme. If not, see .
A copy of the licence is also available at
https://www.cambridgeclarion.org/download/GPLv3.txt
It has the following cryptographic hash
SHA256 3972dc9744f6499f0f9b2dbf76696f2ae7ad8af9b23dde66d6af86c9dfb36986
*/
/*
This is experimental software for research on Fermat factorisation algorithms.
For more information see the articles on www.cambridgeclarion.org
"Empirical explorations of faster Fermat factorisation" by Stephen Hewitt
*/
#include
#include
#include
#include
#include "filter.h"
/* idea of the following is to make the skip array bigger than the table and make it repeat the table
after the table finishes.
This means in the loop we can get away with not bothering to check we have reached the end of the table for a few iterations
This means (say if the array has 10 extra elements at the end
that we only need to check every 10 iterations whether we have reached the end of the table
when we do notice, simply subtract skip_n from the current index and carry on as if nothing had happened
- the extra cost is this subtraction rather than a zeroing and a small increase in code space.
- i suspect that by in fact subtracing (skip_n -1) we can avoid an increment in this case too.
*/
static unsigned const EXTRA_N = 11;
static const unsigned char masks[] = {
1,2,4,8,16,32,64,128,
1,2,4,8,16,32,64,128,
1,2,4,8,16,32,64,128,
1,2,4,8,16,32,64,128,
1,2,4,8,16,32,64,128,
1,2,4,8,16,32,64,128,
1,2,4,8,16,32,64,128,
1,2,4,8,16,32,64,128,
1,2,4,8,16,32,64,128,
1,2,4,8,16,32,64,128,
1,2,4,8,16,32,64,128,
1,2,4,8,16,32,64,128,
1,2,4,8,16,32,64,128,
1,2,4,8,16,32,64,128,
1,2,4,8,16,32,64,128,
1,2,4,8,16,32,64,128,
1,2,4,8,16,32,64,128,
1,2,4,8,16,32,64,128,
1,2,4,8,16,32,64,128,
1,2,4,8,16,32,64,128,
1,2,4,8,16,32,64,128,
1,2,4,8,16,32,64,128,
1,2,4,8,16,32,64,128,
1,2,4,8,16,32,64,128,
1,2,4,8,16,32,64,128,
1,2,4,8,16,32,64,128,
1,2,4,8,16,32,64,128,
1,2,4,8,16,32,64,128,
1,2,4,8,16,32,64,128,
1,2,4,8,16,32,64,128,
1,2,4,8,16,32,64,128,
1,2,4,8,16,32,64,128,
};
#define setbit(a,b) ((a[(b) >> 3]) |= masks[static_cast(b)])
#define getbit(a,b) ((a[(b) >> 3]) & masks[static_cast(b)])
/*
Creates a bit array of size M, with a bit set only where the index into the array is a square mod M
*/
Squares::Squares(unsigned long M): M(M), square_table((M + 7)/8, 0)
{
unsigned long delta = 1;
unsigned long square = 0;
unsigned long iterations = (M + 1)/2; /* will have generated all square values by when it get to half of M */
unsigned char* data = square_table.data();
printf("debug squares constructor table size = %lu, M = %lu, iterations=%lu\n", (unsigned long)square_table.size(), M, iterations);
for (unsigned long i = 0; i < iterations; i++)
{
setbit(data, square);
square += delta;
if (square >= M)
square -= M;
delta += 2;
if (delta >= M)
delta -= M;
}
}
/*
Construct a skip table for a given N number to factorise and a given filter modulus M using the passed square table.
You have to pass skip_n for now. (Although it can be derived from M and N)
The skip or stop table is a list of the increase from one big presquare candidate to the next
*/
Filter::Filter(unsigned long M, unsigned skip_n, unsigned long NmodM, const unsigned char* const valid_squares): M(M), skip(M + EXTRA_N), skip_n(skip_n), max_skip(0)
{
unsigned long delta = 1;
unsigned long square = 0; // small square candidate
unsigned long previous_candidate;
unsigned i;
unsigned long big;
unsigned long temp;
Skint* data = skip.data();
// big is the big presquare, generate successive squares as we go, start at big=0
square = M - NmodM; // note we know that NmodM cannot be zero
previous_candidate = 0; // this is not true, but will correct at the end when we know the previous candidate
for (i = 0, big = 0; big < M; big++)
{
if (getbit(valid_squares, square))
{
temp = big - previous_candidate;
if (temp > max_skip)
max_skip = temp;
data[i++] = temp;
previous_candidate = big;
}
square += delta;
if (square >= M)
square -= M;
delta += 2;
if (delta >= M)
delta -= M;
}
smallest_candidate = data[0];
// now correct first entry by adding on distance from 0 to go backwards to biggest candidate
skip[0] += M - previous_candidate;
if (i != skip_n)
{
fprintf(stderr, "status 233: Filter constructor was passed skip_n = %u (incorrect), actual = %u\n", skip_n, i);
exit(233);
}
// repeat of table at its end, for a little way
if (EXTRA_N >= skip_n)
{
// note this could really happen with some categories at present where table is only 12 entries for example
fprintf(stderr, "status 133 Filter constructor: EXTRA_N is too much for passed skip_n = %u\n", skip_n);
exit(133); // would need to repeat the table more than once to fill the extra and can't be bothered at the moment
}
for (i = 0; i < EXTRA_N; i++)
{
skip[skip_n + i] = skip[i];
}
}
/*
Passed value @guess_modM must be guess_modM < M
It returns the minimum offset that must be added to @guess_modM to make a candidate that passes this filter.
There is no defence against illegal values of guess_modM which will get nonsense values returned.
In addition to returning the offset, it sets @skindex, to the index into the skip table of the entry
which contains the skip to the next candidate after the candidate represented by the offset returned.
(Motivation is this avoids having to relocate this entry at the start of the search.)
(This function is expected to be used at the start of the search, with guess_modM as residue
of the first guess of the big presquare)
*/
unsigned long Filter::get_offset_to_next(unsigned& skindex, unsigned long guess_modM)
{
if (guess_modM > M + smallest_candidate - skip[0])
{
// need to advance and wrap round to first represented by skip[0], which is smallest_candidate
skindex = 1;
return M + smallest_candidate - guess_modM;
}
else
{
unsigned long valid_candidate = smallest_candidate;
skindex = 1;
while (guess_modM > valid_candidate)
{
// If the table is correct, skindex cannot overrun skip[] because the entries of skip[] add up to M
valid_candidate += skip[skindex++];
}
return valid_candidate - guess_modM;
}
}
/* Gets the number of entries in the filter table (skip_n) by iterating over entire input range M.
In principle iteration is not necessary to find this
- see Part 3 and Part 6 of "Empirical explorations of faster Fermat factorisation"
www.cambridgeclarion.org/42.html
Instead skip_n could be calculated analytically.
But keep this function for now until calculations written
*/
unsigned calc_tr(unsigned long M, unsigned long NmodM, const unsigned char* const valid_squares)
{
unsigned long delta = 1;
unsigned long square = 0; // small square candidate
unsigned i;
unsigned long big;
// big is the big presquare, generate successive squares as we go, start at big=0
square = M - NmodM; // note we know that NmodM cannot be zero
for (i = 0, big = 0; big < M; big++)
{
if (getbit(valid_squares, square))
{
i++;
}
square += delta;
if (square >= M)
square -= M;
delta += 2;
if (delta >= M)
delta -= M;
}
return i;
}