Skip to content

Commit

Permalink
Update base.cc
Browse files Browse the repository at this point in the history
  • Loading branch information
shaojian-ant authored Oct 14, 2024
1 parent be751d3 commit 76b82ee
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions heu/algorithms/paillier_zahlen/base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "heu/algorithms/paillier_zahlen/base.h"

namespace heu::algos::paillier_z {

namespace {
size_t kExpUnitBits = 10;
} // namespace

void PublicKey::Init() {
n_square_ = n_ * n_;
n_half_ = n_ / MPInt::_2_;
key_size_ = n_.BitCount();

m_space_ = std::make_shared<MontgomerySpace>(n_square_);
hs_table_ = std::make_shared<BaseTable>();
m_space_->MakeBaseTable(
Expand All @@ -28,6 +33,7 @@ void PublicKey::Init() {
(key_size_ / 2 + MP_DIGIT_BIT - 1) / MP_DIGIT_BIT * MP_DIGIT_BIT,
hs_table_.get());
}

void SecretKey::Init() {
p_square_ = p_ * p_; // p^2
q_square_ = q_ * q_; // q^2
Expand All @@ -41,36 +47,45 @@ void SecretKey::Init() {
phi_q_square_ = q_ * (q_ - MPInt::_1_); // q(q-1)
phi_p_ = p_ - 1_mp; // p-1
phi_q_ = q_ - 1_mp; // q-1

// Precompute hp
MPInt n = p_ * q_;
MPInt g = n + 1_mp;
MPInt::PowMod(g, phi_p_, p_square_, &hp_);
hp_ = hp_.DecrOne() / p_;
MPInt::InvertMod(hp_, p_, &hp_);

// Precompute hq
MPInt::PowMod(g, phi_q_, q_square_, &hq_);
hq_ = hq_.DecrOne() / q_;
MPInt::InvertMod(hq_, q_, &hq_);
}

MPInt SecretKey::PowModNSquareCrt(const MPInt &base, const MPInt &exp) const {
// smaller exponents: exp mod p(p-1), exp mod q(q-1)
MPInt pexp = exp % phi_p_square_;
MPInt qexp = exp % phi_q_square_;

// smaller bases: mod p^2, q^2
MPInt pbase = base % p_square_;
MPInt qbase = base % q_square_;

// smaller exponentiations of base mod p^2, q^2
MPInt pbase_exp, qbase_exp;
MPInt::PowMod(pbase, pexp, p_square_, &pbase_exp);
MPInt::PowMod(qbase, qexp, q_square_, &qbase_exp);

// CRT to calculate base^exp mod n^2
MPInt result =
((pbase_exp - qbase_exp) * q_square_inv_mul_q_square_ + qbase_exp) %
n_square_;
return result;
}

Plaintext ItemTool::Clone(const Plaintext &pt) const { return pt; }

Ciphertext ItemTool::Clone(const Ciphertext &ct) const {
return Ciphertext(ct.c_);
}

} // namespace heu::algos::paillier_z

0 comments on commit 76b82ee

Please sign in to comment.