#ifndef _SMALLJJACTAB_INCLUDE_1#define _SMALLJJACTAB_INCLUDE_23#include <stdint.h>4#include "jac.h"5#include "hecurve.h"6#include "ff.h"78/*9Copyright 2007 Andrew V. Sutherland1011This file is part of smalljac.1213smalljac is free software: you can redistribute it and/or modify14it under the terms of the GNU General Public License as published by15the Free Software Foundation, either version 2 of the License, or16(at your option) any later version.1718smalljac is distributed in the hope that it will be useful,19but WITHOUT ANY WARRANTY; without even the implied warranty of20MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the21GNU General Public License for more details.2223You should have received a copy of the GNU General Public License24along with smalljac. If not, see <http://www.gnu.org/licenses/>.25*/2627/*28Simple table lookup/insert code for smalljac. This is designed for small tables29that fit in cache memory, using 64 bits per entry (128 bits per overflow entry).30This requires that the caller to either store the group entries in a seperate list31or reconstruct them as required.3233The table size is always a power of two and hashing is done by simply bit-masking34the group element for speed. This can occasionally cause performance problems35but works well on average.3637Note that the table size used in smalljac_init may be any power of 2 smaller than38the allocated table size - this is desirable as it reduces the cose of initialization39and improves locality when this is as small as possible without creating too many40collisions. A load factor of around 0.5 is works well.41*/4243#if HECURVE_GENUS == 144#define SMALLJAC_VBITS 17 // must be < 3245#define SMALLJAC_ITEM_BITS 21 // ITEM_BITS+1+VBITS < 6446#endif47#if HECURVE_GENUS == 248#define SMALLJAC_VBITS 22 // must be < 3249#define SMALLJAC_ITEM_BITS 20 // 2*ITEM_BITS+1+VBITS < 6450#endif51#if HECURVE_GENUS == 352#define SMALLJAC_VBITS 20 // must be < 3253#define SMALLJAC_ITEM_BITS 14 // 3*ITEM_BITS+1+VBITS < 6454#endif55#define SMALLJAC_VMASK ((1UL<<SMALLJAC_VBITS)-1)56#define SMALLJAC_MAX_MATCHES 64575859struct smalljac_htab_list_item {60unsigned long item;61struct smalljac_htab_list_item *pNext;62};63unsigned long *smalljac_htab;64struct smalljac_htab_list_item *smalljac_htab_lists, *smalljac_htab_next, *smalljac_htab_end;65ff_t smalljac_tabmask;66int smalljac_table_bits;6768void smalljac_table_alloc (int bits);69void smalljac_table_init (int bits);7071void _smalljac_table_collision_insert (int index, unsigned long item);72int _smalljac_table_get_matches (uint32_t *matches, jac_t a[1], int index);7374static inline void smalljac_table_insert (jac_t a[1], uint32_t value)75{76register ff_t i;77register unsigned long item;7879#if HECURVE_GENUS == 180i = a[0].u[0] & smalljac_tabmask;81item = a[0].u[0];82#else // genus >= 283i = a[0].u[1] & smalljac_tabmask;84item = (_ff_one(a[0].u[HECURVE_GENUS]) ? (1UL<<SMALLJAC_ITEM_BITS) : 0) + a[0].u[HECURVE_GENUS-1];85item <<= SMALLJAC_ITEM_BITS;86item += a[0].u[HECURVE_GENUS-2];87#if HECURVE_GENUS == 388item <<= SMALLJAC_ITEM_BITS;89item += a[0].u[HECURVE_GENUS-3];90#endif91#endif92item <<= SMALLJAC_VBITS;93item += value;94if ( ! smalljac_htab[i] ) { smalljac_htab[i] = item; smalljac_htab_lists[i].item = 0; return; }95_smalljac_table_collision_insert ((int)i, item);96}979899static inline int smalljac_table_lookup (uint32_t matches[SMALLJAC_MAX_MATCHES], jac_t a[1])100{101register ff_t i;102103#if HECURVE_GENUS == 1104i = a[0].u[0] & smalljac_tabmask;105#else106i = a[0].u[1] & smalljac_tabmask;107#endif108if ( ! smalljac_htab[i] ) return 0; // usual case109return _smalljac_table_get_matches (matches, a, (int)i);110}111112#endif113114115