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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
/*
* ecgen, tool for generating Elliptic curve domain parameters
* Copyright (C) 2017 J08nY
*/
#include "subgroups.h"
#include "gen/types.h"
/**
* @brief All prime divisors of a given integer.
* @param order
* @return
*/
static GEN subgroups_factors(GEN order) {
GEN factors = Z_factor(order);
return gel(factors, 1);
}
/**
* @brief All prime divisors of a given integer with multiplicity.
* @param order
* @return
*/
static GEN __attribute__((unused)) subgroups_divisors(GEN order) {
GEN factors = Z_factor(order);
GEN primes = gel(factors, 1);
GEN multiples = gel(factors, 2);
long uniqs = glength(primes);
long size = 0;
for (long i = 1; i <= uniqs; ++i) {
size += itos(gel(multiples, i));
}
GEN result = gtovec0(gen_0, size);
long count = 0;
for (long i = 1; i <= uniqs; ++i) {
long multiple = itos(gel(multiples, i));
for (long j = 1; j <= multiple; ++j) {
gel(result, ++count) = gel(primes, i);
}
}
return result;
}
/*
static GEN subgroups_exponents(GEN order) {
GEN factors = Z_factor(order);
GEN primes = gel(factors, 1);
GEN multiples = gel(factors, 2);
long len = glength(primes);
pari_ulong count = 1;
for (long i = 1; i <= len; ++i) {
count *= itou(gel(multiples,i)) + 1;
}
GEN result = gtovec0(gen_0, count);
}
*/
GEN subgroups_prime(const curve_t *curve, const config_t *cfg) {
if (cfg->prime || isprime(curve->order)) {
return gtovec(curve->order);
} else {
return subgroups_factors(curve->order);
}
}
/**
* @brief
* @param factors
* @param min_bits
* @return
*/
static GEN subgroups_2n(GEN factors, size_t min_bits) {
long nprimes = glength(factors);
if (nprimes == min_bits) return NULL;
GEN amount = int2n(nprimes);
GEN groups = gtovec0(gen_0, itos(amount) - (min_bits * nprimes) - 1);
size_t i = 0;
for (size_t count = 1; count < (size_t)(1 << nprimes); ++count) {
pari_sp btop = avma;
GEN result = gen_1;
size_t bits = 0;
for (long bit = 0; bit < nprimes; ++bit) {
size_t mask = (size_t)(1 << bit);
if (count & mask) {
result = mulii(result, gel(factors, bit + 1));
bits++;
}
}
if (bits > min_bits) {
gel(groups, ++i) = result;
} else {
avma = btop;
}
}
return groups;
}
/**
* @brief
* @param curve
* @param min_bits
* @return
*/
static GEN subgroups_2n_gens(const curve_t *curve, size_t min_bits) {
GEN one_factors = subgroups_factors(curve->generators[0]->order);
GEN one = subgroups_2n(one_factors, min_bits);
GEN other_factors = subgroups_factors(curve->generators[1]->order);
GEN other = subgroups_2n(other_factors, min_bits);
if (!one) {
return other;
}
if (!other) {
return one;
}
GEN result = gtovec0(gen_0, glength(one) + glength(other));
for (long i = 1; i <= glength(result); ++i) {
if (i <= glength(one)) {
gel(result, i) = gel(one, i);
} else {
gel(result, i) = gel(other, i - glength(one));
}
}
return result;
}
GEN subgroups_nonprime(const curve_t *curve, const config_t *cfg) {
if (cfg->prime || isprime(curve->order)) {
return NULL;
} else {
if (curve->ngens == 1) {
GEN factors = subgroups_factors(curve->order);
return subgroups_2n(factors, 1);
} else {
return subgroups_2n_gens(curve, 1);
}
}
}
GEN subgroups_all(const curve_t *curve, const config_t *cfg) {
if (cfg->prime || isprime(curve->order)) {
return gtovec(curve->order);
} else {
if (curve->ngens == 1) {
GEN factors = subgroups_factors(curve->order);
return subgroups_2n(factors, 0);
} else {
return subgroups_2n_gens(curve, 0);
}
}
}
|