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-05-21 15:33:35 -04:00
|
|
|
use curve25519_dalek::{scalar::Scalar, edwards::EdwardsPoint};
|
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;
|
|
|
|
|
|
|
|
|
|
mod core;
|
|
|
|
|
pub(crate) use self::core::MAX_M;
|
|
|
|
|
use self::core::prove;
|
|
|
|
|
|
|
|
|
|
pub(crate) const MAX_OUTPUTS: usize = MAX_M;
|
2022-06-19 12:03:01 -04:00
|
|
|
|
2022-07-22 02:34:36 -04:00
|
|
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
2022-05-21 15:33:35 -04:00
|
|
|
pub struct Bulletproofs {
|
|
|
|
|
pub A: EdwardsPoint,
|
|
|
|
|
pub S: EdwardsPoint,
|
|
|
|
|
pub T1: EdwardsPoint,
|
|
|
|
|
pub T2: EdwardsPoint,
|
|
|
|
|
pub taux: Scalar,
|
|
|
|
|
pub mu: Scalar,
|
|
|
|
|
pub L: Vec<EdwardsPoint>,
|
|
|
|
|
pub R: Vec<EdwardsPoint>,
|
|
|
|
|
pub a: Scalar,
|
|
|
|
|
pub b: Scalar,
|
2022-07-15 01:26:07 -04:00
|
|
|
pub t: Scalar,
|
2022-05-13 20:26:53 -04:00
|
|
|
}
|
2022-04-28 20:09:31 -04:00
|
|
|
|
2022-05-21 15:33:35 -04:00
|
|
|
impl Bulletproofs {
|
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-07-15 01:26:07 -04:00
|
|
|
pub fn new<R: RngCore + CryptoRng>(
|
|
|
|
|
rng: &mut R,
|
|
|
|
|
outputs: &[Commitment],
|
|
|
|
|
) -> 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-26 02:05:15 -05:00
|
|
|
Ok(prove(rng, outputs))
|
2022-05-21 15:33:35 -04:00
|
|
|
}
|
|
|
|
|
|
2022-07-07 14:28:42 -04:00
|
|
|
#[must_use]
|
2022-05-22 01:56:17 -04:00
|
|
|
pub fn verify<R: RngCore + CryptoRng>(&self, rng: &mut R, commitments: &[EdwardsPoint]) -> bool {
|
2022-05-21 15:33:35 -04:00
|
|
|
if commitments.len() > 16 {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-22 01:56:17 -04:00
|
|
|
let mut seed = [0; 32];
|
|
|
|
|
rng.fill_bytes(&mut seed);
|
|
|
|
|
|
2022-05-21 15:33:35 -04:00
|
|
|
let mut serialized = Vec::with_capacity((9 + (2 * self.L.len())) * 32);
|
|
|
|
|
self.serialize(&mut serialized).unwrap();
|
2022-07-15 01:26:07 -04:00
|
|
|
let commitments: Vec<[u8; 32]> = commitments
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|commitment| (commitment * Scalar::from(8u8).invert()).compress().to_bytes())
|
|
|
|
|
.collect();
|
2022-05-22 01:56:17 -04:00
|
|
|
|
2022-05-21 15:33:35 -04:00
|
|
|
unsafe {
|
|
|
|
|
#[link(name = "wrapper")]
|
|
|
|
|
extern "C" {
|
|
|
|
|
fn c_verify_bp(
|
2022-05-22 01:56:17 -04:00
|
|
|
seed: *const u8,
|
2022-05-21 15:33:35 -04:00
|
|
|
serialized_len: usize,
|
|
|
|
|
serialized: *const u8,
|
|
|
|
|
commitments_len: u8,
|
2022-07-15 01:26:07 -04:00
|
|
|
commitments: *const [u8; 32],
|
2022-05-21 15:33:35 -04:00
|
|
|
) -> bool;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-30 02:14:34 -04:00
|
|
|
c_verify_bp(
|
|
|
|
|
seed.as_ptr(),
|
|
|
|
|
serialized.len(),
|
|
|
|
|
serialized.as_ptr(),
|
|
|
|
|
u8::try_from(commitments.len()).unwrap(),
|
2022-07-15 01:26:07 -04:00
|
|
|
commitments.as_ptr(),
|
2022-05-30 02:14:34 -04:00
|
|
|
)
|
2022-05-21 15:33:35 -04:00
|
|
|
}
|
2022-04-28 03:31:09 -04: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-05-21 15:33:35 -04:00
|
|
|
write_point(&self.A, w)?;
|
|
|
|
|
write_point(&self.S, w)?;
|
|
|
|
|
write_point(&self.T1, w)?;
|
|
|
|
|
write_point(&self.T2, w)?;
|
|
|
|
|
write_scalar(&self.taux, w)?;
|
|
|
|
|
write_scalar(&self.mu, w)?;
|
|
|
|
|
specific_write_vec(&self.L, w)?;
|
|
|
|
|
specific_write_vec(&self.R, w)?;
|
|
|
|
|
write_scalar(&self.a, w)?;
|
|
|
|
|
write_scalar(&self.b, w)?;
|
|
|
|
|
write_scalar(&self.t, w)
|
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> {
|
|
|
|
|
let bp = Bulletproofs {
|
|
|
|
|
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-05-21 15:33:35 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if bp.L.len() != bp.R.len() {
|
|
|
|
|
Err(std::io::Error::new(std::io::ErrorKind::Other, "mismatched L/R len"))?;
|
|
|
|
|
}
|
|
|
|
|
Ok(bp)
|
2022-04-28 20:09:31 -04:00
|
|
|
}
|
|
|
|
|
}
|