mirror of
https://git.wownero.com/wowlet/wownero-seed.git
synced 2024-08-15 01:03:29 +00:00
wowify
This commit is contained in:
parent
8d19ea23db
commit
3d70455534
17 changed files with 49 additions and 49 deletions
47
include/wownero_seed/galois_field.hpp
Normal file
47
include/wownero_seed/galois_field.hpp
Normal file
|
@ -0,0 +1,47 @@
|
|||
/*
|
||||
Copyright (c) 2020 tevador <tevador@gmail.com>
|
||||
All rights reserved.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <cassert>
|
||||
|
||||
typedef uint_least16_t gf_storage;
|
||||
typedef uint_fast16_t gf_item;
|
||||
|
||||
template<unsigned bits, gf_item primitive>
|
||||
class galois_field {
|
||||
public:
|
||||
galois_field();
|
||||
gf_item inverse(gf_item i) const {
|
||||
assert(i != 0);
|
||||
return i > 1 ? inv_table[i] : 1;
|
||||
}
|
||||
gf_item mult(gf_item a, gf_item b) const {
|
||||
if (b == 0 || a == 0)
|
||||
return 0;
|
||||
if (b == 1)
|
||||
return a;
|
||||
if (a == 1)
|
||||
return b;
|
||||
return exp_table[log_table[a] + log_table[b]];
|
||||
}
|
||||
gf_item exp(gf_item i) const {
|
||||
return exp_table[i];
|
||||
}
|
||||
static constexpr unsigned size() {
|
||||
return bits;
|
||||
}
|
||||
static constexpr unsigned elements() {
|
||||
return size_;
|
||||
}
|
||||
private:
|
||||
static_assert(bits <= 14, "field is too large");
|
||||
static constexpr gf_item size_ = 1u << bits;
|
||||
gf_storage log_table[size_];
|
||||
gf_storage exp_table[2 * size_];
|
||||
gf_storage inv_table[size_];
|
||||
};
|
||||
|
||||
using gf_2048 = galois_field<11, 2053>;
|
68
include/wownero_seed/gf_elem.hpp
Normal file
68
include/wownero_seed/gf_elem.hpp
Normal file
|
@ -0,0 +1,68 @@
|
|||
/*
|
||||
Copyright (c) 2020 tevador <tevador@gmail.com>
|
||||
All rights reserved.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "galois_field.hpp"
|
||||
|
||||
class gf_elem {
|
||||
public:
|
||||
static constexpr gf_item size() {
|
||||
return gf_2048::size();
|
||||
}
|
||||
constexpr gf_elem() : value_(0)
|
||||
{
|
||||
}
|
||||
constexpr gf_elem(gf_item value) : value_(value)
|
||||
{
|
||||
}
|
||||
gf_elem& operator+=(gf_elem x) {
|
||||
value_ ^= x.value_;
|
||||
return *this;
|
||||
}
|
||||
gf_elem& operator|=(gf_elem x) {
|
||||
value_ |= x.value_;
|
||||
return *this;
|
||||
}
|
||||
gf_elem& operator-=(gf_elem x) {
|
||||
value_ ^= x.value_;
|
||||
return *this;
|
||||
}
|
||||
gf_elem& operator*=(gf_elem x) {
|
||||
value_ = field.mult(value_, x.value_);
|
||||
return *this;
|
||||
}
|
||||
friend gf_elem operator+(gf_elem left, gf_elem right) {
|
||||
left += right;
|
||||
return left;
|
||||
}
|
||||
friend gf_elem operator-(gf_elem left, gf_elem right) {
|
||||
left -= right;
|
||||
return left;
|
||||
}
|
||||
friend gf_elem operator*(gf_elem left, gf_elem right) {
|
||||
left *= right;
|
||||
return left;
|
||||
}
|
||||
friend bool operator==(gf_elem lhs, gf_elem rhs) {
|
||||
return lhs.value_ == rhs.value_;
|
||||
}
|
||||
friend bool operator!=(gf_elem lhs, gf_elem rhs) {
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
gf_elem& inverse() {
|
||||
value_ = field.inverse(value_);
|
||||
return *this;
|
||||
}
|
||||
gf_elem& exp() {
|
||||
value_ = field.exp(value_);
|
||||
return *this;
|
||||
}
|
||||
gf_item value() const {
|
||||
return value_;
|
||||
}
|
||||
private:
|
||||
static const gf_2048 field;
|
||||
gf_item value_;
|
||||
};
|
57
include/wownero_seed/gf_poly.hpp
Normal file
57
include/wownero_seed/gf_poly.hpp
Normal file
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
Copyright (c) 2020 tevador <tevador@gmail.com>
|
||||
All rights reserved.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "gf_elem.hpp"
|
||||
#include <cassert>
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
|
||||
class gf_poly {
|
||||
public:
|
||||
static constexpr size_t max_degree = 13;
|
||||
gf_poly() : degree_(0) //zero polynomial
|
||||
{
|
||||
}
|
||||
gf_poly(gf_elem coeff, unsigned degree); //monomial
|
||||
gf_poly(unsigned degree) : gf_poly(1, degree)
|
||||
{
|
||||
}
|
||||
gf_poly(gf_elem coeff[], unsigned degree);
|
||||
unsigned degree() const {
|
||||
return degree_;
|
||||
}
|
||||
void set_degree();
|
||||
void set_degree(unsigned degree) {
|
||||
degree_ = degree;
|
||||
}
|
||||
bool is_zero() const {
|
||||
return degree_ == 0 && coeff_[0] == 0;
|
||||
}
|
||||
gf_elem operator[](unsigned i) const {
|
||||
return coeff_[i];
|
||||
}
|
||||
gf_elem& operator[](unsigned i) {
|
||||
return coeff_[i];
|
||||
}
|
||||
gf_elem operator()(gf_elem x) const; //evaluate at point x
|
||||
gf_poly& operator+=(const gf_poly& x);
|
||||
gf_poly& operator-=(const gf_poly& x);
|
||||
gf_poly& operator*=(gf_elem x);
|
||||
gf_poly& operator*=(const gf_poly& x);
|
||||
friend gf_poly operator*(const gf_poly& lhs, const gf_poly& rhs) {
|
||||
gf_poly result(lhs);
|
||||
return result *= rhs;
|
||||
}
|
||||
friend gf_poly operator+(const gf_poly& lhs, const gf_poly& rhs) {
|
||||
gf_poly result(lhs);
|
||||
return result += rhs;
|
||||
}
|
||||
static gf_poly div_rem(const gf_poly& nom, const gf_poly& x, gf_poly& rem);
|
||||
friend std::ostream& operator<<(std::ostream& os, const gf_poly& poly);
|
||||
private:
|
||||
gf_elem coeff_[2 * (max_degree + 1)] = { };
|
||||
unsigned degree_;
|
||||
};
|
17
include/wownero_seed/reed_solomon_code.hpp
Normal file
17
include/wownero_seed/reed_solomon_code.hpp
Normal file
|
@ -0,0 +1,17 @@
|
|||
/*
|
||||
Copyright (c) 2020 tevador <tevador@gmail.com>
|
||||
All rights reserved.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "gf_poly.hpp"
|
||||
|
||||
class reed_solomon_code {
|
||||
public:
|
||||
reed_solomon_code(unsigned check_digits);
|
||||
void encode(gf_poly& data) const;
|
||||
bool check(const gf_poly& message) const;
|
||||
private:
|
||||
gf_poly get_syndrome(const gf_poly& message) const;
|
||||
gf_poly generator;
|
||||
};
|
14
include/wownero_seed/secure_random.hpp
Normal file
14
include/wownero_seed/secure_random.hpp
Normal file
|
@ -0,0 +1,14 @@
|
|||
/*
|
||||
Copyright (c) 2020 tevador <tevador@gmail.com>
|
||||
All rights reserved.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
class secure_random {
|
||||
public:
|
||||
static void gen_bytes(void* output, size_t size);
|
||||
};
|
||||
|
25
include/wownero_seed/wordlist.hpp
Normal file
25
include/wownero_seed/wordlist.hpp
Normal file
|
@ -0,0 +1,25 @@
|
|||
/*
|
||||
Copyright (c) 2020 tevador <tevador@gmail.com>
|
||||
All rights reserved.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <assert.h>
|
||||
|
||||
class wordlist {
|
||||
public:
|
||||
static constexpr size_t size = 2048;
|
||||
static const wordlist english;
|
||||
const std::string& get_word(unsigned i) const {
|
||||
assert(i < size);
|
||||
return values_[i];
|
||||
}
|
||||
int parse(const std::string& word) const;
|
||||
private:
|
||||
wordlist(const std::string(&values)[size]) : values_(values)
|
||||
{
|
||||
}
|
||||
const std::string(&values_)[size];
|
||||
};
|
42
include/wownero_seed/wownero_seed.hpp
Normal file
42
include/wownero_seed/wownero_seed.hpp
Normal file
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
Copyright (c) 2020 tevador <tevador@gmail.com>
|
||||
All rights reserved.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#include <ctime>
|
||||
#include "gf_poly.hpp"
|
||||
|
||||
class wownero_seed {
|
||||
public:
|
||||
static const std::string erasure;
|
||||
static constexpr size_t size = 16;
|
||||
static constexpr size_t key_size = 32;
|
||||
using secret_key = std::array<uint8_t, key_size>;
|
||||
using secret_seed = std::array<uint8_t, size>;
|
||||
wownero_seed(const std::string& phrase, const std::string& coin);
|
||||
wownero_seed(std::time_t date_created, const std::string& coin);
|
||||
std::time_t date() const {
|
||||
return date_;
|
||||
}
|
||||
const std::string& correction() const {
|
||||
return correction_;
|
||||
}
|
||||
const secret_key& key() const {
|
||||
return key_;
|
||||
}
|
||||
friend std::ostream& operator<<(std::ostream& os, const wownero_seed& seed);
|
||||
private:
|
||||
secret_seed seed_;
|
||||
secret_key key_;
|
||||
std::time_t date_;
|
||||
unsigned reserved_;
|
||||
std::string correction_;
|
||||
gf_poly message_;
|
||||
};
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, const wownero_seed::secret_key& key);
|
Loading…
Add table
Add a link
Reference in a new issue