Skip to content

Commit

Permalink
Fix the big-endian issue with memcpy in S390/x
Browse files Browse the repository at this point in the history
  • Loading branch information
marcone-almeida committed Nov 7, 2023
1 parent 7a7df9e commit 7568887
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/pke/lib/scheme/bfvrns/bfvrns-cryptoparameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,13 @@ void CryptoParametersBFVRNS::PrecomputeCRTTables(KeySwitchTechnique ksTech, Scal
for (uint32_t i = 0; i < sizeQ; i++) {
BigInteger mu = BarrettBase128Bit / BigInteger(moduliQ[i]);
uint64_t val[2];
#ifdef __s390__
val[1] = (mu % TwoPower64).ConvertToInt();
val[0] = mu.RShift(64).ConvertToInt();
#else
val[0] = (mu % TwoPower64).ConvertToInt();
val[1] = mu.RShift(64).ConvertToInt();
#endif
memcpy(&m_modqBarrettMu[i], val, sizeof(DoubleNativeInt));
}

Expand Down Expand Up @@ -143,8 +148,13 @@ void CryptoParametersBFVRNS::PrecomputeCRTTables(KeySwitchTechnique ksTech, Scal
for (uint32_t i = 0; i < sizeR; i++) {
BigInteger mu = BarrettBase128Bit / BigInteger(moduliR[i]);
uint64_t val[2];
#ifdef __s390__
val[1] = (mu % TwoPower64).ConvertToInt();
val[0] = mu.RShift(64).ConvertToInt();
#else
val[0] = (mu % TwoPower64).ConvertToInt();
val[1] = mu.RShift(64).ConvertToInt();
#endif

memcpy(&m_modrBarrettMu[i], val, sizeof(DoubleNativeInt));
}
Expand Down Expand Up @@ -261,8 +271,13 @@ void CryptoParametersBFVRNS::PrecomputeCRTTables(KeySwitchTechnique ksTech, Scal
for (uint32_t j = 0; j < moduliR.size(); j++) {
BigInteger mu = BarrettBase128Bit / BigInteger(moduliR[j]);
uint64_t val[2];
#ifdef __s390__
val[1] = (mu % TwoPower64).ConvertToInt();
val[0] = mu.RShift(64).ConvertToInt();
#else
val[0] = (mu % TwoPower64).ConvertToInt();
val[1] = mu.RShift(64).ConvertToInt();
#endif

memcpy(&m_modrBarrettMu[j], val, sizeof(DoubleNativeInt));
}
Expand Down Expand Up @@ -716,8 +731,13 @@ void CryptoParametersBFVRNS::PrecomputeCRTTables(KeySwitchTechnique ksTech, Scal
for (uint32_t i = 0; i < m_modbskBarrettMu.size(); i++) {
BigInteger mu = BarrettBase128Bit / BigInteger(m_moduliBsk[i]);
uint64_t val[2];
#ifdef __s390__
val[1] = (mu % TwoPower64).ConvertToInt();
val[0] = mu.RShift(64).ConvertToInt();
#else
val[0] = (mu % TwoPower64).ConvertToInt();
val[1] = mu.RShift(64).ConvertToInt();
#endif
memcpy(&m_modbskBarrettMu[i], val, sizeof(DoubleNativeInt));
}

Expand Down
5 changes: 5 additions & 0 deletions src/pke/lib/scheme/bgvrns/bgvrns-cryptoparameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,13 @@ void CryptoParametersBGVRNS::PrecomputeCRTTables(KeySwitchTechnique ksTech, Scal
for (uint32_t i = 0; i < sizeQ; i++) {
BigInteger mu = BarrettBase128Bit / BigInteger(moduliQ[i]);
uint64_t val[2];
#ifdef __s390__
val[1] = (mu % TwoPower64).ConvertToInt();
val[0] = mu.RShift(64).ConvertToInt();
#else
val[0] = (mu % TwoPower64).ConvertToInt();
val[1] = mu.RShift(64).ConvertToInt();
#endif
memcpy(&m_modqBarrettMu[i], val, sizeof(DoubleNativeInt));
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/pke/lib/scheme/ckksrns/ckksrns-cryptoparameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,13 @@ void CryptoParametersCKKSRNS::PrecomputeCRTTables(KeySwitchTechnique ksTech, Sca
for (uint32_t i = 0; i < sizeQ; i++) {
BigInteger mu = BarrettBase128Bit / BigInteger(moduliQ[i]);
uint64_t val[2];
#ifdef __s390__
val[1] = (mu % TwoPower64).ConvertToInt();
val[0] = mu.RShift(64).ConvertToInt();
#else
val[0] = (mu % TwoPower64).ConvertToInt();
val[1] = mu.RShift(64).ConvertToInt();
#endif

memcpy(&m_modqBarrettMu[i], val, sizeof(DoubleNativeInt));
}
Expand Down
5 changes: 5 additions & 0 deletions src/pke/lib/scheme/ckksrns/ckksrns-multiparty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,13 @@ void PrecomputeRNSExtensionTables(CryptoContext<DCRTPoly>& cc, usint from, usint
for (uint32_t j = 0; j < moduliP.size(); j++) {
BigInteger mu = BarrettBase128Bit / BigInteger(moduliP[j]);
uint64_t val[2];
#ifdef __s390__
val[1] = (mu % TwoPower64).ConvertToInt();
val[0] = mu.RShift(64).ConvertToInt();
#else
val[0] = (mu % TwoPower64).ConvertToInt();
val[1] = mu.RShift(64).ConvertToInt();
#endif

memcpy(&rnsExtTables.modpBarrettMu[j], val, sizeof(DoubleNativeInt));
}
Expand Down
10 changes: 10 additions & 0 deletions src/pke/lib/schemerns/rns-cryptoparameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,13 @@ void CryptoParametersRNS::PrecomputeCRTTables(KeySwitchTechnique ksTech, Scaling
for (uint32_t i = 0; i < moduli.size(); i++) {
BigInteger mu = BarrettBase128Bit / BigInteger(moduli[i]);
uint64_t val[2];
#ifdef __s390__
val[1] = (mu % TwoPower64).ConvertToInt();
val[0] = mu.RShift(64).ConvertToInt();
#else
val[0] = (mu % TwoPower64).ConvertToInt();
val[1] = mu.RShift(64).ConvertToInt();
#endif

memcpy(&m_modComplPartqBarrettMu[l][j][i], val, sizeof(DoubleNativeInt));
}
Expand Down Expand Up @@ -385,8 +390,13 @@ void CryptoParametersRNS::PrecomputeCRTTables(KeySwitchTechnique ksTech, Scaling
m_multipartyModq0BarrettMu.resize(1);
BigInteger mu = BarrettBase128Bit / BigInteger(moduliQ[0]);
uint64_t val[2];
#ifdef __s390__
val[1] = (mu % TwoPower64).ConvertToInt();
val[0] = mu.RShift(64).ConvertToInt();
#else
val[0] = (mu % TwoPower64).ConvertToInt();
val[1] = mu.RShift(64).ConvertToInt();
#endif
memcpy(&m_multipartyModq0BarrettMu[0], val, sizeof(DoubleNativeInt));

// Stores \frac{1/q_i}
Expand Down

0 comments on commit 7568887

Please sign in to comment.