Use const values for our traits where we can

This commit is contained in:
Luke Parker
2022-06-03 23:22:08 -04:00
parent b83ca7d666
commit 3617ed4eb7
12 changed files with 72 additions and 113 deletions

View File

@@ -58,7 +58,7 @@ impl Monero {
pub fn new(url: String) -> Monero {
Monero {
rpc: Rpc::new(url),
view: dfg::Scalar::from_hash(view_key::<Monero>(0)).0
view: *view_key::<Monero>(0)
}
}
}
@@ -73,16 +73,16 @@ impl Coin for Monero {
type Address = Address;
fn id() -> &'static [u8] { b"Monero" }
fn confirmations() -> usize { 10 }
const ID: &'static [u8] = b"Monero";
const CONFIRMATIONS: usize = 10;
// Testnet TX bb4d188a4c571f2f0de70dca9d475abc19078c10ffa8def26dd4f63ce1bcfd79 uses 146 inputs
// while using less than 100kb of space, albeit with just 2 outputs (though outputs share a BP)
// The TX size limit is half the contextual median block weight, where said weight is >= 300,000
// This means any TX which fits into 150kb will be accepted by Monero
// 128, even with 16 outputs, should fit into 100kb. Further efficiency by 192 may be viable
// TODO: Get hard numbers and tune
fn max_inputs() -> usize { 128 }
fn max_outputs() -> usize { 16 }
const MAX_INPUTS: usize = 128;
const MAX_OUTPUTS: usize = 16;
async fn get_height(&self) -> Result<usize, CoinError> {
self.rpc.get_height().await.map_err(|_| CoinError::ConnectionError)

View File

@@ -4,8 +4,6 @@ use async_trait::async_trait;
use thiserror::Error;
use rand_core::{RngCore, CryptoRng};
use blake2::{digest::{Digest, Update}, Blake2b512};
use frost::{Curve, MultisigKeys};
mod coins;
@@ -40,10 +38,10 @@ pub trait Coin {
type Address: Send;
fn id() -> &'static [u8];
fn confirmations() -> usize;
fn max_inputs() -> usize;
fn max_outputs() -> usize;
const ID: &'static [u8];
const CONFIRMATIONS: usize;
const MAX_INPUTS: usize;
const MAX_OUTPUTS: usize;
async fn get_height(&self) -> Result<usize, CoinError>;
async fn get_block(&self, height: usize) -> Result<Self::Block, CoinError>;
@@ -70,11 +68,9 @@ pub trait Coin {
) -> Result<(Vec<u8>, Vec<<Self::Output as Output>::Id>), CoinError>;
}
// Generate a view key for a given chain in a globally consistent manner regardless of the current
// group key
// Generate a static view key for a given chain in a globally consistent manner
// Doesn't consider the current group key to increase the simplicity of verifying Serai's status
// Takes an index, k, for more modern privacy protocols which use multiple view keys
// Doesn't run Curve::hash_to_F, instead returning the hash object, due to hash_to_F being a FROST
// definition instead of a wide reduction from a hash object
pub fn view_key<C: Coin>(k: u64) -> Blake2b512 {
Blake2b512::new().chain(b"Serai DEX View Key").chain(C::id()).chain(k.to_le_bytes())
pub fn view_key<C: Coin>(k: u64) -> <C::Curve as Curve>::F {
C::Curve::hash_to_F(b"Serai DEX View Key", &[C::ID, &k.to_le_bytes()].concat())
}

View File

@@ -16,7 +16,7 @@ impl<C: Curve> WalletKeys<C> {
}
// Bind this key to a specific network by applying an additive offset
// While it would be fine to just C::id(), including the group key creates distinct
// While it would be fine to just C::ID, including the group key creates distinct
// offsets instead of static offsets. Under a statically offset system, a BTC key could
// have X subtracted to find the potential group key, and then have Y added to find the
// potential ETH group key. While this shouldn't be an issue, as this isn't a private
@@ -27,7 +27,7 @@ impl<C: Curve> WalletKeys<C> {
const DST: &[u8] = b"Serai Processor Wallet Chain Bind";
let mut transcript = DigestTranscript::<blake2::Blake2b512>::new(DST);
transcript.append_message(b"chain", chain);
transcript.append_message(b"curve", C::id());
transcript.append_message(b"curve", C::ID);
transcript.append_message(b"group_key", &C::G_to_bytes(&self.keys.group_key()));
self.keys.offset(C::hash_to_F(DST, &transcript.challenge(b"offset")))
}
@@ -73,11 +73,11 @@ impl<C: Coin> Wallet<C> {
pub fn add_keys(&mut self, keys: &WalletKeys<C::Curve>) {
// Doesn't use +1 as this is height, not block index, and poll moves by block index
self.pending.push((self.acknowledged_height(keys.creation_height), keys.bind(C::id())));
self.pending.push((self.acknowledged_height(keys.creation_height), keys.bind(C::ID)));
}
pub async fn poll(&mut self) -> Result<(), CoinError> {
let confirmed_height = self.coin.get_height().await? - C::confirmations();
let confirmed_height = self.coin.get_height().await? - C::CONFIRMATIONS;
for height in self.scanned_height() .. confirmed_height {
// If any keys activated at this height, shift them over
{
@@ -114,7 +114,7 @@ impl<C: Coin> Wallet<C> {
let acknowledged_height = self.acknowledged_height(canonical);
// TODO: Log schedule outputs when max_outputs is low
// TODO: Log schedule outputs when MAX_OUTPUTS is low
// Payments is the first set of TXs in the schedule
// As each payment re-appears, let mut payments = schedule[payment] where the only input is
// the source payment
@@ -129,7 +129,7 @@ impl<C: Coin> Wallet<C> {
while outputs.len() != 0 {
// Select the maximum amount of outputs possible
let mut inputs = &outputs[0 .. C::max_inputs().min(outputs.len())];
let mut inputs = &outputs[0 .. C::MAX_INPUTS.min(outputs.len())];
// Calculate their sum value, minus the fee needed to spend them
let mut sum = inputs.iter().map(|input| input.amount()).sum::<u64>();