maintain switch_commit_hashes in the output pmmr (not used yet) (#583)

This commit is contained in:
AntiochP
2018-01-06 22:18:12 -05:00
committed by GitHub
parent 3515bf748c
commit 33c8e73403
2 changed files with 44 additions and 11 deletions
+32 -4
View File
@@ -467,6 +467,11 @@ impl SwitchCommitHash {
}
SwitchCommitHash { hash: h }
}
/// Build an "zero" switch commitment hash
pub fn zero() -> SwitchCommitHash {
SwitchCommitHash { hash: [0; SWITCH_COMMIT_HASH_SIZE] }
}
}
/// Output for a transaction, defining the new ownership of coins that are being
@@ -571,6 +576,19 @@ impl Output {
pub struct SumCommit {
/// Output commitment
pub commit: Commitment,
/// The corresponding "switch commit hash"
pub switch_commit_hash: SwitchCommitHash,
}
impl SumCommit {
/// For when we do not care about the switch_commit_hash
/// for example when comparing sum_commit hashes
pub fn from_commit(commit: &Commitment) -> SumCommit {
SumCommit {
commit: commit.clone(),
switch_commit_hash: SwitchCommitHash::zero(),
}
}
}
/// Outputs get summed through their commitments.
@@ -578,17 +596,23 @@ impl Summable for SumCommit {
type Sum = SumCommit;
fn sum(&self) -> SumCommit {
SumCommit { commit: self.commit.clone() }
SumCommit {
commit: self.commit.clone(),
switch_commit_hash: self.switch_commit_hash.clone(),
}
}
fn sum_len() -> usize {
secp::constants::PEDERSEN_COMMITMENT_SIZE
secp::constants::PEDERSEN_COMMITMENT_SIZE + SWITCH_COMMIT_HASH_SIZE
}
}
impl Writeable for SumCommit {
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ser::Error> {
self.commit.write(writer)?;
if writer.serialization_mode() == ser::SerializationMode::Full {
self.switch_commit_hash.write(writer)?;
}
Ok(())
}
}
@@ -596,8 +620,12 @@ impl Writeable for SumCommit {
impl Readable for SumCommit {
fn read(reader: &mut Reader) -> Result<SumCommit, ser::Error> {
let commit = Commitment::read(reader)?;
let switch_commit_hash = SwitchCommitHash::read(reader)?;
Ok(SumCommit { commit: commit })
Ok(SumCommit {
commit: commit,
switch_commit_hash: switch_commit_hash,
})
}
}
@@ -616,7 +644,7 @@ impl ops::Add for SumCommit {
Ok(s) => s,
Err(_) => Commitment::from_vec(vec![1; 33]),
};
SumCommit { commit: sum }
SumCommit::from_commit(&sum)
}
}