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
156
157
158
|
/*
* ecgen, tool for generating Elliptic curve domain parameters
* Copyright (C) 2017 J08nY
*/
#include <gen/types.h>
#include "subgroups.h"
/**
* @brief All prime factors of a given integer.
*
* subgroups_factors(27) = [3]
* @param order
* @return a t_VEC of prime factors.
*/
static GEN subgroups_factors(GEN order) {
GEN factors = Z_factor(order);
return gtovec(gel(factors, 1));
}
/**
* @brief All prime divisors of a given integer with multiplicity.
*
* subgroups_divisors(27) = [3, 3, 3]
* @param order
* @return a t_VEC of prime divisors.
*/
static GEN 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;
}
/**
* @brief All factors consisting of at least <code>min_bits</code> prime <code>factors</code>.
*
* @param factors
* @param min_bits
* @return a t_VEC of factors
*/
static GEN subgroups_2n_factors(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;
}
}
GEN sorted = sort(groups);
size_t k = 1;
for (size_t j = 1; j <= i; j++) {
GEN k_value = gel(sorted, k);
GEN j_value = gel(sorted, j);
if (!gequal(k_value, j_value)) {
gel(sorted, ++k) = j_value;
}
}
sorted = vec_shorten(sorted, k);
return sorted;
}
/**
* @brief
* @param curve
* @param min_bits
* @return
*/
static GEN subgroups_2n_gens(const curve_t *curve, size_t min_bits) {
GEN one_factors = subgroups_divisors(curve->generators[0]->order);
GEN one = subgroups_2n_factors(one_factors, min_bits);
GEN other_factors = subgroups_divisors(curve->generators[1]->order);
GEN other = subgroups_2n_factors(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;
}
/**
* @brief
* @param curve
* @param min_bits
* @return
*/
static GEN subgroups_2n(const curve_t *curve, size_t min_bits) {
if (curve->ngens == 1) {
GEN factors = subgroups_divisors(curve->order);
return subgroups_2n_factors(factors, min_bits);
}
return subgroups_2n_gens(curve, min_bits);
}
GEN subgroups_prime(const curve_t *curve, const config_t *cfg) {
if (cfg->prime || isprime(curve->order)) {
return gtovec(curve->order);
}
return subgroups_factors(curve->order);
}
GEN subgroups_nonprime(const curve_t *curve, const config_t *cfg) {
if (cfg->prime || isprime(curve->order)) {
return NULL;
}
return subgroups_2n(curve, 1);
}
GEN subgroups_all(const curve_t *curve, const config_t *cfg) {
if (cfg->prime || isprime(curve->order)) {
return gtovec(curve->order);
}
return subgroups_2n(curve, 0);
}
|