-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
684 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
members = [ | ||
"sigma-chain", | ||
"sigma-chain-c", | ||
"sigma-ser", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use sigma_ser::serializer::SerializationError; | ||
use sigma_ser::serializer::SigmaSerializable; | ||
use sigma_ser::vlq_encode; | ||
use std::io; | ||
|
||
#[cfg(test)] | ||
use proptest::{arbitrary::Arbitrary, collection::vec, prelude::*}; | ||
|
||
pub const BOX_ID_SIZE: usize = crate::constants::DIGEST32_SIZE; | ||
|
||
#[derive(PartialEq, Debug)] | ||
pub struct BoxId(pub [u8; BOX_ID_SIZE]); | ||
|
||
impl SigmaSerializable for BoxId { | ||
fn sigma_serialize<W: vlq_encode::WriteSigmaVlqExt>(&self, mut w: W) -> Result<(), io::Error> { | ||
w.write_all(&self.0)?; | ||
Ok(()) | ||
} | ||
fn sigma_parse<R: vlq_encode::ReadSigmaVlqExt>(mut r: R) -> Result<Self, SerializationError> { | ||
let mut bytes = [0; BOX_ID_SIZE]; | ||
r.read_exact(&mut bytes)?; | ||
Ok(Self(bytes)) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
impl Arbitrary for BoxId { | ||
type Parameters = (); | ||
|
||
fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy { | ||
(vec(any::<u8>(), BOX_ID_SIZE)) | ||
.prop_map(|v| { | ||
let mut bytes = [0; BOX_ID_SIZE]; | ||
bytes.copy_from_slice(v.as_slice()); | ||
Self(bytes) | ||
}) | ||
.boxed() | ||
} | ||
|
||
type Strategy = BoxedStrategy<Self>; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
proptest! { | ||
|
||
#[test] | ||
fn box_id_roundtrip(box_id in any::<BoxId>()) { | ||
let mut data = Vec::new(); | ||
box_id.sigma_serialize(&mut data)?; | ||
let box_id2 = BoxId::sigma_parse(&data[..])?; | ||
prop_assert_eq![box_id, box_id2]; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub const DIGEST32_SIZE: usize = 32; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
use std::collections::HashMap; | ||
|
||
pub struct ContextExtension { | ||
pub values: HashMap<u8, Box<[u8]>>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use crate::box_id::BoxId; | ||
use sigma_ser::serializer::SerializationError; | ||
use sigma_ser::serializer::SigmaSerializable; | ||
use sigma_ser::vlq_encode; | ||
use std::io; | ||
|
||
#[cfg(test)] | ||
use proptest::prelude::*; | ||
#[cfg(test)] | ||
use proptest_derive::Arbitrary; | ||
|
||
#[derive(PartialEq, Debug)] | ||
#[cfg_attr(test, derive(Arbitrary))] | ||
pub struct DataInput { | ||
pub box_id: BoxId, | ||
} | ||
|
||
impl SigmaSerializable for DataInput { | ||
fn sigma_serialize<W: vlq_encode::WriteSigmaVlqExt>(&self, w: W) -> Result<(), io::Error> { | ||
self.box_id.sigma_serialize(w)?; | ||
Ok(()) | ||
} | ||
fn sigma_parse<R: vlq_encode::ReadSigmaVlqExt>(r: R) -> Result<Self, SerializationError> { | ||
let box_id = BoxId::sigma_parse(r)?; | ||
Ok(DataInput { box_id }) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
proptest! { | ||
|
||
#[test] | ||
fn data_input_roundtrip(i in any::<DataInput>()) { | ||
let mut data = Vec::new(); | ||
i.sigma_serialize(&mut data)?; | ||
let decoded = DataInput::sigma_parse(&data[..])?; | ||
prop_assert_eq![i, decoded]; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use crate::ergo_tree::ErgoTree; | ||
use crate::token_info::TokenInfo; | ||
use sigma_ser::serializer::SerializationError; | ||
use sigma_ser::serializer::SigmaSerializable; | ||
use sigma_ser::vlq_encode; | ||
use std::collections::HashMap; | ||
use std::io; | ||
|
||
pub struct NonMandatoryRegisterId(u8); | ||
|
||
pub struct ErgoBoxCandidate { | ||
pub value: u64, | ||
pub ergo_tree: ErgoTree, | ||
pub tokens: Vec<TokenInfo>, | ||
pub additional_registers: HashMap<NonMandatoryRegisterId, Box<[u8]>>, | ||
pub creation_height: u32, | ||
} | ||
|
||
impl SigmaSerializable for ErgoBoxCandidate { | ||
fn sigma_serialize<W: vlq_encode::WriteSigmaVlqExt>(&self, _: W) -> Result<(), io::Error> { | ||
unimplemented!(); | ||
} | ||
fn sigma_parse<R: vlq_encode::ReadSigmaVlqExt>(_: R) -> Result<Self, SerializationError> { | ||
unimplemented!(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use sigma_ser::serializer::SerializationError; | ||
use sigma_ser::serializer::SigmaSerializable; | ||
use sigma_ser::vlq_encode; | ||
use std::io; | ||
|
||
pub struct ErgoTree {} | ||
|
||
impl SigmaSerializable for ErgoTree { | ||
fn sigma_serialize<W: vlq_encode::WriteSigmaVlqExt>(&self, _: W) -> Result<(), io::Error> { | ||
Ok(()) | ||
} | ||
fn sigma_parse<R: vlq_encode::ReadSigmaVlqExt>(_: R) -> Result<Self, SerializationError> { | ||
Ok(ErgoTree {}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use crate::box_id::BoxId; | ||
use crate::prover_result::ProverResult; | ||
|
||
pub struct Input { | ||
pub box_id: BoxId, | ||
pub spending_proof: ProverResult, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
use crate::context_extension::ContextExtension; | ||
|
||
pub struct ProverResult { | ||
pub proof: Box<[u8]>, | ||
pub extension: ContextExtension, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use sigma_ser::serializer::SerializationError; | ||
use sigma_ser::serializer::SigmaSerializable; | ||
use sigma_ser::vlq_encode; | ||
use std::io; | ||
|
||
#[cfg(test)] | ||
use proptest::{arbitrary::Arbitrary, collection::vec, prelude::*}; | ||
|
||
pub const TOKEN_ID_SIZE: usize = crate::constants::DIGEST32_SIZE; | ||
|
||
#[derive(PartialEq, Debug)] | ||
pub struct TokenId(pub [u8; TOKEN_ID_SIZE]); | ||
|
||
impl SigmaSerializable for TokenId { | ||
fn sigma_serialize<W: vlq_encode::WriteSigmaVlqExt>(&self, mut w: W) -> Result<(), io::Error> { | ||
w.write_all(&self.0)?; | ||
Ok(()) | ||
} | ||
fn sigma_parse<R: vlq_encode::ReadSigmaVlqExt>(mut r: R) -> Result<Self, SerializationError> { | ||
let mut bytes = [0; TOKEN_ID_SIZE]; | ||
r.read_exact(&mut bytes)?; | ||
Ok(Self(bytes)) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
impl Arbitrary for TokenId { | ||
type Parameters = (); | ||
|
||
fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy { | ||
(vec(any::<u8>(), TOKEN_ID_SIZE)) | ||
.prop_map(|v| { | ||
let mut bytes = [0; TOKEN_ID_SIZE]; | ||
bytes.copy_from_slice(v.as_slice()); | ||
Self(bytes) | ||
}) | ||
.boxed() | ||
} | ||
|
||
type Strategy = BoxedStrategy<Self>; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
proptest! { | ||
|
||
#[test] | ||
fn token_id_roundtrip(id in any::<TokenId>()) { | ||
let mut data = Vec::new(); | ||
id.sigma_serialize(&mut data)?; | ||
let id2 = TokenId::sigma_parse(&data[..])?; | ||
prop_assert_eq![id, id2]; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use crate::token_id::TokenId; | ||
|
||
#[cfg(test)] | ||
use proptest_derive::Arbitrary; | ||
|
||
#[derive(PartialEq, Debug)] | ||
#[cfg_attr(test, derive(Arbitrary))] | ||
pub struct TokenInfo { | ||
pub token_id: TokenId, | ||
pub amount: u64, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use crate::data_input::DataInput; | ||
use crate::ergo_box::ErgoBoxCandidate; | ||
use crate::input::Input; | ||
use sigma_ser::serializer::SerializationError; | ||
use sigma_ser::serializer::SigmaSerializable; | ||
use sigma_ser::vlq_encode; | ||
use std::io; | ||
|
||
pub struct Transaction { | ||
pub inputs: Vec<Input>, | ||
pub data_inputs: Vec<DataInput>, | ||
pub outputs: Vec<ErgoBoxCandidate>, | ||
} | ||
|
||
impl SigmaSerializable for Transaction { | ||
fn sigma_serialize<W: vlq_encode::WriteSigmaVlqExt>(&self, mut w: W) -> Result<(), io::Error> { | ||
w.put_u16(self.inputs.len() as u16)?; | ||
Ok(()) | ||
} | ||
fn sigma_parse<R: vlq_encode::ReadSigmaVlqExt>(_: R) -> Result<Self, SerializationError> { | ||
unimplemented!() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
[package] | ||
name = "sigma-ser" | ||
version = "0.1.0" | ||
authors = ["Denys Zadorozhnyi <denys@zadorozhnyi.com>"] | ||
edition = "2018" | ||
|
||
[lib] | ||
crate-type = ["cdylib", "rlib"] | ||
|
||
[features] | ||
default = ["console_error_panic_hook"] | ||
|
||
[dependencies] | ||
wasm-bindgen = "0.2" | ||
thiserror = "1" | ||
|
||
# The `console_error_panic_hook` crate provides better debugging of panics by | ||
# logging them with `console.error`. This is great for development, but requires | ||
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for | ||
# code size when deploying. | ||
console_error_panic_hook = { version = "0.1.6", optional = true } | ||
|
||
[dev-dependencies] | ||
wasm-bindgen-test = "0.3.10" | ||
proptest = "0.9" | ||
proptest-derive = "0.1.2" | ||
|
||
[profile.release] | ||
# Tell `rustc` to optimize for small code size. | ||
opt-level = "s" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod serializer; | ||
pub mod vlq_encode; | ||
pub mod zig_zag_encode; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use super::vlq_encode; | ||
use std::io; | ||
use thiserror::Error; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum SerializationError { | ||
#[error("vlq encode error")] | ||
VlqEncode(vlq_encode::VlqEncodingError), | ||
#[error("io error")] | ||
Io(io::Error), | ||
} | ||
|
||
impl From<vlq_encode::VlqEncodingError> for SerializationError { | ||
fn from(error: vlq_encode::VlqEncodingError) -> Self { | ||
SerializationError::VlqEncode(error) | ||
} | ||
} | ||
|
||
impl From<io::Error> for SerializationError { | ||
fn from(error: io::Error) -> Self { | ||
SerializationError::Io(error) | ||
} | ||
} | ||
|
||
pub trait SigmaSerializable: Sized { | ||
fn sigma_serialize<W: vlq_encode::WriteSigmaVlqExt>(&self, w: W) -> Result<(), io::Error>; | ||
fn sigma_parse<R: vlq_encode::ReadSigmaVlqExt>(r: R) -> Result<Self, SerializationError>; | ||
} |
Oops, something went wrong.