|
| 1 | +/* |
| 2 | + * bench_factor_popen.c |
| 3 | + * |
| 4 | + * Benchmarks block solving using coreutils `factor` via popen (batch). |
| 5 | + * Simulates solving NBLOCKS blocks at nBits=32 (regtest difficulty). |
| 6 | + * |
| 7 | + * Build: gcc -O2 -o bench_factor_popen bench_factor_popen.c |
| 8 | + * Usage: poop ./bench_factor_rho ./bench_factor_popen |
| 9 | + */ |
| 10 | + |
| 11 | +#include <stdint.h> |
| 12 | +#include <stdio.h> |
| 13 | +#include <stdlib.h> |
| 14 | +#include <string.h> |
| 15 | + |
| 16 | +#define NBITS 32 |
| 17 | +#define NBLOCKS 200 |
| 18 | +#define TILDEN (16 * NBITS) /* candidate search radius */ |
| 19 | + |
| 20 | +/* Maximum candidates per W: 2 per jj step (+ and - direction) */ |
| 21 | +#define MAX_CANDIDATES (TILDEN * 2) |
| 22 | + |
| 23 | +/* Small-prime sieve: check divisibility by small primes individually. |
| 24 | + * We can't use a single product here since we don't have GMP. */ |
| 25 | +static const uint64_t SMALL_P[] = {3,5,7,11,13,17,19,23,29,31,37,41,43,47}; |
| 26 | +#define NSMALL (sizeof(SMALL_P) / sizeof(SMALL_P[0])) |
| 27 | + |
| 28 | +static int coprime_to_small(uint64_t n) |
| 29 | +{ |
| 30 | + for (int i = 0; i < (int)NSMALL; i++) { |
| 31 | + if (n % SMALL_P[i] == 0) |
| 32 | + return 0; |
| 33 | + } |
| 34 | + return 1; |
| 35 | +} |
| 36 | + |
| 37 | +/* |
| 38 | + * Factor candidates for a single W using one batched `factor` call. |
| 39 | + * Returns 1 if a valid semiprime was found. |
| 40 | + */ |
| 41 | +static int solve_one(uint64_t W, uint64_t *out_factor, int64_t *out_offset) |
| 42 | +{ |
| 43 | + uint64_t one = (W & 1) ? 0 : 1; |
| 44 | + |
| 45 | + /* Collect sieve-surviving candidates with their offsets */ |
| 46 | + uint64_t cands[MAX_CANDIDATES]; |
| 47 | + int64_t offsets[MAX_CANDIDATES]; |
| 48 | + int ncands = 0; |
| 49 | + |
| 50 | + for (int jj = 0; jj < TILDEN; jj += 2) { |
| 51 | + uint64_t N1 = W + jj + one; |
| 52 | + uint64_t N2 = W - jj - one; |
| 53 | + |
| 54 | + if (coprime_to_small(N1)) { |
| 55 | + cands[ncands] = N1; |
| 56 | + offsets[ncands] = jj + (int64_t)one; |
| 57 | + ncands++; |
| 58 | + } |
| 59 | + if (coprime_to_small(N2)) { |
| 60 | + cands[ncands] = N2; |
| 61 | + offsets[ncands] = -jj - (int64_t)one; |
| 62 | + ncands++; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + if (ncands == 0) |
| 67 | + return 0; |
| 68 | + |
| 69 | + /* Build the command: "factor N1 N2 N3 ..." */ |
| 70 | + /* Each number is at most 10 digits + space, plus "factor " prefix */ |
| 71 | + size_t cmd_cap = 8 + (size_t)ncands * 12; |
| 72 | + char *cmd = malloc(cmd_cap); |
| 73 | + if (!cmd) return 0; |
| 74 | + |
| 75 | + int pos = snprintf(cmd, cmd_cap, "factor"); |
| 76 | + for (int i = 0; i < ncands; i++) { |
| 77 | + pos += snprintf(cmd + pos, cmd_cap - pos, " %lu", (unsigned long)cands[i]); |
| 78 | + } |
| 79 | + |
| 80 | + FILE *fp = popen(cmd, "r"); |
| 81 | + free(cmd); |
| 82 | + if (!fp) return 0; |
| 83 | + |
| 84 | + /* Parse output line by line: "N: f1 f2 f3 ..." */ |
| 85 | + char line[1024]; |
| 86 | + int found = 0; |
| 87 | + |
| 88 | + while (fgets(line, sizeof(line), fp) && !found) { |
| 89 | + /* Parse "N: f1 f2 ..." */ |
| 90 | + char *colon = strchr(line, ':'); |
| 91 | + if (!colon) continue; |
| 92 | + |
| 93 | + uint64_t N = strtoull(line, NULL, 10); |
| 94 | + char *rest = colon + 1; |
| 95 | + |
| 96 | + /* Collect distinct prime factors and their total count */ |
| 97 | + uint64_t factors[64]; |
| 98 | + int nfactors = 0; |
| 99 | + int total_factors = 0; |
| 100 | + char *tok = strtok(rest, " \t\n"); |
| 101 | + while (tok && nfactors < 64) { |
| 102 | + uint64_t f = strtoull(tok, NULL, 10); |
| 103 | + total_factors++; |
| 104 | + /* Only add distinct factors */ |
| 105 | + if (nfactors == 0 || factors[nfactors - 1] != f) { |
| 106 | + factors[nfactors++] = f; |
| 107 | + } |
| 108 | + tok = strtok(NULL, " \t\n"); |
| 109 | + } |
| 110 | + |
| 111 | + /* A semiprime is p*q (exactly 2 prime factors total, both distinct) */ |
| 112 | + if (total_factors != 2 || nfactors != 2) continue; |
| 113 | + |
| 114 | + /* factors[] is already sorted (factor outputs in ascending order) */ |
| 115 | + |
| 116 | + /* Smaller factor must have exactly expected bits (matches consensus) */ |
| 117 | + int b0 = 64 - __builtin_clzll(factors[0]); |
| 118 | + int expected = NBITS / 2 + (NBITS & 1); |
| 119 | + |
| 120 | + if (b0 != expected) continue; |
| 121 | + |
| 122 | + /* Edge case: factor must not be a power of 2 */ |
| 123 | + if (factors[0] == (1ULL << (NBITS/2 - 1))) continue; |
| 124 | + |
| 125 | + /* Find matching offset */ |
| 126 | + for (int i = 0; i < ncands; i++) { |
| 127 | + if (cands[i] == N) { |
| 128 | + *out_factor = factors[0]; |
| 129 | + *out_offset = offsets[i]; |
| 130 | + found = 1; |
| 131 | + break; |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + pclose(fp); |
| 137 | + return found; |
| 138 | +} |
| 139 | + |
| 140 | +int main(void) |
| 141 | +{ |
| 142 | + /* Same PRNG and seed as rho benchmark for identical W values */ |
| 143 | + uint64_t state = 0xDEADBEEFCAFEBABEULL; |
| 144 | + int solved = 0; |
| 145 | + |
| 146 | + for (int i = 0; i < NBLOCKS; i++) { |
| 147 | + state ^= state << 13; |
| 148 | + state ^= state >> 7; |
| 149 | + state ^= state << 17; |
| 150 | + |
| 151 | + uint64_t W = state; |
| 152 | + W |= (1ULL << (NBITS - 1)); |
| 153 | + W &= (1ULL << NBITS) - 1; |
| 154 | + W |= 1; |
| 155 | + |
| 156 | + if (i < 5) |
| 157 | + printf("W[%d] = %lu\n", i, (unsigned long)W); |
| 158 | + |
| 159 | + uint64_t factor; |
| 160 | + int64_t offset; |
| 161 | + if (solve_one(W, &factor, &offset)) |
| 162 | + solved++; |
| 163 | + } |
| 164 | + |
| 165 | + printf("popen: solved %d/%d blocks\n", solved, NBLOCKS); |
| 166 | + return 0; |
| 167 | +} |
0 commit comments