blob: 1c7523ead70e2d476d8c4cd6838c6be9d46cc6e4 [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
52 mbedtls_mpi_uint carry, borrow, fixup;
53
54 carry = T[n];
Tom Cosgrove90c426b2022-08-23 16:15:19 +010055 borrow = mbedtls_mpi_core_sub( X, T, N, n );
Hanno Becker71f4b0d2022-08-23 12:09:35 +010056 fixup = carry < borrow;
Tom Cosgrove90c426b2022-08-23 16:15:19 +010057 (void) mbedtls_mpi_core_add_if( X, N, n, fixup );
Hanno Becker71f4b0d2022-08-23 12:09:35 +010058}
59
Tom Cosgrove90c426b2022-08-23 16:15:19 +010060mbedtls_mpi_uint mbedtls_mpi_core_mla( mbedtls_mpi_uint *d, size_t d_len,
61 const mbedtls_mpi_uint *s, size_t s_len,
62 mbedtls_mpi_uint b )
Hanno Becker71f4b0d2022-08-23 12:09:35 +010063{
64 mbedtls_mpi_uint c = 0; /* carry */
65 if( d_len < s_len )
66 s_len = d_len;
67 size_t excess_len = d_len - s_len;
68 size_t steps_x8 = s_len / 8;
69 size_t steps_x1 = s_len & 7;
70
71 while( steps_x8-- )
72 {
73 MULADDC_X8_INIT
74 MULADDC_X8_CORE
75 MULADDC_X8_STOP
76 }
77
78 while( steps_x1-- )
79 {
80 MULADDC_X1_INIT
81 MULADDC_X1_CORE
82 MULADDC_X1_STOP
83 }
84
85 while( excess_len-- )
86 {
87 *d += c; c = ( *d < c ); d++;
88 }
89
90 return( c );
91}
92
Tom Cosgrove90c426b2022-08-23 16:15:19 +010093mbedtls_mpi_uint mbedtls_mpi_core_sub( mbedtls_mpi_uint *d,
94 const mbedtls_mpi_uint *l,
95 const mbedtls_mpi_uint *r,
96 size_t n )
Hanno Becker71f4b0d2022-08-23 12:09:35 +010097{
98 mbedtls_mpi_uint c = 0, t, z;
99
100 for( size_t i = 0; i < n; i++ )
101 {
102 z = ( l[i] < c ); t = l[i] - c;
103 c = ( t < r[i] ) + z; d[i] = t - r[i];
104 }
105
106 return( c );
107}
108
Tom Cosgrove90c426b2022-08-23 16:15:19 +0100109mbedtls_mpi_uint mbedtls_mpi_core_add_if( mbedtls_mpi_uint *d,
110 const mbedtls_mpi_uint *r,
111 size_t n,
112 unsigned cond )
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100113{
114 mbedtls_mpi_uint c = 0, t;
115 for( size_t i = 0; i < n; i++ )
116 {
117 mbedtls_mpi_uint add = cond * r[i];
118 t = c;
119 t += d[i]; c = ( t < d[i] );
120 t += add; c += ( t < add );
121 d[i] = t;
122 }
123 return( c );
124}
125
126#endif /* MBEDTLS_BIGNUM_C */