File: factor.c

package info (click to toggle)
gmp-ecm 7.0.4+ds-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 4,728 kB
  • sloc: asm: 36,431; ansic: 34,057; xml: 885; python: 799; sh: 698; makefile: 348
file content (191 lines) | stat: -rw-r--r-- 5,813 bytes parent folder | download | duplicates (2)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* factor.c - public interface for libecm.

Copyright 2005, 2006, 2007, 2009, 2011 Paul Zimmermann, Alexander Kruppa,
David Cleaver, Cyril Bouvier.

This file is part of the ECM Library.

The ECM Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at your
option) any later version.

The ECM Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
License for more details.

You should have received a copy of the GNU Lesser General Public License
along with the ECM Library; see the file COPYING.LIB.  If not, see
http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */

#include <stdio.h>
#include <math.h>
#include "ecm-impl.h"
#include "ecm-gpu.h"


const char *
ecm_version ()
{
  static const char *version = ECM_VERSION;
  return version;
}

void
ecm_init (ecm_params q)
{
  __ell_curve_struct *ptrE = (__ell_curve_struct *) malloc(sizeof(__ell_curve_struct));

  q->method = ECM_ECM; /* default method */
  mpz_init_set_ui (q->x, 0);
  mpz_init_set_ui (q->y, 0);
  mpz_init_set_ui (q->sigma, 0);
  q->sigma_is_A = 0;
  mpz_init_set_ui (ptrE->a1, 0);
  mpz_init_set_ui (ptrE->a3, 0);
  mpz_init_set_ui (ptrE->a2, 0);
  mpz_init_set_ui (ptrE->a4, 0);
  mpz_init_set_ui (ptrE->a6, 0);
  ptrE->type = ECM_EC_TYPE_MONTGOMERY;
  ptrE->disc = 0;
  mpz_init_set_ui (ptrE->sq[0], 1);
  q->E = ptrE;
  q->param = ECM_PARAM_DEFAULT;
  mpz_init_set_ui (q->go, 1);
  q->B1done = ECM_DEFAULT_B1_DONE;
  mpz_init_set_si (q->B2min, -1.0); /* default: B2min will be set to B1 */
  mpz_init_set_si (q->B2, ECM_DEFAULT_B2);
  q->k = ECM_DEFAULT_K;
  q->S = ECM_DEFAULT_S; /* automatic choice of polynomial */
  q->repr = ECM_MOD_DEFAULT; /* automatic choice of representation */
  q->nobase2step2 = 0; /* continue special base 2 code in ecm step 2, if used */
  q->verbose = 0; /* no output (default in library mode) */
  q->os = stdout; /* standard output */
  q->es = stderr; /* error output */
  q->chkfilename = NULL;
  q->TreeFilename = NULL;
  q->maxmem = 0.0;
  q->stage1time = 0.0;
  gmp_randinit_default (q->rng);
  mpz_set_ui (q->rng->_mp_seed, 0); /* trick to tell that the random number
                                       generator has not been initialized */
  q->use_ntt = 1;
  q->stop_asap = NULL;
  q->batch_last_B1_used = 1.0;
  mpz_init_set_ui (q->batch_s, 1);
  q->gpu = 0; /* no gpu by default in library mode */
  q->gpu_device = -1; 
  q->gpu_device_init = 0; 
  q->gpu_number_of_curves = 0; 
  q->gw_k = 0.0;
  q->gw_b = 0;
  q->gw_n = 0;
  q->gw_c = 0;
}

void
ecm_clear (ecm_params q)
{
  mpz_clear (q->x);
  mpz_clear (q->y);
  mpz_clear (q->sigma);
  mpz_clear (q->go);
  mpz_clear (q->B2min);
  mpz_clear (q->B2);
  gmp_randclear (q->rng);
  mpz_clear (q->batch_s);
  mpz_clear (q->E->a1);
  mpz_clear (q->E->a3);
  mpz_clear (q->E->a2);
  mpz_clear (q->E->a4);
  mpz_clear (q->E->a6);
  mpz_clear (q->E->sq[0]);
  free (q->E);
}

/* returns ECM_FACTOR_FOUND, ECM_NO_FACTOR_FOUND, or ECM_ERROR */
int
ecm_factor (mpz_t f, mpz_t n, double B1, ecm_params p0)
{
  int res; /* return value */
  ecm_params q;
  ecm_params_ptr p;

  if (mpz_cmp_ui (n, 0) <= 0)
    {
      fprintf ((p0 == NULL) ? stderr : p0->es,
               "Error, n should be positive.\n");
      return ECM_ERROR;
    }
  else if (mpz_cmp_ui (n, 1) == 0)
    {
      mpz_set_ui (f, 1);
      return ECM_FACTOR_FOUND_STEP1;
    }
  else if (mpz_divisible_2exp_p (n, 1))
    {
      mpz_set_ui (f, 2);
      return ECM_FACTOR_FOUND_STEP1;
    }
  
  if (p0 == NULL)
    {
      p = q;
      ecm_init (q);
    }
  else
    p = p0;

  if (p->method == ECM_ECM)
    {
#ifdef WITH_GPU
      if (p->gpu == 0)
        {
#endif
            res = ecm (f, p->x, p->y, &(p->param), p->sigma, n, p->go,
		       &(p->B1done),
                       B1, p->B2min, p->B2, p->k, p->S, p->verbose,
                       p->repr, p->nobase2step2, p->use_ntt, 
		       p->sigma_is_A, p->E,
                       p->os, p->es, p->chkfilename, p->TreeFilename, p->maxmem,
                       p->stage1time, p->rng, p->stop_asap, p->batch_s,
                       &(p->batch_last_B1_used), p->gw_k, p->gw_b, p->gw_n,
                       p->gw_c);
#ifdef WITH_GPU
        }
      else
        {
          res = gpu_ecm (f, p->x, &(p->param), p->sigma, n, p->go,
                         &(p->B1done), B1, p->B2min, p->B2, p->k,
                         p->S, p->verbose, p->repr, p->nobase2step2, 
                         p->use_ntt, p->sigma_is_A, p->os, p->es,
                         p->chkfilename, p->TreeFilename, p->maxmem,
                         p->stop_asap, p->batch_s, &(p->batch_last_B1_used),
                         p->gpu_device, &(p->gpu_device_init),
                         &(p->gpu_number_of_curves));
        }
#endif
    }
  else if (p->method == ECM_PM1)
    res = pm1 (f, p->x, n, p->go, &(p->B1done), B1, p->B2min, p->B2,
               p->k, p->verbose, p->repr, p->use_ntt, p->os, p->es,
               p->chkfilename, p->TreeFilename, p->maxmem, p->rng,
               p->stop_asap);
  else if (p->method == ECM_PP1)
    res = pp1 (f, p->x, n, p->go, &(p->B1done), B1, p->B2min, p->B2,
               p->k, p->verbose, p->repr, p->use_ntt, p->os, p->es,
               p->chkfilename, p->TreeFilename, p->maxmem, p->rng,
               p->stop_asap);
  else
    {
      fprintf (p->es, "Error, unknown method: %d\n", p->method);
      res = ECM_ERROR;
    }

  if (p0 == NULL)
    ecm_clear (q);

  return res;
}