summaryrefslogtreecommitdiffstats
path: root/moon-abe/pbc-0.5.14/arith/random.c
blob: 68228b3f2280ffee11c33a1e8f14152a58050349 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <stdio.h>
#include <stdint.h> // for intptr_t
#include <stdlib.h>
#include <gmp.h>
#include "pbc_random.h"
#include "pbc_utils.h"
#include "pbc_memory.h"

void pbc_init_random(void);

// Must use pointer due to lack of gmp_randstate_ptr.
static gmp_randstate_t *get_rs(void) {
  static int rs_is_ready;
  static gmp_randstate_t rs;
  if (!rs_is_ready) {
    gmp_randinit_default(rs);
    rs_is_ready = 1;
  }
  return &rs;
}

static void deterministic_mpz_random(mpz_t z, mpz_t limit, void *data) {
  UNUSED_VAR (data);
  mpz_urandomm(z, *get_rs(), limit);
}

static void file_mpz_random(mpz_t r, mpz_t limit, void *data) {
  char *filename = (char *) data;
  FILE *fp;
  int n, bytecount, leftover;
  unsigned char *bytes;
  mpz_t z;
  mpz_init(z);
  fp = fopen(filename, "rb");
  if (!fp) return;
  n = mpz_sizeinbase(limit, 2);
  bytecount = (n + 7) / 8;
  leftover = n % 8;
  bytes = (unsigned char *) pbc_malloc(bytecount);
  for (;;) {
    if (!fread(bytes, 1, bytecount, fp)) {
      pbc_warn("error reading source of random bits");
      return;
    }
    if (leftover) {
      *bytes = *bytes % (1 << leftover);
    }
    mpz_import(z, bytecount, 1, 1, 0, 0, bytes);
    if (mpz_cmp(z, limit) < 0) break;
  }
  fclose(fp);
  mpz_set(r, z);
  mpz_clear(z);
  pbc_free(bytes);
}

static void (*current_mpz_random)(mpz_t, mpz_t, void *);
static void *current_random_data;
static int random_function_ready = 0;

void pbc_random_set_function(void (*fun)(mpz_t, mpz_t, void *), void *data) {
  current_mpz_random = fun;
  current_random_data = data;
  random_function_ready = 1;
}

void pbc_mpz_random(mpz_t z, mpz_t limit) {
  if (!random_function_ready) pbc_init_random();
  current_mpz_random(z, limit, current_random_data);
}

void pbc_mpz_randomb(mpz_t z, unsigned int bits) {
  mpz_t limit;
  mpz_init(limit);
  mpz_setbit(limit, bits);
  pbc_mpz_random(z, limit);
  mpz_clear(limit);
}

void pbc_random_set_deterministic(unsigned int seed) {
  gmp_randseed_ui(*get_rs(), seed);
  pbc_random_set_function(deterministic_mpz_random, NULL);
}

void pbc_random_set_file(char *filename) {
  pbc_random_set_function(file_mpz_random, filename);
}