std::pair<double, double> LoadILLSANS::calculateQMaxQMin() { double min = std::numeric_limits<double>::max(), max = std::numeric_limits<double>::min(); g_log.debug("Calculating Qmin Qmax..."); std::size_t nHist = m_localWorkspace->getNumberHistograms(); for (std::size_t i = 0; i < nHist; ++i) { Geometry::IDetector_const_sptr det = m_localWorkspace->getDetector(i); if (!det->isMonitor()) { const MantidVec &lambdaBinning = m_localWorkspace->readX(i); Kernel::V3D detPos = det->getPos(); double r, theta, phi; detPos.getSpherical(r, theta, phi); double v1 = calculateQ(*(lambdaBinning.begin()), theta); double v2 = calculateQ(*(lambdaBinning.end() - 1), theta); // std::cout << "i=" << i << " theta="<<theta << " lambda_i=" << // *(lambdaBinning.begin()) << " lambda_f=" << *(lambdaBinning.end()-1) << // " v1=" << v1 << " v2=" << v2 << '\n'; if (i == 0) { min = v1; max = v1; } if (v1 < min) { min = v1; } if (v2 < min) { min = v2; } if (v1 > max) { max = v1; } if (v2 > max) { max = v2; } } else g_log.debug() << "Detector " << i << " is a Monitor : " << det->getID() << '\n'; } g_log.debug() << "Calculating Qmin Qmax. Done : [" << min << "," << max << "]\n"; return std::pair<double, double>(min, max); }
int main(int argc, char* argv[]) { assert(mlockall(MCL_CURRENT | MCL_FUTURE) == 0); char split = 0; uint8_t total_shares = 0, shares_required = 0; char* files[P]; uint8_t files_count = 0; char *in_file = (void*)0, *out_file_param = (void*)0; int i; while((i = getopt(argc, argv, "scn:k:f:o:i:h?")) != -1) switch(i) { case 's': if ((split & 0x2) && !(split & 0x1)) ERROREXIT("-s (split) and -c (combine) are mutually exclusive\n") else split = (0x2 | 0x1); break; case 'c': if ((split & 0x2) && (split & 0x1)) ERROREXIT("-s (split) and -c (combine) are mutually exclusive\n") else split = 0x2; break; case 'n': { int t = atoi(optarg); if (t <= 0 || t >= P) ERROREXIT("n must be > 0 and < %u\n", P) else total_shares = t; break; } case 'k': { int t = atoi(optarg); if (t <= 0 || t >= P) ERROREXIT("n must be > 0 and < %u\n", P) else shares_required = t; break; } case 'i': in_file = optarg; break; case 'o': out_file_param = optarg; break; case 'f': if (files_count >= P-1) ERROREXIT("May only specify up to %u files\n", P-1) files[files_count++] = optarg; break; case 'h': case '?': printf("Split usage: -s -n <total shares> -k <shares required> -i <input file> -o <output file path base>\n"); printf("Combine usage: -c -k <shares provided == shares required> <-f <share>>*k -o <output file>\n"); exit(0); break; default: ERROREXIT("getopt failed?\n") } if (!(split & 0x2)) ERROREXIT("Must specify one of -c, -s or -?\n") split &= 0x1; if (argc != optind) ERROREXIT("Invalid argument\n") if (split) { if (!total_shares || !shares_required) ERROREXIT("n and k must be set.\n") if (shares_required > total_shares) ERROREXIT("k must be <= n\n") if (files_count != 0 || !in_file || !out_file_param) ERROREXIT("Must specify -i <input file> and -o <output file path base> but not -f in split mode.\n") FILE* random = fopen(RAND_SOURCE, "r"); assert(random); FILE* secret_file = fopen(in_file, "r"); if (!secret_file) ERROREXIT("Could not open %s for reading.\n", in_file) uint8_t secret[MAX_LENGTH]; size_t secret_length = fread(secret, 1, MAX_LENGTH*sizeof(uint8_t), secret_file); if (secret_length == 0) ERROREXIT("Error reading secret\n") if (fread(secret, 1, 1, secret_file) > 0) ERROREXIT("Secret may not be longer than %u\n", MAX_LENGTH) fclose(secret_file); printf("Using secret of length %lu\n", secret_length); uint8_t a[shares_required], x[total_shares], D[total_shares][secret_length]; // TODO: The following loop may take a long time and eat lots of /dev/random if total_shares is high for (uint32_t i = 0; i < total_shares; i++) { int32_t j = 0; do { assert(fread(&x[i], sizeof(uint8_t), 1, random) == 1); if (x[i] == 0) continue; for (j = 0; j < i; j++) if (x[j] == x[i]) break; } while (j < i); // Inner loop will get to j = i when x[j] != x[i] for all j if (i % 32 == 31) printf("Finished picking X coordinates for %u shares\n", i+1); } for (uint32_t i = 0; i < secret_length; i++) { a[0] = secret[i]; for (uint8_t j = 1; j < shares_required; j++) assert(fread(&a[j], sizeof(uint8_t), 1, random) == 1); for (uint8_t j = 0; j < total_shares; j++) D[j][i] = calculateQ(a, shares_required, x[j]); // Now, for paranoia's sake, we ensure that no matter which piece we are missing, we can derive no information about the secret check_possible_missing_part_derivations(total_shares, shares_required, &(D[0][0]), x, i, secret_length); if (i % 32 == 31) printf("Finished processing %u bytes.\n", i+1); } char out_file_name_buf[strlen(out_file_param) + 4]; strcpy(out_file_name_buf, out_file_param); for (uint8_t i = 0; i < total_shares; i++) { sprintf(((char*)out_file_name_buf) + strlen(out_file_param), "%u", i); FILE* out_file = fopen(out_file_name_buf, "w+"); if (!out_file) ERROREXIT("Could not open output file %s\n", out_file_name_buf) if (fwrite(&x[i], sizeof(uint8_t), 1, out_file) != 1) ERROREXIT("Could not write 1 byte to %s\n", out_file_name_buf) if (fwrite(D[i], 1, secret_length, out_file) != secret_length) ERROREXIT("Could not write %lu bytes to %s\n", secret_length, out_file_name_buf) fclose(out_file); } // Clear sensitive data (No, GCC 4.7.2 is currently not optimizing this out) memset(secret, 0, sizeof(uint8_t)*secret_length); memset(a, 0, sizeof(uint8_t)*shares_required); memset(x, 0, sizeof(uint8_t)*total_shares); memset(in_file, 0, strlen(in_file)); fclose(random); } else { if (!shares_required)