Skip to content

Instantly share code, notes, and snippets.

@ankane
Forked from jarmo/pickparams.c
Created May 27, 2018 02:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ankane/5008469d8b25fb614e84883c9285994a to your computer and use it in GitHub Desktop.
Save ankane/5008469d8b25fb614e84883c9285994a to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <inttypes.h>
#include <math.h>
// Function, which calculates N, p and r from opslimit and memslimit copied from
// libsodium/crypto_pwhash/scryptsalsa208sha256/pwhash_scryptsalsa208sha256.c
int pickparams(unsigned long long opslimit, const size_t memlimit,
uint32_t *const N_log2, uint32_t *const p, uint32_t *const r) {
unsigned long long maxN;
unsigned long long maxrp;
if (opslimit < 32768) {
opslimit = 32768;
}
*r = 8;
if (opslimit < memlimit / 32) {
*p = 1;
maxN = opslimit / (*r * 4);
for (*N_log2 = 1; *N_log2 < 63; *N_log2 += 1) {
if ((uint64_t)(1) << *N_log2 > maxN / 2) {
break;
}
}
} else {
maxN = memlimit / ((size_t) *r * 128);
for (*N_log2 = 1; *N_log2 < 63; *N_log2 += 1) {
if ((uint64_t)(1) << *N_log2 > maxN / 2) {
break;
}
}
maxrp = (opslimit / 4) / ((uint64_t)(1) << *N_log2);
/* LCOV_EXCL_START */
if (maxrp > 0x3fffffff) {
maxrp = 0x3fffffff;
}
/* LCOV_EXCL_STOP */
*p = (uint32_t)(maxrp) / *r;
}
return 0;
}
int main() {
unsigned long long opslimit = pow(2, 20);
const size_t memlimit = pow(2, 24);
uint32_t N_log2;
uint32_t p;
uint32_t r;
pickparams(opslimit, memlimit, &N_log2, &p, &r);
printf("N: %" PRIu64 "\n", (uint64_t)(1) << N_log2);
printf("r: %" PRIu32 "\n", r);
printf("p: %" PRIu32 "\n", p);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment