fluence-VRF/bls/main.cpp

111 lines
2.0 KiB
C++
Raw Normal View History

2019-04-13 20:07:06 +03:00
#include <sdk.h>
#include <stdlib.h>
#include <bls/bls.hpp>
#include <string>
#define UNUSED(x) (void)(x)
void* memset(void *ptr, int a, size_t b) {
char* dest = (char*) ptr;
for (size_t t = 0; t < b; ++t) {
*(dest + t) = a;
}
2019-04-13 20:34:49 +03:00
return dest;
2019-04-13 20:07:06 +03:00
}
void init() {
static bool inited = false;
if (inited)
return;
bls::init();
}
extern "C" {
char *invoke(const char *str, int length) {
init();
// bls::Signature s;
bls::PublicKey pk;
pk.setStr(std::string(str));
std::string string;
pk.getStr(string);
return (char*)string.data();
}
2019-04-13 20:28:57 +03:00
static int game_id_autoinc = 0;
struct pub_key_type {
char *data;
int len;
};
struct Game {
bool new_game(int n) {
len = 0;
players = (char**)malloc(sizeof(char*) * n);
stakes = (int*)malloc(sizeof(int) * n);
game_id = game_id_autoinc++;
2019-04-13 20:34:49 +03:00
state = STATE_GATHERING_STAKES;
2019-04-13 20:28:57 +03:00
return true;
}
bool add_stake(int stake, pub_key_type pub_key) {
2019-04-13 20:34:49 +03:00
if (state != STATE_GATHERING_STAKES)
return false;
2019-04-13 20:28:57 +03:00
stakes[len] = stake;
players[len] = pub_key.data;
++len;
}
int get_stake(pub_key_type pub_key) {
2019-04-13 20:34:49 +03:00
if (state == STATE_NOT_STARTED)
return 0;
2019-04-13 20:28:57 +03:00
for (int i = 0; i < len; i++) {
if (compare(players[i], pub_key.data, pub_key.len))
return stakes[i];
}
return 0;
}
bool compare(char *a, char *b, int len) {
for (int i = 0; i < len; i++) {
if (a[i] != b[i])
return false;
}
return true;
}
int get_game_id() {
return game_id;
}
char** get_participants() {
2019-04-13 20:34:49 +03:00
if (state == STATE_NOT_STARTED)
return 0;
2019-04-13 20:28:57 +03:00
return players;
}
private:
2019-04-13 20:34:49 +03:00
static const int STATE_NOT_STARTED = 0;
static const int STATE_GATHERING_STAKES = 1;
static const int STATE_REVEALING = 2;
int state;
2019-04-13 20:28:57 +03:00
int *stakes;
char **players;
int len;
int game_id;
};
2019-04-13 20:07:06 +03:00
}