Skip to content

Instantly share code, notes, and snippets.

@rHermes
Created November 15, 2021 21:55
Show Gist options
  • Save rHermes/2c45859f4f0febc183a184d658f64db9 to your computer and use it in GitHub Desktop.
Save rHermes/2c45859f4f0febc183a184d658f64db9 to your computer and use it in GitHub Desktop.
Doing advent of code 2015 day 4 with https://github.com/intel/isa-l_crypto to try t oget it as fast as possibe
#include <string_view>
#include <cstdio>
#include <cstdlib>
#include <fmt/core.h>
#include <fmt/format.h>
#include <md5_mb.h>
#include <endian_helper.h>
#define DIGEST_NWORDS MD5_DIGEST_NWORDS
#define MB_BUFS MD5_MAX_LANES
#define HASH_CTX_MGR MD5_HASH_CTX_MGR
#define HASH_CTX MD5_HASH_CTX
#define CTX_MGR_INIT md5_ctx_mgr_init_avx2
#define CTX_MGR_SUBMIT md5_ctx_mgr_submit_avx2
#define CTX_MGR_FLUSH md5_ctx_mgr_flush_avx2
#define rounds_buf MD5_MAX_LANES
int main() {
HASH_CTX *ctx = nullptr;
HASH_CTX_MGR* mgr = static_cast<HASH_CTX_MGR*>(std::aligned_alloc(16, sizeof *mgr));
CTX_MGR_INIT(mgr);
HASH_CTX ctxpool[16];
for (int i = 0; i < 16; i++) {
hash_ctx_init(&ctxpool[i]);
ctxpool[i].user_data = (void *)((uint64_t) i);
}
std::string base = "iwrupvqb";
// We calculate 16 md5hashes at a time.
for (int i = 0; i < 16; i++) {
auto data = base + fmt::to_string(i+1);
ctx = CTX_MGR_SUBMIT(mgr, &ctxpool[i], data.data(), data.size(), HASH_ENTIRE);
}
int idx = 16;
bool found_first = false;
while (ctx) {
if (ctx->error != HASH_CTX_ERROR_NONE) {
fmt::print("WE GOT A FUCKING ERROR\n");
return -1;
}
auto dg = ctx->job.result_digest[0];
if (!found_first && ((to_be32(dg) & 0xFFFFF000) == 0)) {
found_first = true;
fmt::print("Part 1: {:d}\t0x", (uint64_t)ctx->user_data + 1, to_be32(dg));
for (int i = 0; i < DIGEST_NWORDS; i++) {
fmt::print("{:08x}", to_be32(ctx->job.result_digest[i]));
}
fmt::print("\n");
}
if ((dg & 0xFFFFFF) == 0) {
fmt::print("Part 2: {:d}\t0x", (uint64_t)ctx->user_data + 1);
for (int i = 0; i < DIGEST_NWORDS; i++) {
fmt::print("{:08x}", to_be32(ctx->job.result_digest[i]));
}
fmt::print("\n");
break;
}
auto data = base + fmt::to_string(idx+1);
ctx->user_data = (void *)((uint64_t) idx);
ctx = CTX_MGR_SUBMIT(mgr, ctx, data.data(), data.size(), HASH_ENTIRE);
idx++;
}
std::free(mgr);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment