generator_test.hpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*
  2. * This file is part of ygg-brute
  3. * Copyright (c) 2020 ygg-brute authors
  4. * See LICENSE for licensing information
  5. */
  6. #pragma once
  7. #include <cstring>
  8. #include <cstdio>
  9. #include <stdexcept>
  10. #include <sodium.h>
  11. #include "address.hpp"
  12. #include "generator.hpp"
  13. #define DECLSPEC
  14. #include "generic/node_id.h"
  15. inline void run_generator_test(Generator& gen, GeneratorCtx& ctx, bool ensure_uniqueness = false)
  16. {
  17. char str[64]{0};
  18. gen.produce(ctx);
  19. for(auto i = 0; i < ctx.batch_size(); ++i) {
  20. if(i % 10000 == 0) printf("generator test: %d\n", i);
  21. uint8_t skey[32], pkey[32], node_id[64], ipv6[16];
  22. memcpy(skey, ctx.skey(i), 32);
  23. crypto_scalarmult_curve25519_base(pkey, skey);
  24. crypto_hash_sha512(node_id, pkey, 32);
  25. node_id_to_ipv6(node_id, ipv6);
  26. auto* gpu_ipv6 = ctx.address(i);
  27. if(memcmp(ipv6, gpu_ipv6, 16)) {
  28. ipv6_to_string(ipv6, str);
  29. printf("computed address: %s\n", str);
  30. ipv6_to_string(gpu_ipv6, str);
  31. printf("gpu address: %s\n", str);
  32. throw std::runtime_error{"generator test failed at " + std::to_string(i)};
  33. }
  34. }
  35. }