blob: d275fe81f7417216a56accff4dafdda143dc082c [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 Cosgrove79b70f62022-08-17 06:17:00 +010059/*
60 * Fast Montgomery initialization (thanks to Tom St Denis).
61 */
62mbedtls_mpi_uint mbedtls_mpi_montg_init( mbedtls_mpi_uint m0 )
63{
64 mbedtls_mpi_uint x = m0;
65
66 x += ( ( m0 + 2 ) & 4 ) << 1;
67
68 for( unsigned int i = biL; i >= 8; i /= 2 )
69 x *= ( 2 - ( m0 * x ) );
70
71 return( ~x + 1 );
72}
73
Tom Cosgrove90c426b2022-08-23 16:15:19 +010074mbedtls_mpi_uint mbedtls_mpi_core_mla( mbedtls_mpi_uint *d, size_t d_len,
75 const mbedtls_mpi_uint *s, size_t s_len,
76 mbedtls_mpi_uint b )
Hanno Becker71f4b0d2022-08-23 12:09:35 +010077{
78 mbedtls_mpi_uint c = 0; /* carry */
79 if( d_len < s_len )
80 s_len = d_len;
81 size_t excess_len = d_len - s_len;
82 size_t steps_x8 = s_len / 8;
83 size_t steps_x1 = s_len & 7;
84
85 while( steps_x8-- )
86 {
87 MULADDC_X8_INIT
88 MULADDC_X8_CORE
89 MULADDC_X8_STOP
90 }
91
92 while( steps_x1-- )
93 {
94 MULADDC_X1_INIT
95 MULADDC_X1_CORE
96 MULADDC_X1_STOP
97 }
98
99 while( excess_len-- )
100 {
101 *d += c; c = ( *d < c ); d++;
102 }
103
104 return( c );
105}
106
Tom Cosgrove90c426b2022-08-23 16:15:19 +0100107mbedtls_mpi_uint mbedtls_mpi_core_sub( mbedtls_mpi_uint *d,
108 const mbedtls_mpi_uint *l,
109 const mbedtls_mpi_uint *r,
110 size_t n )
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100111{
112 mbedtls_mpi_uint c = 0, t, z;
113
114 for( size_t i = 0; i < n; i++ )
115 {
116 z = ( l[i] < c ); t = l[i] - c;
117 c = ( t < r[i] ) + z; d[i] = t - r[i];
118 }
119
120 return( c );
121}
122
Tom Cosgrove90c426b2022-08-23 16:15:19 +0100123mbedtls_mpi_uint mbedtls_mpi_core_add_if( mbedtls_mpi_uint *d,
124 const mbedtls_mpi_uint *r,
125 size_t n,
126 unsigned cond )
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100127{
128 mbedtls_mpi_uint c = 0, t;
129 for( size_t i = 0; i < n; i++ )
130 {
131 mbedtls_mpi_uint add = cond * r[i];
132 t = c;
133 t += d[i]; c = ( t < d[i] );
134 t += add; c += ( t < add );
135 d[i] = t;
136 }
137 return( c );
138}
139
140#endif /* MBEDTLS_BIGNUM_C */