Skip to content

Commit f6cc554

Browse files
Extending PM1 Stage 1
1 parent fbd3503 commit f6cc554

File tree

6 files changed

+979
-232
lines changed

6 files changed

+979
-232
lines changed

include/core/AlgoUtils.hpp

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,33 @@ static inline void write_u16(std::ofstream& o, uint16_t v){ o.write(reinterpret_
447447
static inline void write_f64(std::ofstream& o, double v){ o.write(reinterpret_cast<const char*>(&v),8); }
448448
static inline void write_u8 (std::ofstream& o, uint8_t v){ o.write(reinterpret_cast<const char*>(&v),1); }
449449

450+
451+
static inline bool read_u32(std::ifstream& in, uint32_t& v){
452+
in.read(reinterpret_cast<char*>(&v), 4);
453+
return (bool)in;
454+
}
455+
static inline bool read_i32(std::ifstream& in, int32_t& v){
456+
in.read(reinterpret_cast<char*>(&v), 4);
457+
return (bool)in;
458+
}
459+
static inline bool read_u64(std::ifstream& in, uint64_t& v){
460+
in.read(reinterpret_cast<char*>(&v), 8);
461+
return (bool)in;
462+
}
463+
static inline bool read_u16(std::ifstream& in, uint16_t& v){
464+
in.read(reinterpret_cast<char*>(&v), 2);
465+
return (bool)in;
466+
}
467+
static inline bool read_f64(std::ifstream& in, double& v){
468+
in.read(reinterpret_cast<char*>(&v), 8);
469+
return (bool)in;
470+
}
471+
static inline bool read_u8(std::ifstream& in, uint8_t& v){
472+
in.read(reinterpret_cast<char*>(&v), 1);
473+
return (bool)in;
474+
}
475+
476+
450477
static inline bool read_text_file(const std::string& path, std::string& out){
451478
std::ifstream f(path);
452479
if(!f) return false;
@@ -562,7 +589,7 @@ static inline bool write_prime95_s1_from_bytes(const std::string& outPath, uint3
562589
#elif defined(__arm__) || defined(_M_ARM)
563590
arch_str = "ARM";
564591
#else
565-
arch_str = "unknown";
592+
arch_str = "";
566593
#endif
567594

568595
std::string json = std::string("{\"programs\":[{\"work\":{\"type\":\"PM1\",\"stage\":\"1\"},\"program\":{\"name\":\"prmers\",\"version\":\"" + core::PRMERS_VERSION + "\"},\"os\":{\"os\":\"") + os_str + "\",\"architecture\":\"" + arch_str + "\"},\"date_start\":\"" + ds + "\",\"date_end\":\"" + de + "\"}]}";
@@ -595,6 +622,83 @@ static inline bool write_prime95_s1_from_bytes(const std::string& outPath, uint3
595622
return (bool)out;
596623
}
597624

625+
static inline bool read_prime95_s1_to_bytes(const std::string& path,
626+
uint32_t& p_out,
627+
uint64_t& B1_out,
628+
std::vector<uint8_t>& data_out)
629+
{
630+
std::ifstream in(path, std::ios::binary);
631+
if (!in) {
632+
std::cerr << "Error: cannot open Prime95 S1 file " << path << " for reading\n";
633+
return false;
634+
}
635+
636+
uint32_t magic = 0;
637+
uint32_t ver = 0;
638+
double fver = 0.0;
639+
int32_t type = 0;
640+
uint32_t p = 0;
641+
int32_t unused_n = 0;
642+
uint8_t stage0 = 0, stage1 = 0;
643+
uint16_t reserved = 0;
644+
uint64_t zero = 0;
645+
double f1 = 0.0;
646+
uint32_t chk_file = 0;
647+
int32_t flag5 = 0;
648+
uint64_t B1_a = 0;
649+
uint64_t B1_b = 0;
650+
int32_t flag1 = 0;
651+
int32_t nWords = 0;
652+
653+
// Lire l’en-tête dans le même ordre que write_prime95_s1_from_bytes
654+
if (!read_u32(in, magic)) return false;
655+
if (!read_u32(in, ver)) return false;
656+
if (!read_f64(in, fver)) return false;
657+
if (!read_i32(in, type)) return false;
658+
if (!read_u32(in, p)) return false;
659+
if (!read_i32(in, unused_n))return false;
660+
if (!read_u8(in, stage0)) return false;
661+
if (!read_u8(in, stage1)) return false;
662+
if (!read_u16(in, reserved))return false;
663+
if (!read_u64(in, zero)) return false;
664+
if (!read_f64(in, f1)) return false;
665+
if (!read_u32(in, chk_file))return false;
666+
if (!read_i32(in, flag5)) return false;
667+
if (!read_u64(in, B1_a)) return false;
668+
if (!read_u64(in, B1_b)) return false;
669+
if (!read_i32(in, flag1)) return false;
670+
if (!read_i32(in, nWords)) return false;
671+
672+
if (nWords < 0) {
673+
std::cerr << "Error: negative word count in Prime95 S1 file " << path << "\n";
674+
return false;
675+
}
676+
677+
size_t nBytes = static_cast<size_t>(nWords) * 4u;
678+
data_out.assign(nBytes, 0);
679+
680+
in.read(reinterpret_cast<char*>(data_out.data()),
681+
static_cast<std::streamsize>(nBytes));
682+
if (!in) {
683+
std::cerr << "Error: truncated Prime95 S1 data in " << path << "\n";
684+
return false;
685+
}
686+
687+
// Sorties
688+
p_out = p;
689+
B1_out = B1_a; // B1_a == B1_b dans ton writer
690+
691+
// Vérifier le checksum (même formule que lors de l’écriture)
692+
uint32_t chk_calc = checksum_prime95_s1(B1_out, data_out);
693+
if (chk_calc != chk_file) {
694+
std::cerr << "Warning: checksum mismatch in Prime95 S1 file " << path
695+
<< " (file=" << chk_file << ", computed=" << chk_calc << ")\n";
696+
// On continue quand même : les données sont probablement correctes.
697+
}
698+
699+
return true;
700+
}
701+
598702

599703
static inline mpz_class compute_X_with_dots(engine* eng, engine::Reg reg, const mpz_class& Mp) {
600704
using namespace std::chrono;

include/core/Version.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <string>
55

66
namespace core {
7-
const std::string PRMERS_VERSION = "4.15.35-alpha";
7+
const std::string PRMERS_VERSION = "4.15.36-alpha";
88
} // namespace core
99

1010
#endif // VERSION_HPP

include/io/CliParser.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ struct CliOptions {
2929
bool verify = true;
3030
bool gerbiczli = true;
3131
uint64_t B1 = 10000;
32+
uint64_t B1old = 0;
33+
uint64_t B1_new = 0;
3234
uint64_t B2 = 0;
3335
uint64_t checklevel = 0;
3436
uint64_t gerbicz_error_count = 0;
@@ -57,6 +59,7 @@ struct CliOptions {
5759
std::string computer_name;
5860
std::string config_path;
5961
std::string worktodo_path = "worktodo.txt";
62+
std::string pm1_extend_save_path = "";
6063
int max_local_size1 = 0;
6164
int max_local_size2 = 0;
6265
int max_local_size3 = 0;

src/io/CliParser.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ void printUsage(const char* progName) {
5858
std::cout << " -pm1 : (Optional) Run factoring P-1" << std::endl;
5959
std::cout << " -b1 : (Optional) B1 for factoring P-1" << std::endl;
6060
std::cout << " -b2 : (Optional) B2 for factoring P-1" << std::endl;
61+
std::cout << " -b1old : (Optional) Continue P-1 factoring stage 1 from a previous run. "
62+
"Loads a file named 'resume_p[Exponent]_B1_[oldB1].save (GMP-ECM) or .p95 (if .save not found) '. "
63+
"Use -b1 to specify the new B1 bound for extension." << std::endl;
6164
std::cout << " -K <value> : Exponent K for the n^K variant of P-1 stage 2" << std::endl;
6265
std::cout << " -nmax <value> : Maximum value of n for the n^K variant of P-1 stage 2" << std::endl;
6366
std::cout << " -t <seconds> : (Optional) Specify backup interval in seconds (default: 120)" << std::endl;
@@ -82,6 +85,7 @@ void printUsage(const char* progName) {
8285
std::cout << " -checklevel <value> : (Optional) Will force gerbicz check every B*<value> by default check is done every 10 min and at the end." << std::endl;
8386
std::cout << " -wagstaff : (Optional) will check PRP if (2^p + 1)/3 is probably prime" << std::endl;
8487
std::cout << " -ecm -b1 <B1> [-b2 <B2>] -K <curves> : Run ECM factoring with bounds B1 [and optional B2], on given number of curves" << std::endl;
88+
8589
std::cout << " -montgomery : (Optional) compute in Montgomery and use Montgomery (compute done in montgomery)" << std::endl;
8690
std::cout << " -edwards : (Optional) compute in Montgomery and use (twisted) Edwards curve converted to Montgomery (compute done in Montgomery)" << std::endl;
8791
std::cout << " -ced : (Optional) compute in Edwards and use (twisted) Edwards curves (notorsion twisted or torsion 2x8 possible no twist a=1) " << std::endl;
@@ -287,6 +291,10 @@ CliOptions CliParser::parse(int argc, char** argv ) {
287291
opts.B1 = std::strtoull(argv[i + 1], nullptr, 10); // base 10
288292
++i;
289293
}
294+
else if (std::strcmp(argv[i], "-b1old") == 0 && i + 1 < argc) {
295+
opts.B1old = std::strtoull(argv[i + 1], nullptr, 10); // base 10
296+
++i;
297+
}
290298
else if (std::strcmp(argv[i], "-b2") == 0 && i + 1 < argc) {
291299
opts.B2 = std::strtoull(argv[i + 1], nullptr, 10); // base 10
292300
++i;
@@ -398,6 +406,7 @@ CliOptions CliParser::parse(int argc, char** argv ) {
398406
else if (strcmp(argv[i], "-factors") == 0 && i + 1 < argc) {
399407
opts.knownFactors = util::split(argv[++i], ',');
400408
}
409+
401410
else if (argv[i][0] != '-') {
402411
if (opts.exponent == 0) {
403412
opts.exponent = std::strtoull(argv[i], nullptr, 10);

src/io/JsonBuilder.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,11 @@ static std::string generatePrimeNetJson(
270270
<< ",\"version\":" << jsonEscape(programVersion)
271271
<< ",\"port\":" << programPort << "},"
272272
<< "\"os\":{"
273-
<< "\"os\":" << jsonEscape(osName)
274-
<< ",\"architecture\":" << jsonEscape(osArchitecture)
273+
<< "\"os\":" << jsonEscape(osName);
274+
if(!osArchitecture.empty()){
275+
oss << ",\"architecture\":" << jsonEscape(osArchitecture);
276+
}
277+
oss
275278
<< "},"
276279
<< "\"user\":" << jsonEscape(user);
277280
if (!computer.empty()) oss << ",\"computer\":" << jsonEscape(computer);
@@ -306,7 +309,7 @@ static std::string generatePrimeNetJson(
306309
canon << ";";
307310
if (canonWT == "PRP") canon << toLower(res2048);
308311
canon << ";";
309-
canon << "0_3_1;";
312+
canon << "0_3_" << residueType << ";";
310313
canon << fftLength << ";";
311314
canon << "gerbicz:" << gerbiczError << ";";
312315
canon << programName << ";";
@@ -367,7 +370,7 @@ std::string JsonBuilder::generate(const CliOptions& opts,
367370
<< "\"port\":" << opts.portCode
368371
<< "},"
369372
<< "\"timestamp\":" << jsonEscape(timestampBuf) << ","
370-
<< "\"user\":" << jsonEscape(opts.user.empty() ? "prmers" : opts.user);
373+
<< "\"user\":" << jsonEscape(opts.user.empty() ? "" : opts.user);
371374
if (!opts.computer_name.empty())
372375
oss << ",\"computer\":" << jsonEscape(opts.computer_name);
373376
if (!opts.aid.empty())

0 commit comments

Comments
 (0)