2022-06-28 00:01:20 -04:00
|
|
|
use std::string::ToString;
|
|
|
|
|
|
|
|
|
|
use thiserror::Error;
|
|
|
|
|
|
Utilize zeroize (#76)
* Apply Zeroize to nonces used in Bulletproofs
Also makes bit decomposition constant time for a given amount of
outputs.
* Fix nonce reuse for single-signer CLSAG
* Attach Zeroize to most structures in Monero, and ZOnDrop to anything with private data
* Zeroize private keys and nonces
* Merge prepare_outputs and prepare_transactions
* Ensure CLSAG is constant time
* Pass by borrow where needed, bug fixes
The past few commitments have been one in-progress chunk which I've
broken up as best read.
* Add Zeroize to FROST structs
Still needs to zeroize internally, yet next step. Not quite as
aggressive as Monero, partially due to the limitations of HashMaps,
partially due to less concern about metadata, yet does still delete a
few smaller items of metadata (group key, context string...).
* Remove Zeroize from most Monero multisig structs
These structs largely didn't have private data, just fields with private
data, yet those fields implemented ZeroizeOnDrop making them already
covered. While there is still traces of the transaction left in RAM,
fully purging that was never the intent.
* Use Zeroize within dleq
bitvec doesn't offer Zeroize, so a manual zeroing has been implemented.
* Use Zeroize for random_nonce
It isn't perfect, due to the inability to zeroize the digest, and due to
kp256 requiring a few transformations. It does the best it can though.
Does move the per-curve random_nonce to a provided one, which is allowed
as of https://github.com/cfrg/draft-irtf-cfrg-frost/pull/231.
* Use Zeroize on FROST keygen/signing
* Zeroize constant time multiexp.
* Correct when FROST keygen zeroizes
* Move the FROST keys Arc into FrostKeys
Reduces amount of instances in memory.
* Manually implement Debug for FrostCore to not leak the secret share
* Misc bug fixes
* clippy + multiexp test bug fixes
* Correct FROST key gen share summation
It leaked our own share for ourself.
* Fix cross-group DLEq tests
2022-08-03 03:25:18 -05:00
|
|
|
use zeroize::Zeroize;
|
|
|
|
|
|
2022-07-15 01:26:07 -04:00
|
|
|
use curve25519_dalek::{
|
|
|
|
|
constants::ED25519_BASEPOINT_TABLE,
|
|
|
|
|
edwards::{EdwardsPoint, CompressedEdwardsY},
|
|
|
|
|
};
|
2022-06-28 00:01:20 -04:00
|
|
|
|
|
|
|
|
use base58_monero::base58::{encode_check, decode_check};
|
|
|
|
|
|
|
|
|
|
use crate::wallet::ViewPair;
|
|
|
|
|
|
Utilize zeroize (#76)
* Apply Zeroize to nonces used in Bulletproofs
Also makes bit decomposition constant time for a given amount of
outputs.
* Fix nonce reuse for single-signer CLSAG
* Attach Zeroize to most structures in Monero, and ZOnDrop to anything with private data
* Zeroize private keys and nonces
* Merge prepare_outputs and prepare_transactions
* Ensure CLSAG is constant time
* Pass by borrow where needed, bug fixes
The past few commitments have been one in-progress chunk which I've
broken up as best read.
* Add Zeroize to FROST structs
Still needs to zeroize internally, yet next step. Not quite as
aggressive as Monero, partially due to the limitations of HashMaps,
partially due to less concern about metadata, yet does still delete a
few smaller items of metadata (group key, context string...).
* Remove Zeroize from most Monero multisig structs
These structs largely didn't have private data, just fields with private
data, yet those fields implemented ZeroizeOnDrop making them already
covered. While there is still traces of the transaction left in RAM,
fully purging that was never the intent.
* Use Zeroize within dleq
bitvec doesn't offer Zeroize, so a manual zeroing has been implemented.
* Use Zeroize for random_nonce
It isn't perfect, due to the inability to zeroize the digest, and due to
kp256 requiring a few transformations. It does the best it can though.
Does move the per-curve random_nonce to a provided one, which is allowed
as of https://github.com/cfrg/draft-irtf-cfrg-frost/pull/231.
* Use Zeroize on FROST keygen/signing
* Zeroize constant time multiexp.
* Correct when FROST keygen zeroizes
* Move the FROST keys Arc into FrostKeys
Reduces amount of instances in memory.
* Manually implement Debug for FrostCore to not leak the secret share
* Misc bug fixes
* clippy + multiexp test bug fixes
* Correct FROST key gen share summation
It leaked our own share for ourself.
* Fix cross-group DLEq tests
2022-08-03 03:25:18 -05:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Debug, Zeroize)]
|
2022-06-28 00:01:20 -04:00
|
|
|
pub enum Network {
|
|
|
|
|
Mainnet,
|
|
|
|
|
Testnet,
|
2022-07-15 01:26:07 -04:00
|
|
|
Stagenet,
|
2022-06-28 00:01:20 -04:00
|
|
|
}
|
|
|
|
|
|
Utilize zeroize (#76)
* Apply Zeroize to nonces used in Bulletproofs
Also makes bit decomposition constant time for a given amount of
outputs.
* Fix nonce reuse for single-signer CLSAG
* Attach Zeroize to most structures in Monero, and ZOnDrop to anything with private data
* Zeroize private keys and nonces
* Merge prepare_outputs and prepare_transactions
* Ensure CLSAG is constant time
* Pass by borrow where needed, bug fixes
The past few commitments have been one in-progress chunk which I've
broken up as best read.
* Add Zeroize to FROST structs
Still needs to zeroize internally, yet next step. Not quite as
aggressive as Monero, partially due to the limitations of HashMaps,
partially due to less concern about metadata, yet does still delete a
few smaller items of metadata (group key, context string...).
* Remove Zeroize from most Monero multisig structs
These structs largely didn't have private data, just fields with private
data, yet those fields implemented ZeroizeOnDrop making them already
covered. While there is still traces of the transaction left in RAM,
fully purging that was never the intent.
* Use Zeroize within dleq
bitvec doesn't offer Zeroize, so a manual zeroing has been implemented.
* Use Zeroize for random_nonce
It isn't perfect, due to the inability to zeroize the digest, and due to
kp256 requiring a few transformations. It does the best it can though.
Does move the per-curve random_nonce to a provided one, which is allowed
as of https://github.com/cfrg/draft-irtf-cfrg-frost/pull/231.
* Use Zeroize on FROST keygen/signing
* Zeroize constant time multiexp.
* Correct when FROST keygen zeroizes
* Move the FROST keys Arc into FrostKeys
Reduces amount of instances in memory.
* Manually implement Debug for FrostCore to not leak the secret share
* Misc bug fixes
* clippy + multiexp test bug fixes
* Correct FROST key gen share summation
It leaked our own share for ourself.
* Fix cross-group DLEq tests
2022-08-03 03:25:18 -05:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Debug, Zeroize)]
|
2022-06-28 00:01:20 -04:00
|
|
|
pub enum AddressType {
|
|
|
|
|
Standard,
|
|
|
|
|
Integrated([u8; 8]),
|
2022-07-15 01:26:07 -04:00
|
|
|
Subaddress,
|
2022-08-22 04:27:58 -04:00
|
|
|
Featured(bool, Option<[u8; 8]>, bool),
|
2022-06-28 00:01:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl AddressType {
|
2022-08-22 04:27:58 -04:00
|
|
|
fn network_bytes(network: Network) -> (u8, u8, u8, u8) {
|
2022-06-28 00:01:20 -04:00
|
|
|
match network {
|
2022-08-22 04:27:58 -04:00
|
|
|
Network::Mainnet => (18, 19, 42, 70),
|
|
|
|
|
Network::Testnet => (53, 54, 63, 111),
|
|
|
|
|
Network::Stagenet => (24, 25, 36, 86),
|
2022-06-28 00:01:20 -04:00
|
|
|
}
|
|
|
|
|
}
|
2022-08-22 04:27:58 -04:00
|
|
|
|
|
|
|
|
pub fn subaddress(&self) -> bool {
|
|
|
|
|
matches!(self, AddressType::Subaddress) || matches!(self, AddressType::Featured(true, ..))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn payment_id(&self) -> Option<[u8; 8]> {
|
|
|
|
|
if let AddressType::Integrated(id) = self {
|
|
|
|
|
Some(*id)
|
|
|
|
|
} else if let AddressType::Featured(_, id, _) = self {
|
|
|
|
|
*id
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn guaranteed(&self) -> bool {
|
|
|
|
|
matches!(self, AddressType::Featured(_, _, true))
|
|
|
|
|
}
|
2022-06-28 00:01:20 -04:00
|
|
|
}
|
|
|
|
|
|
Utilize zeroize (#76)
* Apply Zeroize to nonces used in Bulletproofs
Also makes bit decomposition constant time for a given amount of
outputs.
* Fix nonce reuse for single-signer CLSAG
* Attach Zeroize to most structures in Monero, and ZOnDrop to anything with private data
* Zeroize private keys and nonces
* Merge prepare_outputs and prepare_transactions
* Ensure CLSAG is constant time
* Pass by borrow where needed, bug fixes
The past few commitments have been one in-progress chunk which I've
broken up as best read.
* Add Zeroize to FROST structs
Still needs to zeroize internally, yet next step. Not quite as
aggressive as Monero, partially due to the limitations of HashMaps,
partially due to less concern about metadata, yet does still delete a
few smaller items of metadata (group key, context string...).
* Remove Zeroize from most Monero multisig structs
These structs largely didn't have private data, just fields with private
data, yet those fields implemented ZeroizeOnDrop making them already
covered. While there is still traces of the transaction left in RAM,
fully purging that was never the intent.
* Use Zeroize within dleq
bitvec doesn't offer Zeroize, so a manual zeroing has been implemented.
* Use Zeroize for random_nonce
It isn't perfect, due to the inability to zeroize the digest, and due to
kp256 requiring a few transformations. It does the best it can though.
Does move the per-curve random_nonce to a provided one, which is allowed
as of https://github.com/cfrg/draft-irtf-cfrg-frost/pull/231.
* Use Zeroize on FROST keygen/signing
* Zeroize constant time multiexp.
* Correct when FROST keygen zeroizes
* Move the FROST keys Arc into FrostKeys
Reduces amount of instances in memory.
* Manually implement Debug for FrostCore to not leak the secret share
* Misc bug fixes
* clippy + multiexp test bug fixes
* Correct FROST key gen share summation
It leaked our own share for ourself.
* Fix cross-group DLEq tests
2022-08-03 03:25:18 -05:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Debug, Zeroize)]
|
2022-06-28 00:01:20 -04:00
|
|
|
pub struct AddressMeta {
|
|
|
|
|
pub network: Network,
|
|
|
|
|
pub kind: AddressType,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Error, Debug)]
|
|
|
|
|
pub enum AddressError {
|
|
|
|
|
#[error("invalid address byte")]
|
|
|
|
|
InvalidByte,
|
|
|
|
|
#[error("invalid address encoding")]
|
|
|
|
|
InvalidEncoding,
|
|
|
|
|
#[error("invalid length")]
|
|
|
|
|
InvalidLength,
|
|
|
|
|
#[error("invalid key")]
|
2022-07-15 01:26:07 -04:00
|
|
|
InvalidKey,
|
2022-08-22 04:27:58 -04:00
|
|
|
#[error("unknown features")]
|
|
|
|
|
UnknownFeatures,
|
|
|
|
|
#[error("different network than expected")]
|
|
|
|
|
DifferentNetwork,
|
2022-06-28 00:01:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl AddressMeta {
|
2022-07-22 02:34:36 -04:00
|
|
|
#[allow(clippy::wrong_self_convention)]
|
2022-06-28 00:01:20 -04:00
|
|
|
fn to_byte(&self) -> u8 {
|
|
|
|
|
let bytes = AddressType::network_bytes(self.network);
|
2022-08-22 04:27:58 -04:00
|
|
|
match self.kind {
|
2022-06-28 00:01:20 -04:00
|
|
|
AddressType::Standard => bytes.0,
|
|
|
|
|
AddressType::Integrated(_) => bytes.1,
|
2022-07-15 01:26:07 -04:00
|
|
|
AddressType::Subaddress => bytes.2,
|
2022-08-22 04:27:58 -04:00
|
|
|
AddressType::Featured(..) => bytes.3,
|
|
|
|
|
}
|
2022-06-28 00:01:20 -04:00
|
|
|
}
|
|
|
|
|
|
2022-08-22 04:27:58 -04:00
|
|
|
// Returns an incomplete type in the case of Integrated/Featured addresses
|
2022-06-28 00:01:20 -04:00
|
|
|
fn from_byte(byte: u8) -> Result<AddressMeta, AddressError> {
|
|
|
|
|
let mut meta = None;
|
|
|
|
|
for network in [Network::Mainnet, Network::Testnet, Network::Stagenet] {
|
2022-08-22 04:27:58 -04:00
|
|
|
let (standard, integrated, subaddress, featured) = AddressType::network_bytes(network);
|
|
|
|
|
if let Some(kind) = match byte {
|
|
|
|
|
_ if byte == standard => Some(AddressType::Standard),
|
|
|
|
|
_ if byte == integrated => Some(AddressType::Integrated([0; 8])),
|
|
|
|
|
_ if byte == subaddress => Some(AddressType::Subaddress),
|
|
|
|
|
_ if byte == featured => Some(AddressType::Featured(false, None, false)),
|
2022-07-15 01:26:07 -04:00
|
|
|
_ => None,
|
2022-06-28 00:01:20 -04:00
|
|
|
} {
|
2022-08-22 04:27:58 -04:00
|
|
|
meta = Some(AddressMeta { network, kind });
|
2022-06-28 00:01:20 -04:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
meta.ok_or(AddressError::InvalidByte)
|
|
|
|
|
}
|
2022-08-22 04:27:58 -04:00
|
|
|
|
|
|
|
|
pub fn subaddress(&self) -> bool {
|
|
|
|
|
self.kind.subaddress()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn payment_id(&self) -> Option<[u8; 8]> {
|
|
|
|
|
self.kind.payment_id()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn guaranteed(&self) -> bool {
|
|
|
|
|
self.kind.guaranteed()
|
|
|
|
|
}
|
2022-06-28 00:01:20 -04:00
|
|
|
}
|
|
|
|
|
|
Utilize zeroize (#76)
* Apply Zeroize to nonces used in Bulletproofs
Also makes bit decomposition constant time for a given amount of
outputs.
* Fix nonce reuse for single-signer CLSAG
* Attach Zeroize to most structures in Monero, and ZOnDrop to anything with private data
* Zeroize private keys and nonces
* Merge prepare_outputs and prepare_transactions
* Ensure CLSAG is constant time
* Pass by borrow where needed, bug fixes
The past few commitments have been one in-progress chunk which I've
broken up as best read.
* Add Zeroize to FROST structs
Still needs to zeroize internally, yet next step. Not quite as
aggressive as Monero, partially due to the limitations of HashMaps,
partially due to less concern about metadata, yet does still delete a
few smaller items of metadata (group key, context string...).
* Remove Zeroize from most Monero multisig structs
These structs largely didn't have private data, just fields with private
data, yet those fields implemented ZeroizeOnDrop making them already
covered. While there is still traces of the transaction left in RAM,
fully purging that was never the intent.
* Use Zeroize within dleq
bitvec doesn't offer Zeroize, so a manual zeroing has been implemented.
* Use Zeroize for random_nonce
It isn't perfect, due to the inability to zeroize the digest, and due to
kp256 requiring a few transformations. It does the best it can though.
Does move the per-curve random_nonce to a provided one, which is allowed
as of https://github.com/cfrg/draft-irtf-cfrg-frost/pull/231.
* Use Zeroize on FROST keygen/signing
* Zeroize constant time multiexp.
* Correct when FROST keygen zeroizes
* Move the FROST keys Arc into FrostKeys
Reduces amount of instances in memory.
* Manually implement Debug for FrostCore to not leak the secret share
* Misc bug fixes
* clippy + multiexp test bug fixes
* Correct FROST key gen share summation
It leaked our own share for ourself.
* Fix cross-group DLEq tests
2022-08-03 03:25:18 -05:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Debug, Zeroize)]
|
2022-06-28 00:01:20 -04:00
|
|
|
pub struct Address {
|
|
|
|
|
pub meta: AddressMeta,
|
|
|
|
|
pub spend: EdwardsPoint,
|
2022-07-15 01:26:07 -04:00
|
|
|
pub view: EdwardsPoint,
|
2022-06-28 00:01:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ViewPair {
|
2022-08-22 04:27:58 -04:00
|
|
|
pub fn address(&self, network: Network, kind: AddressType) -> Address {
|
2022-06-28 00:01:20 -04:00
|
|
|
Address {
|
2022-08-22 04:27:58 -04:00
|
|
|
meta: AddressMeta { network, kind },
|
2022-06-28 00:01:20 -04:00
|
|
|
spend: self.spend,
|
2022-07-15 01:26:07 -04:00
|
|
|
view: &self.view * &ED25519_BASEPOINT_TABLE,
|
2022-06-28 00:01:20 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ToString for Address {
|
|
|
|
|
fn to_string(&self) -> String {
|
|
|
|
|
let mut data = vec![self.meta.to_byte()];
|
|
|
|
|
data.extend(self.spend.compress().to_bytes());
|
|
|
|
|
data.extend(self.view.compress().to_bytes());
|
2022-08-22 04:27:58 -04:00
|
|
|
if let AddressType::Featured(subaddress, payment_id, guaranteed) = self.meta.kind {
|
|
|
|
|
// Technically should be a VarInt, yet we don't have enough features it's needed
|
|
|
|
|
data.push(
|
|
|
|
|
(if subaddress { 1 } else { 0 }) +
|
|
|
|
|
((if payment_id.is_some() { 1 } else { 0 }) << 1) +
|
|
|
|
|
((if guaranteed { 1 } else { 0 }) << 2),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if let Some(id) = self.meta.kind.payment_id() {
|
2022-06-28 00:01:20 -04:00
|
|
|
data.extend(id);
|
|
|
|
|
}
|
|
|
|
|
encode_check(&data).unwrap()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Address {
|
2022-08-22 04:27:58 -04:00
|
|
|
pub fn new(meta: AddressMeta, spend: EdwardsPoint, view: EdwardsPoint) -> Address {
|
|
|
|
|
Address { meta, spend, view }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn from_str_raw(s: &str) -> Result<Address, AddressError> {
|
2022-06-28 00:01:20 -04:00
|
|
|
let raw = decode_check(s).map_err(|_| AddressError::InvalidEncoding)?;
|
2022-08-22 04:27:58 -04:00
|
|
|
if raw.len() < (1 + 32 + 32) {
|
2022-06-28 00:01:20 -04:00
|
|
|
Err(AddressError::InvalidLength)?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut meta = AddressMeta::from_byte(raw[0])?;
|
2022-07-15 01:26:07 -04:00
|
|
|
let spend = CompressedEdwardsY(raw[1 .. 33].try_into().unwrap())
|
|
|
|
|
.decompress()
|
|
|
|
|
.ok_or(AddressError::InvalidKey)?;
|
|
|
|
|
let view = CompressedEdwardsY(raw[33 .. 65].try_into().unwrap())
|
|
|
|
|
.decompress()
|
|
|
|
|
.ok_or(AddressError::InvalidKey)?;
|
2022-08-22 04:27:58 -04:00
|
|
|
let mut read = 65;
|
|
|
|
|
|
|
|
|
|
if matches!(meta.kind, AddressType::Featured(..)) {
|
|
|
|
|
if raw[read] >= (2 << 3) {
|
|
|
|
|
Err(AddressError::UnknownFeatures)?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let subaddress = (raw[read] & 1) == 1;
|
|
|
|
|
let integrated = ((raw[read] >> 1) & 1) == 1;
|
|
|
|
|
let guaranteed = ((raw[read] >> 2) & 1) == 1;
|
|
|
|
|
|
|
|
|
|
meta.kind =
|
|
|
|
|
AddressType::Featured(subaddress, Some([0; 8]).filter(|_| integrated), guaranteed);
|
|
|
|
|
read += 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update read early so we can verify the length
|
|
|
|
|
if meta.kind.payment_id().is_some() {
|
|
|
|
|
read += 8;
|
|
|
|
|
}
|
|
|
|
|
if raw.len() != read {
|
|
|
|
|
Err(AddressError::InvalidLength)?;
|
|
|
|
|
}
|
2022-06-28 00:01:20 -04:00
|
|
|
|
2022-08-22 04:27:58 -04:00
|
|
|
if let AddressType::Integrated(ref mut id) = meta.kind {
|
|
|
|
|
id.copy_from_slice(&raw[(read - 8) .. read]);
|
|
|
|
|
}
|
|
|
|
|
if let AddressType::Featured(_, Some(ref mut id), _) = meta.kind {
|
|
|
|
|
id.copy_from_slice(&raw[(read - 8) .. read]);
|
2022-06-28 00:01:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(Address { meta, spend, view })
|
|
|
|
|
}
|
2022-08-22 04:27:58 -04:00
|
|
|
|
|
|
|
|
pub fn from_str(s: &str, network: Network) -> Result<Address, AddressError> {
|
|
|
|
|
Self::from_str_raw(s).and_then(|addr| {
|
|
|
|
|
if addr.meta.network == network {
|
|
|
|
|
Ok(addr)
|
|
|
|
|
} else {
|
|
|
|
|
Err(AddressError::DifferentNetwork)?
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn network(&self) -> Network {
|
|
|
|
|
self.meta.network
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn subaddress(&self) -> bool {
|
|
|
|
|
self.meta.subaddress()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn payment_id(&self) -> Option<[u8; 8]> {
|
|
|
|
|
self.meta.payment_id()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn guaranteed(&self) -> bool {
|
|
|
|
|
self.meta.guaranteed()
|
|
|
|
|
}
|
2022-06-28 00:01:20 -04:00
|
|
|
}
|