提交 946cd9a5 编写于 作者: B Bodo Möller

Change submitted files so that they compile (in particular,

use BN_CTX_start/get/end instead of accessing ctx->tos).

Change indentation to "EAY" style.
上级 0ac87024
......@@ -20,70 +20,69 @@
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;
BN_CTX_start(ctx);
x = BN_CTX_get(ctx);
y = BN_CTX_get(ctx);
y2 = BN_CTX_get(ctx);
if (y2 == NULL) goto err;
if (!BN_nnmod(x, a, p, ctx)) goto err;
if (BN_is_zero(x))
{
ctx->tos -= 3;
{
BN_CTX_end(ctx);
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;
{
BN_CTX_end(ctx);
return L;
}
}
if (BN_mod_word(x, 4) == 3 && BN_mod_word(y, 4) == 3) L = -L;
if (!BN_swap(x, y)) goto err;
BN_swap(x, y);
if (!BN_nnmod(x, x, y, ctx)) goto err;
}
}
err:
ctx->tos -= 3;
BN_CTX_end(ctx);
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;
......@@ -94,14 +93,15 @@ int BN_mod_sqrt(BIGNUM *x, BIGNUM *a, BIGNUM *p, BN_CTX *ctx)
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;
BN_CTX_start(ctx);
n0 = BN_CTX_get(ctx);
n1 = BN_CTX_get(ctx);
if (n1 == NULL) goto err;
if ((r = BN_new()) == NULL) goto err;
if ((b = BN_new()) == NULL) goto err;
......@@ -116,22 +116,23 @@ int BN_mod_sqrt(BIGNUM *x, BIGNUM *a, BIGNUM *p, BN_CTX *ctx)
max = 0;
do{
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);
}
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;
......@@ -145,15 +146,14 @@ int BN_mod_sqrt(BIGNUM *x, BIGNUM *a, BIGNUM *p, BN_CTX *ctx)
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;
......@@ -161,16 +161,16 @@ int BN_mod_sqrt(BIGNUM *x, BIGNUM *a, BIGNUM *p, BN_CTX *ctx)
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
......@@ -181,12 +181,12 @@ int BN_mod_sqrt(BIGNUM *x, BIGNUM *a, BIGNUM *p, BN_CTX *ctx)
if (r != NULL) BN_clear_free(r);
if (b != NULL) BN_clear_free(b);
if (m != NULL) BN_clear_free(m);
ctx->tos -= 2;
BN_CTX_end(ctx);
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;
BN_CTX_end(ctx);
return 0;
}
}
......@@ -15,7 +15,7 @@
#include <openssl/bn.h>
#include "bn_mont2.h"
#include "../bn/bn_mont2.h" /* XXX */
typedef struct bn_ec_struct /* E: y^2 = x^3 + Ax + B (mod p) */
{
......
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册