mirror of https://github.com/oxen-io/lokinet
awe yeh
parent
773eb21a71
commit
4718281202
@ -0,0 +1,37 @@
|
||||
#ifndef LLARP_CRYPTO_ASYNC_H_
|
||||
#define LLARP_CRYPTO_ASYNC_H_
|
||||
#include <llarp/crypto.h>
|
||||
#include <llarp/ev.h>
|
||||
#include <llarp/threadpool.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct llarp_async_dh;
|
||||
|
||||
struct llarp_async_dh * llarp_async_dh_new(struct llarp_crypto * crypto, struct llarp_ev_loop * ev, struct llarp_threadpool * tp);
|
||||
void llarp_async_dh_free(struct llarp_async_dh ** dh);
|
||||
|
||||
struct llarp_dh_result;
|
||||
|
||||
typedef void (*llarp_dh_complete_hook)(struct llarp_dh_result *);
|
||||
|
||||
|
||||
struct llarp_dh_internal;
|
||||
|
||||
struct llarp_dh_result
|
||||
{
|
||||
struct llarp_dh_internal * impl;
|
||||
llarp_sharedkey_t result;
|
||||
void * user;
|
||||
llarp_dh_complete_hook hook;
|
||||
};
|
||||
|
||||
void llarp_async_client_dh(struct llarp_async_dh * dh, llarp_seckey_t ourkey, llarp_pubkey_t theirkey, llarp_tunnel_nounce_t nounce, llarp_dh_complete_hook result, void * user);
|
||||
void llarp_async_server_dh(struct llarp_async_dh * dh, llarp_seckey_t ourkey, llarp_pubkey_t theirkey, llarp_tunnel_nounce_t nounce, llarp_dh_complete_hook result, void * user);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
@ -0,0 +1,85 @@
|
||||
#include <llarp/crypto_async.h>
|
||||
#include <llarp/mem.h>
|
||||
#include <string.h>
|
||||
|
||||
struct llarp_async_dh
|
||||
{
|
||||
llarp_dh_func client;
|
||||
llarp_dh_func server;
|
||||
struct llarp_threadpool * tp;
|
||||
struct llarp_ev_caller * caller;
|
||||
};
|
||||
|
||||
struct llarp_dh_internal
|
||||
{
|
||||
llarp_dh_func func;
|
||||
llarp_pubkey_t theirkey;
|
||||
llarp_seckey_t ourkey;
|
||||
llarp_tunnel_nounce_t nounce;
|
||||
struct llarp_dh_result result;
|
||||
};
|
||||
|
||||
static void llarp_crypto_dh_work(void * user)
|
||||
{
|
||||
struct llarp_dh_internal * impl = (struct llarp_dh_internal *)user;
|
||||
impl->func(&impl->result.result, impl->theirkey, impl->nounce, impl->ourkey);
|
||||
}
|
||||
|
||||
static void llarp_crypto_dh_result(struct llarp_ev_async_call * call)
|
||||
{
|
||||
struct llarp_dh_internal * impl = (struct llarp_dh_internal *) call->user;
|
||||
impl->result.hook(&impl->result);
|
||||
llarp_g_mem.free(impl);
|
||||
}
|
||||
|
||||
static void llarp_async_dh_exec(struct llarp_async_dh * dh, llarp_dh_func func, llarp_seckey_t ourkey, llarp_pubkey_t theirkey, llarp_tunnel_nounce_t nounce, llarp_dh_complete_hook result, void * user)
|
||||
{
|
||||
struct llarp_dh_internal * impl = llarp_g_mem.alloc(sizeof(struct llarp_dh_internal), 16);
|
||||
memcpy(impl->theirkey, theirkey, sizeof(llarp_pubkey_t));
|
||||
memcpy(impl->ourkey, ourkey, sizeof(llarp_seckey_t));
|
||||
memcpy(impl->nounce, nounce, sizeof(llarp_tunnel_nounce_t));
|
||||
impl->result.impl = impl;
|
||||
impl->result.user = user;
|
||||
impl->result.hook = result;
|
||||
impl->func = func;
|
||||
struct llarp_thread_job job = {
|
||||
.caller = dh->caller,
|
||||
.data = impl,
|
||||
.user = impl,
|
||||
.work = &llarp_crypto_dh_work
|
||||
};
|
||||
llarp_threadpool_queue_job(dh->tp, job);
|
||||
}
|
||||
|
||||
|
||||
void llarp_async_client_dh(struct llarp_async_dh * dh, llarp_seckey_t ourkey, llarp_pubkey_t theirkey, llarp_tunnel_nounce_t nounce, llarp_dh_complete_hook result, void * user)
|
||||
{
|
||||
llarp_async_dh_exec(dh, dh->client, ourkey, theirkey, nounce, result, user);
|
||||
}
|
||||
|
||||
void llarp_async_server_dh(struct llarp_async_dh * dh, llarp_seckey_t ourkey, llarp_pubkey_t theirkey, llarp_tunnel_nounce_t nounce, llarp_dh_complete_hook result, void * user)
|
||||
{
|
||||
llarp_async_dh_exec(dh, dh->server, ourkey, theirkey, nounce, result, user);
|
||||
}
|
||||
|
||||
|
||||
|
||||
struct llarp_async_dh * llarp_async_dh_new(struct llarp_crypto * crypto, struct llarp_ev_loop * ev, struct llarp_threadpool * tp)
|
||||
{
|
||||
struct llarp_async_dh * dh = llarp_g_mem.alloc(sizeof(struct llarp_async_dh), 16);
|
||||
dh->client = crypto->dh_client;
|
||||
dh->server = crypto->dh_server;
|
||||
dh->tp = tp;
|
||||
dh->caller = llarp_ev_prepare_async(ev, &llarp_crypto_dh_result);
|
||||
return dh;
|
||||
}
|
||||
|
||||
void llarp_async_dh_free(struct llarp_async_dh ** dh)
|
||||
{
|
||||
if(*dh)
|
||||
{
|
||||
llarp_ev_caller_stop((*dh)->caller);
|
||||
llarp_g_mem.free(*dh);
|
||||
*dh = NULL;
|
||||
}
|
||||
}
|
@ -0,0 +1,74 @@
|
||||
#include <llarp/crypto_async.h>
|
||||
#include <llarp/mem.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
struct dh_bench_main
|
||||
{
|
||||
size_t completed;
|
||||
size_t num;
|
||||
struct llarp_ev_loop * ev;
|
||||
struct llarp_async_dh * dh;
|
||||
};
|
||||
|
||||
static void handle_dh_complete(struct llarp_dh_result * res)
|
||||
{
|
||||
struct dh_bench_main * m = (struct dh_bench_main *) res->user;
|
||||
m->completed++;
|
||||
if(m->completed % 1000 == 0)
|
||||
printf("completed %ld\n", m->completed);
|
||||
if(m->completed == m->num)
|
||||
{
|
||||
printf("we done\n");
|
||||
llarp_ev_loop_stop(m->ev);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
struct dh_bench_main dh_main;
|
||||
struct llarp_crypto crypto;
|
||||
struct llarp_threadpool * tp;
|
||||
|
||||
llarp_mem_jemalloc();
|
||||
llarp_crypto_libsodium_init(&crypto);
|
||||
llarp_ev_loop_alloc(&dh_main.ev);
|
||||
|
||||
tp = llarp_init_threadpool(8);
|
||||
dh_main.dh = llarp_async_dh_new(&crypto, dh_main.ev, tp);
|
||||
llarp_threadpool_start(tp);
|
||||
|
||||
/* do work here */
|
||||
dh_main.num = 100000;
|
||||
dh_main.completed = 0;
|
||||
struct llarp_keypair ourkey;
|
||||
struct llarp_keypair theirkey;
|
||||
|
||||
crypto.keygen(&ourkey);
|
||||
crypto.keygen(&theirkey);
|
||||
|
||||
|
||||
llarp_tunnel_nounce_t nounce;
|
||||
llarp_buffer_t n_buff;
|
||||
n_buff.base = nounce;
|
||||
n_buff.cur = n_buff.base;
|
||||
n_buff.sz = sizeof(llarp_tunnel_nounce_t);
|
||||
|
||||
size_t sz = dh_main.num;
|
||||
printf("starting %ld dh jobs\n", sz);
|
||||
while(sz--)
|
||||
{
|
||||
crypto.randomize(n_buff);
|
||||
llarp_async_client_dh(dh_main.dh, ourkey.sec, theirkey.pub, nounce, handle_dh_complete, &dh_main);
|
||||
}
|
||||
printf("started %ld dh jobs\n", dh_main.num);
|
||||
llarp_ev_loop_run(dh_main.ev);
|
||||
|
||||
llarp_threadpool_join(tp);
|
||||
llarp_async_dh_free(&dh_main.dh);
|
||||
|
||||
llarp_ev_loop_free(&dh_main.ev);
|
||||
llarp_free_threadpool(&tp);
|
||||
printf("did %ld of %ld work\n", dh_main.completed, dh_main.num);
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
|
||||
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue