Skip to content

Instantly share code, notes, and snippets.

@jamwt
Created March 11, 2015 22:17
Show Gist options
  • Save jamwt/172841c8067e4e3fe813 to your computer and use it in GitHub Desktop.
Save jamwt/172841c8067e4e3fe813 to your computer and use it in GitHub Desktop.
// Basic idea is to perturb a PRNG state using each word in a bytestream.
#include <assert.h>
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "pcg_variants.h"
void pcg_checksum_init(pcg64si_random_t *rng) {
pcg64si_srandom_r(rng, 0u);
}
void pcg_checksum_update(pcg64si_random_t *rng, uint8_t *data, size_t len) {
assert((len % sizeof(uint64_t)) == 0);
uint64_t *words = (uint64_t *)data;
size_t word_count = len / sizeof(uint64_t);
uint64_t *end = words + word_count;
while (words != end) {
(void)pcg64si_random_r(rng);
rng->state ^= *words;
words++;
}
}
uint64_t pcg_checksum_final(pcg64si_random_t *rng) {
return pcg64si_random_r(rng);
}
int main(int argc, char** argv)
{
pcg64si_random_t rng;
pcg_checksum_init(&rng);
char *one_mb = calloc(1, 1024 * 1024);
one_mb[0] = '3';
int i;
for (i=0; i < 1024; i++) {
pcg_checksum_update(&rng, (uint8_t *)one_mb, 1024 * 1024);
}
printf("Final was: %" PRIx64 "\n", pcg_checksum_final(&rng));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment