My Project
Loading...
Searching...
No Matches
kChinese2.cc
Go to the documentation of this file.
1#include "misc/auxiliary.h"
2
3#include "misc/intvec.h"
5#include "polys/matpol.h"
7
8#include <gmp.h>
9#include <stdlib.h>
10#include <stdio.h>
11
12// Garner's Algorithm.
13// See Algorithm 14.71, Handbook of Cryptography.
14
15// x - result v residuals m - primes t-size of vectors
16// prod: product of m_i
17// prepare:
18static void prepareCRT(mpz_ptr C[], mpz_ptr *m, mpz_t prod, int t)
19{
20 mpz_t u;
21 int i, j;
22
23 mpz_init(u);
24 mpz_init_set(prod,m[0]);
25 for (i=1; i<t; i++)
26 {
27 mpz_mul(prod,prod,m[i]);
28 mpz_init(C[i]);
29 mpz_set_ui(C[i], 1);
30 for (j=0; j<i; j++)
31 {
32 mpz_invert(u, m[j], m[i]);
33 mpz_mul(C[i], C[i], u);
34 mpz_mod(C[i], C[i], m[i]);
35 }
36 }
37 mpz_clear(u);
38}
39
40// the proper CRT
41// x - result v residuals m - primes t-size of vectors
42static void CRT(mpz_t x, mpz_ptr *v, mpz_ptr* C, mpz_ptr *m, int t)
43{
44 mpz_t u;
45 int i, j;
46
47 mpz_init(u);
48 mpz_set(u, v[0]);
49 mpz_set(x, u);
50 for (i=1; i<t; i++)
51 {
52 mpz_sub(u, v[i], x);
53 mpz_mul(u, u, C[i]);
54 mpz_mod(u, u, m[i]);
55 for (j=0; j<i; j++)
56 {
57 mpz_mul(u, u, m[j]);
58 }
59 mpz_add(x, x, u);
60 }
61 mpz_clear(u);
62}
63
64/*2
65* xx,q: arrays of length 0..rl-1
66* xx[i]: SB mod q[i]
67* assume: char=0
68* assume: q[i]!=0
69* destroys xx
70*/
71poly p_ChineseRemainder(poly *xx, mpz_ptr *x,mpz_ptr *q, int rl, mpz_ptr *C, mpz_t prod2, mpz_t prod,const ring R)
72{
73 poly r,h,hh;
74 int j;
75 poly res_p=NULL;
76 loop
77 {
78 /* search the lead term */
79 r=NULL;
80 for(j=rl-1;j>=0;j--)
81 {
82 h=xx[j];
83 if ((h!=NULL)
84 &&((r==NULL)||(p_LmCmp(r,h,R)==-1)))
85 r=h;
86 }
87 /* nothing found -> return */
88 if (r==NULL) break;
89 /* create the monomial in h */
90 h=p_Head(r,R);
91 /* collect the coeffs in x[..]*/
92 for(j=rl-1;j>=0;j--)
93 {
94 hh=xx[j];
95 if ((hh!=NULL) && (p_LmCmp(h,hh,R)==0))
96 {
97 mpz_set_si(x[j],n_Int(pGetCoeff(hh),R->cf));
98 hh=p_LmFreeAndNext(hh,R);
99 xx[j]=hh;
100 }
101 else
102 mpz_set_si(x[j],0);
103 }
104 mpz_t nn; mpz_init(nn);
105 CRT(nn,x,C,q,rl);
106 if (mpz_cmp(nn,prod2)>0) mpz_sub(nn,nn,prod);
107 number n=n_InitMPZ(nn,R->cf);
108 if (n_IsZero(n,R->cf)) p_Delete(&h,R);
109 else
110 {
111 //Print("new mon:");pWrite(h);
112 p_SetCoeff(h,n,R);
113 pNext(h)=res_p;
114 res_p=h; // building res_p in reverse order!
115 }
116 }
117 res_p=pReverse(res_p);
118 p_Test(res_p, R);
119 return res_p;
120}
121
122ideal id_ChineseRemainder_0(ideal *xx, number *q, int rl, const ring r)
123{
124 int cnt=0;int rw=0; int cl=0;
125 int i,j;
126 // find max. size of xx[.]:
127 for(j=rl-1;j>=0;j--)
128 {
129 i=IDELEMS(xx[j])*xx[j]->nrows;
130 if (i>cnt) cnt=i;
131 if (xx[j]->nrows >rw) rw=xx[j]->nrows; // for lifting matrices
132 if (xx[j]->ncols >cl) cl=xx[j]->ncols; // for lifting matrices
133 }
134 if (rw*cl !=cnt)
135 {
136 WerrorS("format mismatch in CRT");
137 return NULL;
138 }
139 ideal result=idInit(cnt,xx[0]->rank);
140 result->nrows=rw; // for lifting matrices
141 result->ncols=cl; // for lifting matrices
142 mpz_ptr x[rl]; // work space
143 mpz_ptr qq[rl];// moduli
144 mpz_ptr C[rl];// Cache
145 for(j=rl-1;j>=0;j--)
146 {
147 x[j]=(mpz_ptr)omAlloc(sizeof(mpz_t));
148 mpz_init(x[j]);
149 qq[j]=(mpz_ptr)omAlloc(sizeof(mpz_t));
150 n_MPZ(qq[j],q[j],r->cf);
151 C[j]=(mpz_ptr)omAlloc(sizeof(mpz_t));
152 }
153 poly *p=(poly *)omAlloc(rl*sizeof(poly));
154 // x - result v residuals m - primes t-size of vectors
155 // prepare:
156 mpz_t prod,prod2;
157 prepareCRT(C, qq, prod, rl);
158 mpz_init_set(prod2,prod);
159 mpz_tdiv_ui(prod2,2);
160 for(i=cnt-1;i>=0;i--)
161 {
162 for(j=rl-1;j>=0;j--)
163 {
164 if(i>=IDELEMS(xx[j])*xx[j]->nrows) // out of range of this ideal
165 p[j]=NULL;
166 else
167 p[j]=xx[j]->m[i];
168 }
169 result->m[i]=p_ChineseRemainder(p,x,qq,rl,C,prod2,prod,r);
170 for(j=rl-1;j>=0;j--)
171 {
172 if(i<IDELEMS(xx[j])*xx[j]->nrows) xx[j]->m[i]=p[j];
173 }
174 }
175 mpz_clear(prod2);
176 mpz_clear(prod);
177 omFreeSize(p,rl*sizeof(poly));
178 for(j=rl-1;j>=0;j--)
179 {
180 mpz_clear(qq[j]);omFree(qq[j]);
181 mpz_clear(x[j]);omFree(x[j]);
182 }
183 for(j=rl-1;j>0;j--)
184 {
185 mpz_clear(C[j]); omFree(C[j]);
186 }
187 for(i=rl-1;i>=0;i--) id_Delete(&(xx[i]),r);
188 omFreeSize(xx,rl*sizeof(ideal));
189 return result;
190}
All the auxiliary stuff.
int m
Definition: cfEzgcd.cc:128
int i
Definition: cfEzgcd.cc:132
Variable x
Definition: cfModGcd.cc:4082
int p
Definition: cfModGcd.cc:4078
cl
Definition: cfModGcd.cc:4100
int int ncols
Definition: cf_linsys.cc:32
int nrows
Definition: cf_linsys.cc:32
static FORCE_INLINE long n_Int(number &n, const coeffs r)
conversion of n to an int; 0 if not possible in Z/pZ: the representing int lying in (-p/2 ....
Definition: coeffs.h:544
static FORCE_INLINE void n_MPZ(mpz_t result, number &n, const coeffs r)
conversion of n to a GMP integer; 0 if not possible
Definition: coeffs.h:548
static FORCE_INLINE BOOLEAN n_IsZero(number n, const coeffs r)
TRUE iff 'n' represents the zero element.
Definition: coeffs.h:461
static FORCE_INLINE number n_InitMPZ(mpz_t n, const coeffs r)
conversion of a GMP integer to number
Definition: coeffs.h:539
return result
Definition: facAbsBiFact.cc:75
const Variable & v
< [in] a sqrfree bivariate poly
Definition: facBivar.h:39
int j
Definition: facHensel.cc:110
fq_nmod_poly_t prod
Definition: facHensel.cc:100
void WerrorS(const char *s)
Definition: feFopen.cc:24
STATIC_VAR Poly * h
Definition: janet.cc:971
poly p_ChineseRemainder(poly *xx, mpz_ptr *x, mpz_ptr *q, int rl, mpz_ptr *C, mpz_t prod2, mpz_t prod, const ring R)
Definition: kChinese2.cc:71
static void CRT(mpz_t x, mpz_ptr *v, mpz_ptr *C, mpz_ptr *m, int t)
Definition: kChinese2.cc:42
ideal id_ChineseRemainder_0(ideal *xx, number *q, int rl, const ring r)
Definition: kChinese2.cc:122
static void prepareCRT(mpz_ptr C[], mpz_ptr *m, mpz_t prod, int t)
Definition: kChinese2.cc:18
#define pNext(p)
Definition: monomials.h:36
static number & pGetCoeff(poly p)
return an alias to the leading coefficient of p assumes that p != NULL NOTE: not copy
Definition: monomials.h:44
#define omFreeSize(addr, size)
Definition: omAllocDecl.h:260
#define omAlloc(size)
Definition: omAllocDecl.h:210
#define omFree(addr)
Definition: omAllocDecl.h:261
#define NULL
Definition: omList.c:12
static number p_SetCoeff(poly p, number n, ring r)
Definition: p_polys.h:410
static poly pReverse(poly p)
Definition: p_polys.h:333
static poly p_Head(const poly p, const ring r)
copy the (leading) term of p
Definition: p_polys.h:858
static int p_LmCmp(poly p, poly q, const ring r)
Definition: p_polys.h:1578
static void p_Delete(poly *p, const ring r)
Definition: p_polys.h:899
static poly p_LmFreeAndNext(poly p, ring)
Definition: p_polys.h:709
#define p_Test(p, r)
Definition: p_polys.h:159
ideal idInit(int idsize, int rank)
initialise an ideal / module
Definition: simpleideals.cc:35
void id_Delete(ideal *h, ring r)
deletes an ideal/module/matrix
#define IDELEMS(i)
Definition: simpleideals.h:23
#define R
Definition: sirandom.c:27
#define loop
Definition: structs.h:75