2022-05-21 15:33:35 -04:00
|
|
|
#![allow(non_snake_case)]
|
2022-04-28 03:31:09 -04:00
|
|
|
|
2023-06-29 04:14:29 -04:00
|
|
|
use std_shims::{
|
|
|
|
|
vec::Vec,
|
|
|
|
|
io::{self, Read, Write},
|
|
|
|
|
};
|
2023-01-07 05:18:35 -05:00
|
|
|
|
2022-05-22 01:56:17 -04:00
|
|
|
use rand_core::{RngCore, CryptoRng};
|
|
|
|
|
|
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-26 08:06:56 -04:00
|
|
|
use curve25519_dalek::edwards::EdwardsPoint;
|
2022-07-31 21:45:53 -05:00
|
|
|
use multiexp::BatchVerifier;
|
2022-04-28 03:31:09 -04:00
|
|
|
|
2022-05-21 15:33:35 -04:00
|
|
|
use crate::{Commitment, wallet::TransactionError, serialize::*};
|
2022-05-13 20:26:53 -04:00
|
|
|
|
2022-07-26 02:05:15 -05:00
|
|
|
pub(crate) mod scalar_vector;
|
2022-07-31 23:12:45 -04:00
|
|
|
pub(crate) mod core;
|
2022-08-21 10:35:10 -04:00
|
|
|
use self::core::LOG_N;
|
2022-07-26 02:05:15 -05:00
|
|
|
|
2022-07-31 23:12:45 -04:00
|
|
|
pub(crate) mod original;
|
2022-08-21 11:06:17 -04:00
|
|
|
pub use original::GENERATORS as BULLETPROOFS_GENERATORS;
|
2022-07-31 23:12:45 -04:00
|
|
|
pub(crate) mod plus;
|
2022-08-21 11:06:17 -04:00
|
|
|
pub use plus::GENERATORS as BULLETPROOFS_PLUS_GENERATORS;
|
2022-07-26 02:05:15 -05:00
|
|
|
|
2022-07-31 23:12:45 -04:00
|
|
|
pub(crate) use self::original::OriginalStruct;
|
|
|
|
|
pub(crate) use self::plus::PlusStruct;
|
|
|
|
|
|
|
|
|
|
pub(crate) const MAX_OUTPUTS: usize = self::core::MAX_M;
|
|
|
|
|
|
2022-09-28 07:44:49 -05:00
|
|
|
/// Bulletproofs enum, supporting the original and plus formulations.
|
2022-07-31 23:12:45 -04:00
|
|
|
#[allow(clippy::large_enum_variant)]
|
|
|
|
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
|
|
|
|
pub enum Bulletproofs {
|
|
|
|
|
Original(OriginalStruct),
|
|
|
|
|
Plus(PlusStruct),
|
|
|
|
|
}
|
2022-06-19 12:03:01 -04:00
|
|
|
|
2022-05-21 15:33:35 -04:00
|
|
|
impl Bulletproofs {
|
2022-08-21 10:35:10 -04:00
|
|
|
pub(crate) fn fee_weight(plus: bool, outputs: usize) -> usize {
|
|
|
|
|
let fields = if plus { 6 } else { 9 };
|
|
|
|
|
|
2023-03-12 03:54:30 -04:00
|
|
|
// TODO: Shouldn't this use u32/u64?
|
2022-08-21 10:35:10 -04:00
|
|
|
#[allow(non_snake_case)]
|
|
|
|
|
let mut LR_len = usize::try_from(usize::BITS - (outputs - 1).leading_zeros()).unwrap();
|
|
|
|
|
let padded_outputs = 1 << LR_len;
|
|
|
|
|
LR_len += LOG_N;
|
|
|
|
|
|
|
|
|
|
let len = (fields + (2 * LR_len)) * 32;
|
|
|
|
|
len +
|
|
|
|
|
if padded_outputs <= 2 {
|
|
|
|
|
0
|
|
|
|
|
} else {
|
|
|
|
|
let base = ((fields + (2 * (LOG_N + 1))) * 32) / 2;
|
|
|
|
|
let size = (fields + (2 * LR_len)) * 32;
|
|
|
|
|
((base * padded_outputs) - size) * 4 / 5
|
|
|
|
|
}
|
2022-06-19 12:03:01 -04:00
|
|
|
}
|
|
|
|
|
|
2022-09-28 07:44:49 -05:00
|
|
|
/// Prove the list of commitments are within [0 .. 2^64).
|
2022-07-26 08:06:56 -04:00
|
|
|
pub fn prove<R: RngCore + CryptoRng>(
|
2022-07-15 01:26:07 -04:00
|
|
|
rng: &mut R,
|
|
|
|
|
outputs: &[Commitment],
|
2022-07-27 04:05:43 -05:00
|
|
|
plus: bool,
|
2023-07-08 00:56:43 -04:00
|
|
|
) -> Result<Self, TransactionError> {
|
2022-06-19 12:03:01 -04:00
|
|
|
if outputs.len() > MAX_OUTPUTS {
|
2022-05-21 15:33:35 -04:00
|
|
|
return Err(TransactionError::TooManyOutputs)?;
|
|
|
|
|
}
|
2022-07-31 23:12:45 -04:00
|
|
|
Ok(if !plus {
|
2023-07-08 00:56:43 -04:00
|
|
|
Self::Plus(PlusStruct::prove(rng, outputs))
|
2022-07-31 23:12:45 -04:00
|
|
|
} else {
|
2023-07-08 00:56:43 -04:00
|
|
|
Self::Original(OriginalStruct::prove(rng, outputs))
|
2022-07-31 23:12:45 -04:00
|
|
|
})
|
2022-05-21 15:33:35 -04:00
|
|
|
}
|
|
|
|
|
|
2022-09-28 07:44:49 -05:00
|
|
|
/// Verify the given Bulletproofs.
|
2022-07-31 21:45:53 -05:00
|
|
|
#[must_use]
|
|
|
|
|
pub fn verify<R: RngCore + CryptoRng>(&self, rng: &mut R, commitments: &[EdwardsPoint]) -> bool {
|
|
|
|
|
match self {
|
2023-07-08 00:56:43 -04:00
|
|
|
Self::Original(bp) => bp.verify(rng, commitments),
|
|
|
|
|
Self::Plus(bp) => bp.verify(rng, commitments),
|
2022-07-31 21:45:53 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-28 07:44:49 -05:00
|
|
|
/// Accumulate the verification for the given Bulletproofs into the specified BatchVerifier.
|
|
|
|
|
/// Returns false if the Bulletproofs aren't sane, without mutating the BatchVerifier.
|
|
|
|
|
/// Returns true if the Bulletproofs are sane, regardless of their validity.
|
2022-07-31 21:45:53 -05:00
|
|
|
#[must_use]
|
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 batch_verify<ID: Copy + Zeroize, R: RngCore + CryptoRng>(
|
2022-07-31 21:45:53 -05:00
|
|
|
&self,
|
|
|
|
|
rng: &mut R,
|
|
|
|
|
verifier: &mut BatchVerifier<ID, dalek_ff_group::EdwardsPoint>,
|
|
|
|
|
id: ID,
|
|
|
|
|
commitments: &[EdwardsPoint],
|
|
|
|
|
) -> bool {
|
|
|
|
|
match self {
|
2023-07-08 00:56:43 -04:00
|
|
|
Self::Original(bp) => bp.batch_verify(rng, verifier, id, commitments),
|
|
|
|
|
Self::Plus(bp) => bp.batch_verify(rng, verifier, id, commitments),
|
2022-07-31 21:45:53 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-07 05:18:35 -05:00
|
|
|
fn write_core<W: Write, F: Fn(&[EdwardsPoint], &mut W) -> io::Result<()>>(
|
2022-07-15 01:26:07 -04:00
|
|
|
&self,
|
|
|
|
|
w: &mut W,
|
|
|
|
|
specific_write_vec: F,
|
2023-01-07 05:18:35 -05:00
|
|
|
) -> io::Result<()> {
|
2022-07-26 08:06:56 -04:00
|
|
|
match self {
|
2023-07-08 00:56:43 -04:00
|
|
|
Self::Original(bp) => {
|
2022-07-31 21:45:53 -05:00
|
|
|
write_point(&bp.A, w)?;
|
|
|
|
|
write_point(&bp.S, w)?;
|
|
|
|
|
write_point(&bp.T1, w)?;
|
|
|
|
|
write_point(&bp.T2, w)?;
|
|
|
|
|
write_scalar(&bp.taux, w)?;
|
|
|
|
|
write_scalar(&bp.mu, w)?;
|
|
|
|
|
specific_write_vec(&bp.L, w)?;
|
|
|
|
|
specific_write_vec(&bp.R, w)?;
|
|
|
|
|
write_scalar(&bp.a, w)?;
|
|
|
|
|
write_scalar(&bp.b, w)?;
|
|
|
|
|
write_scalar(&bp.t, w)
|
2022-07-26 08:06:56 -04:00
|
|
|
}
|
2022-07-27 04:05:43 -05:00
|
|
|
|
2023-07-08 00:56:43 -04:00
|
|
|
Self::Plus(bp) => {
|
2022-07-31 21:45:53 -05:00
|
|
|
write_point(&bp.A, w)?;
|
|
|
|
|
write_point(&bp.A1, w)?;
|
|
|
|
|
write_point(&bp.B, w)?;
|
|
|
|
|
write_scalar(&bp.r1, w)?;
|
|
|
|
|
write_scalar(&bp.s1, w)?;
|
|
|
|
|
write_scalar(&bp.d1, w)?;
|
|
|
|
|
specific_write_vec(&bp.L, w)?;
|
|
|
|
|
specific_write_vec(&bp.R, w)
|
2022-07-27 04:05:43 -05:00
|
|
|
}
|
2022-07-26 08:06:56 -04:00
|
|
|
}
|
2022-04-28 03:31:09 -04:00
|
|
|
}
|
|
|
|
|
|
2023-01-07 05:18:35 -05:00
|
|
|
pub(crate) fn signature_write<W: Write>(&self, w: &mut W) -> io::Result<()> {
|
|
|
|
|
self.write_core(w, |points, w| write_raw_vec(write_point, points, w))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn write<W: Write>(&self, w: &mut W) -> io::Result<()> {
|
|
|
|
|
self.write_core(w, |points, w| write_vec(write_point, points, w))
|
2022-05-21 15:33:35 -04:00
|
|
|
}
|
2022-04-28 20:09:31 -04:00
|
|
|
|
2023-01-07 05:18:35 -05:00
|
|
|
pub fn serialize(&self) -> Vec<u8> {
|
|
|
|
|
let mut serialized = vec![];
|
|
|
|
|
self.write(&mut serialized).unwrap();
|
|
|
|
|
serialized
|
2022-04-28 20:09:31 -04:00
|
|
|
}
|
|
|
|
|
|
2023-01-07 05:18:35 -05:00
|
|
|
/// Read Bulletproofs.
|
2023-07-08 00:56:43 -04:00
|
|
|
pub fn read<R: Read>(r: &mut R) -> io::Result<Self> {
|
|
|
|
|
Ok(Self::Original(OriginalStruct {
|
2022-05-21 15:33:35 -04:00
|
|
|
A: read_point(r)?,
|
|
|
|
|
S: read_point(r)?,
|
|
|
|
|
T1: read_point(r)?,
|
|
|
|
|
T2: read_point(r)?,
|
|
|
|
|
taux: read_scalar(r)?,
|
|
|
|
|
mu: read_scalar(r)?,
|
2022-05-21 20:27:21 -04:00
|
|
|
L: read_vec(read_point, r)?,
|
|
|
|
|
R: read_vec(read_point, r)?,
|
2022-05-21 15:33:35 -04:00
|
|
|
a: read_scalar(r)?,
|
|
|
|
|
b: read_scalar(r)?,
|
2022-07-15 01:26:07 -04:00
|
|
|
t: read_scalar(r)?,
|
2022-07-31 21:45:53 -05:00
|
|
|
}))
|
2022-04-28 20:09:31 -04:00
|
|
|
}
|
2022-07-27 04:05:43 -05:00
|
|
|
|
2023-01-07 05:18:35 -05:00
|
|
|
/// Read Bulletproofs+.
|
2023-07-08 00:56:43 -04:00
|
|
|
pub fn read_plus<R: Read>(r: &mut R) -> io::Result<Self> {
|
|
|
|
|
Ok(Self::Plus(PlusStruct {
|
2022-07-27 04:05:43 -05:00
|
|
|
A: read_point(r)?,
|
|
|
|
|
A1: read_point(r)?,
|
|
|
|
|
B: read_point(r)?,
|
|
|
|
|
r1: read_scalar(r)?,
|
|
|
|
|
s1: read_scalar(r)?,
|
|
|
|
|
d1: read_scalar(r)?,
|
|
|
|
|
L: read_vec(read_point, r)?,
|
|
|
|
|
R: read_vec(read_point, r)?,
|
2022-07-31 21:45:53 -05:00
|
|
|
}))
|
2022-07-27 04:05:43 -05:00
|
|
|
}
|
2022-04-28 20:09:31 -04:00
|
|
|
}
|