Exemple #1
0
//we don't have to bother with making sure that gcd(R,M) == 1 since M is odd.
uberzahl modexp_mm(mm_t & mm, uberzahl base, uberzahl exp, uberzahl M){
	if(!mm.initialized){
		mm.R = next_power(M);
		mm.Rbits = mm.R.bitLength();
		mm.Mprime = (mm.R-M.inverse(mm.R));
		uberzahl z("1");
		uberzahl t("2");
		mm.Rsq = modexp(mm.R,t,M);
		//mm.z_init = mm.R % M;
		mm.z_init = montgomery_reduction(mm.Rsq, M, mm.Mprime, mm.Rbits, mm.R);
		mm.initialized = true;
	}

	//convert into Montgomery space
	uberzahl z = mm.z_init;

	//According to Piazza post we don't even need to calculate the residues with mod
	if(base * mm.Rsq < mm.R*M)
		base = montgomery_reduction(base * mm.Rsq, M, mm.Mprime, mm.Rbits, mm.R);
	else
		base = base * mm.R % M;

	mediumType i = exp.bitLength() - 1;

	while(i >= 0) {
		z = montgomery_reduction(z * z, M, mm.Mprime, mm.Rbits, mm.R);
		if(exp.bit(i) == 1){
			z = montgomery_reduction(z * base , M, mm.Mprime, mm.Rbits, mm.R);
		}
		if(i == 0)
			break;
		i -= 1;
	}
	return montgomery_reduction(z, M, mm.Mprime, mm.Rbits, mm.R);
}
Exemple #2
0
/* Expand or create the hashtable */
int ht_expand(struct hashtable *t, size_t size)
{
	struct hashtable n; /* the new hashtable */
	unsigned int realsize = next_power(size), i;

	/* the size is invalid if it is smaller than the number of
	 * elements already inside the hashtable */
	if (t->used >= size)
		return HT_INVALID;

	ht_init(&n);
	n.size = realsize;
	n.sizemask = realsize-1;
	n.table = malloc(realsize*sizeof(struct ht_ele*));
	if (n.table == NULL)
		return HT_NOMEM;
	/* Copy methods */
	n.hashf = t->hashf;
	n.key_destructor = t->key_destructor;
	n.val_destructor = t->val_destructor;
	n.key_compare= t->key_compare;

	/* Initialize all the pointers to NULL */
	memset(n.table, 0, realsize*sizeof(struct ht_ele*));

	/* Copy all the elements from the old to the new table:
	 * note that if the old hash table is empty t->size is zero,
	 * so ht_expand() acts like an ht_create() */
	n.used = t->used;
	for (i = 0; i < t->size && t->used > 0; i++) {
		if (t->table[i] != NULL && t->table[i] != ht_free_element) {
			u_int32_t h;

			/* Get the new element index: note that we
			 * know that there aren't freed elements in 'n' */
			h = n.hashf(t->table[i]->key) & n.sizemask;
			if (n.table[h]) {
				n.collisions++;
				while(1) {
					h = (h+1) & n.sizemask;
					if (!n.table[h])
						break;
					n.collisions++;
				}
			}
			/* Move the element */
			n.table[h] = t->table[i];
			t->used--;
		}
	}
	assert(t->used == 0);
	free(t->table);

	/* Remap the new hashtable in the old */
	*t = n;
	return HT_OK;
}
Exemple #3
0
struct buffer_node* buffernode_new(unsigned long len)
{
	struct buffer_node * ret;
	len = next_power(len);
	ret = (struct buffer_node *)malloc(sizeof(*ret) + len);
	ret->next = NULL;
	ret->size = len;
	ret->in = 0;
	ret->out = 0;
	return ret;
}
Exemple #4
0
/* Initialize an allocated hash table. */
int hash_init(hash_t *ht, unsigned int slots) {
    assert(ht);

    ht->slots = next_power(slots);
    ht->count = 0;

    ht->data = (hash_entry_t **)calloc(ht->slots, sizeof(hash_entry_t *));
    if (!ht->data) {
        return -1;
    }
    return 0;
}
Exemple #5
0
static int buffer_extendby(buffer_t *buf, size_t extby)
{
    char *data;
    size_t newlen = _unlikely_(!buf->buflen && extby < 64)
                    ? 64 : buf->len + extby;

    if (newlen > buf->buflen) {
        newlen = next_power(newlen);
        data = realloc(buf->data, newlen);
        if (!data)
            return -errno;

        buf->buflen = newlen;
        buf->data = data;
    }

    return 0;
}
Exemple #6
0
/**
 * NAME: pad_matrix
 * INPUT: MATRIX* mOrig, MARIX* mNew
 * USAGE: Converts a matrix to the smallest 2^n by 2^n square matrix.
 *          Pads missing values with 0.  Stores the result in mNew.
 *          Original Input is unmodified.
 * 
 * NOTES: Assumes mOrig and mNew are already initalized.
 */
void pad_matrix(MATRIX* mOrig, MATRIX* mNew)
{
    // Calculate the new dimensions.
    int origDims;
    if(mOrig->numRows > mOrig->numCols)
        origDims = mOrig->numRows;
    else
        origDims = mOrig->numCols;
    int newDims = next_power(origDims);

    // Allocate memory for the new matrix and add zeros.
    zero_matrix(newDims, newDims, mNew);
    for(int i = 0; i < newDims; i++)
    {
        for (int j=0; j< newDims; j++)
            // Grab entries if they are in the original matrix.
            // Otherwise, pad with 0.
            if(i < mOrig->numRows && j < mOrig->numCols)
                mNew->matrix[i][j] = mOrig->matrix[i][j];   
    }
}