Skip to content

Commit

Permalink
fix(core)!: add missing consensus encoding length byte rangeproof & c…
Browse files Browse the repository at this point in the history
…ovenants (#3730)

Description
---

- Adds consensus encode impl for RangeProof and ComSig
- Adds length varint for covenant
- Move version as first encoded byte
- Implement zero alloc hashing for outputs and witness hash
- Limit covenant and tariscript byte size
- Migration to set length byte in existing wallet transactions
- Ensure that an RPC message can never be zero length.
- covenant serde implementation does not use consensus encoding

Motivation and Context
---
Rangeproof is not fixed size and so requires a length varint. 

It is important to ensure that a remote peer cannot cause unbounded allocation, so various length checks are implemented. 

The wallet previously only ever stored a 0-byte covenant which is now invalid encoding.

Potential fix for WriteZero, ensuring that all rpc messages are non-zero in length (rare, but possible).

How Has This Been Tested?
---
Existing tests updated, manually - mined blocks on dibbler
  • Loading branch information
sdbondi authored Jan 25, 2022
1 parent a77303d commit d56da1a
Show file tree
Hide file tree
Showing 42 changed files with 644 additions and 228 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use std::convert::{TryFrom, TryInto};

use tari_common_types::types::{Commitment, PublicKey};
use tari_core::{
consensus::{ConsensusDecoding, ToConsensusBytes},
covenants::Covenant,
transactions::transaction::{TransactionInput, TransactionInputVersion},
};
Expand Down Expand Up @@ -55,7 +54,7 @@ impl TryFrom<grpc::TransactionInput> for TransactionInput {

let sender_offset_public_key =
PublicKey::from_bytes(input.sender_offset_public_key.as_bytes()).map_err(|err| format!("{:?}", err))?;
let covenant = Covenant::consensus_decode(&mut input.covenant.as_slice()).map_err(|err| err.to_string())?;
let covenant = Covenant::from_bytes(&input.covenant).map_err(|err| err.to_string())?;

Ok(TransactionInput::new_with_output_data(
TransactionInputVersion::try_from(
Expand Down Expand Up @@ -129,7 +128,7 @@ impl TryFrom<TransactionInput> for grpc::TransactionInput {
covenant: input
.covenant()
.map_err(|_| "Non-compact Transaction input should contain covenant".to_string())?
.to_consensus_bytes(),
.to_bytes(),
version: input.version as u32,
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use std::convert::{TryFrom, TryInto};

use tari_common_types::types::{BulletRangeProof, Commitment, PublicKey};
use tari_core::{
consensus::{ConsensusDecoding, ToConsensusBytes},
covenants::Covenant,
transactions::transaction::{TransactionOutput, TransactionOutputVersion},
};
Expand Down Expand Up @@ -55,7 +54,7 @@ impl TryFrom<grpc::TransactionOutput> for TransactionOutput {
.ok_or_else(|| "Metadata signature not provided".to_string())?
.try_into()
.map_err(|_| "Metadata signature could not be converted".to_string())?;
let covenant = Covenant::consensus_decode(&mut output.covenant.as_slice()).map_err(|err| err.to_string())?;
let covenant = Covenant::from_bytes(&output.covenant).map_err(|err| err.to_string())?;
Ok(Self::new(
TransactionOutputVersion::try_from(
u8::try_from(output.version).map_err(|_| "Invalid version: overflowed u8")?,
Expand Down Expand Up @@ -86,7 +85,7 @@ impl From<TransactionOutput> for grpc::TransactionOutput {
signature_u: Vec::from(output.metadata_signature.u().as_bytes()),
signature_v: Vec::from(output.metadata_signature.v().as_bytes()),
}),
covenant: output.covenant.to_consensus_bytes(),
covenant: output.covenant.to_bytes(),
version: output.version as u32,
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use std::convert::{TryFrom, TryInto};

use tari_common_types::types::{PrivateKey, PublicKey};
use tari_core::{
consensus::{ConsensusDecoding, ToConsensusBytes},
covenants::Covenant,
transactions::{
tari_amount::MicroTari,
Expand Down Expand Up @@ -52,7 +51,7 @@ impl From<UnblindedOutput> for grpc::UnblindedOutput {
signature_v: Vec::from(output.metadata_signature.v().as_bytes()),
}),
script_lock_height: output.script_lock_height,
covenant: output.covenant.to_consensus_bytes(),
covenant: output.covenant.to_bytes(),
}
}
}
Expand Down Expand Up @@ -86,7 +85,7 @@ impl TryFrom<grpc::UnblindedOutput> for UnblindedOutput {
.try_into()
.map_err(|_| "Metadata signature could not be converted".to_string())?;

let covenant = Covenant::consensus_decode(&mut output.covenant.as_slice()).map_err(|err| err.to_string())?;
let covenant = Covenant::from_bytes(&output.covenant).map_err(|err| err.to_string())?;

Ok(Self::new(
TransactionOutputVersion::try_from(0u8)?,
Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/src/blocks/faucets/dibbler_faucet.json
Original file line number Diff line number Diff line change
Expand Up @@ -3998,4 +3998,4 @@
{"version":"V0","features":{"version":"V0","flags":{"bits":0},"maturity":0,"metadata":[],"unique_id":null,"parent_public_key":null,"asset":null,"mint_non_fungible":null,"sidechain_checkpoint":null},"commitment":"580d59e6b65746d005650bd11b45ae223bba9d09d4e6d41c2e7b0fa2d2f0a467","proof":"689d4a503d042e37e11921b8998e8ab61bee10a6c4d252b1a81306bb2e03f00072c1c0df431f98ff99fc9ad6599dc83d4694f44003e6d2d1107b94b88924d2651cf5641ef5257db58b64abaf422c6f2d63ec8bae2e92197cb9c3a8b4fa24bc2dda8ec6c0bd900e508803031c9bf1deee6c398879331fd7d60ffe8eb07fd30d5e8516a25279a3a35fbbb48f84113bd1457ae173e39f503fc38c7d16f545bcd10474f470abfe56f872ac21301cc676ee3e2a365e4cc8bf7f82a3167bd75aa4f60035920b5afb272f31a44e1d2f17f5b3cc5beb420b53321d5c4bf88e2848403a0b1206844455b6914846b9ad9163cdc556a4241d7b48d6a060d4846f2a0f372c00a4dc6839b2ee8327d8cbefa2e66d342dbbee75c0b7c24f6a7014f32490a0b566841b76e48917b22ca0b5321ba99ce994c14b6ce2537b096b24bf6958c9dd6828ec3ca5e9deb36dd126f58339befe3d11ebec3af1db2649c0095917fb8d11e467405908e182370abb13150347578c092df58007d80253878647f5b4c8a48286354090ca49325bbad4f9565bac89a16ebb4ecc8b4aea6585f0f7d827f868d5480ee67bd66a76170875953a391de4aebb73ead8f83f602af30929529096abbdd74408aa0f5a2314c007734ef4769d569abfe8f5ccced6858c3f7206490810d5855492393976352b0a2ff211529c52ff6139238c03bda2ee84cf6c61564d39826e5c84fe56915894d1c62e316d00c50092304538e8de3667cde81ca07f63bac30676c272e72e11b608b30d421adc8f45e2e216b969c5aad0f6d28de0de65c54be5610e9f7ba5ed845398ed3ce0f4b1cb3ff53a139382c4503a2eb3071432c1a1906cdb38f2cb393d686e155f87a16d20e0c63c400cd551e455f192f1d8e465f6f00cdfac32f26b2860adfd9f6886a5e5c5daa49febb52c05bb5acb8ce5e8a42e6d0b","script":"73","sender_offset_public_key":"347adc2cfd94eb141f3051d5dca6e351f6605c00487b98310e34088025ff8e2e","metadata_signature":{"public_nonce":"3c9b1272c23d3a86c4d5f1329f6ee5306ad8ba873841452d4a242b5865d49a68","u":"04e8da54e730a5ae4e8a540606961a5004237724aadd522daf843ed0885ee603","v":"6fddd03c511c6a8c9b5e59e3ae18965ae379074af97ce1c2998dd393e8b97a01"},"covenant":""}
{"version":"V0","features":{"version":"V0","flags":{"bits":0},"maturity":0,"metadata":[],"unique_id":null,"parent_public_key":null,"asset":null,"mint_non_fungible":null,"sidechain_checkpoint":null},"commitment":"eca7bb64c58719924cc6f644dd6b7c9e62896825d4f94d18c84261dff9bdd57a","proof":"3889b6683e2611f02cc6b2f498eff13fc323a8141a5a774438ceead1287f606c02007ca5fbed8500957430c9567b94b5a20055161854c575d3dff3e2c1beec53ee131e61886eef21474e1a9f9d070f044965e1265ee40c9e83820b9fd6fa653b4a2821620623f73846a2cfc00f06f2f651d5d8f310912d24e6ee3c11519fca2440ccba4327360f306281209e78510702c7fec160bfbd3a4aa3b689ec828aa1040d49977b9bd7840280b74702d6a8306ae4c3326420d305c1f5e92a75f2107b080e88ccc9b3f9ec661d472111522b9ac2bc8e962482c334fa845a1783fc63d60c12028c6c85a9f1ff7e5288967e02688fcad74aa3e77ec65fb66e164cb763d879a240579fa2feb67b6ec6302aaa60936812229eb1e064430bc286630b41f6a3349cf1b09cf202f9668b42a8f04f6a070e3ab8d40c9024e666f64adbeaaea55a7c98d6b799e06001ddbe2947b7d46ce10d8fac84f642b7ed77e3ccc6153057d360587ac3e9bf0df49523807fdc2e6f0b2e2c4955ecd268617538f16c08f6374f3b62394cf1f3401fc325464eb7c8c51230e4a97e2a705af5c638f0f33616b69d5242e991aafaff244c6f658519072991705dd5b77b15c6c22e83fe63b039122651de7e3c55c2e4e040e80e0b45fde6975738a439f61c50ddd0453d01cdbaefc342c2b228f08538739887b8802a063e155c09199699a3917b5f5e0e2d0915fed21dd4c65c2fc808ee9dc98a14a9097d14e00b21bbb442eb4140d9f6c00e8f4b002494c61343748f27254fd3c30e8a7bf7d84301ce9d699082fc08f11d0428e722775e79b9bb87e73bcef77835159e2cd55f183b2cf4c50a0451a2a1b3a38cd7a945a0e63b6d89a141d5de82233d28795f459e5d02feaf46460a06e791165cccef029dc87f5d087f65571ba304596963b4b366aca6ee9b7981a069452918f0ae3200","script":"73","sender_offset_public_key":"068ab2fa03005837681d3afdfea3b48b914ebec7a390c72fce13a84a617dec46","metadata_signature":{"public_nonce":"687c668ff89129fb570ae18228a895d8c039a38c31cf084b5832dc7336576f73","u":"ecda1e75067b591960d911e6e8d786b04be207689edb64aae2175f87c7d87909","v":"af920b783d48a2bd9b406d9302e29f59f047bcae0df09a8915c60762f7ef8701"},"covenant":""}
{"version":"V0","features":{"version":"V0","flags":{"bits":0},"maturity":0,"metadata":[],"unique_id":null,"parent_public_key":null,"asset":null,"mint_non_fungible":null,"sidechain_checkpoint":null},"commitment":"ee4bc0b809687dc8dbbd0472be03a65d3a78d7c789cba86b12b916d8ac51a827","proof":"00ed8c51475945ec39f71a87b6daee9c5ac987d20e0511f6d92d8fa1d5ad5060befaad3058c4d8404064fb83f569bd978956eddbee0afbae5a67074c678f444f786813ecdf50bc1bd388561de479999b77a90ea0fdcfcbcb932daf12781b0d51061e01623ffc16fb1484a93a291d4e7bcb4ca3b4894dc6d58ee56887ce39b204539082017efd537831b91935878009cd4bfb5edd5fed48f50cbe3fab09b2ad0c38b2226e861bcb7a78aa28c21f737900e4c2d12c148abf9d5b7c90b6a779b00c4eee9c45e62044dc798f4d3ca8f816fb6b40420ffcd0540b99b92eb719bad90faa2b190a041908f3e9211e62736e92b3d42cfe94ffeab6fe885955b7162801595ac43b6098ac65df726a640fce5f6ae95005b655645cb65848f159a484ed70215248f0483225db2e01ac9d9f10d8fc51e3e317c38b16df6aa1ce3f26960ee603fc3ca08c8d547672694209cbeaf72137d23ef601b4b5987a3b8cf3fb4dfc4e0cf05bfdc128f9f49b1a2f8294c3cc63cf7760634e3b8a725b59bb93b236333145d0cdf86ddb22b885d4c50de94e24fe2b7313fae7aa7b7970f87ff3a19e605b79989b2cb8710d8fceaae20c84348084cdcc4257cd6fe7dd541c43afcaf048694f7ad64e0661e0be85f185ab391567a84215e2bff582e724e9ad54b8004815f524d239b18e831cfe57a3710fb06ca3cef76d4e08a7f8ab7b6526359b1622302538acbdb95c0f82cbeeaad700c157bdbd2a74d85582f6b5ac7d65010cb37377f37440022db6ac63eced6d2fdcdb78a9c9d944de6a989e4e4641c79380961838885d688c1e3e6850c081fcfe8ea6ecfa68b60435fe3affc3fbf2d28ce80d6ac6f052db56d8281cfd7f1128faff5219dc347c88f8e4385e91cb7151426aca1fdc7d0e51e55c030c25cf31aded5c86e12ff5d7f499aa770864ff1b6cb80b1133b0f601","script":"73","sender_offset_public_key":"22375dd307b7a1e1b282b86aa7edaad630e866cc851a14c957fefba09a19451d","metadata_signature":{"public_nonce":"cc2c4ad9a8e27cc5c1dc8cec469d6f2e30d520f7ca99b5cdef4c618357217e3a","u":"1a5236ef22e481a927f372d0cf467577477526e7d5b19674137a146046f7b90f","v":"71bf2d796b8878617ed06b7d30f44f5bc6a9dc380de5811ca3ae9b1799dcd508"},"covenant":""}
{"version":"V0","features":{"bits":0},"fee":0,"lock_height":0,"excess":"b8159195e8e7ce37ff5e03eff242093a72d13f1fc46ed1c169efd6e0cee34e4a","excess_sig":{"public_nonce":"0c6be155f074e897c1eb9773edf4114a6d2561a819c2b0589aea774cd37d1169","signature":"6ee44e359cedf04113484842200079675ba590648a537a46817319b714834905"}}
{"version":"V0","features":{"bits":0},"fee":0,"lock_height":0,"excess":"b8159195e8e7ce37ff5e03eff242093a72d13f1fc46ed1c169efd6e0cee34e4a","excess_sig":{"public_nonce":"0c6be155f074e897c1eb9773edf4114a6d2561a819c2b0589aea774cd37d1169","signature":"6ee44e359cedf04113484842200079675ba590648a537a46817319b714834905"}}
54 changes: 54 additions & 0 deletions base_layer/core/src/common/hash_writer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2022, The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use std::io::Write;

use digest::{consts::U32, Digest, FixedOutput, Update};

pub struct HashWriter<H> {
digest: H,
}

impl<H: Digest> HashWriter<H> {
pub fn new(digest: H) -> Self {
Self { digest }
}
}

impl<H> HashWriter<H>
where H: FixedOutput<OutputSize = U32>
{
pub fn finalize(self) -> [u8; 32] {
self.digest.finalize_fixed().into()
}
}

impl<H: Update> Write for HashWriter<H> {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.digest.update(buf);
Ok(buf.len())
}

fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
52 changes: 52 additions & 0 deletions base_layer/core/src/common/limited_reader.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2022, The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use std::{io, io::Read};

pub struct LimitedBytesReader<R> {
byte_limit: usize,
num_read: usize,
inner: R,
}

impl<R: Read> LimitedBytesReader<R> {
pub fn new(byte_limit: usize, reader: R) -> Self {
Self {
byte_limit,
num_read: 0,
inner: reader,
}
}
}
impl<R: Read> Read for LimitedBytesReader<R> {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
let read = self.inner.read(buf)?;
self.num_read += read;
if self.num_read > self.byte_limit {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
format!("Read more bytes than the maximum ({})", self.byte_limit),
));
}
Ok(read)
}
}
2 changes: 2 additions & 0 deletions base_layer/core/src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

pub mod byte_counter;
pub mod hash_writer;
pub mod limited_reader;
#[cfg(feature = "base_node")]
pub mod rolling_vec;
1 change: 1 addition & 0 deletions base_layer/core/src/consensus/consensus_encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mod bytes;
mod crypto;
mod generic;
mod integers;
mod micro_tari;
mod script;
mod vec;

Expand Down
19 changes: 17 additions & 2 deletions base_layer/core/src/consensus/consensus_encoding/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use std::{
io,
io::{Error, Read, Write},
ops::Deref,
};

use integer_encoding::{VarInt, VarIntReader, VarIntWriter};
Expand Down Expand Up @@ -56,7 +57,7 @@ impl<const MAX: usize> From<MaxSizeBytes<MAX>> for Vec<u8> {
}

impl<const MAX: usize> ConsensusDecoding for MaxSizeBytes<MAX> {
fn consensus_decode<R: Read>(reader: &mut R) -> Result<Self, Error> {
fn consensus_decode<R: Read>(reader: &mut R) -> Result<Self, io::Error> {
let len = reader.read_varint()?;
if len > MAX {
return Err(io::Error::new(
Expand All @@ -70,6 +71,20 @@ impl<const MAX: usize> ConsensusDecoding for MaxSizeBytes<MAX> {
}
}

impl<const MAX: usize> AsRef<[u8]> for MaxSizeBytes<MAX> {
fn as_ref(&self) -> &[u8] {
&self.inner
}
}

impl<const MAX: usize> Deref for MaxSizeBytes<MAX> {
type Target = [u8];

fn deref(&self) -> &Self::Target {
&self.inner
}
}

impl ConsensusEncoding for &[u8] {
fn consensus_encode<W: Write>(&self, writer: &mut W) -> Result<usize, io::Error> {
let len = self.len();
Expand Down Expand Up @@ -100,7 +115,7 @@ impl<const N: usize> ConsensusEncodingSized for [u8; N] {
}

impl<const N: usize> ConsensusDecoding for [u8; N] {
fn consensus_decode<R: Read>(reader: &mut R) -> Result<Self, Error> {
fn consensus_decode<R: Read>(reader: &mut R) -> Result<Self, io::Error> {
let mut buf = [0u8; N];
reader.read_exact(&mut buf)?;
Ok(buf)
Expand Down
59 changes: 55 additions & 4 deletions base_layer/core/src/consensus/consensus_encoding/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use std::{io, io::Read};
use std::{
io,
io::{Error, Read, Write},
};

use tari_common_types::types::{Commitment, PrivateKey, PublicKey, Signature};
use tari_common_types::types::{ComSignature, Commitment, PrivateKey, PublicKey, RangeProof, Signature};
use tari_crypto::keys::{PublicKey as PublicKeyTrait, SecretKey};
use tari_utilities::ByteArray;

use crate::consensus::{ConsensusDecoding, ConsensusEncoding, ConsensusEncodingSized};
use crate::consensus::{ConsensusDecoding, ConsensusEncoding, ConsensusEncodingSized, MaxSizeBytes};

//---------------------------------- PublicKey --------------------------------------------//

Expand Down Expand Up @@ -87,7 +90,7 @@ impl ConsensusEncoding for Commitment {

impl ConsensusEncodingSized for Commitment {
fn consensus_encode_exact_size(&self) -> usize {
32
PublicKey::key_length()
}
}

Expand Down Expand Up @@ -133,3 +136,51 @@ impl ConsensusDecoding for Signature {
Ok(Signature::new(pub_nonce, sig))
}
}

//---------------------------------- RangeProof --------------------------------------------//

impl ConsensusEncoding for RangeProof {
fn consensus_encode<W: Write>(&self, writer: &mut W) -> Result<usize, Error> {
self.0.consensus_encode(writer)
}
}

impl ConsensusEncodingSized for RangeProof {
fn consensus_encode_exact_size(&self) -> usize {
self.0.consensus_encode_exact_size()
}
}

impl ConsensusDecoding for RangeProof {
fn consensus_decode<R: Read>(reader: &mut R) -> Result<Self, io::Error> {
const MAX_RANGEPROOF_SIZE: usize = 1024;
let bytes = MaxSizeBytes::<MAX_RANGEPROOF_SIZE>::consensus_decode(reader)?;
Ok(Self(bytes.into()))
}
}

//---------------------------------- Commitment Signature --------------------------------------------//

impl ConsensusEncoding for ComSignature {
fn consensus_encode<W: Write>(&self, writer: &mut W) -> Result<usize, Error> {
let mut written = self.u().consensus_encode(writer)?;
written += self.v().consensus_encode(writer)?;
written += self.public_nonce().consensus_encode(writer)?;
Ok(written)
}
}

impl ConsensusEncodingSized for ComSignature {
fn consensus_encode_exact_size(&self) -> usize {
PrivateKey::key_length() * 2 + PublicKey::key_length()
}
}

impl ConsensusDecoding for ComSignature {
fn consensus_decode<R: Read>(reader: &mut R) -> Result<Self, io::Error> {
let u = PrivateKey::consensus_decode(reader)?;
let v = PrivateKey::consensus_decode(reader)?;
let nonce = Commitment::consensus_decode(reader)?;
Ok(ComSignature::new(nonce, u, v))
}
}
Loading

0 comments on commit d56da1a

Please sign in to comment.