mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-09 20:59:23 +00:00
Remove monero-rs types
Still missing an updated RPC file. Restructures the library as it makes sense
This commit is contained in:
@@ -1,132 +0,0 @@
|
||||
#[cfg(feature = "multisig")]
|
||||
use std::{rc::Rc, cell::RefCell};
|
||||
|
||||
use rand::{RngCore, rngs::OsRng};
|
||||
|
||||
use curve25519_dalek::{constants::ED25519_BASEPOINT_TABLE, scalar::Scalar};
|
||||
|
||||
use monero::VarInt;
|
||||
|
||||
use monero_serai::{Commitment, random_scalar, generate_key_image, transaction::decoys::Decoys, clsag};
|
||||
#[cfg(feature = "multisig")]
|
||||
use monero_serai::frost::{MultisigError, Transcript};
|
||||
|
||||
#[cfg(feature = "multisig")]
|
||||
mod frost;
|
||||
#[cfg(feature = "multisig")]
|
||||
use crate::frost::{THRESHOLD, generate_keys, sign};
|
||||
|
||||
const RING_LEN: u64 = 11;
|
||||
const AMOUNT: u64 = 1337;
|
||||
|
||||
#[cfg(feature = "multisig")]
|
||||
const RING_INDEX: u8 = 3;
|
||||
|
||||
#[test]
|
||||
fn clsag() {
|
||||
for real in 0 .. RING_LEN {
|
||||
let msg = [1; 32];
|
||||
|
||||
let mut secrets = [Scalar::zero(), Scalar::zero()];
|
||||
let mut ring = vec![];
|
||||
for i in 0 .. RING_LEN {
|
||||
let dest = random_scalar(&mut OsRng);
|
||||
let mask = random_scalar(&mut OsRng);
|
||||
let amount;
|
||||
if i == u64::from(real) {
|
||||
secrets = [dest, mask];
|
||||
amount = AMOUNT;
|
||||
} else {
|
||||
amount = OsRng.next_u64();
|
||||
}
|
||||
ring.push([&dest * &ED25519_BASEPOINT_TABLE, Commitment::new(mask, amount).calculate()]);
|
||||
}
|
||||
|
||||
let image = generate_key_image(&secrets[0]);
|
||||
let (clsag, pseudo_out) = clsag::sign(
|
||||
&mut OsRng,
|
||||
&vec![(
|
||||
secrets[0],
|
||||
image,
|
||||
clsag::Input::new(
|
||||
Commitment::new(secrets[1], AMOUNT),
|
||||
Decoys {
|
||||
i: u8::try_from(real).unwrap(),
|
||||
offsets: (1 ..= RING_LEN).into_iter().map(|o| VarInt(o)).collect(),
|
||||
ring: ring.clone()
|
||||
}
|
||||
).unwrap()
|
||||
)],
|
||||
random_scalar(&mut OsRng),
|
||||
msg
|
||||
).unwrap().swap_remove(0);
|
||||
clsag::verify(&clsag, &ring, &image, &pseudo_out, &msg).unwrap();
|
||||
#[cfg(feature = "experimental")]
|
||||
clsag::rust_verify(&clsag, &ring, &image, &pseudo_out, &msg).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "multisig")]
|
||||
#[test]
|
||||
fn clsag_multisig() -> Result<(), MultisigError> {
|
||||
let (keys, group_private) = generate_keys();
|
||||
let t = keys[0].params().t();
|
||||
|
||||
let randomness = random_scalar(&mut OsRng);
|
||||
let mut ring = vec![];
|
||||
for i in 0 .. RING_LEN {
|
||||
let dest;
|
||||
let mask;
|
||||
let amount;
|
||||
if i != u64::from(RING_INDEX) {
|
||||
dest = random_scalar(&mut OsRng);
|
||||
mask = random_scalar(&mut OsRng);
|
||||
amount = OsRng.next_u64();
|
||||
} else {
|
||||
dest = group_private.0;
|
||||
mask = randomness;
|
||||
amount = AMOUNT;
|
||||
}
|
||||
ring.push([&dest * &ED25519_BASEPOINT_TABLE, Commitment::new(mask, amount).calculate()]);
|
||||
}
|
||||
|
||||
let mask_sum = random_scalar(&mut OsRng);
|
||||
let mut machines = Vec::with_capacity(t);
|
||||
for i in 1 ..= t {
|
||||
machines.push(
|
||||
sign::AlgorithmMachine::new(
|
||||
clsag::Multisig::new(
|
||||
Transcript::new(b"Monero Serai CLSAG Test".to_vec()),
|
||||
Rc::new(RefCell::new(Some(
|
||||
clsag::Details::new(
|
||||
clsag::Input::new(
|
||||
Commitment::new(randomness, AMOUNT),
|
||||
Decoys {
|
||||
i: RING_INDEX,
|
||||
offsets: (1 ..= RING_LEN).into_iter().map(|o| VarInt(o)).collect(),
|
||||
ring: ring.clone()
|
||||
}
|
||||
).unwrap(),
|
||||
mask_sum
|
||||
)
|
||||
)))
|
||||
).unwrap(),
|
||||
Rc::new(keys[i - 1].clone()),
|
||||
&(1 ..= THRESHOLD).collect::<Vec<usize>>()
|
||||
).unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
let mut signatures = sign(&mut machines, &[1; 32]);
|
||||
let signature = signatures.swap_remove(0);
|
||||
for s in 0 .. (t - 1) {
|
||||
// Verify the commitments and the non-decoy s scalar are identical to every other signature
|
||||
// FROST will already have called verify on the produced signature, before checking individual
|
||||
// key shares. For FROST Schnorr, it's cheaper. For CLSAG, it may be more expensive? Yet it
|
||||
// ensures we have usable signatures, not just signatures we think are usable
|
||||
assert_eq!(signatures[s].1, signature.1);
|
||||
assert_eq!(signatures[s].0.s[RING_INDEX as usize], signature.0.s[RING_INDEX as usize]);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -15,10 +15,8 @@ use monero::{
|
||||
network::Network,
|
||||
util::{key::PublicKey, address::Address}
|
||||
};
|
||||
#[cfg(feature = "multisig")]
|
||||
use monero::cryptonote::hash::Hashable;
|
||||
|
||||
use monero_serai::{random_scalar, transaction::{self, SignableTransaction}};
|
||||
use monero_serai::{random_scalar, wallet::SignableTransaction};
|
||||
|
||||
mod rpc;
|
||||
use crate::rpc::{rpc, mine_block};
|
||||
@@ -32,8 +30,24 @@ lazy_static! {
|
||||
static ref SEQUENTIAL: Mutex<()> = Mutex::new(());
|
||||
}
|
||||
|
||||
pub async fn send_core(test: usize, multisig: bool) {
|
||||
let _guard = SEQUENTIAL.lock().unwrap();
|
||||
macro_rules! async_sequential {
|
||||
($(async fn $name: ident() $body: block)*) => {
|
||||
$(
|
||||
#[tokio::test]
|
||||
async fn $name() {
|
||||
let guard = SEQUENTIAL.lock().unwrap();
|
||||
let local = tokio::task::LocalSet::new();
|
||||
local.run_until(async move {
|
||||
if let Err(_) = tokio::task::spawn_local(async move { $body }).await {
|
||||
drop(guard);
|
||||
}
|
||||
}).await;
|
||||
}
|
||||
)*
|
||||
};
|
||||
}
|
||||
|
||||
async fn send_core(test: usize, multisig: bool) {
|
||||
let rpc = rpc().await;
|
||||
|
||||
// Generate an address
|
||||
@@ -86,7 +100,7 @@ pub async fn send_core(test: usize, multisig: bool) {
|
||||
|
||||
// Grab the largest output available
|
||||
let output = {
|
||||
let mut outputs = transaction::scan(tx.as_ref().unwrap(), view, spend_pub);
|
||||
let mut outputs = tx.as_ref().unwrap().scan(view, spend_pub);
|
||||
outputs.sort_by(|x, y| x.commitment.amount.cmp(&y.commitment.amount).reverse());
|
||||
outputs.swap_remove(0)
|
||||
};
|
||||
@@ -102,7 +116,7 @@ pub async fn send_core(test: usize, multisig: bool) {
|
||||
|
||||
for i in (start + 1) .. (start + 9) {
|
||||
let tx = rpc.get_block_transactions(i).await.unwrap().swap_remove(0);
|
||||
let output = transaction::scan(&tx, view, spend_pub).swap_remove(0);
|
||||
let output = tx.scan(view, spend_pub).swap_remove(0);
|
||||
amount += output.commitment.amount;
|
||||
outputs.push(output);
|
||||
}
|
||||
@@ -144,24 +158,23 @@ pub async fn send_core(test: usize, multisig: bool) {
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
pub async fn send_single_input() {
|
||||
send_core(0, false).await;
|
||||
}
|
||||
async_sequential! {
|
||||
async fn send_single_input() {
|
||||
send_core(0, false).await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
pub async fn send_multiple_inputs() {
|
||||
send_core(1, false).await;
|
||||
async fn send_multiple_inputs() {
|
||||
send_core(1, false).await;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "multisig")]
|
||||
#[tokio::test]
|
||||
pub async fn multisig_send_single_input() {
|
||||
send_core(0, true).await;
|
||||
}
|
||||
async_sequential! {
|
||||
async fn multisig_send_single_input() {
|
||||
send_core(0, true).await;
|
||||
}
|
||||
|
||||
#[cfg(feature = "multisig")]
|
||||
#[tokio::test]
|
||||
pub async fn multisig_send_multiple_inputs() {
|
||||
send_core(1, true).await;
|
||||
async fn multisig_send_multiple_inputs() {
|
||||
send_core(1, true).await;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user