47 srand(
static_cast<unsigned>(time(0)));
63 std::pair<std::string, std::string> keys =
64 std::make_pair(
"__NO_PRIV_KEY__",
"__NO_PUB_KEY__");
82 if (
m_n > 0 &&
m_n != keys.first ) {
94 if (
m_n > 0 &&
m_n != keys.second ) {
154 <<
a << Form(
" (%a) !",
a));
167 if (
a > 0. || std::isnan(
a) ) {
175 <<
a << Form(
" (%a) !",
a));
201 while ( p == q || dlog2 < 0.1 || dlog2 > 30. ) {
204 dlog2 = fabs(log2(p)-log2(q));
232 for (
LLI_t i = 2; i < (
LLI_t)sqrt(n) + 1; ++i) {
247 std::vector<LLI_t>
r;
249 r.push_back(std::max(n1, n2));
250 r.push_back(std::min(n1, n2));
253 r.push_back(
r[i-2] %
r[i-1]);
277 if ( ((
phi * i + 1) %
e) == 0 ) {
289 unsigned int ra = (
unsigned int)(log(
a)/log(16.))+1;
290 unsigned int rb = (
unsigned int)(log(b)/log(16.))+1;
299 TString tstr = Form(
"%02x%02x%02x%0*x%0*llx%0*x%0*llx",
304 return std::string(tstr.Data());
309 std::pair<SimpleEncrypter::ULLI_t, SimpleEncrypter::ULLI_t>
312 std::pair<ULLI_t, ULLI_t> keys(0,0);
315 if (
str.IsHex() &&
str.Length() > 3) {
317 unsigned int ndigits = strtoul(TString(
str(0,2)).
Data(),
nullptr, 16);
318 unsigned int ra = strtoul(TString(
str(2,2)).
Data(),
nullptr, 16);
319 unsigned int rb = strtoul(TString(
str(4,2)).
Data(),
nullptr, 16);
320 if (
str.Length() == (
int)(2*ndigits + 6) ) {
321 keys.first = strtoll(TString(
str(ndigits+6-ra, ra)).
Data(),
323 keys.second = strtoll(TString(
str(2*ndigits+6-rb, rb)).
Data(),
326 ATH_MSG_ERROR(
"Private/public key must be a hex string of " <<
330 ATH_MSG_ERROR(
"Private/public key must be a hex string of " <<
347 if (
sizeof(
float) <=
sizeof(
ULLI_t) ) {
349 if (
sizeof(
float) ==
sizeof(
int) ) {
358 char* pval =
reinterpret_cast<char*
>(&val);
360 for (
unsigned int i=0; i<
sizeof(
float); ++i) {
362 for (
unsigned int j=0; j<CHAR_BIT; ++j) {
363 unsigned int n = i*CHAR_BIT + j;
364 unsigned int bit = (*(pval+i) >> j) & 1;
365 if ( bit > 0 )
res |= 1 << n;
371 <<
sizeof(
float) <<
" > " <<
sizeof(
LLI_t));
385 unsigned int r = (
int)(std::log2(val))+1;
388 if (
sizeof(
float)*CHAR_BIT >=
r ) {
390 if (
sizeof(
float) ==
sizeof(
int) ) {
399 char* pres =
reinterpret_cast<char*
>(&
res);
401 for (
unsigned int i=0; i<
sizeof(
float); ++i) {
403 for (
unsigned int j=0; j<CHAR_BIT; ++j) {
404 unsigned int n = i*CHAR_BIT + j;
405 unsigned int bit = (val >> n) & 1;
406 if ( bit > 0 ) *(pres+i) |= 1 << j;
412 <<
sizeof(
float)*CHAR_BIT <<
" < " <<
r);
431 }
while ( c > rmask );
450 }
while ( d > rmask );
482 bin[
r-i-1] = tmp % 2;
483 tmp = (
LLI_t)(tmp/2);
489 ULLI_t d2 = dec[i-1]*dec[i-1] % n;
490 if (
bin[i] > 0 ) d2 *=
a;
505 ATH_MSG_ERROR(
"Setup not OK for encryption: public key set?");
521 ATH_MSG_ERROR(
"Setup not OK for decryption: private key set?");
#define ATH_MSG_WARNING(x)
std::pair< std::vector< unsigned int >, bool > res
Provide simple asymmetric encryption for blinding of float values.
constexpr int pow(int base, int exp) noexcept
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.