#![allow(non_snake_case)] use rand_core::{RngCore, CryptoRng}; use curve25519_dalek::{scalar::Scalar, edwards::EdwardsPoint}; use crate::{Commitment, wallet::TransactionError, serialize::*}; 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; #[derive(Clone, PartialEq, Eq, Debug)] pub struct Bulletproofs { pub A: EdwardsPoint, pub S: EdwardsPoint, pub T1: EdwardsPoint, pub T2: EdwardsPoint, pub taux: Scalar, pub mu: Scalar, pub L: Vec, pub R: Vec, pub a: Scalar, pub b: Scalar, pub t: Scalar, } impl Bulletproofs { 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 } pub fn new( rng: &mut R, outputs: &[Commitment], ) -> Result { if outputs.len() > MAX_OUTPUTS { return Err(TransactionError::TooManyOutputs)?; } Ok(prove(rng, outputs)) } #[must_use] pub fn verify(&self, rng: &mut R, commitments: &[EdwardsPoint]) -> bool { if commitments.len() > 16 { return false; } let mut seed = [0; 32]; rng.fill_bytes(&mut seed); let mut serialized = Vec::with_capacity((9 + (2 * self.L.len())) * 32); self.serialize(&mut serialized).unwrap(); let commitments: Vec<[u8; 32]> = commitments .iter() .map(|commitment| (commitment * Scalar::from(8u8).invert()).compress().to_bytes()) .collect(); unsafe { #[link(name = "wrapper")] extern "C" { fn c_verify_bp( seed: *const u8, serialized_len: usize, serialized: *const u8, commitments_len: u8, commitments: *const [u8; 32], ) -> bool; } c_verify_bp( seed.as_ptr(), serialized.len(), serialized.as_ptr(), u8::try_from(commitments.len()).unwrap(), commitments.as_ptr(), ) } } fn serialize_core std::io::Result<()>>( &self, w: &mut W, specific_write_vec: F, ) -> std::io::Result<()> { 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) } pub fn signature_serialize(&self, w: &mut W) -> std::io::Result<()> { self.serialize_core(w, |points, w| write_raw_vec(write_point, points, w)) } pub fn serialize(&self, w: &mut W) -> std::io::Result<()> { self.serialize_core(w, |points, w| write_vec(write_point, points, w)) } pub fn deserialize(r: &mut R) -> std::io::Result { 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)?, L: read_vec(read_point, r)?, R: read_vec(read_point, r)?, a: read_scalar(r)?, b: read_scalar(r)?, t: read_scalar(r)?, }; if bp.L.len() != bp.R.len() { Err(std::io::Error::new(std::io::ErrorKind::Other, "mismatched L/R len"))?; } Ok(bp) } }