1 #ifndef MTTOOLBOX_UTIL_HPP
2 #define MTTOOLBOX_UTIL_HPP
34 #include <openssl/sha.h>
65 return static_cast<int>(
sizeof(T) * 8);
103 return 2 * floor2p<T>(n / 2);
129 bool breakline =
true) {
132 os <<
"0deg=-1" << std::endl;
135 for(
int i = 0; i <= deg(poly); i++) {
136 if(rep(coeff(poly, i)) == 1) {
141 if (breakline && ((i % 32) == 31)) {
145 os <<
"deg=" << deg(poly) << std::endl;
173 std::cout <<
"get_range:" << start <<
", " << end << std::endl;
176 return input % (end - start + 1) + start;
198 for(
int i = 1; i < size; i++) {
199 for(
int j = 1, k = 0; j <= i; j <<= 1, k++) {
201 dist_tbl[i] ^= src_tbl[k];
232 inline static void poly_sha1(std::string& str,
const NTL::GF2X& poly) {
238 SHA1_Update(&ctx,
"-1", 2);
240 for(
int i = 0; i <= deg(poly); i++) {
241 if(rep(coeff(poly, i)) == 1) {
242 SHA1_Update(&ctx,
"1", 1);
244 SHA1_Update(&ctx,
"0", 1);
247 unsigned char md[SHA_DIGEST_LENGTH];
248 SHA1_Final(md, &ctx);
250 for (
int i = 0; i < SHA_DIGEST_LENGTH; i++) {
251 ss << setfill(
'0') << setw(2) << hex
252 <<
static_cast<int>(md[i]);
284 int16_t y = (int16_t)x;
318 int32_t y = (int32_t)x;
352 int64_t y = (int64_t)x;
378 x -= (x >> 1) & UINT16_C(0x5555);
379 x = ((x >> 2) & UINT16_C(0x3333)) + (x & UINT16_C(0x3333));
380 x = ((x >> 4) + x) & UINT16_C(0x0f0f);
382 return (
int)(x & 0x1f);
399 x -= (x >> 1) & UINT32_C(0x55555555);
400 x = ((x >> 2) & UINT32_C(0x33333333)) + (x & UINT32_C(0x33333333));
401 x = ((x >> 4) + x) & UINT32_C(0x0f0f0f0f);
404 return (
int)(x & 0x3f);
421 x -= (x >> 1) & UINT64_C(0x5555555555555555);
422 x = ((x >> 2) & UINT64_C(0x3333333333333333))
423 + (x & UINT64_C(0x3333333333333333));
424 x = ((x >> 4) + x) & UINT64_C(0x0f0f0f0f0f0f0f0f);
428 return (
int)(x & 0x7f);
455 uint32_t y = 0x55555555;
456 x = (((x >> 1) & y) | ((x & y) << 1));
458 x = (((x >> 2) & y) | ((x & y) << 2));
460 x = (((x >> 4) & y) | ((x & y) << 4));
462 x = (((x >> 8) & y) | ((x & y) << 8));
463 return((x >> 16) | (x << 16));
481 uint64_t y = UINT64_C(0x5555555555555555);
482 x = (((x >> 1) & y) | ((x & y) << 1));
483 y = UINT64_C(0x3333333333333333);
484 x = (((x >> 2) & y) | ((x & y) << 2));
485 y = UINT64_C(0x0f0f0f0f0f0f0f0f);
486 x = (((x >> 4) & y) | ((x & y) << 4));
487 y = UINT64_C(0x00ff00ff00ff00ff);
488 x = (((x >> 8) & y) | ((x & y) << 8));
489 y = UINT64_C(0x0000ffff0000ffff);
490 x = (((x >> 16) & y) | ((x & y) << 16));
491 return((x >> 32) | (x << 32));
509 inline static void LCM(NTL::GF2X& lcm,
const NTL::GF2X& x,
510 const NTL::GF2X& y) {
536 inline static void toGF2Vec(NTL::vec_GF2& result, U value) {
538 int bitSize = bit_size<U>();
539 result.SetLength(bitSize);
540 mask = mask << (bitSize - 1);
541 for (
int i = 0; i < bitSize; i++) {
568 return static_cast<U
>(1);
610 return (bits >> pos) & 1;
634 U mask = ~(
static_cast<U
>(1) << pos);
636 *bits |=
static_cast<U
>(b) << pos;
660 int bitSize = bit_size<U>();
661 int pos = bitSize - 1;
663 for (
int i = 0; i < bitSize; i++) {
664 if (!IsZero(value[i])) {
713 template<
typename U,
typename V>
715 return static_cast<U
>(x);
718 #endif //MTTOOLBOX_UTIL_HPP