Skip to content

Commit

Permalink
Merge 77f4a22 into db19ee5
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat committed Apr 17, 2020
2 parents db19ee5 + 77f4a22 commit 9497564
Show file tree
Hide file tree
Showing 19 changed files with 684 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
members = [
"sigma-chain",
"sigma-chain-c",
"sigma-ser",
]
3 changes: 3 additions & 0 deletions sigma-chain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ default = ["console_error_panic_hook"]

[dependencies]
wasm-bindgen = "0.2"
sigma-ser = { path = "../sigma-ser" }

# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
Expand All @@ -21,6 +22,8 @@ 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.
Expand Down
57 changes: 57 additions & 0 deletions sigma-chain/src/box_id.rs
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];
}
}
}
1 change: 1 addition & 0 deletions sigma-chain/src/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub const DIGEST32_SIZE: usize = 32;
5 changes: 5 additions & 0 deletions sigma-chain/src/context_extension.rs
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]>>,
}
43 changes: 43 additions & 0 deletions sigma-chain/src/data_input.rs
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];
}
}
}
26 changes: 26 additions & 0 deletions sigma-chain/src/ergo_box.rs
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!();
}
}
15 changes: 15 additions & 0 deletions sigma-chain/src/ergo_tree.rs
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 {})
}
}
7 changes: 7 additions & 0 deletions sigma-chain/src/input.rs
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,
}
11 changes: 11 additions & 0 deletions sigma-chain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,14 @@ mod misc;
mod utils;

pub use misc::*;
pub mod box_id;
pub mod constants;
pub mod context_extension;
pub mod data_input;
pub mod ergo_box;
pub mod ergo_tree;
pub mod input;
pub mod prover_result;
pub mod token_id;
pub mod token_info;
pub mod transaction;
6 changes: 6 additions & 0 deletions sigma-chain/src/prover_result.rs
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,
}
57 changes: 57 additions & 0 deletions sigma-chain/src/token_id.rs
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];
}
}
}
11 changes: 11 additions & 0 deletions sigma-chain/src/token_info.rs
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,
}
23 changes: 23 additions & 0 deletions sigma-chain/src/transaction.rs
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!()
}
}
30 changes: 30 additions & 0 deletions sigma-ser/Cargo.toml
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"
3 changes: 3 additions & 0 deletions sigma-ser/src/lib.rs
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;
28 changes: 28 additions & 0 deletions sigma-ser/src/serializer.rs
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>;
}

0 comments on commit 9497564

Please sign in to comment.