Skip to content

Commit 691b9c4

Browse files
Use EdwardsPoint and Scalar for ringct types
Fixes #18.
1 parent 49e0383 commit 691b9c4

File tree

6 files changed

+155
-254
lines changed

6 files changed

+155
-254
lines changed

src/blockdata/transaction.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -601,24 +601,24 @@ impl Transaction {
601601
let mut keccak = tiny_keccak::Keccak::v256();
602602

603603
for bp in bulletproofs {
604-
keccak.update(&bp.A.key);
605-
keccak.update(&bp.S.key);
606-
keccak.update(&bp.T1.key);
607-
keccak.update(&bp.T2.key);
608-
keccak.update(&bp.taux.key);
609-
keccak.update(&bp.mu.key);
604+
keccak.update(bp.A.compress().as_bytes());
605+
keccak.update(bp.S.compress().as_bytes());
606+
keccak.update(bp.T1.compress().as_bytes());
607+
keccak.update(bp.T2.compress().as_bytes());
608+
keccak.update(bp.taux.as_bytes());
609+
keccak.update(bp.mu.as_bytes());
610610

611611
for i in &bp.L {
612-
keccak.update(&i.key);
612+
keccak.update(i.compress().as_bytes());
613613
}
614614

615615
for i in &bp.R {
616-
keccak.update(&i.key);
616+
keccak.update(i.compress().as_bytes());
617617
}
618618

619-
keccak.update(&bp.a.key);
620-
keccak.update(&bp.b.key);
621-
keccak.update(&bp.t.key);
619+
keccak.update(bp.a.as_bytes());
620+
keccak.update(bp.b.as_bytes());
621+
keccak.update(bp.t.as_bytes());
622622
}
623623

624624
let mut hash = [0u8; 32];
@@ -659,7 +659,7 @@ impl hash::Hashable for Transaction {
659659
match *self.prefix.version {
660660
1 => hash::Hash::hash(&serialize(self)),
661661
_ => {
662-
let mut hashes: Vec<hash::Hash> = vec![self.prefix.hash()];
662+
let mut hashes = vec![self.prefix.hash()];
663663
if let Some(sig_base) = &self.rct_signatures.sig {
664664
hashes.push(sig_base.hash());
665665
if sig_base.rct_type == RctType::Null {
@@ -697,7 +697,7 @@ impl hash::Hashable for Transaction {
697697

698698
impl Decodable for ExtraField {
699699
fn consensus_decode<D: io::Read>(d: &mut D) -> Result<ExtraField, encode::Error> {
700-
let mut fields: Vec<SubField> = vec![];
700+
let mut fields = vec![];
701701
let bytes: Vec<u8> = Decodable::consensus_decode(d)?;
702702
let mut decoder = io::Cursor::new(&bytes[..]);
703703
// Decode each extra field

src/bulletproof/mod.rs

Lines changed: 104 additions & 178 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use core::iter;
99
use std::convert::TryFrom;
1010

11-
use curve25519_dalek::edwards::{CompressedEdwardsY, EdwardsPoint};
11+
use curve25519_dalek::edwards::EdwardsPoint;
1212
use curve25519_dalek::scalar::Scalar;
1313
use curve25519_dalek::traits::{IsIdentity, VartimeMultiscalarMul};
1414
use keccak_hash::keccak_256;
@@ -540,117 +540,48 @@ pub enum MpcError {
540540

541541
impl From<RangeProof> for crate::util::ringct::Bulletproof {
542542
fn from(from: RangeProof) -> Self {
543-
use crate::util::ringct::Key;
544-
545543
Self {
546-
A: Key {
547-
key: from.A.compress().to_bytes(),
548-
},
549-
S: Key {
550-
key: from.S.compress().to_bytes(),
551-
},
552-
T1: Key {
553-
key: from.T_1.compress().to_bytes(),
554-
},
555-
T2: Key {
556-
key: from.T_2.compress().to_bytes(),
557-
},
558-
taux: Key {
559-
key: from.t_x_blinding.to_bytes(),
560-
},
561-
mu: Key {
562-
key: from.e_blinding.to_bytes(),
563-
},
564-
L: from
565-
.ipp_proof
566-
.L_vec
567-
.iter()
568-
.map(|l| Key {
569-
key: l.compress().to_bytes(),
570-
})
571-
.collect(),
572-
R: from
573-
.ipp_proof
574-
.R_vec
575-
.iter()
576-
.map(|r| Key {
577-
key: r.compress().to_bytes(),
578-
})
579-
.collect(),
580-
a: Key {
581-
key: from.ipp_proof.a.to_bytes(),
582-
},
583-
b: Key {
584-
key: from.ipp_proof.b.to_bytes(),
585-
},
586-
t: Key {
587-
key: from.t_x.to_bytes(),
588-
},
544+
A: from.A,
545+
S: from.S,
546+
T1: from.T_1,
547+
T2: from.T_2,
548+
taux: from.t_x_blinding,
549+
mu: from.e_blinding,
550+
L: from.ipp_proof.L_vec,
551+
R: from.ipp_proof.R_vec,
552+
a: from.ipp_proof.a,
553+
b: from.ipp_proof.b,
554+
t: from.t_x,
589555
}
590556
}
591557
}
592558

593-
#[derive(Debug)]
594-
pub enum ConversionError {
595-
InvalidPoint,
596-
NonCanonicalScalar,
597-
}
598-
599-
impl TryFrom<crate::util::ringct::Bulletproof> for RangeProof {
600-
type Error = ConversionError;
601-
602-
fn try_from(from: crate::util::ringct::Bulletproof) -> Result<Self, ConversionError> {
603-
Ok(Self {
604-
A: CompressedEdwardsY::from_slice(&from.A.key)
605-
.decompress()
606-
.ok_or(ConversionError::InvalidPoint)?,
607-
S: CompressedEdwardsY::from_slice(&from.S.key)
608-
.decompress()
609-
.ok_or(ConversionError::InvalidPoint)?,
610-
T_1: CompressedEdwardsY::from_slice(&from.T1.key)
611-
.decompress()
612-
.ok_or(ConversionError::InvalidPoint)?,
613-
T_2: CompressedEdwardsY::from_slice(&from.T2.key)
614-
.decompress()
615-
.ok_or(ConversionError::InvalidPoint)?,
616-
t_x: Scalar::from_canonical_bytes(from.t.key)
617-
.ok_or(ConversionError::NonCanonicalScalar)?,
618-
t_x_blinding: Scalar::from_canonical_bytes(from.taux.key)
619-
.ok_or(ConversionError::NonCanonicalScalar)?,
620-
e_blinding: Scalar::from_canonical_bytes(from.mu.key)
621-
.ok_or(ConversionError::NonCanonicalScalar)?,
559+
impl From<crate::util::ringct::Bulletproof> for RangeProof {
560+
fn from(from: crate::util::ringct::Bulletproof) -> Self {
561+
Self {
562+
A: from.A,
563+
S: from.S,
564+
T_1: from.T1,
565+
T_2: from.T2,
566+
t_x: from.t,
567+
t_x_blinding: from.taux,
568+
e_blinding: from.mu,
622569
ipp_proof: InnerProductProof {
623-
L_vec: from
624-
.L
625-
.iter()
626-
.map(|L| {
627-
CompressedEdwardsY::from_slice(&L.key)
628-
.decompress()
629-
.ok_or(ConversionError::InvalidPoint)
630-
})
631-
.collect::<Result<Vec<_>, _>>()?,
632-
R_vec: from
633-
.R
634-
.iter()
635-
.map(|R| {
636-
CompressedEdwardsY::from_slice(&R.key)
637-
.decompress()
638-
.ok_or(ConversionError::InvalidPoint)
639-
})
640-
.collect::<Result<Vec<_>, _>>()?,
641-
a: Scalar::from_canonical_bytes(from.a.key)
642-
.ok_or(ConversionError::NonCanonicalScalar)?,
643-
b: Scalar::from_canonical_bytes(from.b.key)
644-
.ok_or(ConversionError::NonCanonicalScalar)?,
570+
L_vec: from.L,
571+
R_vec: from.R,
572+
a: from.a,
573+
b: from.b,
645574
},
646-
})
575+
}
647576
}
648577
}
649578

650579
#[cfg(test)]
651580
mod tests {
652581
use super::*;
582+
use curve25519_dalek::edwards::CompressedEdwardsY;
653583
use rand::thread_rng;
584+
use std::convert::TryInto;
654585

655586
#[test]
656587
fn public_api() {
@@ -780,101 +711,96 @@ mod tests {
780711
#[test]
781712
fn verify_monero_mainnet_bulletproof() {
782713
use crate::util::ringct::Bulletproof;
783-
use crate::util::ringct::Key;
784714
use hex_literal::hex;
785715

786716
// data from:
787717
// https://xmrchain.net/tx/f34e0414a413cc7d6d4452b1a962f08be6de937eeb76fed9ca0774f5cb3b161b/1
788718

789719
let proof = Bulletproof {
790-
A: Key {
791-
key: hex!("78ddbccf2e1ced3b68835600768770ebe3e219db19a35f5ebe6495ec58c763d4"),
792-
},
793-
S: Key {
794-
key: hex!("e61bd5f461172a14d31149207a9f473289f89dbf4c42dff5f7cbcbd87a12210e"),
795-
},
796-
T1: Key {
797-
key: hex!("74989471b2e26755d60128a0a54de6e8d0a3d30e9c6810f885f09be27339765f"),
798-
},
799-
T2: Key {
800-
key: hex!("bd0b0fb338cc8f16a3c8b05f504a34223263f6fb61865cff29f62d7731581a85"),
801-
},
802-
taux: Key {
803-
key: hex!("df0abd33124389ef8c32fb948b5e4b40259757b5f0ca6c7010f33c0ee625880f"),
804-
},
805-
mu: Key {
806-
key: hex!("5b98150bedb8ba4861246bb31f3f0cb7a0d9a915475c9be92b847be8c3236602"),
807-
},
720+
A: hex!("78ddbccf2e1ced3b68835600768770ebe3e219db19a35f5ebe6495ec58c763d4")
721+
.try_into()
722+
.unwrap(),
723+
S: hex!("e61bd5f461172a14d31149207a9f473289f89dbf4c42dff5f7cbcbd87a12210e")
724+
.try_into()
725+
.unwrap(),
726+
T1: hex!("74989471b2e26755d60128a0a54de6e8d0a3d30e9c6810f885f09be27339765f")
727+
.try_into()
728+
.unwrap(),
729+
T2: hex!("bd0b0fb338cc8f16a3c8b05f504a34223263f6fb61865cff29f62d7731581a85")
730+
.try_into()
731+
.unwrap(),
732+
taux: hex!("df0abd33124389ef8c32fb948b5e4b40259757b5f0ca6c7010f33c0ee625880f")
733+
.try_into()
734+
.unwrap(),
735+
mu: hex!("5b98150bedb8ba4861246bb31f3f0cb7a0d9a915475c9be92b847be8c3236602")
736+
.try_into()
737+
.unwrap(),
808738
L: vec![
809-
Key {
810-
key: hex!("0568cb5dc56fd8077435a87268931c5995367e9f45ad8527248c69c87840f17e"),
811-
},
812-
Key {
813-
key: hex!("3818ef23fb0da1edb0180be8a06fe66e0c12b85955b96a329eccffeb4f0af152"),
814-
},
815-
Key {
816-
key: hex!("c1f9e3d157143326e3f60101e2119c2e8528bcada27087b8248226b9ad827db5"),
817-
},
818-
Key {
819-
key: hex!("46443a7d575c97658f2ffd4cdfaf53de6b39ca340e59f40d195068e4725feb89"),
820-
},
821-
Key {
822-
key: hex!("b0019ac9d69c511c899ab647695bb6e5c5fff5256aa3b168ecb57b20a5ad6fa8"),
823-
},
824-
Key {
825-
key: hex!("24f25935783d645279e575eac839beba4c91b04efb4fc0c8d7f4a0fa27d95fe1"),
826-
},
827-
Key {
828-
key: hex!("5d8f4d63b5ce10d9ab579c30da28108c13abd54e876a0308636fdc8b0e69d059"),
829-
},
739+
hex!("0568cb5dc56fd8077435a87268931c5995367e9f45ad8527248c69c87840f17e")
740+
.try_into()
741+
.unwrap(),
742+
hex!("3818ef23fb0da1edb0180be8a06fe66e0c12b85955b96a329eccffeb4f0af152")
743+
.try_into()
744+
.unwrap(),
745+
hex!("c1f9e3d157143326e3f60101e2119c2e8528bcada27087b8248226b9ad827db5")
746+
.try_into()
747+
.unwrap(),
748+
hex!("46443a7d575c97658f2ffd4cdfaf53de6b39ca340e59f40d195068e4725feb89")
749+
.try_into()
750+
.unwrap(),
751+
hex!("b0019ac9d69c511c899ab647695bb6e5c5fff5256aa3b168ecb57b20a5ad6fa8")
752+
.try_into()
753+
.unwrap(),
754+
hex!("24f25935783d645279e575eac839beba4c91b04efb4fc0c8d7f4a0fa27d95fe1")
755+
.try_into()
756+
.unwrap(),
757+
hex!("5d8f4d63b5ce10d9ab579c30da28108c13abd54e876a0308636fdc8b0e69d059")
758+
.try_into()
759+
.unwrap(),
830760
],
831761
R: vec![
832-
Key {
833-
key: hex!("88f99b0bfb5a4e052b209400594c2c423a95497e3be315d9e8fbb4410bd73102"),
834-
},
835-
Key {
836-
key: hex!("e2bdf54f0b3456c5816004549e76c88f004baf8a84aa3d581d7dbffde4316ec4"),
837-
},
838-
Key {
839-
key: hex!("6d808eec11aa732e94040894517806aa615fadf826c9fc351f73f7c13097cc02"),
840-
},
841-
Key {
842-
key: hex!("8e44c3df858a0991f5b176ae4c862f79bdb153cfb35d1e4c75c28f8493c4a3ff"),
843-
},
844-
Key {
845-
key: hex!("b0334d4f506cd30173ce6398de28084fc8b687a4cfe4eca08476e8a042a8e6fd"),
846-
},
847-
Key {
848-
key: hex!("cc11034e07e9c80029b4220cf15574ded93ba96a2f2bc94bd504a30abfddba5a"),
849-
},
850-
Key {
851-
key: hex!("e5ede5ed6e0d603a668baa586bfa2139553ef487c1a9474fbafaa5ba5b8760d0"),
852-
},
762+
hex!("88f99b0bfb5a4e052b209400594c2c423a95497e3be315d9e8fbb4410bd73102")
763+
.try_into()
764+
.unwrap(),
765+
hex!("e2bdf54f0b3456c5816004549e76c88f004baf8a84aa3d581d7dbffde4316ec4")
766+
.try_into()
767+
.unwrap(),
768+
hex!("6d808eec11aa732e94040894517806aa615fadf826c9fc351f73f7c13097cc02")
769+
.try_into()
770+
.unwrap(),
771+
hex!("8e44c3df858a0991f5b176ae4c862f79bdb153cfb35d1e4c75c28f8493c4a3ff")
772+
.try_into()
773+
.unwrap(),
774+
hex!("b0334d4f506cd30173ce6398de28084fc8b687a4cfe4eca08476e8a042a8e6fd")
775+
.try_into()
776+
.unwrap(),
777+
hex!("cc11034e07e9c80029b4220cf15574ded93ba96a2f2bc94bd504a30abfddba5a")
778+
.try_into()
779+
.unwrap(),
780+
hex!("e5ede5ed6e0d603a668baa586bfa2139553ef487c1a9474fbafaa5ba5b8760d0")
781+
.try_into()
782+
.unwrap(),
853783
],
854-
a: Key {
855-
key: hex!("d782e742fafc78de94aa51bfd89ec61cbf54180093b3617b694652e6a4cea005"),
856-
},
857-
b: Key {
858-
key: hex!("8ae6cc60d17472f9ca87ffa8932ff480bc55e00d95e60b39aa866bb94ac8f90a"),
859-
},
860-
t: Key {
861-
key: hex!("0f42ab37f27887291eb3f3126708e5ff4fdf4c4499bc43c61516684e9f176100"),
862-
},
784+
a: hex!("d782e742fafc78de94aa51bfd89ec61cbf54180093b3617b694652e6a4cea005")
785+
.try_into()
786+
.unwrap(),
787+
b: hex!("8ae6cc60d17472f9ca87ffa8932ff480bc55e00d95e60b39aa866bb94ac8f90a")
788+
.try_into()
789+
.unwrap(),
790+
t: hex!("0f42ab37f27887291eb3f3126708e5ff4fdf4c4499bc43c61516684e9f176100")
791+
.try_into()
792+
.unwrap(),
863793
};
864794

865795
let commitments = vec![
866-
CompressedEdwardsY::from_slice(
867-
hex::decode("5bef186a6d084a0372e3d91446f6b7ec4a900ab7b0abf7b205c5f2b2f105b32c")
868-
.unwrap()
869-
.as_slice(),
870-
)
796+
CompressedEdwardsY(hex!(
797+
"5bef186a6d084a0372e3d91446f6b7ec4a900ab7b0abf7b205c5f2b2f105b32c"
798+
))
871799
.decompress()
872800
.unwrap(),
873-
CompressedEdwardsY::from_slice(
874-
hex::decode("22d187e6a788eaeecf0fd4d31f1718e03c259f39fd120fd8ef660ddb1c36a852")
875-
.unwrap()
876-
.as_slice(),
877-
)
801+
CompressedEdwardsY(hex!(
802+
"22d187e6a788eaeecf0fd4d31f1718e03c259f39fd120fd8ef660ddb1c36a852"
803+
))
878804
.decompress()
879805
.unwrap(),
880806
];

src/clsag/sign.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ pub fn sign(
100100
responses[signing_key_index] = alpha - h_prev * ((mu_P * signing_key) + (mu_C * z));
101101

102102
Clsag {
103-
s: responses.iter().map(|s| s.to_bytes().into()).collect(),
104-
c1: h_0.to_bytes().into(),
105-
D: D_inv_8.compress().to_bytes().into(),
103+
s: responses.to_vec(),
104+
c1: h_0,
105+
D: D_inv_8,
106106
}
107107
}

0 commit comments

Comments
 (0)