1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci#include "internal/cryptlib.h"
11e1051a39Sopenharmony_ci#include "bn_local.h"
12e1051a39Sopenharmony_ci
13e1051a39Sopenharmony_ci/* least significant word */
14e1051a39Sopenharmony_ci#define BN_lsw(n) (((n)->top == 0) ? (BN_ULONG) 0 : (n)->d[0])
15e1051a39Sopenharmony_ci
16e1051a39Sopenharmony_ci/* Returns -2 for errors because both -1 and 0 are valid results. */
17e1051a39Sopenharmony_ciint BN_kronecker(const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
18e1051a39Sopenharmony_ci{
19e1051a39Sopenharmony_ci    int i;
20e1051a39Sopenharmony_ci    int ret = -2;               /* avoid 'uninitialized' warning */
21e1051a39Sopenharmony_ci    int err = 0;
22e1051a39Sopenharmony_ci    BIGNUM *A, *B, *tmp;
23e1051a39Sopenharmony_ci    /*-
24e1051a39Sopenharmony_ci     * In 'tab', only odd-indexed entries are relevant:
25e1051a39Sopenharmony_ci     * For any odd BIGNUM n,
26e1051a39Sopenharmony_ci     *     tab[BN_lsw(n) & 7]
27e1051a39Sopenharmony_ci     * is $(-1)^{(n^2-1)/8}$ (using TeX notation).
28e1051a39Sopenharmony_ci     * Note that the sign of n does not matter.
29e1051a39Sopenharmony_ci     */
30e1051a39Sopenharmony_ci    static const int tab[8] = { 0, 1, 0, -1, 0, -1, 0, 1 };
31e1051a39Sopenharmony_ci
32e1051a39Sopenharmony_ci    bn_check_top(a);
33e1051a39Sopenharmony_ci    bn_check_top(b);
34e1051a39Sopenharmony_ci
35e1051a39Sopenharmony_ci    BN_CTX_start(ctx);
36e1051a39Sopenharmony_ci    A = BN_CTX_get(ctx);
37e1051a39Sopenharmony_ci    B = BN_CTX_get(ctx);
38e1051a39Sopenharmony_ci    if (B == NULL)
39e1051a39Sopenharmony_ci        goto end;
40e1051a39Sopenharmony_ci
41e1051a39Sopenharmony_ci    err = !BN_copy(A, a);
42e1051a39Sopenharmony_ci    if (err)
43e1051a39Sopenharmony_ci        goto end;
44e1051a39Sopenharmony_ci    err = !BN_copy(B, b);
45e1051a39Sopenharmony_ci    if (err)
46e1051a39Sopenharmony_ci        goto end;
47e1051a39Sopenharmony_ci
48e1051a39Sopenharmony_ci    /*
49e1051a39Sopenharmony_ci     * Kronecker symbol, implemented according to Henri Cohen,
50e1051a39Sopenharmony_ci     * "A Course in Computational Algebraic Number Theory"
51e1051a39Sopenharmony_ci     * (algorithm 1.4.10).
52e1051a39Sopenharmony_ci     */
53e1051a39Sopenharmony_ci
54e1051a39Sopenharmony_ci    /* Cohen's step 1: */
55e1051a39Sopenharmony_ci
56e1051a39Sopenharmony_ci    if (BN_is_zero(B)) {
57e1051a39Sopenharmony_ci        ret = BN_abs_is_word(A, 1);
58e1051a39Sopenharmony_ci        goto end;
59e1051a39Sopenharmony_ci    }
60e1051a39Sopenharmony_ci
61e1051a39Sopenharmony_ci    /* Cohen's step 2: */
62e1051a39Sopenharmony_ci
63e1051a39Sopenharmony_ci    if (!BN_is_odd(A) && !BN_is_odd(B)) {
64e1051a39Sopenharmony_ci        ret = 0;
65e1051a39Sopenharmony_ci        goto end;
66e1051a39Sopenharmony_ci    }
67e1051a39Sopenharmony_ci
68e1051a39Sopenharmony_ci    /* now  B  is non-zero */
69e1051a39Sopenharmony_ci    i = 0;
70e1051a39Sopenharmony_ci    while (!BN_is_bit_set(B, i))
71e1051a39Sopenharmony_ci        i++;
72e1051a39Sopenharmony_ci    err = !BN_rshift(B, B, i);
73e1051a39Sopenharmony_ci    if (err)
74e1051a39Sopenharmony_ci        goto end;
75e1051a39Sopenharmony_ci    if (i & 1) {
76e1051a39Sopenharmony_ci        /* i is odd */
77e1051a39Sopenharmony_ci        /* (thus  B  was even, thus  A  must be odd!)  */
78e1051a39Sopenharmony_ci
79e1051a39Sopenharmony_ci        /* set 'ret' to $(-1)^{(A^2-1)/8}$ */
80e1051a39Sopenharmony_ci        ret = tab[BN_lsw(A) & 7];
81e1051a39Sopenharmony_ci    } else {
82e1051a39Sopenharmony_ci        /* i is even */
83e1051a39Sopenharmony_ci        ret = 1;
84e1051a39Sopenharmony_ci    }
85e1051a39Sopenharmony_ci
86e1051a39Sopenharmony_ci    if (B->neg) {
87e1051a39Sopenharmony_ci        B->neg = 0;
88e1051a39Sopenharmony_ci        if (A->neg)
89e1051a39Sopenharmony_ci            ret = -ret;
90e1051a39Sopenharmony_ci    }
91e1051a39Sopenharmony_ci
92e1051a39Sopenharmony_ci    /*
93e1051a39Sopenharmony_ci     * now B is positive and odd, so what remains to be done is to compute
94e1051a39Sopenharmony_ci     * the Jacobi symbol (A/B) and multiply it by 'ret'
95e1051a39Sopenharmony_ci     */
96e1051a39Sopenharmony_ci
97e1051a39Sopenharmony_ci    while (1) {
98e1051a39Sopenharmony_ci        /* Cohen's step 3: */
99e1051a39Sopenharmony_ci
100e1051a39Sopenharmony_ci        /*  B  is positive and odd */
101e1051a39Sopenharmony_ci
102e1051a39Sopenharmony_ci        if (BN_is_zero(A)) {
103e1051a39Sopenharmony_ci            ret = BN_is_one(B) ? ret : 0;
104e1051a39Sopenharmony_ci            goto end;
105e1051a39Sopenharmony_ci        }
106e1051a39Sopenharmony_ci
107e1051a39Sopenharmony_ci        /* now  A  is non-zero */
108e1051a39Sopenharmony_ci        i = 0;
109e1051a39Sopenharmony_ci        while (!BN_is_bit_set(A, i))
110e1051a39Sopenharmony_ci            i++;
111e1051a39Sopenharmony_ci        err = !BN_rshift(A, A, i);
112e1051a39Sopenharmony_ci        if (err)
113e1051a39Sopenharmony_ci            goto end;
114e1051a39Sopenharmony_ci        if (i & 1) {
115e1051a39Sopenharmony_ci            /* i is odd */
116e1051a39Sopenharmony_ci            /* multiply 'ret' by  $(-1)^{(B^2-1)/8}$ */
117e1051a39Sopenharmony_ci            ret = ret * tab[BN_lsw(B) & 7];
118e1051a39Sopenharmony_ci        }
119e1051a39Sopenharmony_ci
120e1051a39Sopenharmony_ci        /* Cohen's step 4: */
121e1051a39Sopenharmony_ci        /* multiply 'ret' by  $(-1)^{(A-1)(B-1)/4}$ */
122e1051a39Sopenharmony_ci        if ((A->neg ? ~BN_lsw(A) : BN_lsw(A)) & BN_lsw(B) & 2)
123e1051a39Sopenharmony_ci            ret = -ret;
124e1051a39Sopenharmony_ci
125e1051a39Sopenharmony_ci        /* (A, B) := (B mod |A|, |A|) */
126e1051a39Sopenharmony_ci        err = !BN_nnmod(B, B, A, ctx);
127e1051a39Sopenharmony_ci        if (err)
128e1051a39Sopenharmony_ci            goto end;
129e1051a39Sopenharmony_ci        tmp = A;
130e1051a39Sopenharmony_ci        A = B;
131e1051a39Sopenharmony_ci        B = tmp;
132e1051a39Sopenharmony_ci        tmp->neg = 0;
133e1051a39Sopenharmony_ci    }
134e1051a39Sopenharmony_ci end:
135e1051a39Sopenharmony_ci    BN_CTX_end(ctx);
136e1051a39Sopenharmony_ci    if (err)
137e1051a39Sopenharmony_ci        return -2;
138e1051a39Sopenharmony_ci    else
139e1051a39Sopenharmony_ci        return ret;
140e1051a39Sopenharmony_ci}
141