Пример #1
0
Montgomery_Params::Montgomery_Params(const BigInt& p)
   {

   if(p.is_negative() || p.is_even())
      throw Invalid_Argument("Montgomery_Params invalid modulus");

   m_p = p;
   m_p_words = m_p.sig_words();
   m_p_dash = monty_inverse(m_p.word_at(0));

   const BigInt r = BigInt::power_of_2(m_p_words * BOTAN_MP_WORD_BITS);

   Modular_Reducer mod_p(p);

   m_r1 = mod_p.reduce(r);
   m_r2 = mod_p.square(m_r1);
   m_r3 = mod_p.multiply(m_r1, m_r2);
   }
Пример #2
0
static void create_systematic_generator_matrices(MINDIST *MD)
//create systematic generator matrices
//$(S[1]=(I,*),...,S[z]=(*,I,*),...,S[M]=(*,I))$
//with identity-matrix $I$ beginning at $P+1 = k*(z-1)+1$
//(k = \# lines, m = \# systematic generator matrices),
//by elementary row-transformations and permutations in
//generator matrix G
//allocates S[u] for $1 \le u \le M$,
//allocates S[u][i] for $1 \le u \le M, 1 \le i \le k$,
//allocates Size
{
	int n = MD->n;
	int k = MD->k;
	int q = MD->q;
	
int i,I = 0,j,J,l;
int P, h, h1, h2, h3, pivot, pivot_inv;
int K;
int M;

	/* allocate memory for systematic generator matrix S[1] */
	MD->S[1] = (int **)calloc(k+2,sizeof(int *));
		for (i=1;i<=k;i++)
			MD->S[1][i] = (int *)calloc(n+2,sizeof(int));

	/* S[1] :=  G */
	for (i=1;i<=k;i++) 
		for (j=1;j<=n;j++)
				MD->S[1][i][j] = MD->G[i][j];
	/* allocate memory for size of information subsets of S[1] */
	MD->Size = (int *)calloc(n+1,sizeof(int ));

	M = 1;
	P = 0;
	K = k;

	while (1) {
		if (MD->f_vvv) {
			printf("loop with M = %d, P = %d K = %d\n",M, P, K);
			}

		/*        create identity matrix, columns P+1,...,P+k          */

		for (i=1;i<=K;i++) {
			if (MD->f_vvv) {
				printf("i = %d ", i);
				printf(" (M = %d, P = %d K = %d)\n",M, P, K);
				fflush(stdout);
				}
			/* search for pivot: 
		    	        if the entry is 0 at (i,P+i),
				    first check the entries below
				    (-> row-permutation necessary)
				    then check the columns behind  
				    (-> column-permutation necessary)    */
			/* printf("i=%d, P=%d, S[M][i][P+i]=%d\n",i,P,S[M][i][P+i]);  */
			for (J=P+i;J<=n;J++) {
				for (I = i; I <= K; I++) {
					if (MD->S[M][I][J] != MD->idx_zero)
						break;
					}
				if (I <= K) /* pivot found ? */
					break;
			} // next J
			if (MD->f_vvv) {
				printf("I=%d, J=%d\n",I,J); fflush(stdout);
				}
			
			/*   if pivot found but end of columns reached: */
			if ((I <= K) && (J == n)) {
				if (MD->f_vvv) {printf("end reached, I = %d\n",I);}
				if (P+K >= n)
					K = n - P;
			}
			if (MD->f_vvv) {
				printf("pivot in I=%d J=%d\n", I, J);
				fflush(stdout);
				}

			/*   if there doesn't exist a pivot: */
			/*   P(s,t)=0 for s>=i and t>=P+i */
			if (I > K && J > n) {
				K = i-1; /* */
				if (MD->f_vvv) {
					printf("no pivot\n");
					fflush(stdout);
					}
				break;
			}

			/*   if necessary: row-permutation i<->I         */
			if (I != i) {
				if (MD->f_vvv) {
					printf("\nswapping rows: %d<->%d\n",i,I);
					fflush(stdout);
					}
				for (j=1;j<=n;j++) {
					h = MD->S[M][i][j];
					MD->S[M][i][j] = MD->S[M][I][j];
					MD->S[M][I][j] = h;
				}
			}

			/*    if necessary: column-permutation P+i<->J   */
			if (J != P+i) {
				if (MD->f_vvv) {
					printf("\nswapping columns: %d<->%d\n",P+i,J);
					fflush(stdout);
					}
				for (j=1;j<=k;j++) {
					h = MD->S[M][j][i+P];
					MD->S[M][j][i+P] = MD->S[M][j][J];
					MD->S[M][j][J] = h;
				}
			}
		
			pivot = MD->S[M][i][P + i];
			if (pivot == MD->idx_zero) {
				printf("pivot is 0, exiting!\n");
				exit(1);
				}
			pivot_inv = MD->ff_inv[pivot];
			if (MD->f_vvv) {
				printf("pivot = %d, pivot_inv = %d\n", pivot, pivot_inv);
				fflush(stdout);
				}
			/* replace pivot by 1 [multiply pivot-row by inv(pivot)] */
			if (MD->S[M][i][P+i] != MD->idx_one) {
				for (j=1;j<=n;j++) {
					MD->S[M][i][j] = MD->ff_mult[MD->S[M][i][j] * q +  pivot_inv];
					}
				}
			if (MD->f_vvv) {
				printf("pivot row normalized\n");
				fflush(stdout);
				}

			/* replace all elements of pivot-column, except pivot
				element, by 0 by elementary row-trans. */	 
			for (l=1;l<=k;l++) {
				if (l != i) {
					if ((h = MD->S[M][l][i+P]) != MD->idx_zero)
					for (j=1;j<=n;j++) {
						h1 = MD->ff_mult[MD->S[M][i][j] * q + h];
						h2 = MD->ff_mult[h1 * q + MD->idx_mone];
						h3 = MD->ff_add[MD->S[M][l][j] * q + h2];
						MD->S[M][l][j] = h3;


#if 0
						MD->S[M][l][j] = 
						mod_p(MD->S[M][l][j] - MD->S[M][i][j] * h);
#endif
					} // next j
				} /* if */
			} /* l */
			if (MD->f_vvv) {
				printf("pivot col normalized\n");
				fflush(stdout);
				}

		} /* i */

		if (MD->f_v) {
			printf("\nsystematic generator matrix s[%d]:\n",M);
			print_matrix(MD, MD->S[M]);
			//printf("K = %d\n",K);
			}
		MD->Size[M] = K;

		if (K == 0) {
			if (MD->f_v) {
				printf("K = 0, the generator matrix has %d zero columns\n", n - P);
				}
			}
		if (P+K >= n || K == 0) {
			if (K == 0)
				MD->ZC = n - P; /* number of zero columns in G */
			break;
			}
		else {
			P = P + K;
			}

		M++;
		/*        find first column P+1      */ 
		MD->S[M] = (int **)calloc(k+2,sizeof(int *));
			for (i=1;i<=k;i++)
				MD->S[M][i] = (int *)calloc(n+2,sizeof(int));
		/*   S[M] :=  S[M-1] */
		for (i=1;i<=k;i++) 
			for (j=1;j<=n;j++)
				MD->S[M][i][j] = MD->S[M-1][i][j];
	} /* infinite loop */
	if (MD->f_v) {
		//printf("M = %d\n", M);
		//printf("ZC = %d\n", MD->ZC);
		printf("size of information subsets:\n");
		for (i = 1; i <= M; i++) {
			printf("%d ", MD->Size[i]);
			}
		printf("\n");
		}
	MD->M = M;

}