Permalink
Browse files

Add utility to print secret table keys

  • Loading branch information...
1 parent 06fdb86 commit 15b96b9e4d9da0ab3abe1288709658a5b6ae6006 @markflorisson markflorisson committed Apr 8, 2013
Showing with 31 additions and 12 deletions.
  1. +6 −4 deps/pyextensibletype/extensibletype/methodtable.pyx
  2. +25 −8 deps/pyextensibletype/include/interning.h
@@ -26,22 +26,23 @@ class HashingError(Exception):
cdef PyCustomSlots_Table *allocate_hash_table(uint16_t size) except NULL:
cdef PyCustomSlots_Table *table
+ cdef int nbins = size * 2
size = roundup(size)
table = <PyCustomSlots_Table *> stdlib.calloc(
- 1, sizeof(PyCustomSlots_Table) + sizeof(uint16_t) * size +
+ 1, sizeof(PyCustomSlots_Table) + sizeof(uint16_t) * nbins +
sizeof(PyCustomSlots_Entry) * size)
if table == NULL:
raise MemoryError
table.n = size
- table.b = size
+ table.b = nbins
table.flags = 0
table.entries = <PyCustomSlots_Entry *> ((<char *> &table[1]) +
- size * sizeof(uint16_t))
+ table.b * sizeof(uint16_t))
return table
@@ -90,7 +91,7 @@ cdef class PerfectHashMethodTable(object):
self.displacements = <uint16_t *> (<char *> self.table +
sizeof(PyCustomSlots_Table))
- hashes = np.empty(self.table.n, dtype=np.uint64)
+ hashes = np.zeros(self.table.n, dtype=np.uint64)
intern.global_intern_initialize()
@@ -108,6 +109,7 @@ cdef class PerfectHashMethodTable(object):
hashes[n:self.table.n] = extensibletype.draw_hashes(np.random,
self.table.n - n)
+ assert len(np.unique(hashes)) == len(hashes)
# Perfect hash our table
if PyCustomSlots_PerfectHash(self.table, &hashes[0]) < 0:
@@ -9,6 +9,7 @@ extern "C" {
#include <Python.h>
#include <stdlib.h>
+#include <stdint.h>
#include "siphash24.h"
#if PY_MAJOR_VERSION < 3
@@ -28,20 +29,34 @@ extern "C" {
typedef struct _intern_table_t {
PyObject *signature_to_key;
PyObject *key_to_signature;
- char secrets[16*4]; /* 4 secret keys, which we try in succession */
+ char secrets[16*4]; /* 4 secret keys, which we try in succession */
} intern_table_t;
/* Prototypes */
static void intern_destroy_table(intern_table_t *table);
/* API */
+static void
+_print_secrets(intern_table_t *table)
+{
+ int i, j;
+
+ for (i = 0; i < 4; i++) {
+ printf("secret key[%d] = {", i);
+ for (j = 0; j < 16; j += 4) {
+ printf(" %-8x, ", *(int32_t *) &table->secrets[i * 16 + j]);
+ }
+ printf("}\n");
+ }
+}
+
/* Create an intern table from preallocated memory.
Returns NULL on failure with an appropriate exception set. */
static intern_table_t *
intern_create_table(intern_table_t *table)
{
- int i, randval;
+ int i;
table->signature_to_key = NULL;
table->key_to_signature = NULL;
@@ -52,15 +67,17 @@ intern_create_table(intern_table_t *table)
if (!table->signature_to_key || !table->key_to_signature)
goto bad;
- for (i = 0; i < 16 * 4; i+=2) {
- randval = rand(); /* TODO: use a better prng */
-
+ for (i = 0; i < 16 * 4; i += 2) {
/* Take the lower two bytes from the random value, since
- RAND_MAX is at least 2**16 */
- table->secrets[i + 0] = ((char *) &randval)[sizeof(int) - 2];
- table->secrets[i + 1] = ((char *) &randval)[sizeof(int) - 1];
+ RAND_MAX is at least 2**16 */
+ short randval = (short) rand(); /* TODO: use a better prng */
+
+ table->secrets[i + 0] = ((char *) &randval)[0];
+ table->secrets[i + 1] = ((char *) &randval)[1];
}
+ _print_secrets(table);
+
return table;
bad:
intern_destroy_table(table);

0 comments on commit 15b96b9

Please sign in to comment.