2022-08-21 08:41:19 -04:00
|
|
|
use std::io::Cursor;
|
2022-05-21 15:33:35 -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
|
|
|
use zeroize::{Zeroize, ZeroizeOnDrop};
|
|
|
|
|
|
2022-07-15 01:26:07 -04:00
|
|
|
use curve25519_dalek::{constants::ED25519_BASEPOINT_TABLE, scalar::Scalar, edwards::EdwardsPoint};
|
2022-05-21 15:33:35 -04:00
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
|
Commitment,
|
2022-08-21 08:41:19 -04:00
|
|
|
serialize::{read_byte, read_u64, read_bytes, read_scalar, read_point},
|
2022-06-02 00:00:26 -04:00
|
|
|
transaction::{Timelock, Transaction},
|
2022-08-21 08:41:19 -04:00
|
|
|
wallet::{ViewPair, Extra, uniqueness, shared_key, amount_decryption, commitment_mask},
|
2022-05-21 15:33:35 -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, PartialEq, Eq, Debug, Zeroize, ZeroizeOnDrop)]
|
2022-05-21 15:33:35 -04:00
|
|
|
pub struct SpendableOutput {
|
|
|
|
|
pub tx: [u8; 32],
|
2022-05-26 03:51:27 -04:00
|
|
|
pub o: u8,
|
2022-05-21 15:33:35 -04:00
|
|
|
pub key: EdwardsPoint,
|
|
|
|
|
pub key_offset: Scalar,
|
2022-07-15 01:26:07 -04:00
|
|
|
pub commitment: Commitment,
|
2022-05-21 15:33:35 -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(Zeroize, ZeroizeOnDrop)]
|
2022-07-09 18:53:52 -04:00
|
|
|
pub struct Timelocked(Timelock, Vec<SpendableOutput>);
|
|
|
|
|
impl Timelocked {
|
|
|
|
|
pub fn timelock(&self) -> Timelock {
|
|
|
|
|
self.0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn not_locked(&self) -> Vec<SpendableOutput> {
|
|
|
|
|
if self.0 == Timelock::None {
|
|
|
|
|
return self.1.clone();
|
|
|
|
|
}
|
|
|
|
|
vec![]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns None if the Timelocks aren't comparable. Returns Some(vec![]) if none are unlocked
|
|
|
|
|
pub fn unlocked(&self, timelock: Timelock) -> Option<Vec<SpendableOutput>> {
|
|
|
|
|
// If the Timelocks are comparable, return the outputs if they're now unlocked
|
|
|
|
|
self.0.partial_cmp(&timelock).filter(|_| self.0 <= timelock).map(|_| self.1.clone())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn ignore_timelock(&self) -> Vec<SpendableOutput> {
|
|
|
|
|
self.1.clone()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-26 03:51:27 -04:00
|
|
|
impl SpendableOutput {
|
|
|
|
|
pub fn serialize(&self) -> Vec<u8> {
|
|
|
|
|
let mut res = Vec::with_capacity(32 + 1 + 32 + 32 + 40);
|
|
|
|
|
res.extend(&self.tx);
|
|
|
|
|
res.push(self.o);
|
|
|
|
|
res.extend(self.key.compress().to_bytes());
|
|
|
|
|
res.extend(self.key_offset.to_bytes());
|
|
|
|
|
res.extend(self.commitment.mask.to_bytes());
|
|
|
|
|
res.extend(self.commitment.amount.to_le_bytes());
|
|
|
|
|
res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn deserialize<R: std::io::Read>(r: &mut R) -> std::io::Result<SpendableOutput> {
|
2022-07-15 01:26:07 -04:00
|
|
|
Ok(SpendableOutput {
|
2022-08-21 04:41:55 -04:00
|
|
|
tx: read_bytes(r)?,
|
|
|
|
|
o: read_byte(r)?,
|
2022-07-15 01:26:07 -04:00
|
|
|
key: read_point(r)?,
|
|
|
|
|
key_offset: read_scalar(r)?,
|
2022-08-21 04:41:55 -04:00
|
|
|
commitment: Commitment::new(read_scalar(r)?, read_u64(r)?),
|
2022-07-15 01:26:07 -04:00
|
|
|
})
|
2022-05-26 03:51:27 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-21 15:33:35 -04:00
|
|
|
impl Transaction {
|
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
|
|
|
pub fn scan(&self, view: &ViewPair, guaranteed: bool) -> Timelocked {
|
2022-08-21 08:41:19 -04:00
|
|
|
let extra = Extra::deserialize(&mut Cursor::new(&self.prefix.extra));
|
|
|
|
|
let keys;
|
2022-05-21 15:33:35 -04:00
|
|
|
if let Ok(extra) = extra {
|
2022-08-21 08:41:19 -04:00
|
|
|
keys = extra.keys();
|
2022-05-21 15:33:35 -04:00
|
|
|
} else {
|
2022-07-09 18:53:52 -04:00
|
|
|
return Timelocked(self.prefix.timelock, vec![]);
|
2022-05-21 15:33:35 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let mut res = vec![];
|
|
|
|
|
for (o, output) in self.prefix.outputs.iter().enumerate() {
|
2022-08-21 08:41:19 -04:00
|
|
|
for key in &keys {
|
2022-07-27 06:29:14 -04:00
|
|
|
let (view_tag, key_offset) = shared_key(
|
2022-06-28 00:01:20 -04:00
|
|
|
Some(uniqueness(&self.prefix.inputs)).filter(|_| guaranteed),
|
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
|
|
|
&view.view,
|
2022-08-21 08:41:19 -04:00
|
|
|
key,
|
2022-07-15 01:26:07 -04:00
|
|
|
o,
|
2022-06-28 00:01:20 -04:00
|
|
|
);
|
2022-07-27 06:29:14 -04:00
|
|
|
|
|
|
|
|
if let Some(actual_view_tag) = output.view_tag {
|
|
|
|
|
if actual_view_tag != view_tag {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-21 15:33:35 -04:00
|
|
|
// P - shared == spend
|
2022-06-28 00:01:20 -04:00
|
|
|
if (output.key - (&key_offset * &ED25519_BASEPOINT_TABLE)) != view.spend {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2022-05-21 15:33:35 -04:00
|
|
|
|
2022-06-28 00:01:20 -04:00
|
|
|
// Since we've found an output to us, get its amount
|
|
|
|
|
let mut commitment = Commitment::zero();
|
2022-05-21 15:33:35 -04:00
|
|
|
|
2022-06-28 00:01:20 -04:00
|
|
|
// Miner transaction
|
|
|
|
|
if output.amount != 0 {
|
|
|
|
|
commitment.amount = output.amount;
|
|
|
|
|
// Regular transaction
|
|
|
|
|
} else {
|
|
|
|
|
let amount = match self.rct_signatures.base.ecdh_info.get(o) {
|
|
|
|
|
Some(amount) => amount_decryption(*amount, key_offset),
|
|
|
|
|
// This should never happen, yet it may be possible with miner transactions?
|
|
|
|
|
// Using get just decreases the possibility of a panic and lets us move on in that case
|
2022-07-15 01:26:07 -04:00
|
|
|
None => break,
|
2022-06-28 00:01:20 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Rebuild the commitment to verify it
|
|
|
|
|
commitment = Commitment::new(commitment_mask(key_offset), amount);
|
|
|
|
|
// If this is a malicious commitment, move to the next output
|
|
|
|
|
// Any other R value will calculate to a different spend key and are therefore ignorable
|
|
|
|
|
if Some(&commitment.calculate()) != self.rct_signatures.base.commitments.get(o) {
|
|
|
|
|
break;
|
2022-05-21 15:33:35 -04:00
|
|
|
}
|
2022-06-28 00:01:20 -04:00
|
|
|
}
|
2022-05-21 15:33:35 -04:00
|
|
|
|
2022-06-28 00:01:20 -04:00
|
|
|
if commitment.amount != 0 {
|
|
|
|
|
res.push(SpendableOutput {
|
|
|
|
|
tx: self.hash(),
|
|
|
|
|
o: o.try_into().unwrap(),
|
|
|
|
|
key: output.key,
|
|
|
|
|
key_offset,
|
2022-07-15 01:26:07 -04:00
|
|
|
commitment,
|
2022-06-28 00:01:20 -04:00
|
|
|
});
|
2022-05-21 15:33:35 -04:00
|
|
|
}
|
2022-06-28 00:01:20 -04:00
|
|
|
// Break to prevent public keys from being included multiple times, triggering multiple
|
|
|
|
|
// inclusions of the same output
|
|
|
|
|
break;
|
2022-05-21 15:33:35 -04:00
|
|
|
}
|
|
|
|
|
}
|
2022-06-02 00:00:26 -04:00
|
|
|
|
2022-07-09 18:53:52 -04:00
|
|
|
Timelocked(self.prefix.timelock, res)
|
2022-05-21 15:33:35 -04:00
|
|
|
}
|
|
|
|
|
}
|