rsaz_exp.h
Go to the documentation of this file.
1 /*
2  * Copyright 2013-2016 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright (c) 2012, Intel Corporation. All Rights Reserved.
4  *
5  * Licensed under the OpenSSL license (the "License"). You may not use
6  * this file except in compliance with the License. You can obtain a copy
7  * in the file LICENSE in the source distribution or at
8  * https://www.openssl.org/source/license.html
9  *
10  * Originally written by Shay Gueron (1, 2), and Vlad Krasnov (1)
11  * (1) Intel Corporation, Israel Development Center, Haifa, Israel
12  * (2) University of Haifa, Israel
13  */
14 
15 #ifndef OPENSSL_HEADER_BN_RSAZ_EXP_H
16 #define OPENSSL_HEADER_BN_RSAZ_EXP_H
17 
18 #include <openssl/bn.h>
19 #include <openssl/cpu.h>
20 
21 #include "internal.h"
22 
23 #if defined(__cplusplus)
24 extern "C" {
25 #endif
26 
27 #if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64)
28 #define RSAZ_ENABLED
29 
30 
31 // RSAZ_1024_mod_exp_avx2 sets |result| to |base_norm| raised to |exponent|
32 // modulo |m_norm|. |base_norm| must be fully-reduced and |exponent| must have
33 // the high bit set (it is 1024 bits wide). |RR| and |k0| must be |RR| and |n0|,
34 // respectively, extracted from |m_norm|'s |BN_MONT_CTX|. |storage_words| is a
35 // temporary buffer that must be aligned to |MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH|
36 // bytes.
37 void RSAZ_1024_mod_exp_avx2(BN_ULONG result[16], const BN_ULONG base_norm[16],
38  const BN_ULONG exponent[16],
39  const BN_ULONG m_norm[16], const BN_ULONG RR[16],
40  BN_ULONG k0,
41  BN_ULONG storage_words[MOD_EXP_CTIME_STORAGE_LEN]);
42 
43 OPENSSL_INLINE int rsaz_avx2_capable(void) {
44  const uint32_t *cap = OPENSSL_ia32cap_get();
45  return (cap[2] & (1 << 5)) != 0; // AVX2
46 }
47 
48 OPENSSL_INLINE int rsaz_avx2_preferred(void) {
49  const uint32_t *cap = OPENSSL_ia32cap_get();
50  static const uint32_t kBMI2AndADX = (1 << 8) | (1 << 19);
51  if ((cap[2] & kBMI2AndADX) == kBMI2AndADX) {
52  // If BMI2 and ADX are available, x86_64-mont5.pl is faster.
53  return 0;
54  }
55  return (cap[2] & (1 << 5)) != 0; // AVX2
56 }
57 
58 
59 // Assembly functions.
60 
61 // RSAZ represents 1024-bit integers using unsaturated 29-bit limbs stored in
62 // 64-bit integers. This requires 36 limbs but padded up to 40.
63 //
64 // See crypto/bn/asm/rsaz-avx2.pl for further details.
65 
66 // rsaz_1024_norm2red_avx2 converts |norm| from |BIGNUM| to RSAZ representation
67 // and writes the result to |red|.
68 void rsaz_1024_norm2red_avx2(BN_ULONG red[40], const BN_ULONG norm[16]);
69 
70 // rsaz_1024_mul_avx2 computes |a| * |b| mod |n| and writes the result to |ret|.
71 // Inputs and outputs are in Montgomery form, using RSAZ's representation. |k|
72 // is -|n|^-1 mod 2^64 or |n0| from |BN_MONT_CTX|.
73 void rsaz_1024_mul_avx2(BN_ULONG ret[40], const BN_ULONG a[40],
74  const BN_ULONG b[40], const BN_ULONG n[40], BN_ULONG k);
75 
76 // rsaz_1024_mul_avx2 computes |a|^(2*|count|) mod |n| and writes the result to
77 // |ret|. Inputs and outputs are in Montgomery form, using RSAZ's
78 // representation. |k| is -|n|^-1 mod 2^64 or |n0| from |BN_MONT_CTX|.
79 void rsaz_1024_sqr_avx2(BN_ULONG ret[40], const BN_ULONG a[40],
80  const BN_ULONG n[40], BN_ULONG k, int count);
81 
82 // rsaz_1024_scatter5_avx2 stores |val| at index |i| of |tbl|. |i| must be
83 // positive and at most 31. Note the table only uses 18 |BN_ULONG|s per entry
84 // instead of 40. It packs two 29-bit limbs into each |BN_ULONG| and only stores
85 // 36 limbs rather than the padded 40.
86 void rsaz_1024_scatter5_avx2(BN_ULONG tbl[32 * 18], const BN_ULONG val[40],
87  int i);
88 
89 // rsaz_1024_gather5_avx2 loads index |i| of |tbl| and writes it to |val|.
90 void rsaz_1024_gather5_avx2(BN_ULONG val[40], const BN_ULONG tbl[32 * 18],
91  int i);
92 
93 // rsaz_1024_red2norm_avx2 converts |red| from RSAZ to |BIGNUM| representation
94 // and writes the result to |norm|.
95 void rsaz_1024_red2norm_avx2(BN_ULONG norm[16], const BN_ULONG red[40]);
96 
97 
98 #endif // !OPENSSL_NO_ASM && OPENSSL_X86_64
99 
100 #if defined(__cplusplus)
101 } // extern "C"
102 #endif
103 
104 #endif // OPENSSL_HEADER_BN_RSAZ_EXP_H
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
bn.h
exponent
int exponent
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1100
rsaz_1024_red2norm_avx2
#define rsaz_1024_red2norm_avx2
Definition: boringssl_prefix_symbols.h:3366
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
rsaz_1024_scatter5_avx2
#define rsaz_1024_scatter5_avx2
Definition: boringssl_prefix_symbols.h:3367
setup.k
k
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:42
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
absl::hash_internal::k0
static const uint64_t k0
Definition: abseil-cpp/absl/hash/internal/city.cc:53
MOD_EXP_CTIME_STORAGE_LEN
#define MOD_EXP_CTIME_STORAGE_LEN
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/bn/internal.h:200
rsaz_1024_mul_avx2
#define rsaz_1024_mul_avx2
Definition: boringssl_prefix_symbols.h:3364
rsaz_1024_sqr_avx2
#define rsaz_1024_sqr_avx2
Definition: boringssl_prefix_symbols.h:3368
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
OPENSSL_INLINE
#define OPENSSL_INLINE
Definition: base.h:282
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
rsaz_1024_norm2red_avx2
#define rsaz_1024_norm2red_avx2
Definition: boringssl_prefix_symbols.h:3365
count
int * count
Definition: bloaty/third_party/googletest/googlemock/test/gmock_stress_test.cc:96
internal.h
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
RSAZ_1024_mod_exp_avx2
#define RSAZ_1024_mod_exp_avx2
Definition: boringssl_prefix_symbols.h:2077
cpu.h
rsaz_1024_gather5_avx2
#define rsaz_1024_gather5_avx2
Definition: boringssl_prefix_symbols.h:3363
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:13