2022-05-21 15:33:35 -04:00
|
|
|
#![allow(non_snake_case)]
|
2022-04-28 03:31:09 -04:00
|
|
|
|
2022-05-22 01:56:17 -04:00
|
|
|
use rand_core::{RngCore, CryptoRng};
|
|
|
|
|
|
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-07-26 02:05:15 -05:00
|
|
|
|
2022-07-31 23:12:45 -04:00
|
|
|
pub(crate) mod original;
|
|
|
|
|
pub(crate) mod plus;
|
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;
|
|
|
|
|
|
|
|
|
|
#[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-07-27 04:05:43 -05:00
|
|
|
// TODO
|
2022-06-19 12:03:01 -04:00
|
|
|
pub(crate) fn fee_weight(outputs: usize) -> usize {
|
|
|
|
|
let proofs = 6 + usize::try_from(usize::BITS - (outputs - 1).leading_zeros()).unwrap();
|
|
|
|
|
let len = (9 + (2 * proofs)) * 32;
|
|
|
|
|
|
|
|
|
|
let mut clawback = 0;
|
|
|
|
|
let padded = 1 << (proofs - 6);
|
|
|
|
|
if padded > 2 {
|
|
|
|
|
const BP_BASE: usize = 368;
|
|
|
|
|
clawback = ((BP_BASE * padded) - len) * 4 / 5;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
len + clawback
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-02 15:52:27 -04:00
|
|
|
pub fn init(plus: bool) {
|
|
|
|
|
if !plus {
|
|
|
|
|
OriginalStruct::init();
|
|
|
|
|
} else {
|
|
|
|
|
PlusStruct::init();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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,
|
2022-07-15 01:26:07 -04:00
|
|
|
) -> Result<Bulletproofs, 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 {
|
|
|
|
|
Bulletproofs::Original(OriginalStruct::prove(rng, outputs))
|
|
|
|
|
} else {
|
|
|
|
|
Bulletproofs::Plus(PlusStruct::prove(rng, outputs))
|
|
|
|
|
})
|
2022-05-21 15:33:35 -04:00
|
|
|
}
|
|
|
|
|
|
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 {
|
|
|
|
|
Bulletproofs::Original(bp) => bp.verify(rng, commitments),
|
2022-07-31 23:12:45 -04:00
|
|
|
Bulletproofs::Plus(bp) => bp.verify(rng, commitments),
|
2022-07-31 21:45:53 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[must_use]
|
|
|
|
|
pub fn batch_verify<ID: Copy, R: RngCore + CryptoRng>(
|
|
|
|
|
&self,
|
|
|
|
|
rng: &mut R,
|
|
|
|
|
verifier: &mut BatchVerifier<ID, dalek_ff_group::EdwardsPoint>,
|
|
|
|
|
id: ID,
|
|
|
|
|
commitments: &[EdwardsPoint],
|
|
|
|
|
) -> bool {
|
|
|
|
|
match self {
|
|
|
|
|
Bulletproofs::Original(bp) => bp.batch_verify(rng, verifier, id, commitments),
|
2022-07-31 23:12:45 -04:00
|
|
|
Bulletproofs::Plus(bp) => bp.batch_verify(rng, verifier, id, commitments),
|
2022-07-31 21:45:53 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-15 01:26:07 -04:00
|
|
|
fn serialize_core<W: std::io::Write, F: Fn(&[EdwardsPoint], &mut W) -> std::io::Result<()>>(
|
|
|
|
|
&self,
|
|
|
|
|
w: &mut W,
|
|
|
|
|
specific_write_vec: F,
|
|
|
|
|
) -> std::io::Result<()> {
|
2022-07-26 08:06:56 -04:00
|
|
|
match self {
|
2022-07-31 21:45:53 -05:00
|
|
|
Bulletproofs::Original(bp) => {
|
|
|
|
|
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
|
|
|
|
2022-07-31 21:45:53 -05:00
|
|
|
Bulletproofs::Plus(bp) => {
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2022-05-21 15:33:35 -04:00
|
|
|
pub fn signature_serialize<W: std::io::Write>(&self, w: &mut W) -> std::io::Result<()> {
|
|
|
|
|
self.serialize_core(w, |points, w| write_raw_vec(write_point, points, w))
|
|
|
|
|
}
|
2022-04-28 20:09:31 -04:00
|
|
|
|
2022-05-21 15:33:35 -04:00
|
|
|
pub fn serialize<W: std::io::Write>(&self, w: &mut W) -> std::io::Result<()> {
|
|
|
|
|
self.serialize_core(w, |points, w| write_vec(write_point, points, w))
|
2022-04-28 20:09:31 -04:00
|
|
|
}
|
|
|
|
|
|
2022-05-21 15:33:35 -04:00
|
|
|
pub fn deserialize<R: std::io::Read>(r: &mut R) -> std::io::Result<Bulletproofs> {
|
2022-07-31 21:45:53 -05:00
|
|
|
Ok(Bulletproofs::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
|
|
|
|
|
|
|
|
pub fn deserialize_plus<R: std::io::Read>(r: &mut R) -> std::io::Result<Bulletproofs> {
|
2022-07-31 21:45:53 -05:00
|
|
|
Ok(Bulletproofs::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
|
|
|
}
|