initialise a memory structure or smth
Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
This commit is contained in:
		
							
								
								
									
										39
									
								
								src/util/bits.hh
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								src/util/bits.hh
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,39 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include <concepts>
 | 
			
		||||
#include <limits>
 | 
			
		||||
 | 
			
		||||
using std::size_t;
 | 
			
		||||
 | 
			
		||||
template<std::integral Int>
 | 
			
		||||
inline bool
 | 
			
		||||
get_nth_bit(Int num, size_t n) {
 | 
			
		||||
    return (1 && (num >> n));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
template<std::integral Int>
 | 
			
		||||
inline void
 | 
			
		||||
set_nth_bit(Int& num, size_t n) {
 | 
			
		||||
    num |= (1 << n);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
template<std::integral Int>
 | 
			
		||||
inline void
 | 
			
		||||
rst_nth_bit(Int& num, size_t n) {
 | 
			
		||||
    num &= ~(1 << n);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
template<std::integral Int>
 | 
			
		||||
inline void
 | 
			
		||||
chg_nth_bit(Int& num, size_t n, bool x) {
 | 
			
		||||
    num ^= (num ^ -x) & 1 << n;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// read range of bits from start to end inclusive
 | 
			
		||||
template<std::unsigned_integral Int>
 | 
			
		||||
inline Int
 | 
			
		||||
get_bit_range(Int& num, size_t start, size_t end) {
 | 
			
		||||
    // NOTE: we do not require -1 if it is a signed integral (which it is not)
 | 
			
		||||
    Int left = std::numeric_limits<Int>::digits - 1 - end;
 | 
			
		||||
    return num << left >> (left + start);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										49
									
								
								src/util/log.cc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										49
									
								
								src/util/log.cc
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,49 @@
 | 
			
		||||
#include "log.hh"
 | 
			
		||||
 | 
			
		||||
namespace logging {
 | 
			
		||||
 | 
			
		||||
namespace ansi {
 | 
			
		||||
static const char* RED     = "\033[31m";
 | 
			
		||||
static const char* YELLOW  = "\033[33m";
 | 
			
		||||
static const char* MAGENTA = "\033[35m";
 | 
			
		||||
static const char* WHITE   = "\033[37m";
 | 
			
		||||
static const char* BOLD    = "\033[1m";
 | 
			
		||||
static const char* RESET   = "\033[0m";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
Logger::Logger(std::ostream& os)
 | 
			
		||||
  : os(os){};
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
Logger::set_level(Level level) {
 | 
			
		||||
    switch (level) {
 | 
			
		||||
        case Level::Debug:
 | 
			
		||||
            os << ansi::MAGENTA << ansi::BOLD << "[DEBUG] ";
 | 
			
		||||
            break;
 | 
			
		||||
 | 
			
		||||
        case Level::Info:
 | 
			
		||||
            os << ansi::WHITE << "[INFO] ";
 | 
			
		||||
            break;
 | 
			
		||||
 | 
			
		||||
        case Level::Warn:
 | 
			
		||||
            os << ansi::YELLOW << "[WARN] ";
 | 
			
		||||
            break;
 | 
			
		||||
 | 
			
		||||
        case Level::Error:
 | 
			
		||||
            os << ansi::RED << ansi::BOLD << "[ERROR] ";
 | 
			
		||||
            break;
 | 
			
		||||
 | 
			
		||||
            // unreachable
 | 
			
		||||
        default: {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
Logger::reset_level() {
 | 
			
		||||
    os << ansi::RESET;
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Global logger
 | 
			
		||||
logging::Logger logger;
 | 
			
		||||
							
								
								
									
										42
									
								
								src/util/log.hh
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								src/util/log.hh
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,42 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include <iomanip>
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <source_location>
 | 
			
		||||
#include <streambuf>
 | 
			
		||||
 | 
			
		||||
namespace logging {
 | 
			
		||||
enum class Level {
 | 
			
		||||
    Debug,
 | 
			
		||||
    Info,
 | 
			
		||||
    Warn,
 | 
			
		||||
    Error,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class Logger {
 | 
			
		||||
  public:
 | 
			
		||||
    Logger(std::ostream& os = std::clog);
 | 
			
		||||
 | 
			
		||||
    template<typename... Args>
 | 
			
		||||
    void print(Level level, Args&&... args) {
 | 
			
		||||
        set_level(level);
 | 
			
		||||
        (os << ... << args);
 | 
			
		||||
        reset_level();
 | 
			
		||||
        os << std::endl;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
  private:
 | 
			
		||||
    std::ostream& os;
 | 
			
		||||
    void set_level(Level level);
 | 
			
		||||
    void reset_level();
 | 
			
		||||
};
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
extern logging::Logger logger;
 | 
			
		||||
 | 
			
		||||
#define log_debug(...) logger.print(logging::Level::Debug, __VA_ARGS__)
 | 
			
		||||
#define log_info(...) logger.print(logging::Level::Info, __VA_ARGS__)
 | 
			
		||||
#define log_warn(...) logger.print(logging::Level::Warn, __VA_ARGS__)
 | 
			
		||||
#define log_error(...) logger.print(logging::Level::Error, __VA_ARGS__)
 | 
			
		||||
#define log(...) log_info(__VA_ARGS__)
 | 
			
		||||
#define debug(value) log_debug(#value, " = ", value)
 | 
			
		||||
							
								
								
									
										4
									
								
								src/util/meson.build
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								src/util/meson.build
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,4 @@
 | 
			
		||||
lib_sources += files(
 | 
			
		||||
  'log.cc',
 | 
			
		||||
  'utils.cc'
 | 
			
		||||
)
 | 
			
		||||
							
								
								
									
										113
									
								
								src/util/utils.cc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										113
									
								
								src/util/utils.cc
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,113 @@
 | 
			
		||||
#include "utils.hh"
 | 
			
		||||
#include <bit>
 | 
			
		||||
#include <iomanip>
 | 
			
		||||
#include <sstream>
 | 
			
		||||
#include <vector>
 | 
			
		||||
 | 
			
		||||
namespace crypto {
 | 
			
		||||
 | 
			
		||||
using std::rotr;
 | 
			
		||||
 | 
			
		||||
std::string
 | 
			
		||||
sha256(const uint8_t* data, const size_t size) {
 | 
			
		||||
    // Assuming 1 byte = 8 bits
 | 
			
		||||
    std::stringstream string;
 | 
			
		||||
    size_t k = 512 - (size * 8 + 65) % 512;
 | 
			
		||||
    size_t L = size + (65 + k) / 8;
 | 
			
		||||
    size_t i, j;
 | 
			
		||||
    bool c = 0;
 | 
			
		||||
 | 
			
		||||
    static constexpr uint32_t K[64] = {
 | 
			
		||||
        0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
 | 
			
		||||
        0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
 | 
			
		||||
        0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
 | 
			
		||||
        0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
 | 
			
		||||
        0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
 | 
			
		||||
        0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
 | 
			
		||||
        0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
 | 
			
		||||
        0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
 | 
			
		||||
        0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
 | 
			
		||||
        0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
 | 
			
		||||
        0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    uint32_t h[8] = { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
 | 
			
		||||
                      0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 };
 | 
			
		||||
 | 
			
		||||
    for (i = 0; i < L; i += 64) {
 | 
			
		||||
        size_t n = (size > i ? size - i : 0);
 | 
			
		||||
        uint32_t h0[8];
 | 
			
		||||
        uint32_t w[64] = { 0 };
 | 
			
		||||
 | 
			
		||||
        if (n == 64)
 | 
			
		||||
            c = 1;
 | 
			
		||||
 | 
			
		||||
        if (n >= 64) {
 | 
			
		||||
            for (j = 0; j < 16; j++) {
 | 
			
		||||
                w[j] = data[i + j * 4] << 24 | data[i + j * 4 + 1] << 16 |
 | 
			
		||||
                       data[i + j * 4 + 2] << 8 | data[i + j * 4 + 3];
 | 
			
		||||
            }
 | 
			
		||||
        } else {
 | 
			
		||||
            uint8_t cur[64] = { 0 };
 | 
			
		||||
 | 
			
		||||
            if (n) {
 | 
			
		||||
                std::copy(data + i, data + size, cur);
 | 
			
		||||
                cur[n] = 0x80;
 | 
			
		||||
            } else if (c) {
 | 
			
		||||
                cur[n] = 0x80;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (n < 54) {
 | 
			
		||||
                for (j = 56; j < 64; j++) {
 | 
			
		||||
                    cur[j] = (size * 8 >> ((63 - j) * 8)) & 0xFF;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            for (j = 0; j < 16; j++) {
 | 
			
		||||
                w[j] = cur[j * 4] << 24 | cur[j * 4 + 1] << 16 |
 | 
			
		||||
                       cur[j * 4 + 2] << 8 | cur[j * 4 + 3];
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        for (j = 16; j < 64; j++) {
 | 
			
		||||
            uint32_t s0 =
 | 
			
		||||
              rotr(w[j - 15], 7) ^ rotr(w[j - 15], 18) ^ (w[j - 15] >> 3);
 | 
			
		||||
            uint32_t s1 =
 | 
			
		||||
              rotr(w[j - 2], 17) ^ rotr(w[j - 2], 19) ^ (w[j - 2] >> 10);
 | 
			
		||||
 | 
			
		||||
            w[j] = w[j - 16] + w[j - 7] + s0 + s1;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        std::copy(h, h + 8, h0);
 | 
			
		||||
 | 
			
		||||
        for (j = 0; j < 64; j++) {
 | 
			
		||||
            uint32_t s1  = rotr(h0[4], 6) ^ rotr(h0[4], 11) ^ rotr(h0[4], 25);
 | 
			
		||||
            uint32_t ch  = (h0[4] & h0[5]) ^ (~h0[4] & h0[6]);
 | 
			
		||||
            uint32_t t1  = h0[7] + s1 + ch + K[j] + w[j];
 | 
			
		||||
            uint32_t s0  = rotr(h0[0], 2) ^ rotr(h0[0], 13) ^ rotr(h0[0], 22);
 | 
			
		||||
            uint32_t maj = (h0[0] & h0[1]) ^ (h0[0] & h0[2]) ^ (h0[1] & h0[2]);
 | 
			
		||||
            uint32_t t2  = s0 + maj;
 | 
			
		||||
 | 
			
		||||
            h0[7] = h0[6];
 | 
			
		||||
            h0[6] = h0[5];
 | 
			
		||||
            h0[5] = h0[4];
 | 
			
		||||
            h0[4] = h0[3] + t1;
 | 
			
		||||
            h0[3] = h0[2];
 | 
			
		||||
            h0[2] = h0[1];
 | 
			
		||||
            h0[1] = h0[0];
 | 
			
		||||
            h0[0] = t1 + t2;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        for (j = 0; j < 8; j++)
 | 
			
		||||
            h[j] += h0[j];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    string << std::hex << std::setfill('0');
 | 
			
		||||
 | 
			
		||||
    for (j = 0; j < 8; j++)
 | 
			
		||||
        for (i = 0; i < 4; i++)
 | 
			
		||||
            string << std::setw(2) << ((h[j] >> (24 - i * 8)) & 0xFF);
 | 
			
		||||
 | 
			
		||||
    return string.str();
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										10
									
								
								src/util/utils.hh
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								src/util/utils.hh
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,10 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include <string>
 | 
			
		||||
 | 
			
		||||
// Why I wrote this myself? I do not know
 | 
			
		||||
// I will off myself 😹😹😹😹
 | 
			
		||||
namespace crypto {
 | 
			
		||||
std::string
 | 
			
		||||
sha256(const uint8_t* data, const size_t size);
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user