1109998Smarkm/* crypto/bn/bn_kron.c */ 2109998Smarkm/* ==================================================================== 3109998Smarkm * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved. 4109998Smarkm * 5109998Smarkm * Redistribution and use in source and binary forms, with or without 6109998Smarkm * modification, are permitted provided that the following conditions 7109998Smarkm * are met: 8109998Smarkm * 9109998Smarkm * 1. Redistributions of source code must retain the above copyright 10296341Sdelphij * notice, this list of conditions and the following disclaimer. 11109998Smarkm * 12109998Smarkm * 2. Redistributions in binary form must reproduce the above copyright 13109998Smarkm * notice, this list of conditions and the following disclaimer in 14109998Smarkm * the documentation and/or other materials provided with the 15109998Smarkm * distribution. 16109998Smarkm * 17109998Smarkm * 3. All advertising materials mentioning features or use of this 18109998Smarkm * software must display the following acknowledgment: 19109998Smarkm * "This product includes software developed by the OpenSSL Project 20109998Smarkm * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 21109998Smarkm * 22109998Smarkm * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 23109998Smarkm * endorse or promote products derived from this software without 24109998Smarkm * prior written permission. For written permission, please contact 25109998Smarkm * openssl-core@openssl.org. 26109998Smarkm * 27109998Smarkm * 5. Products derived from this software may not be called "OpenSSL" 28109998Smarkm * nor may "OpenSSL" appear in their names without prior written 29109998Smarkm * permission of the OpenSSL Project. 30109998Smarkm * 31109998Smarkm * 6. Redistributions of any form whatsoever must retain the following 32109998Smarkm * acknowledgment: 33109998Smarkm * "This product includes software developed by the OpenSSL Project 34109998Smarkm * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 35109998Smarkm * 36109998Smarkm * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 37109998Smarkm * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 38109998Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 39109998Smarkm * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 40109998Smarkm * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 41109998Smarkm * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 42109998Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 43109998Smarkm * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 44109998Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 45109998Smarkm * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 46109998Smarkm * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 47109998Smarkm * OF THE POSSIBILITY OF SUCH DAMAGE. 48109998Smarkm * ==================================================================== 49109998Smarkm * 50109998Smarkm * This product includes cryptographic software written by Eric Young 51109998Smarkm * (eay@cryptsoft.com). This product includes software written by Tim 52109998Smarkm * Hudson (tjh@cryptsoft.com). 53109998Smarkm * 54109998Smarkm */ 55109998Smarkm 56160814Ssimon#include "cryptlib.h" 57109998Smarkm#include "bn_lcl.h" 58109998Smarkm 59109998Smarkm/* least significant word */ 60109998Smarkm#define BN_lsw(n) (((n)->top == 0) ? (BN_ULONG) 0 : (n)->d[0]) 61109998Smarkm 62109998Smarkm/* Returns -2 for errors because both -1 and 0 are valid results. */ 63109998Smarkmint BN_kronecker(const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) 64296341Sdelphij{ 65296341Sdelphij int i; 66296341Sdelphij int ret = -2; /* avoid 'uninitialized' warning */ 67296341Sdelphij int err = 0; 68296341Sdelphij BIGNUM *A, *B, *tmp; 69296341Sdelphij /*- 70296341Sdelphij * In 'tab', only odd-indexed entries are relevant: 71296341Sdelphij * For any odd BIGNUM n, 72296341Sdelphij * tab[BN_lsw(n) & 7] 73296341Sdelphij * is $(-1)^{(n^2-1)/8}$ (using TeX notation). 74296341Sdelphij * Note that the sign of n does not matter. 75296341Sdelphij */ 76296341Sdelphij static const int tab[8] = { 0, 1, 0, -1, 0, -1, 0, 1 }; 77109998Smarkm 78296341Sdelphij bn_check_top(a); 79296341Sdelphij bn_check_top(b); 80160814Ssimon 81296341Sdelphij BN_CTX_start(ctx); 82296341Sdelphij A = BN_CTX_get(ctx); 83296341Sdelphij B = BN_CTX_get(ctx); 84296341Sdelphij if (B == NULL) 85296341Sdelphij goto end; 86109998Smarkm 87296341Sdelphij err = !BN_copy(A, a); 88296341Sdelphij if (err) 89296341Sdelphij goto end; 90296341Sdelphij err = !BN_copy(B, b); 91296341Sdelphij if (err) 92296341Sdelphij goto end; 93109998Smarkm 94296341Sdelphij /* 95296341Sdelphij * Kronecker symbol, imlemented according to Henri Cohen, 96296341Sdelphij * "A Course in Computational Algebraic Number Theory" 97296341Sdelphij * (algorithm 1.4.10). 98296341Sdelphij */ 99109998Smarkm 100296341Sdelphij /* Cohen's step 1: */ 101109998Smarkm 102296341Sdelphij if (BN_is_zero(B)) { 103296341Sdelphij ret = BN_abs_is_word(A, 1); 104296341Sdelphij goto end; 105296341Sdelphij } 106109998Smarkm 107296341Sdelphij /* Cohen's step 2: */ 108109998Smarkm 109296341Sdelphij if (!BN_is_odd(A) && !BN_is_odd(B)) { 110296341Sdelphij ret = 0; 111296341Sdelphij goto end; 112296341Sdelphij } 113109998Smarkm 114296341Sdelphij /* now B is non-zero */ 115296341Sdelphij i = 0; 116296341Sdelphij while (!BN_is_bit_set(B, i)) 117296341Sdelphij i++; 118296341Sdelphij err = !BN_rshift(B, B, i); 119296341Sdelphij if (err) 120296341Sdelphij goto end; 121296341Sdelphij if (i & 1) { 122296341Sdelphij /* i is odd */ 123296341Sdelphij /* (thus B was even, thus A must be odd!) */ 124109998Smarkm 125296341Sdelphij /* set 'ret' to $(-1)^{(A^2-1)/8}$ */ 126296341Sdelphij ret = tab[BN_lsw(A) & 7]; 127296341Sdelphij } else { 128296341Sdelphij /* i is even */ 129296341Sdelphij ret = 1; 130296341Sdelphij } 131109998Smarkm 132296341Sdelphij if (B->neg) { 133296341Sdelphij B->neg = 0; 134296341Sdelphij if (A->neg) 135296341Sdelphij ret = -ret; 136296341Sdelphij } 137109998Smarkm 138296341Sdelphij /* 139296341Sdelphij * now B is positive and odd, so what remains to be done is to compute 140296341Sdelphij * the Jacobi symbol (A/B) and multiply it by 'ret' 141296341Sdelphij */ 142109998Smarkm 143296341Sdelphij while (1) { 144296341Sdelphij /* Cohen's step 3: */ 145296341Sdelphij 146296341Sdelphij /* B is positive and odd */ 147296341Sdelphij 148296341Sdelphij if (BN_is_zero(A)) { 149296341Sdelphij ret = BN_is_one(B) ? ret : 0; 150296341Sdelphij goto end; 151296341Sdelphij } 152296341Sdelphij 153296341Sdelphij /* now A is non-zero */ 154296341Sdelphij i = 0; 155296341Sdelphij while (!BN_is_bit_set(A, i)) 156296341Sdelphij i++; 157296341Sdelphij err = !BN_rshift(A, A, i); 158296341Sdelphij if (err) 159296341Sdelphij goto end; 160296341Sdelphij if (i & 1) { 161296341Sdelphij /* i is odd */ 162296341Sdelphij /* multiply 'ret' by $(-1)^{(B^2-1)/8}$ */ 163296341Sdelphij ret = ret * tab[BN_lsw(B) & 7]; 164296341Sdelphij } 165296341Sdelphij 166296341Sdelphij /* Cohen's step 4: */ 167296341Sdelphij /* multiply 'ret' by $(-1)^{(A-1)(B-1)/4}$ */ 168296341Sdelphij if ((A->neg ? ~BN_lsw(A) : BN_lsw(A)) & BN_lsw(B) & 2) 169296341Sdelphij ret = -ret; 170296341Sdelphij 171296341Sdelphij /* (A, B) := (B mod |A|, |A|) */ 172296341Sdelphij err = !BN_nnmod(B, B, A, ctx); 173296341Sdelphij if (err) 174296341Sdelphij goto end; 175296341Sdelphij tmp = A; 176296341Sdelphij A = B; 177296341Sdelphij B = tmp; 178296341Sdelphij tmp->neg = 0; 179296341Sdelphij } 180296341Sdelphij end: 181296341Sdelphij BN_CTX_end(ctx); 182296341Sdelphij if (err) 183296341Sdelphij return -2; 184296341Sdelphij else 185296341Sdelphij return ret; 186296341Sdelphij} 187