53 srand(
static_cast<unsigned>(time(0)));
69 std::pair<std::string, std::string> keys =
70 std::make_pair(
"__NO_PRIV_KEY__",
"__NO_PUB_KEY__");
88 if (
m_n > 0 &&
m_n != keys.first ) {
100 if (
m_n > 0 &&
m_n != keys.second ) {
160 <<
a << Form(
" (%a) !",
a));
173 if (
a > 0. || std::isnan(
a) ) {
181 <<
a << Form(
" (%a) !",
a));
207 while ( p == q || dlog2 < 0.1 || dlog2 > 30. ) {
210 dlog2 = fabs(log2(p)-log2(q));
238 for (
LLI_t i = 2; i < (
LLI_t)sqrt(n) + 1; ++i) {
253 std::vector<LLI_t>
r;
255 r.push_back(std::max(n1, n2));
256 r.push_back(std::min(n1, n2));
259 r.push_back(
r[i-2] %
r[i-1]);
283 if ( ((
phi * i + 1) %
e) == 0 ) {
295 unsigned int ra = (
unsigned int)(log(
a)/log(16.))+1;
296 unsigned int rb = (
unsigned int)(log(b)/log(16.))+1;
305 TString tstr = Form(
"%02x%02x%02x%0*x%0*llx%0*x%0*llx",
310 return std::string(tstr.Data());
315 std::pair<SimpleEncrypter::ULLI_t, SimpleEncrypter::ULLI_t>
318 std::pair<ULLI_t, ULLI_t> keys(0,0);
321 if (
str.IsHex() &&
str.Length() > 3) {
323 unsigned int ndigits = strtoul(TString(
str(0,2)).
Data(),
nullptr, 16);
324 unsigned int ra = strtoul(TString(
str(2,2)).
Data(),
nullptr, 16);
325 unsigned int rb = strtoul(TString(
str(4,2)).
Data(),
nullptr, 16);
326 if (
str.Length() == (
int)(2*ndigits + 6) ) {
327 keys.first = strtoll(TString(
str(ndigits+6-ra, ra)).
Data(),
329 keys.second = strtoll(TString(
str(2*ndigits+6-rb, rb)).
Data(),
332 ATH_MSG_ERROR(
"Private/public key must be a hex string of " <<
336 ATH_MSG_ERROR(
"Private/public key must be a hex string of " <<
347 static_assert(
sizeof(
float) ==
sizeof(std::uint32_t),
"This code assumes a 32-bit float");
352 return std::bit_cast<std::uint32_t>(val);
359 static_assert(
sizeof(
float) ==
sizeof(std::uint32_t),
"This code assumes a 32-bit float");
360 if (val > std::numeric_limits<std::uint32_t>::max()) {
361 ATH_MSG_WARNING(
"Value does not fit in float bit representation: "<< val);
364 return std::bit_cast<float>(
static_cast<std::uint32_t
>(val));
380 }
while ( c > rmask );
399 }
while ( d > rmask );
431 bin[
r-i-1] = tmp % 2;
432 tmp = (
LLI_t)(tmp/2);
438 ULLI_t d2 = dec[i-1]*dec[i-1] % n;
439 if (
bin[i] > 0 ) d2 *=
a;
454 ATH_MSG_ERROR(
"Setup not OK for encryption: public key set?");
470 ATH_MSG_ERROR(
"Setup not OK for decryption: private key set?");
#define ATH_MSG_WARNING(x)
Provide simple asymmetric encryption for blinding of float values.
AsgMessaging(const std::string &name)
Constructor with a name.
ULLI_t powerMod(ULLI_t a, ULLI_t d, ULLI_t n) const
Exponentiate a with d observing modulus n.
ULLI_t encryptInternal(ULLI_t x) const
Encrypt integer (internal).
bool isOkForDec()
Check setup readiness for decryption.
bool m_isOkForEnc
indicates that keys are set and range checks are ok
virtual ULLI_t greatestCommonDenominator(ULLI_t n1, ULLI_t n2) const
Find greatest common denominator.
ULLI_t decryptInternal(ULLI_t x) const
Decrypt integer (internal).
ULLI_t decryptFPECycle(ULLI_t a) const
Decrypt using format preserving encryption w.r.t.
virtual void genKeyPairInternal()
virtual ULLI_t encrypt(ULLI_t x)
Encrypt a positive integer value.
virtual void setPrivKey(std::string keystr)
Set private key.
virtual ULLI_t decrypt(ULLI_t x)
Decrypt a positive integer value.
static const ULLI_t m_MAXRANGE
static const unsigned int m_MAXHEXDIGITS
maximum number of hex digits for key parts
unsigned long long int ULLI_t
virtual std::string getPrivKey() const
Get private key.
virtual ULLI_t genCoprime(ULLI_t n) const
Find a coprime number.
virtual void setPubKey(std::string keystr)
Set public key.
virtual std::string keyToString(ULLI_t a, ULLI_t b) const
static const ULLI_t m_MINRANGE
ULLI_t encryptFPECycle(ULLI_t a) const
virtual std::string getPubKey() const
Get public key.
virtual ULLI_t genPrime() const
Find a prime number.
bool isOkForEnc()
Check setup readiness for encryption.
virtual std::pair< std::string, std::string > genKeyPair()
Generate private and public keys.
ULLI_t m_e
encryption exponent: public key part II
SimpleEncrypter(const std::string &name="SimpleEncrypter")
Main constructor.
virtual ULLI_t genDecryptionExponent(ULLI_t phi, ULLI_t e) const
Find decryption exponent.
virtual float intBitsToFloat(ULLI_t val) const
Interpret bits of integer as floating point number.
ULLI_t m_d
decryption exponent: private key part II
virtual ~SimpleEncrypter()
Default destructor.
virtual std::pair< ULLI_t, ULLI_t > decodeKeyString(std::string str) const
Decode hex string to two integers.
long long int LLI_t
Useful typedefs.
virtual bool isPrime(ULLI_t n) const
Check for being a prime number.
virtual ULLI_t floatBitsToInt(float val) const
ICaloAffectedTool is abstract interface for tools checking if 4 mom is in calo affected region.