⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jacobi.c

📁 包含哈希,对称以及非对称的经典算法 包含经典事例
💻 C
字号:
/* * Compute the Jacobi symbol (small prime case only). * * Copyright (c) 1995  Colin Plumb.  All rights reserved. * For licensing and other legal details, see the file legal.c. */#include "bn.h"#include "jacobi.h"/* * For a small (usually prime, but not necessarily) prime p, * compute Jacobi(p,bn), which is -1, 0 or +1, using the following rules: * Jacobi(x, y) = Jacobi(x mod y, y) * Jacobi(0, y) = 0 * Jacobi(1, y) = 1 * Jacobi(2, y) = 0 if y is even, +1 if y is +/-1 mod 8, -1 if y = +/-3 mod 8 * Jacobi(x1*x2, y) = Jacobi(x1, y) * Jacobi(x2, y) (used with x1 = 2 & x1 = 4) * If x and y are both odd, then * Jacobi(x, y) = Jacobi(y, x) * (-1 if x = y = 3 mod 4, +1 otherwise) */intbnJacobiQ(unsigned p, struct BigNum const *bn){	int j = 1;	unsigned u = bnLSWord(bn);	if (!(u & 1))		return 0;	/* Don't *do* that */	/* First, get rid of factors of 2 in p */	while ((p & 3) == 0)		p >>= 2;	if ((p & 1) == 0) {		p >>= 1;		if ((u ^ u>>1) & 2)			j = -j;		/* 3 (011) or 5 (101) mod 8 */	}	if (p == 1)		return j;	/* Then, apply quadratic reciprocity */	if (p & u & 2)	/* p = u = 3 (mod 4? */		j = -j;	/* And reduce u mod p */	u = bnModQ(bn, p);	/* Now compute Jacobi(u,p), u < p */	while (u) {		while ((u & 3) == 0)			u >>= 2;		if ((u & 1) == 0) {			u >>= 1;			if ((p ^ p>>1) & 2)				j = -j;	/* 3 (011) or 5 (101) mod 8 */		}		if (u == 1)			return j;		/* Now both u and p are odd, so use quadratic reciprocity */		if (u < p) {			unsigned t = u; u = p; p = t;			if (u & p & 2)	/* u = p = 3 (mod 4? */				j = -j;		}		/* Now u >= p, so it can be reduced */		u %= p;	}	return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -