update MLSAG, ss2_elements will always be 2

This commit is contained in:
Boog900
2023-06-23 23:29:43 +01:00
parent c1a2aafc83
commit e4a615aa76
5 changed files with 19 additions and 27 deletions

View File

@@ -11,6 +11,7 @@ use sha3::{Digest, Keccak256};
use curve25519_dalek::edwards::{EdwardsPoint as DalekPoint, CompressedEdwardsY};
use curve25519_dalek::scalar::Scalar;
use std_shims::vec::Vec;
use group::{Group, GroupEncoding};
use dalek_ff_group::EdwardsPoint;

View File

@@ -29,8 +29,8 @@ pub struct BorroSig {
impl BorroSig {
pub fn read<R: Read>(r: &mut R) -> io::Result<BorroSig> {
Ok(BorroSig {
s0: read_64_array(read_bytes, r)?,
s1: read_64_array(read_bytes, r)?,
s0: read_array(read_bytes, r)?,
s1: read_array(read_bytes, r)?,
ee: read_bytes(r)?,
})
}
@@ -53,7 +53,7 @@ pub struct RangeSig {
impl RangeSig {
pub fn read<R: Read>(r: &mut R) -> io::Result<RangeSig> {
Ok(RangeSig { asig: BorroSig::read(r)?, Ci: read_64_array(read_point, r)? })
Ok(RangeSig { asig: BorroSig::read(r)?, Ci: read_array(read_point, r)? })
}
pub fn write<W: Write>(&self, w: &mut W) -> io::Result<()> {
self.asig.write(w)?;

View File

@@ -9,14 +9,14 @@ use curve25519_dalek::edwards::EdwardsPoint;
use crate::{hash_to_scalar, serialize::*};
use crate::ringct::hash_to_point;
/// MLSAG signature, as used in Monero.
/// MgSig part of MLSAG, as used in Monero.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Mlsag {
pub ss: Vec<Vec<Scalar>>,
pub struct MgSig {
pub ss: Vec<[Scalar; 2]>,
pub cc: Scalar,
}
impl Mlsag {
impl MgSig {
pub fn write<W: Write>(&self, w: &mut W) -> io::Result<()> {
for ss in self.ss.iter() {
write_raw_vec(write_scalar, ss, w)?;
@@ -24,11 +24,9 @@ impl Mlsag {
write_scalar(&self.cc, w)
}
pub fn read<R: Read>(mixins: usize, ss2_elements: usize, r: &mut R) -> io::Result<Mlsag> {
Ok(Mlsag {
ss: (0 .. mixins)
.map(|_| read_raw_vec(read_scalar, ss2_elements, r))
.collect::<Result<_, _>>()?,
pub fn read<R: Read>(mixins: usize, r: &mut R) -> io::Result<MgSig> {
Ok(MgSig {
ss: (0 .. mixins).map(|_| read_array(read_scalar, r)).collect::<Result<_, _>>()?,
cc: read_scalar(r)?,
})
}

View File

@@ -20,7 +20,7 @@ pub mod bulletproofs;
use crate::{
Protocol,
serialize::*,
ringct::{clsag::Clsag, mlsag::Mlsag, bulletproofs::Bulletproofs, borromean::RangeSig},
ringct::{clsag::Clsag, mlsag::MgSig, bulletproofs::Bulletproofs, borromean::RangeSig},
};
/// Generate a key image for a given key. Defined as `x * hash_to_point(xG)`.
@@ -197,17 +197,10 @@ impl RctPrunable {
) -> io::Result<RctPrunable> {
Ok(match rct_type {
0 => RctPrunable::Null,
1 => RctPrunable::Borromean {
1 | 2 => RctPrunable::Borromean {
range_sigs: read_raw_vec(RangeSig::read, outputs, r)?,
mlsags: vec![Mlsag::read(decoys[0], 1 + decoys.len(), r)?],
simple: false,
},
2 => RctPrunable::Borromean {
range_sigs: read_raw_vec(RangeSig::read, outputs, r)?,
mlsags: (0 .. decoys.len())
.map(|o| Mlsag::read(decoys[o], 2, r))
.collect::<Result<_, _>>()?,
simple: true,
mlsags: decoys.iter().map(|d| MgSig::read(*d, r)).collect::<Result<_, _>>()?,
simple: rct_type == 2,
},
3 | 4 => RctPrunable::BulletProof {
bulletproofs: read_raw_vec(
@@ -219,7 +212,7 @@ impl RctPrunable {
},
r,
)?,
mlsags: decoys.iter().map(|d| Mlsag::read(*d, 2, r)).collect::<Result<_, _>>()?,
mlsags: decoys.iter().map(|d| MgSig::read(*d, r)).collect::<Result<_, _>>()?,
pseudo_outs: read_raw_vec(read_point, decoys.len(), r)?,
v2: rct_type == 4,
},

View File

@@ -145,9 +145,9 @@ pub(crate) fn read_vec<R: Read, T, F: Fn(&mut R) -> io::Result<T>>(
read_raw_vec(f, read_varint(r)?.try_into().unwrap(), r)
}
pub(crate) fn read_64_array<R: Read, T: Debug, F: Fn(&mut R) -> io::Result<T>>(
pub(crate) fn read_array<const N: usize, R: Read, T: Debug, F: Fn(&mut R) -> io::Result<T>>(
f: F,
r: &mut R,
) -> io::Result<[T; 64]> {
(0 .. 64).map(|_| f(r)).collect::<io::Result<Vec<T>>>().map(|vec| vec.try_into().unwrap())
) -> io::Result<[T; N]> {
(0 .. N).map(|_| f(r)).collect::<io::Result<Vec<T>>>().map(|vec| vec.try_into().unwrap())
}