xref: /aosp_15_r20/external/private-join-and-compute/private_join_and_compute/crypto/fixed_base_exp.h (revision a6aa18fbfbf9cb5cd47356a9d1b057768998488c)
1 /*
2  * Copyright 2019 Google LLC.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     https://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 // A class for modular exponentiating a fixed base with arbitrary exponents
17 // based on a modulus. This class delegates the modular exponentiation
18 // operation to one of its subclasses.
19 
20 #ifndef PRIVATE_JOIN_AND_COMPUTE_CRYPTO_FIXED_BASE_H_
21 #define PRIVATE_JOIN_AND_COMPUTE_CRYPTO_FIXED_BASE_H_
22 
23 #include <memory>
24 
25 #include "absl/flags/declare.h"
26 #include "private_join_and_compute/crypto/big_num.h"
27 #include "private_join_and_compute/crypto/context.h"
28 #include "private_join_and_compute/util/status.inc"
29 
30 // Declared for test-only.
31 ABSL_DECLARE_FLAG(bool, two_k_ary_exp);
32 
33 namespace private_join_and_compute {
34 namespace internal {
35 class FixedBaseExpImplBase;
36 }  // namespace internal
37 
38 class FixedBaseExp {
39  public:
40   // FixedBaseExp is neither copyable nor movable.
41   FixedBaseExp(const FixedBaseExp&) = delete;
42   FixedBaseExp& operator=(const FixedBaseExp&) = delete;
43 
44   ~FixedBaseExp();
45 
46   // Computes fixed_base^exp mod modulus.
47   // Returns INVALID_ARGUMENT if the exponent is negative.
48   StatusOr<BigNum> ModExp(const BigNum& exp) const;
49 
50   static std::unique_ptr<FixedBaseExp> GetFixedBaseExp(Context* ctx,
51                                                        const BigNum& fixed_base,
52                                                        const BigNum& modulus);
53 
54  private:
55   explicit FixedBaseExp(internal::FixedBaseExpImplBase* impl);
56 
57   std::unique_ptr<internal::FixedBaseExpImplBase> impl_;
58 };
59 
60 }  // namespace private_join_and_compute
61 
62 #endif  // PRIVATE_JOIN_AND_COMPUTE_CRYPTO_FIXED_BASE_H_
63