Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
241818 views
1
#ifndef _SMALLJJACTAB_INCLUDE_
2
#define _SMALLJJACTAB_INCLUDE_
3
4
#include <stdint.h>
5
#include "jac.h"
6
#include "hecurve.h"
7
#include "ff.h"
8
9
/*
10
Copyright 2007 Andrew V. Sutherland
11
12
This file is part of smalljac.
13
14
smalljac is free software: you can redistribute it and/or modify
15
it under the terms of the GNU General Public License as published by
16
the Free Software Foundation, either version 2 of the License, or
17
(at your option) any later version.
18
19
smalljac is distributed in the hope that it will be useful,
20
but WITHOUT ANY WARRANTY; without even the implied warranty of
21
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
GNU General Public License for more details.
23
24
You should have received a copy of the GNU General Public License
25
along with smalljac. If not, see <http://www.gnu.org/licenses/>.
26
*/
27
28
/*
29
Simple table lookup/insert code for smalljac. This is designed for small tables
30
that fit in cache memory, using 64 bits per entry (128 bits per overflow entry).
31
This requires that the caller to either store the group entries in a seperate list
32
or reconstruct them as required.
33
34
The table size is always a power of two and hashing is done by simply bit-masking
35
the group element for speed. This can occasionally cause performance problems
36
but works well on average.
37
38
Note that the table size used in smalljac_init may be any power of 2 smaller than
39
the allocated table size - this is desirable as it reduces the cose of initialization
40
and improves locality when this is as small as possible without creating too many
41
collisions. A load factor of around 0.5 is works well.
42
*/
43
44
#if HECURVE_GENUS == 1
45
#define SMALLJAC_VBITS 17 // must be < 32
46
#define SMALLJAC_ITEM_BITS 21 // ITEM_BITS+1+VBITS < 64
47
#endif
48
#if HECURVE_GENUS == 2
49
#define SMALLJAC_VBITS 22 // must be < 32
50
#define SMALLJAC_ITEM_BITS 20 // 2*ITEM_BITS+1+VBITS < 64
51
#endif
52
#if HECURVE_GENUS == 3
53
#define SMALLJAC_VBITS 20 // must be < 32
54
#define SMALLJAC_ITEM_BITS 14 // 3*ITEM_BITS+1+VBITS < 64
55
#endif
56
#define SMALLJAC_VMASK ((1UL<<SMALLJAC_VBITS)-1)
57
#define SMALLJAC_MAX_MATCHES 64
58
59
60
struct smalljac_htab_list_item {
61
unsigned long item;
62
struct smalljac_htab_list_item *pNext;
63
};
64
unsigned long *smalljac_htab;
65
struct smalljac_htab_list_item *smalljac_htab_lists, *smalljac_htab_next, *smalljac_htab_end;
66
ff_t smalljac_tabmask;
67
int smalljac_table_bits;
68
69
void smalljac_table_alloc (int bits);
70
void smalljac_table_init (int bits);
71
72
void _smalljac_table_collision_insert (int index, unsigned long item);
73
int _smalljac_table_get_matches (uint32_t *matches, jac_t a[1], int index);
74
75
static inline void smalljac_table_insert (jac_t a[1], uint32_t value)
76
{
77
register ff_t i;
78
register unsigned long item;
79
80
#if HECURVE_GENUS == 1
81
i = a[0].u[0] & smalljac_tabmask;
82
item = a[0].u[0];
83
#else // genus >= 2
84
i = a[0].u[1] & smalljac_tabmask;
85
item = (_ff_one(a[0].u[HECURVE_GENUS]) ? (1UL<<SMALLJAC_ITEM_BITS) : 0) + a[0].u[HECURVE_GENUS-1];
86
item <<= SMALLJAC_ITEM_BITS;
87
item += a[0].u[HECURVE_GENUS-2];
88
#if HECURVE_GENUS == 3
89
item <<= SMALLJAC_ITEM_BITS;
90
item += a[0].u[HECURVE_GENUS-3];
91
#endif
92
#endif
93
item <<= SMALLJAC_VBITS;
94
item += value;
95
if ( ! smalljac_htab[i] ) { smalljac_htab[i] = item; smalljac_htab_lists[i].item = 0; return; }
96
_smalljac_table_collision_insert ((int)i, item);
97
}
98
99
100
static inline int smalljac_table_lookup (uint32_t matches[SMALLJAC_MAX_MATCHES], jac_t a[1])
101
{
102
register ff_t i;
103
104
#if HECURVE_GENUS == 1
105
i = a[0].u[0] & smalljac_tabmask;
106
#else
107
i = a[0].u[1] & smalljac_tabmask;
108
#endif
109
if ( ! smalljac_htab[i] ) return 0; // usual case
110
return _smalljac_table_get_matches (matches, a, (int)i);
111
}
112
113
#endif
114
115