bn_modfs.c 3.5 KB
Newer Older
B
Bodo Möller 已提交
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
/*
 *
 *	bn_modfs.c
 *
 *	Some Modular Arithmetic Functions.
 *
 *	Copyright (C) Lenka Fibikova 2000
 *
 *
 */


#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#include "bn_modfs.h"

#define MAX_ROUNDS	10


int BN_legendre(BIGNUM *a, BIGNUM *p, BN_CTX *ctx) 
{
	BIGNUM *x, *y, *y2;
	BN_ULONG m;
	int L;

	assert(a != NULL && p != NULL && ctx != NULL);

	x = ctx->bn[ctx->tos]; 
	y = ctx->bn[ctx->tos + 1]; 
	y2 = ctx->bn[ctx->tos + 2]; 

	ctx->tos += 3;

B
Bodo Möller 已提交
36
	if (!BN_nnmod(x, a, p, ctx)) goto err;
B
Bodo Möller 已提交
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
	if (BN_is_zero(x)) 
	{

		ctx->tos -= 3;
		return 0;
	}

	if (BN_copy(y, p) == NULL) goto err;
	L = 1;

	while (1)
	{
		if (!BN_rshift1(y2, y)) goto err;
		if (BN_cmp(x, y2) > 0)
		{
			if (!BN_sub(x, y, x)) goto err;
			if (BN_mod_word(y, 4) == 3)
				L = -L;			
		}
		while (BN_mod_word(x, 4) == 0)
			BN_div_word(x, 4);
		if (BN_mod_word(x, 2) == 0)
		{
			BN_div_word(x, 2);
			m = BN_mod_word(y, 8);
			if (m == 3 || m == 5) L = -L;			
		}
		if (BN_is_one(x)) 
		{
			ctx->tos -= 3;
			return L;
		}
		
		if (BN_mod_word(x, 4) == 3 && BN_mod_word(y, 4) == 3) L = -L;
		if (!BN_swap(x, y)) goto err;

B
Bodo Möller 已提交
73
		if (!BN_nnmod(x, x, y, ctx)) goto err;
B
Bodo Möller 已提交
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 192

	}


err:
	ctx->tos -= 3;
	return -2;

}

int BN_mod_sqrt(BIGNUM *x, BIGNUM *a, BIGNUM *p, BN_CTX *ctx) 
/* x^2 = a (mod p) */
{
	int ret;
	BIGNUM *n0, *n1, *r, *b, *m;
	int max;

	assert(x != NULL && a != NULL && p != NULL && ctx != NULL);
	assert(BN_cmp(a, p) < 0);

	ret = BN_legendre(a, p, ctx);
	if (ret < 0 || ret > 1) return 0;
	if (ret == 0)
	{
		if (!BN_zero(x)) return 0;
		return 1;
	}

	n0 = ctx->bn[ctx->tos]; 
	n1 = ctx->bn[ctx->tos + 1]; 
	ctx->tos += 2;

	if ((r = BN_new()) == NULL) goto err;
	if ((b = BN_new()) == NULL) goto err;
	if ((m = BN_new()) == NULL) goto err;


	if (!BN_zero(n0)) goto err;
	if (!BN_zero(n1)) goto err;
	if (!BN_zero(r)) goto err;
	if (!BN_zero(b)) goto err;
	if (!BN_zero(m)) goto err;

	max = 0;

	do{
		if (max++ > MAX_ROUNDS) goto err; /* if p is not prime could never stop*/
		if (!BN_add_word(m, 1)) goto err;
		ret = BN_legendre(m, p, ctx);
		if (ret < -1 || ret > 1) goto err;

	}while(ret != -1);

	if (BN_copy(n1, p) == NULL) goto err;
	if (!BN_sub_word(n1, 1)) goto err;

	while (!BN_is_odd(n1))
	{
		if (!BN_add_word(r, 1)) goto err;
		if (!BN_rshift1(n1, n1)) goto err;
	}

	if (!BN_mod_exp_simple(n0, m, n1, p, ctx)) goto err;

	if (!BN_sub_word(n1, 1)) goto err;
	if (!BN_rshift1(n1, n1)) goto err;
	if (!BN_mod_exp_simple(x, a, n1, p, ctx)) goto err;

	if (!BN_mod_sqr(b, x, p, ctx)) goto err;
	if (!BN_mod_mul(b, b, a, p, ctx)) goto err;

	if (!BN_mod_mul(x, x, a, p, ctx)) goto err;

	while (!BN_is_one(b))
	{
		
		if (!BN_one(m)) goto err;
		if (!BN_mod_sqr(n1, b, p, ctx)) goto err;
		while(!BN_is_one(n1))
		{
			if (!BN_mod_mul(n1, n1, n1, p, ctx)) goto err;
			if (!BN_add_word(m, 1)) goto err;
		}

		if (!BN_sub(r, r, m)) goto err;
		if (!BN_sub_word(r, 1)) goto err;
		if (r->neg) goto err;

		if (BN_copy(n1, n0) == NULL) goto err;
		while(!BN_is_zero(r))
		{
			if (!BN_mod_mul(n1, n1, n1, p, ctx)) goto err;
			if (!BN_sub_word(r, 1)) goto err;
		}

		if (!BN_mod_mul(n0, n1, n1, p, ctx)) goto err;
		if (BN_copy(r, m) == NULL) goto err;
		if (!BN_mod_mul(x, x, n1, p, ctx)) goto err;
		if (!BN_mod_mul(b, b, n0, p, ctx)) goto err;
	}


#ifdef TEST
	BN_mod_sqr(n0, x, p, ctx);
	if (BN_cmp(n0, a)) goto err;
#endif

	if (r != NULL) BN_clear_free(r);
	if (b != NULL) BN_clear_free(b);
	if (m != NULL) BN_clear_free(m);
	ctx->tos -= 2;
	return 1;
err:
	if (r != NULL) BN_clear_free(r);
	if (b != NULL) BN_clear_free(b);
	if (m != NULL) BN_clear_free(m);
	ctx->tos -= 2;
	return 0;
}