blob: 024348ad4a3ae561b1112f28bb75d01a47423b4f [file] [log] [blame]
Hanno Becker71f4b0d2022-08-23 12:09:35 +01001/*
2 * Multi-precision integer library
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#include "common.h"
21
22#if defined(MBEDTLS_BIGNUM_C)
23
24#include "mbedtls/bignum.h"
25#include "bignum_core.h"
26#include "bn_mul.h"
27
28#include <string.h>
29
Tom Cosgrove90c426b2022-08-23 16:15:19 +010030void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X,
31 const mbedtls_mpi_uint *A,
32 const mbedtls_mpi_uint *B,
33 size_t B_len,
34 const mbedtls_mpi_uint *N,
35 size_t n,
36 mbedtls_mpi_uint mm,
37 mbedtls_mpi_uint *T )
Hanno Becker71f4b0d2022-08-23 12:09:35 +010038{
39 memset( T, 0, (2*n+1)*ciL );
40
41 for( size_t i = 0; i < n; i++, T++ )
42 {
43 mbedtls_mpi_uint u0, u1;
44 /* T = (T + u0*B + u1*N) / 2^biL */
45 u0 = A[i];
46 u1 = ( T[0] + u0 * B[0] ) * mm;
47
Tom Cosgrove90c426b2022-08-23 16:15:19 +010048 (void) mbedtls_mpi_core_mla( T, n + 2, B, B_len, u0 );
49 (void) mbedtls_mpi_core_mla( T, n + 2, N, n, u1 );
Hanno Becker71f4b0d2022-08-23 12:09:35 +010050 }
51
Tom Cosgrove268f96b2022-07-31 22:30:16 +010052 mbedtls_mpi_uint carry, borrow;
Hanno Becker71f4b0d2022-08-23 12:09:35 +010053
54 carry = T[n];
Tom Cosgrove90c426b2022-08-23 16:15:19 +010055 borrow = mbedtls_mpi_core_sub( X, T, N, n );
Tom Cosgrove268f96b2022-07-31 22:30:16 +010056 (void) mbedtls_mpi_core_add_if( X, N, n, ( carry < borrow ) );
Hanno Becker71f4b0d2022-08-23 12:09:35 +010057}
58
Tom Cosgrove90c426b2022-08-23 16:15:19 +010059mbedtls_mpi_uint mbedtls_mpi_core_mla( mbedtls_mpi_uint *d, size_t d_len,
60 const mbedtls_mpi_uint *s, size_t s_len,
61 mbedtls_mpi_uint b )
Hanno Becker71f4b0d2022-08-23 12:09:35 +010062{
63 mbedtls_mpi_uint c = 0; /* carry */
64 if( d_len < s_len )
65 s_len = d_len;
66 size_t excess_len = d_len - s_len;
67 size_t steps_x8 = s_len / 8;
68 size_t steps_x1 = s_len & 7;
69
70 while( steps_x8-- )
71 {
72 MULADDC_X8_INIT
73 MULADDC_X8_CORE
74 MULADDC_X8_STOP
75 }
76
77 while( steps_x1-- )
78 {
79 MULADDC_X1_INIT
80 MULADDC_X1_CORE
81 MULADDC_X1_STOP
82 }
83
84 while( excess_len-- )
85 {
86 *d += c; c = ( *d < c ); d++;
87 }
88
89 return( c );
90}
91
Tom Cosgrove90c426b2022-08-23 16:15:19 +010092mbedtls_mpi_uint mbedtls_mpi_core_sub( mbedtls_mpi_uint *d,
93 const mbedtls_mpi_uint *l,
94 const mbedtls_mpi_uint *r,
95 size_t n )
Hanno Becker71f4b0d2022-08-23 12:09:35 +010096{
97 mbedtls_mpi_uint c = 0, t, z;
98
99 for( size_t i = 0; i < n; i++ )
100 {
101 z = ( l[i] < c ); t = l[i] - c;
102 c = ( t < r[i] ) + z; d[i] = t - r[i];
103 }
104
105 return( c );
106}
107
Tom Cosgrove90c426b2022-08-23 16:15:19 +0100108mbedtls_mpi_uint mbedtls_mpi_core_add_if( mbedtls_mpi_uint *d,
109 const mbedtls_mpi_uint *r,
110 size_t n,
111 unsigned cond )
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100112{
113 mbedtls_mpi_uint c = 0, t;
114 for( size_t i = 0; i < n; i++ )
115 {
116 mbedtls_mpi_uint add = cond * r[i];
117 t = c;
118 t += d[i]; c = ( t < d[i] );
119 t += add; c += ( t < add );
120 d[i] = t;
121 }
122 return( c );
123}
124
125#endif /* MBEDTLS_BIGNUM_C */