2022-04-22 01:36:18 +00:00
|
|
|
use lazy_static::lazy_static;
|
|
|
|
use rand_core::{RngCore, CryptoRng};
|
|
|
|
|
|
|
|
use tiny_keccak::{Hasher, Keccak};
|
|
|
|
|
|
|
|
use curve25519_dalek::{
|
2022-07-24 12:57:33 +00:00
|
|
|
constants::{ED25519_BASEPOINT_POINT, ED25519_BASEPOINT_TABLE},
|
2022-04-22 01:36:18 +00:00
|
|
|
scalar::Scalar,
|
2022-07-15 05:26:07 +00:00
|
|
|
edwards::{EdwardsPoint, EdwardsBasepointTable, CompressedEdwardsY},
|
2022-04-22 01:36:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#[cfg(feature = "multisig")]
|
|
|
|
pub mod frost;
|
|
|
|
|
2022-05-21 19:33:35 +00:00
|
|
|
mod serialize;
|
|
|
|
|
2022-05-22 06:24:24 +00:00
|
|
|
pub mod ringct;
|
2022-04-22 01:36:18 +00:00
|
|
|
|
2022-04-28 07:31:09 +00:00
|
|
|
pub mod transaction;
|
2022-05-22 01:35:25 +00:00
|
|
|
pub mod block;
|
2022-04-28 07:31:09 +00:00
|
|
|
|
2022-05-22 01:35:25 +00:00
|
|
|
pub mod rpc;
|
|
|
|
pub mod wallet;
|
2022-05-21 19:33:35 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
2022-04-22 01:36:18 +00:00
|
|
|
|
2022-07-27 09:05:43 +00:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
pub enum Protocol {
|
|
|
|
Unsupported,
|
|
|
|
v14,
|
|
|
|
v16,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Protocol {
|
2022-08-02 19:52:27 +00:00
|
|
|
pub fn ring_len(&self) -> usize {
|
2022-07-27 09:05:43 +00:00
|
|
|
match self {
|
|
|
|
Protocol::Unsupported => panic!("Unsupported protocol version"),
|
|
|
|
Protocol::v14 => 11,
|
|
|
|
Protocol::v16 => 16,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-02 19:52:27 +00:00
|
|
|
pub fn bp_plus(&self) -> bool {
|
2022-07-27 09:05:43 +00:00
|
|
|
match self {
|
|
|
|
Protocol::Unsupported => panic!("Unsupported protocol version"),
|
|
|
|
Protocol::v14 => false,
|
|
|
|
Protocol::v16 => true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-22 01:36:18 +00:00
|
|
|
lazy_static! {
|
2022-07-24 12:57:33 +00:00
|
|
|
static ref H: EdwardsPoint =
|
|
|
|
CompressedEdwardsY(hash(&ED25519_BASEPOINT_POINT.compress().to_bytes()))
|
|
|
|
.decompress()
|
2022-07-15 05:26:07 +00:00
|
|
|
.unwrap()
|
2022-07-24 12:57:33 +00:00
|
|
|
.mul_by_cofactor();
|
2022-07-22 06:34:36 +00:00
|
|
|
static ref H_TABLE: EdwardsBasepointTable = EdwardsBasepointTable::create(&H);
|
2022-04-22 01:36:18 +00:00
|
|
|
}
|
|
|
|
|
2022-04-28 07:31:09 +00:00
|
|
|
#[allow(non_snake_case)]
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
|
|
|
pub struct Commitment {
|
|
|
|
pub mask: Scalar,
|
2022-07-15 05:26:07 +00:00
|
|
|
pub amount: u64,
|
2022-04-22 01:36:18 +00:00
|
|
|
}
|
|
|
|
|
2022-04-28 07:31:09 +00:00
|
|
|
impl Commitment {
|
|
|
|
pub fn zero() -> Commitment {
|
2022-07-15 05:26:07 +00:00
|
|
|
Commitment { mask: Scalar::one(), amount: 0 }
|
2022-04-28 07:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new(mask: Scalar, amount: u64) -> Commitment {
|
|
|
|
Commitment { mask, amount }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn calculate(&self) -> EdwardsPoint {
|
|
|
|
(&self.mask * &ED25519_BASEPOINT_TABLE) + (&Scalar::from(self.amount) * &*H_TABLE)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-03 12:49:46 +00:00
|
|
|
// Allows using a modern rand as dalek's is notoriously dated
|
|
|
|
pub fn random_scalar<R: RngCore + CryptoRng>(rng: &mut R) -> Scalar {
|
|
|
|
let mut r = [0; 64];
|
|
|
|
rng.fill_bytes(&mut r);
|
|
|
|
Scalar::from_bytes_mod_order_wide(&r)
|
|
|
|
}
|
|
|
|
|
2022-04-28 07:31:09 +00:00
|
|
|
pub fn hash(data: &[u8]) -> [u8; 32] {
|
2022-04-22 01:36:18 +00:00
|
|
|
let mut keccak = Keccak::v256();
|
|
|
|
keccak.update(data);
|
|
|
|
let mut res = [0; 32];
|
|
|
|
keccak.finalize(&mut res);
|
2022-04-28 07:31:09 +00:00
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn hash_to_scalar(data: &[u8]) -> Scalar {
|
2022-07-26 07:25:57 +00:00
|
|
|
let scalar = Scalar::from_bytes_mod_order(hash(data));
|
|
|
|
// Monero will explicitly error in this case
|
|
|
|
// This library acknowledges its practical impossibility of it occurring, and doesn't bother to
|
|
|
|
// code in logic to handle it. That said, if it ever occurs, something must happen in order to
|
|
|
|
// not generate/verify a proof we believe to be valid when it isn't
|
|
|
|
assert!(scalar != Scalar::zero(), "ZERO HASH: {:?}", data);
|
|
|
|
scalar
|
2022-04-22 01:36:18 +00:00
|
|
|
}
|