xref: /onnv-gate/usr/src/common/openssl/crypto/bn/bn_kron.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* crypto/bn/bn_kron.c */
20Sstevel@tonic-gate /* ====================================================================
30Sstevel@tonic-gate  * Copyright (c) 1998-2000 The OpenSSL Project.  All rights reserved.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
60Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
70Sstevel@tonic-gate  * are met:
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
100Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
110Sstevel@tonic-gate  *
120Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
130Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
140Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
150Sstevel@tonic-gate  *    distribution.
160Sstevel@tonic-gate  *
170Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
180Sstevel@tonic-gate  *    software must display the following acknowledgment:
190Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
200Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
210Sstevel@tonic-gate  *
220Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
230Sstevel@tonic-gate  *    endorse or promote products derived from this software without
240Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
250Sstevel@tonic-gate  *    openssl-core@openssl.org.
260Sstevel@tonic-gate  *
270Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
280Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
290Sstevel@tonic-gate  *    permission of the OpenSSL Project.
300Sstevel@tonic-gate  *
310Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
320Sstevel@tonic-gate  *    acknowledgment:
330Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
340Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
350Sstevel@tonic-gate  *
360Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
370Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
380Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
390Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
400Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
410Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
420Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
430Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
440Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
450Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
460Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
470Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
480Sstevel@tonic-gate  * ====================================================================
490Sstevel@tonic-gate  *
500Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
510Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
520Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
530Sstevel@tonic-gate  *
540Sstevel@tonic-gate  */
550Sstevel@tonic-gate 
56*2139Sjp161948 #include "cryptlib.h"
570Sstevel@tonic-gate #include "bn_lcl.h"
580Sstevel@tonic-gate 
590Sstevel@tonic-gate /* least significant word */
600Sstevel@tonic-gate #define BN_lsw(n) (((n)->top == 0) ? (BN_ULONG) 0 : (n)->d[0])
610Sstevel@tonic-gate 
620Sstevel@tonic-gate /* Returns -2 for errors because both -1 and 0 are valid results. */
BN_kronecker(const BIGNUM * a,const BIGNUM * b,BN_CTX * ctx)630Sstevel@tonic-gate int BN_kronecker(const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
640Sstevel@tonic-gate 	{
650Sstevel@tonic-gate 	int i;
660Sstevel@tonic-gate 	int ret = -2; /* avoid 'uninitialized' warning */
670Sstevel@tonic-gate 	int err = 0;
680Sstevel@tonic-gate 	BIGNUM *A, *B, *tmp;
690Sstevel@tonic-gate 	/* In 'tab', only odd-indexed entries are relevant:
700Sstevel@tonic-gate 	 * For any odd BIGNUM n,
710Sstevel@tonic-gate 	 *     tab[BN_lsw(n) & 7]
720Sstevel@tonic-gate 	 * is $(-1)^{(n^2-1)/8}$ (using TeX notation).
730Sstevel@tonic-gate 	 * Note that the sign of n does not matter.
740Sstevel@tonic-gate 	 */
750Sstevel@tonic-gate 	static const int tab[8] = {0, 1, 0, -1, 0, -1, 0, 1};
760Sstevel@tonic-gate 
77*2139Sjp161948 	bn_check_top(a);
78*2139Sjp161948 	bn_check_top(b);
79*2139Sjp161948 
800Sstevel@tonic-gate 	BN_CTX_start(ctx);
810Sstevel@tonic-gate 	A = BN_CTX_get(ctx);
820Sstevel@tonic-gate 	B = BN_CTX_get(ctx);
830Sstevel@tonic-gate 	if (B == NULL) goto end;
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	err = !BN_copy(A, a);
860Sstevel@tonic-gate 	if (err) goto end;
870Sstevel@tonic-gate 	err = !BN_copy(B, b);
880Sstevel@tonic-gate 	if (err) goto end;
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 	/*
910Sstevel@tonic-gate 	 * Kronecker symbol, imlemented according to Henri Cohen,
920Sstevel@tonic-gate 	 * "A Course in Computational Algebraic Number Theory"
930Sstevel@tonic-gate 	 * (algorithm 1.4.10).
940Sstevel@tonic-gate 	 */
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	/* Cohen's step 1: */
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	if (BN_is_zero(B))
990Sstevel@tonic-gate 		{
1000Sstevel@tonic-gate 		ret = BN_abs_is_word(A, 1);
1010Sstevel@tonic-gate 		goto end;
1020Sstevel@tonic-gate  		}
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	/* Cohen's step 2: */
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	if (!BN_is_odd(A) && !BN_is_odd(B))
1070Sstevel@tonic-gate 		{
1080Sstevel@tonic-gate 		ret = 0;
1090Sstevel@tonic-gate 		goto end;
1100Sstevel@tonic-gate 		}
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	/* now  B  is non-zero */
1130Sstevel@tonic-gate 	i = 0;
1140Sstevel@tonic-gate 	while (!BN_is_bit_set(B, i))
1150Sstevel@tonic-gate 		i++;
1160Sstevel@tonic-gate 	err = !BN_rshift(B, B, i);
1170Sstevel@tonic-gate 	if (err) goto end;
1180Sstevel@tonic-gate 	if (i & 1)
1190Sstevel@tonic-gate 		{
1200Sstevel@tonic-gate 		/* i is odd */
1210Sstevel@tonic-gate 		/* (thus  B  was even, thus  A  must be odd!)  */
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 		/* set 'ret' to $(-1)^{(A^2-1)/8}$ */
1240Sstevel@tonic-gate 		ret = tab[BN_lsw(A) & 7];
1250Sstevel@tonic-gate 		}
1260Sstevel@tonic-gate 	else
1270Sstevel@tonic-gate 		{
1280Sstevel@tonic-gate 		/* i is even */
1290Sstevel@tonic-gate 		ret = 1;
1300Sstevel@tonic-gate 		}
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	if (B->neg)
1330Sstevel@tonic-gate 		{
1340Sstevel@tonic-gate 		B->neg = 0;
1350Sstevel@tonic-gate 		if (A->neg)
1360Sstevel@tonic-gate 			ret = -ret;
1370Sstevel@tonic-gate 		}
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	/* now  B  is positive and odd, so what remains to be done is
1400Sstevel@tonic-gate 	 * to compute the Jacobi symbol  (A/B)  and multiply it by 'ret' */
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	while (1)
1430Sstevel@tonic-gate 		{
1440Sstevel@tonic-gate 		/* Cohen's step 3: */
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 		/*  B  is positive and odd */
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 		if (BN_is_zero(A))
1490Sstevel@tonic-gate 			{
1500Sstevel@tonic-gate 			ret = BN_is_one(B) ? ret : 0;
1510Sstevel@tonic-gate 			goto end;
1520Sstevel@tonic-gate 			}
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 		/* now  A  is non-zero */
1550Sstevel@tonic-gate 		i = 0;
1560Sstevel@tonic-gate 		while (!BN_is_bit_set(A, i))
1570Sstevel@tonic-gate 			i++;
1580Sstevel@tonic-gate 		err = !BN_rshift(A, A, i);
1590Sstevel@tonic-gate 		if (err) goto end;
1600Sstevel@tonic-gate 		if (i & 1)
1610Sstevel@tonic-gate 			{
1620Sstevel@tonic-gate 			/* i is odd */
1630Sstevel@tonic-gate 			/* multiply 'ret' by  $(-1)^{(B^2-1)/8}$ */
1640Sstevel@tonic-gate 			ret = ret * tab[BN_lsw(B) & 7];
1650Sstevel@tonic-gate 			}
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 		/* Cohen's step 4: */
1680Sstevel@tonic-gate 		/* multiply 'ret' by  $(-1)^{(A-1)(B-1)/4}$ */
1690Sstevel@tonic-gate 		if ((A->neg ? ~BN_lsw(A) : BN_lsw(A)) & BN_lsw(B) & 2)
1700Sstevel@tonic-gate 			ret = -ret;
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 		/* (A, B) := (B mod |A|, |A|) */
1730Sstevel@tonic-gate 		err = !BN_nnmod(B, B, A, ctx);
1740Sstevel@tonic-gate 		if (err) goto end;
1750Sstevel@tonic-gate 		tmp = A; A = B; B = tmp;
1760Sstevel@tonic-gate 		tmp->neg = 0;
1770Sstevel@tonic-gate 		}
178*2139Sjp161948 end:
1790Sstevel@tonic-gate 	BN_CTX_end(ctx);
1800Sstevel@tonic-gate 	if (err)
1810Sstevel@tonic-gate 		return -2;
1820Sstevel@tonic-gate 	else
1830Sstevel@tonic-gate 		return ret;
1840Sstevel@tonic-gate 	}
185