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
  
/* 
|| This file is part of Pike. For copyright information see COPYRIGHT. 
|| Pike is distributed under GPL, LGPL and MPL. See the file COPYING 
|| for more information. 
*/ 
 
#include "global.h" 
#include "gmp_machine.h" 
#include "my_gmp.h" 
 
#include "interpret.h" 
#include "program.h" 
 
/* #define ENABLE_SMPZ_INVERT */ 
 
#undef THIS 
#define DECLARE_THIS() struct pike_frame *_fp = Pike_fp 
#define THIS ((MP_INT *)(_fp->current_storage)) 
#define THIS_PROGRAM (_fp->context->prog) 
 
static struct program *smpz_program = NULL; 
 
#ifdef HAVE_GMP5 
static void smpz_powm(INT32 args) 
{ 
  DECLARE_THIS(); 
  struct object *res = NULL; 
  MP_INT *n, *e; 
 
  if(args != 2) 
    SIMPLE_WRONG_NUM_ARGS_ERROR ("powm", 2); 
 
  e = get_mpz(Pike_sp - 2, 1, "powm", 1, 2); 
  n = get_mpz(Pike_sp - 1, 1, "powm", 2, 2); 
 
  if (!mpz_sgn(n)) 
    SIMPLE_DIVISION_BY_ZERO_ERROR ("powm"); 
  res = fast_clone_object(THIS_PROGRAM); 
  mpz_powm_sec(OBTOMPZ(res), THIS, e, n); 
  pop_n_elems(args); 
  push_object(res); 
} 
#endif 
 
#ifdef HAVE_GMP6 
 
#ifdef ENABLE_SMPZ_INVERT 
/* This function has multiple issues: 
 * 
 *   * It doesn't work as implemented (eg argument 6 to mpb_sec_invert() 
 *     is wrong). 
 * 
 *   * It would clobber THIS. Gmp manual 8.1: 
 *     "In either case, the input A is destroyed." 
 * 
 *   * To work, the number of limbs in THIS, modulo and res MUST 
 *     be the same (aka n). This can probably be accomplished 
 *     by using mpz_realloc2(), of which the Gmp manual 5.1 says: 
 *     "Calling this function is never necessary; reallocation is 
 *     handled automatically by GMP when needed." 
 * 
 * Fixing the above issues while still keeping the _sec property 
 * is non-trivial, and best left to the Gmp people, so we wait for 
 * an mpz_invert_sec(). 
 * 
 *      /grubba 2014-12-18 
 */ 
 
/* int mpn_sec_invert (mp_limb_t *rp, mp_limb_t *ap, const mp_limb_t *mp, 
                       mp_size_t n, mp_bitcnt_t nbcnt, mp_limb_t *tp) */ 
/* int mpz_invert (mpz_t rop, const mpz_t op1, const mpz_t op2) */ 
static void smpz_invert(INT32 args) 
{ 
  DECLARE_THIS(); 
  MP_INT *modulo; 
  struct object *res; 
 
  if (args != 1) 
    SIMPLE_WRONG_NUM_ARGS_ERROR ("invert", 1); 
  modulo = get_mpz(Pike_sp-1, 1, "invert", 1, 1); 
 
  if (!mpz_sgn(modulo)) 
    SIMPLE_DIVISION_BY_ZERO_ERROR ("invert"); 
  res = fast_clone_object(THIS_PROGRAM); 
  if (mpn_sec_invert(LIMBS(OBTOMPZ(res)), LIMBS(THIS), LIMBS(modulo), 
                     NLIMBS(OBTOMPZ(res)), NLIMBS(THIS), NLIMBS(modulo)) == 0) 
  { 
    free_object(res); 
    Pike_error("Not invertible.\n"); 
  } 
  pop_n_elems(args); 
  push_object(res); 
} 
#endif /* ENABLE_SMPZ_INVERT */ 
#endif 
 
void pike_init_smpz_module(void) 
{ 
  start_new_program(); 
  low_inherit(mpzmod_program, NULL, -1, 0, 0, NULL); 
 
#ifdef HAVE_GMP5 
  ADD_INT_CONSTANT("sec_powm", 1, 0); 
  ADD_FUNCTION("powm",smpz_powm,tFunc(tMpz_arg tMpz_arg,tMpz_ret),0); 
#endif 
 
#ifdef HAVE_GMP6 
#ifdef ENABLE_SMPZ_INVERT 
  ADD_INT_CONSTANT("sec_invert", 1, 0); 
  ADD_FUNCTION("invert", smpz_invert,tFunc(tMpz_arg,tMpz_ret),0); 
#endif /* ENABLE_SMPZ_INVERT */ 
#endif 
 
  smpz_program = end_program(); 
  add_program_constant("smpz", smpz_program, 0); 
} 
 
void pike_exit_smpz_module(void) 
{ 
  if( smpz_program ) 
  { 
    free_program(smpz_program); 
    smpz_program = NULL; 
  } 
}