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 10280304Sjkim * 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) 64280304Sjkim{ 65280304Sjkim int i; 66280304Sjkim int ret = -2; /* avoid 'uninitialized' warning */ 67280304Sjkim int err = 0; 68280304Sjkim BIGNUM *A, *B, *tmp; 69280304Sjkim /*- 70280304Sjkim * In 'tab', only odd-indexed entries are relevant: 71280304Sjkim * For any odd BIGNUM n, 72280304Sjkim * tab[BN_lsw(n) & 7] 73280304Sjkim * is $(-1)^{(n^2-1)/8}$ (using TeX notation). 74280304Sjkim * Note that the sign of n does not matter. 75280304Sjkim */ 76280304Sjkim static const int tab[8] = { 0, 1, 0, -1, 0, -1, 0, 1 }; 77109998Smarkm 78280304Sjkim bn_check_top(a); 79280304Sjkim bn_check_top(b); 80160814Ssimon 81280304Sjkim BN_CTX_start(ctx); 82280304Sjkim A = BN_CTX_get(ctx); 83280304Sjkim B = BN_CTX_get(ctx); 84280304Sjkim if (B == NULL) 85280304Sjkim goto end; 86109998Smarkm 87280304Sjkim err = !BN_copy(A, a); 88280304Sjkim if (err) 89280304Sjkim goto end; 90280304Sjkim err = !BN_copy(B, b); 91280304Sjkim if (err) 92280304Sjkim goto end; 93109998Smarkm 94280304Sjkim /* 95280304Sjkim * Kronecker symbol, imlemented according to Henri Cohen, 96280304Sjkim * "A Course in Computational Algebraic Number Theory" 97280304Sjkim * (algorithm 1.4.10). 98280304Sjkim */ 99109998Smarkm 100280304Sjkim /* Cohen's step 1: */ 101109998Smarkm 102280304Sjkim if (BN_is_zero(B)) { 103280304Sjkim ret = BN_abs_is_word(A, 1); 104280304Sjkim goto end; 105280304Sjkim } 106109998Smarkm 107280304Sjkim /* Cohen's step 2: */ 108109998Smarkm 109280304Sjkim if (!BN_is_odd(A) && !BN_is_odd(B)) { 110280304Sjkim ret = 0; 111280304Sjkim goto end; 112280304Sjkim } 113109998Smarkm 114280304Sjkim /* now B is non-zero */ 115280304Sjkim i = 0; 116280304Sjkim while (!BN_is_bit_set(B, i)) 117280304Sjkim i++; 118280304Sjkim err = !BN_rshift(B, B, i); 119280304Sjkim if (err) 120280304Sjkim goto end; 121280304Sjkim if (i & 1) { 122280304Sjkim /* i is odd */ 123280304Sjkim /* (thus B was even, thus A must be odd!) */ 124109998Smarkm 125280304Sjkim /* set 'ret' to $(-1)^{(A^2-1)/8}$ */ 126280304Sjkim ret = tab[BN_lsw(A) & 7]; 127280304Sjkim } else { 128280304Sjkim /* i is even */ 129280304Sjkim ret = 1; 130280304Sjkim } 131109998Smarkm 132280304Sjkim if (B->neg) { 133280304Sjkim B->neg = 0; 134280304Sjkim if (A->neg) 135280304Sjkim ret = -ret; 136280304Sjkim } 137109998Smarkm 138280304Sjkim /* 139280304Sjkim * now B is positive and odd, so what remains to be done is to compute 140280304Sjkim * the Jacobi symbol (A/B) and multiply it by 'ret' 141280304Sjkim */ 142109998Smarkm 143280304Sjkim while (1) { 144280304Sjkim /* Cohen's step 3: */ 145280304Sjkim 146280304Sjkim /* B is positive and odd */ 147280304Sjkim 148280304Sjkim if (BN_is_zero(A)) { 149280304Sjkim ret = BN_is_one(B) ? ret : 0; 150280304Sjkim goto end; 151280304Sjkim } 152280304Sjkim 153280304Sjkim /* now A is non-zero */ 154280304Sjkim i = 0; 155280304Sjkim while (!BN_is_bit_set(A, i)) 156280304Sjkim i++; 157280304Sjkim err = !BN_rshift(A, A, i); 158280304Sjkim if (err) 159280304Sjkim goto end; 160280304Sjkim if (i & 1) { 161280304Sjkim /* i is odd */ 162280304Sjkim /* multiply 'ret' by $(-1)^{(B^2-1)/8}$ */ 163280304Sjkim ret = ret * tab[BN_lsw(B) & 7]; 164280304Sjkim } 165280304Sjkim 166280304Sjkim /* Cohen's step 4: */ 167280304Sjkim /* multiply 'ret' by $(-1)^{(A-1)(B-1)/4}$ */ 168280304Sjkim if ((A->neg ? ~BN_lsw(A) : BN_lsw(A)) & BN_lsw(B) & 2) 169280304Sjkim ret = -ret; 170280304Sjkim 171280304Sjkim /* (A, B) := (B mod |A|, |A|) */ 172280304Sjkim err = !BN_nnmod(B, B, A, ctx); 173280304Sjkim if (err) 174280304Sjkim goto end; 175280304Sjkim tmp = A; 176280304Sjkim A = B; 177280304Sjkim B = tmp; 178280304Sjkim tmp->neg = 0; 179280304Sjkim } 180280304Sjkim end: 181280304Sjkim BN_CTX_end(ctx); 182280304Sjkim if (err) 183280304Sjkim return -2; 184280304Sjkim else 185280304Sjkim return ret; 186280304Sjkim} 187