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 std::{io::Cursor, collections::HashMap};
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2022-05-25 00:28:57 -04:00
|
|
|
use rand_core::{RngCore, CryptoRng};
|
2022-04-23 03:49:30 -04:00
|
|
|
|
2022-06-19 06:36:47 -04:00
|
|
|
use group::ff::Field;
|
2022-05-30 16:37:51 -04:00
|
|
|
|
2022-05-25 00:28:57 -04:00
|
|
|
use crate::{
|
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
|
|
|
Curve, FrostParams, FrostCore, FrostKeys, lagrange,
|
2022-06-24 08:40:14 -04:00
|
|
|
key_gen::KeyGenMachine,
|
2022-05-25 00:28:57 -04:00
|
|
|
algorithm::Algorithm,
|
2022-07-15 01:26:07 -04:00
|
|
|
sign::{PreprocessMachine, SignMachine, SignatureMachine, AlgorithmMachine},
|
2022-04-21 21:36:18 -04:00
|
|
|
};
|
|
|
|
|
|
2022-09-29 06:02:43 -04:00
|
|
|
/// Curve tests.
|
2022-05-25 00:28:57 -04:00
|
|
|
pub mod curve;
|
2022-09-29 06:02:43 -04:00
|
|
|
/// Schnorr signature tests.
|
2022-06-03 19:08:25 -04:00
|
|
|
pub mod schnorr;
|
2022-09-29 06:02:43 -04:00
|
|
|
/// Promotion tests.
|
2022-08-13 08:49:38 -04:00
|
|
|
pub mod promote;
|
2022-09-29 06:02:43 -04:00
|
|
|
/// Vectorized test suite to ensure consistency.
|
2022-06-03 01:25:46 -04:00
|
|
|
pub mod vectors;
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2022-05-25 00:28:57 -04:00
|
|
|
// Literal test definitions to run during `cargo test`
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod literal;
|
|
|
|
|
|
|
|
|
|
pub const PARTICIPANTS: u16 = 5;
|
|
|
|
|
pub const THRESHOLD: u16 = ((PARTICIPANTS / 3) * 2) + 1;
|
|
|
|
|
|
|
|
|
|
pub fn clone_without<K: Clone + std::cmp::Eq + std::hash::Hash, V: Clone>(
|
2022-05-24 21:41:14 -04:00
|
|
|
map: &HashMap<K, V>,
|
2022-07-15 01:26:07 -04:00
|
|
|
without: &K,
|
2022-05-24 21:41:14 -04:00
|
|
|
) -> HashMap<K, V> {
|
|
|
|
|
let mut res = map.clone();
|
|
|
|
|
res.remove(without).unwrap();
|
|
|
|
|
res
|
2022-04-21 21:36:18 -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
|
|
|
pub fn core_gen<R: RngCore + CryptoRng, C: Curve>(rng: &mut R) -> HashMap<u16, FrostCore<C>> {
|
2022-05-24 21:41:14 -04:00
|
|
|
let mut machines = HashMap::new();
|
|
|
|
|
let mut commitments = HashMap::new();
|
2022-04-21 21:36:18 -04:00
|
|
|
for i in 1 ..= PARTICIPANTS {
|
2022-06-24 08:40:14 -04:00
|
|
|
let machine = KeyGenMachine::<C>::new(
|
2022-06-28 00:06:12 -04:00
|
|
|
FrostParams::new(THRESHOLD, PARTICIPANTS, i).unwrap(),
|
2022-07-15 01:26:07 -04:00
|
|
|
"FROST Test key_gen".to_string(),
|
2022-05-24 21:41:14 -04:00
|
|
|
);
|
2022-06-24 08:40:14 -04:00
|
|
|
let (machine, these_commitments) = machine.generate_coefficients(rng);
|
|
|
|
|
machines.insert(i, machine);
|
2022-07-13 02:38:29 -04:00
|
|
|
commitments.insert(i, Cursor::new(these_commitments));
|
2022-04-21 21:36:18 -04:00
|
|
|
}
|
|
|
|
|
|
2022-05-24 21:41:14 -04:00
|
|
|
let mut secret_shares = HashMap::new();
|
2022-07-15 01:26:07 -04:00
|
|
|
let mut machines = machines
|
|
|
|
|
.drain()
|
|
|
|
|
.map(|(l, machine)| {
|
|
|
|
|
let (machine, shares) =
|
|
|
|
|
machine.generate_secret_shares(rng, clone_without(&commitments, &l)).unwrap();
|
|
|
|
|
secret_shares.insert(l, shares);
|
|
|
|
|
(l, machine)
|
|
|
|
|
})
|
|
|
|
|
.collect::<HashMap<_, _>>();
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2022-05-24 21:41:14 -04:00
|
|
|
let mut verification_shares = None;
|
2022-04-21 21:36:18 -04:00
|
|
|
let mut group_key = None;
|
2022-07-15 01:26:07 -04:00
|
|
|
machines
|
|
|
|
|
.drain()
|
|
|
|
|
.map(|(i, machine)| {
|
|
|
|
|
let mut our_secret_shares = HashMap::new();
|
|
|
|
|
for (l, shares) in &secret_shares {
|
|
|
|
|
if i == *l {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
our_secret_shares.insert(*l, Cursor::new(shares[&i].clone()));
|
2022-05-24 21:41:14 -04:00
|
|
|
}
|
2022-07-15 01:26:07 -04:00
|
|
|
let these_keys = machine.complete(rng, our_secret_shares).unwrap();
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2022-07-15 01:26:07 -04:00
|
|
|
// Verify the verification_shares are agreed upon
|
|
|
|
|
if verification_shares.is_none() {
|
|
|
|
|
verification_shares = Some(these_keys.verification_shares());
|
|
|
|
|
}
|
|
|
|
|
assert_eq!(verification_shares.as_ref().unwrap(), &these_keys.verification_shares());
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2022-07-15 01:26:07 -04:00
|
|
|
// Verify the group keys are agreed upon
|
|
|
|
|
if group_key.is_none() {
|
|
|
|
|
group_key = Some(these_keys.group_key());
|
|
|
|
|
}
|
|
|
|
|
assert_eq!(group_key.unwrap(), these_keys.group_key());
|
2022-05-24 21:41:14 -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
|
|
|
(i, these_keys)
|
2022-07-15 01:26:07 -04:00
|
|
|
})
|
|
|
|
|
.collect::<HashMap<_, _>>()
|
2022-05-24 21:41:14 -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
|
|
|
pub fn key_gen<R: RngCore + CryptoRng, C: Curve>(rng: &mut R) -> HashMap<u16, FrostKeys<C>> {
|
|
|
|
|
core_gen(rng).drain().map(|(i, core)| (i, FrostKeys::new(core))).collect()
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-28 00:06:12 -04:00
|
|
|
pub fn recover<C: Curve>(keys: &HashMap<u16, FrostKeys<C>>) -> C::F {
|
2022-05-30 16:37:51 -04:00
|
|
|
let first = keys.values().next().expect("no keys provided");
|
|
|
|
|
assert!(keys.len() >= first.params().t().into(), "not enough keys provided");
|
|
|
|
|
let included = keys.keys().cloned().collect::<Vec<_>>();
|
|
|
|
|
|
2022-07-15 01:26:07 -04:00
|
|
|
let group_private = keys.iter().fold(C::F::zero(), |accum, (i, keys)| {
|
|
|
|
|
accum + (keys.secret_share() * lagrange::<C::F>(*i, &included))
|
|
|
|
|
});
|
2022-08-13 05:07:07 -04:00
|
|
|
assert_eq!(C::generator() * group_private, first.group_key(), "failed to recover keys");
|
2022-05-30 16:37:51 -04:00
|
|
|
group_private
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-25 00:28:57 -04:00
|
|
|
pub fn algorithm_machines<R: RngCore, C: Curve, A: Algorithm<C>>(
|
|
|
|
|
rng: &mut R,
|
2022-05-24 21:41:14 -04:00
|
|
|
algorithm: A,
|
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
|
|
|
keys: &HashMap<u16, FrostKeys<C>>,
|
2022-05-25 00:28:57 -04:00
|
|
|
) -> HashMap<u16, AlgorithmMachine<C, A>> {
|
|
|
|
|
let mut included = vec![];
|
|
|
|
|
while included.len() < usize::from(keys[&1].params().t()) {
|
|
|
|
|
let n = u16::try_from((rng.next_u64() % u64::try_from(keys.len()).unwrap()) + 1).unwrap();
|
|
|
|
|
if included.contains(&n) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
included.push(n);
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-15 01:26:07 -04:00
|
|
|
keys
|
|
|
|
|
.iter()
|
|
|
|
|
.filter_map(|(i, keys)| {
|
2022-07-22 02:34:36 -04:00
|
|
|
if included.contains(i) {
|
2022-07-15 01:26:07 -04:00
|
|
|
Some((
|
|
|
|
|
*i,
|
|
|
|
|
AlgorithmMachine::new(algorithm.clone(), keys.clone(), &included.clone()).unwrap(),
|
|
|
|
|
))
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.collect()
|
2022-05-25 00:28:57 -04:00
|
|
|
}
|
|
|
|
|
|
2022-06-24 08:40:14 -04:00
|
|
|
pub fn sign<R: RngCore + CryptoRng, M: PreprocessMachine>(
|
2022-05-25 00:28:57 -04:00
|
|
|
rng: &mut R,
|
|
|
|
|
mut machines: HashMap<u16, M>,
|
2022-07-15 01:26:07 -04:00
|
|
|
msg: &[u8],
|
2022-05-25 00:28:57 -04:00
|
|
|
) -> M::Signature {
|
2022-05-24 21:41:14 -04:00
|
|
|
let mut commitments = HashMap::new();
|
2022-07-15 01:26:07 -04:00
|
|
|
let mut machines = machines
|
|
|
|
|
.drain()
|
|
|
|
|
.map(|(i, machine)| {
|
|
|
|
|
let (machine, preprocess) = machine.preprocess(rng);
|
|
|
|
|
commitments.insert(i, Cursor::new(preprocess));
|
|
|
|
|
(i, machine)
|
|
|
|
|
})
|
|
|
|
|
.collect::<HashMap<_, _>>();
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2022-05-24 21:41:14 -04:00
|
|
|
let mut shares = HashMap::new();
|
2022-07-15 01:26:07 -04:00
|
|
|
let mut machines = machines
|
|
|
|
|
.drain()
|
|
|
|
|
.map(|(i, machine)| {
|
|
|
|
|
let (machine, share) = machine.sign(clone_without(&commitments, &i), msg).unwrap();
|
|
|
|
|
shares.insert(i, Cursor::new(share));
|
|
|
|
|
(i, machine)
|
|
|
|
|
})
|
|
|
|
|
.collect::<HashMap<_, _>>();
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2022-05-24 21:41:14 -04:00
|
|
|
let mut signature = None;
|
2022-06-24 08:40:14 -04:00
|
|
|
for (i, machine) in machines.drain() {
|
|
|
|
|
let sig = machine.complete(clone_without(&shares, &i)).unwrap();
|
2022-05-24 21:41:14 -04:00
|
|
|
if signature.is_none() {
|
2022-05-25 00:28:57 -04:00
|
|
|
signature = Some(sig.clone());
|
2022-05-24 21:41:14 -04:00
|
|
|
}
|
2022-05-25 00:28:57 -04:00
|
|
|
assert_eq!(&sig, signature.as_ref().unwrap());
|
2022-05-24 21:41:14 -04:00
|
|
|
}
|
2022-05-25 00:28:57 -04:00
|
|
|
signature.unwrap()
|
2022-04-21 21:36:18 -04:00
|
|
|
}
|