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