/* 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 */ #include #include "gmp.h" #include "zf.h" /* @N is number factorised, presumed to be of form pq where p and q are large primes @candidate is small square s^2 where b^2 = s^2 + N */ int check_result(mpz_t candidate, mpz_t N) { mpz_t pp; mpz_init(pp); mpz_t qq; mpz_init(qq); mpz_t check; mpz_init(check); mpz_t small; mpz_init(small); mpz_t big; mpz_init(big); mpz_sqrt(small, candidate); mpz_add(candidate, N, candidate); mpz_sqrt(big, candidate); mpz_add(pp, big, small); mpz_sub(qq, big, small); mpz_mul(check, pp, qq); if (mpz_cmp(check, N)) { return 75; // factorisation failed, supposed pq is not equal to N } return 0; } /* returns the time in seconds since time tv_begin until time tv_end, as a float */ double elapsed_secs(struct timeval tv_end, struct timeval tv_begin) { // following as integer for precision because may be big numbers with a small difference time_t elapsed_integer = tv_end.tv_sec - tv_begin.tv_sec; return elapsed_integer + ((double)tv_end.tv_usec - tv_begin.tv_usec)/1000000; }