Skip to content

Commit

Permalink
Merge a3a70d3 into 2493da1
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat committed Aug 11, 2020
2 parents 2493da1 + a3a70d3 commit 00a93e1
Show file tree
Hide file tree
Showing 38 changed files with 577 additions and 226 deletions.
5 changes: 0 additions & 5 deletions sigma-ser/src/lib.rs
Expand Up @@ -13,12 +13,7 @@

/// io::Read wrapper with `peek` operation
pub mod peekable_reader;
/// Sigma serializer
pub mod serializer;
/// VLQ encoder
pub mod vlq_encode;
/// ZigZag encoder
pub mod zig_zag_encode;

// #[cfg(test)]
pub mod test_helpers;
12 changes: 0 additions & 12 deletions sigma-ser/src/test_helpers.rs

This file was deleted.

26 changes: 11 additions & 15 deletions sigma-tree/src/ast.rs
Expand Up @@ -8,15 +8,17 @@ pub mod ops;

pub use constant::*;

#[derive(PartialEq, Eq, Debug)]
#[derive(PartialEq, Eq, Debug, Clone)]
/// newtype for box register id
pub struct RegisterId(u8);

#[derive(PartialEq, Eq, Debug)]
#[derive(PartialEq, Eq, Debug, Clone)]
/// Expression in ErgoTree
pub enum Expr {
/// Constant value
Const(Constant),
/// Placeholder for a constant
ConstPlaceholder(ConstantPlaceholder),
/// Collection of values (same type)
Coll {
/// Collection type
Expand Down Expand Up @@ -58,15 +60,9 @@ impl Expr {
/// Code (used in serialization)
pub fn op_code(&self) -> OpCode {
match self {
Const { .. } => todo!(),
Coll { .. } => todo!(),
Tup { .. } => todo!(),
BoxM(boxm) => boxm.op_code(),
CollM(_) => todo!(),
CtxM(_) => todo!(),
MethodCall { .. } => todo!(),
PredefFunc(_) => todo!(),
BinOp(_, _, _) => todo!(),
Const(_) => todo!(),
ConstPlaceholder(cp) => cp.op_code(),
_ => todo!(),
}
}

Expand All @@ -85,7 +81,7 @@ impl fmt::Display for Expr {
}
}

#[derive(PartialEq, Eq, Debug)]
#[derive(PartialEq, Eq, Debug, Clone)]
/// Methods for Collection type instance
pub enum CollMethods {
/// Fold method
Expand All @@ -99,7 +95,7 @@ pub enum CollMethods {
},
}

#[derive(PartialEq, Eq, Debug)]
#[derive(PartialEq, Eq, Debug, Clone)]
/// Methods for Box type instance
pub enum BoxMethods {
/// Box.RX methods
Expand All @@ -118,7 +114,7 @@ impl BoxMethods {
}
}

#[derive(PartialEq, Eq, Debug)]
#[derive(PartialEq, Eq, Debug, Clone)]
/// Methods for Context type instance
pub enum ContextMethods {
/// Tx inputs
Expand All @@ -127,7 +123,7 @@ pub enum ContextMethods {
Outputs,
}

#[derive(PartialEq, Eq, Debug)]
#[derive(PartialEq, Eq, Debug, Clone)]
/// Predefined (global) functions
pub enum PredefFunc {
/// SHA256
Expand Down
22 changes: 20 additions & 2 deletions sigma-tree/src/ast/constant.rs
@@ -1,13 +1,12 @@
use crate::chain::{Base16DecodedBytes, Base16EncodedBytes};
use crate::{
chain::ErgoBox,
serialization::{op_code::OpCode, SerializationError, SigmaSerializable},
sigma_protocol::{dlog_group::EcPoint, SigmaProp},
types::{LiftIntoSType, SType},
};
#[cfg(feature = "with-serde")]
use serde::{Deserialize, Serialize};
use sigma_ser::serializer::SerializationError;
use sigma_ser::serializer::SigmaSerializable;
use std::convert::TryFrom;

#[derive(PartialEq, Eq, Debug, Clone)]
Expand Down Expand Up @@ -261,6 +260,25 @@ impl Into<Constant> for Vec<i8> {
}
}

/// Placeholder for a constant in ErgoTree.
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct ConstantPlaceholder {
/// Zero based index in ErgoTree.constants array.
pub id: u32,
/// Type of the constant value
pub tpe: SType,
}

impl ConstantPlaceholder {
/// OpCode value
pub const OP_CODE: OpCode = OpCode::CONSTANT_PLACEHOLDER;

/// OpCode for the serialization
pub fn op_code(&self) -> OpCode {
OpCode::CONSTANT_PLACEHOLDER
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
4 changes: 2 additions & 2 deletions sigma-tree/src/ast/ops.rs
@@ -1,12 +1,12 @@
//! Operators in ErgoTree
#[derive(PartialEq, Eq, Debug)]
#[derive(PartialEq, Eq, Debug, Clone)]
/// Operations for numerical types
pub enum NumOp {
/// Addition
Add,
}

#[derive(PartialEq, Eq, Debug)]
#[derive(PartialEq, Eq, Debug, Clone)]
/// Binary operations
pub enum BinOp {
/// Binary operations for numerical types
Expand Down
1 change: 1 addition & 0 deletions sigma-tree/src/big_integer.rs
Expand Up @@ -39,6 +39,7 @@ mod tests {
let bytes = b.0.to_signed_bytes_be();
let s2 =
Scalar::from_bytes(bytes.as_slice().try_into().expect("expected 32 bytes")).unwrap();
// TODO: failed on CI with non-32 bytes length
let b2: BigInteger = s2.into();
assert_eq!(b, b2);
}
Expand Down
2 changes: 1 addition & 1 deletion sigma-tree/src/chain/address.rs
@@ -1,12 +1,12 @@
use super::digest32;
use crate::{
ast::Expr,
serialization::{SerializationError, SigmaSerializable},
sigma_protocol::{
dlog_group::EcPoint, ProveDlog, SigmaBoolean, SigmaProofOfKnowledgeTree, SigmaProp,
},
ErgoTree,
};
use sigma_ser::serializer::{SerializationError, SigmaSerializable};
use std::{
convert::{TryFrom, TryInto},
rc::Rc,
Expand Down
13 changes: 7 additions & 6 deletions sigma-tree/src/chain/box_id.rs
@@ -1,13 +1,14 @@
//! Box id type
use sigma_ser::serializer::SerializationError;
use sigma_ser::serializer::SigmaSerializable;
use sigma_ser::vlq_encode;
use std::io;

#[cfg(feature = "with-serde")]
use serde::{Deserialize, Serialize};

use super::digest32::Digest32;
use crate::serialization::{
sigma_byte_reader::SigmaByteRead, sigma_byte_writer::SigmaByteWrite, SerializationError,
SigmaSerializable,
};
#[cfg(test)]
use proptest_derive::Arbitrary;

Expand Down Expand Up @@ -41,20 +42,20 @@ impl Into<String> for BoxId {
}

impl SigmaSerializable for BoxId {
fn sigma_serialize<W: vlq_encode::WriteSigmaVlqExt>(&self, w: &mut W) -> Result<(), io::Error> {
fn sigma_serialize<W: SigmaByteWrite>(&self, w: &mut W) -> Result<(), io::Error> {
self.0.sigma_serialize(w)?;
Ok(())
}
fn sigma_parse<R: vlq_encode::ReadSigmaVlqExt>(r: &mut R) -> Result<Self, SerializationError> {
fn sigma_parse<R: SigmaByteRead>(r: &mut R) -> Result<Self, SerializationError> {
Ok(Self(Digest32::sigma_parse(r)?))
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::serialization::sigma_serialize_roundtrip;
use proptest::prelude::*;
use sigma_ser::test_helpers::*;

proptest! {

Expand Down
7 changes: 4 additions & 3 deletions sigma-tree/src/chain/context_extension.rs
@@ -1,8 +1,9 @@
//! ContextExtension type
use crate::serialization::{
sigma_byte_reader::SigmaByteRead, SerializationError, SigmaSerializable,
};
#[cfg(feature = "with-serde")]
use serde::{Deserialize, Serialize};
use sigma_ser::serializer::SerializationError;
use sigma_ser::serializer::SigmaSerializable;
use sigma_ser::vlq_encode;
use std::collections::HashMap;
use std::io;
Expand Down Expand Up @@ -30,7 +31,7 @@ impl SigmaSerializable for ContextExtension {
assert!(self.values.is_empty(), "implemented only for empty");
Ok(())
}
fn sigma_parse<R: vlq_encode::ReadSigmaVlqExt>(r: &mut R) -> Result<Self, SerializationError> {
fn sigma_parse<R: SigmaByteRead>(r: &mut R) -> Result<Self, SerializationError> {
let _ = r.get_u8()?;
Ok(ContextExtension::empty())
}
Expand Down
3 changes: 1 addition & 2 deletions sigma-tree/src/chain/contract.rs
@@ -1,8 +1,7 @@
//! Ergo contract

use super::Address;
use crate::ErgoTree;
use sigma_ser::serializer::SerializationError;
use crate::{serialization::SerializationError, ErgoTree};

/// High-level wrapper for ErgoTree
pub struct Contract {
Expand Down
13 changes: 7 additions & 6 deletions sigma-tree/src/chain/data_input.rs
@@ -1,11 +1,12 @@
//! DataInput type

use sigma_ser::serializer::SerializationError;
use sigma_ser::serializer::SigmaSerializable;
use sigma_ser::vlq_encode;
use std::io;

use super::box_id::BoxId;
use crate::serialization::{
sigma_byte_reader::SigmaByteRead, sigma_byte_writer::SigmaByteWrite, SerializationError,
SigmaSerializable,
};
#[cfg(test)]
use proptest::prelude::*;
#[cfg(test)]
Expand All @@ -24,11 +25,11 @@ pub struct DataInput {
}

impl SigmaSerializable for DataInput {
fn sigma_serialize<W: vlq_encode::WriteSigmaVlqExt>(&self, w: &mut W) -> Result<(), io::Error> {
fn sigma_serialize<W: SigmaByteWrite>(&self, w: &mut W) -> Result<(), io::Error> {
self.box_id.sigma_serialize(w)?;
Ok(())
}
fn sigma_parse<R: vlq_encode::ReadSigmaVlqExt>(r: &mut R) -> Result<Self, SerializationError> {
fn sigma_parse<R: SigmaByteRead>(r: &mut R) -> Result<Self, SerializationError> {
let box_id = BoxId::sigma_parse(r)?;
Ok(DataInput { box_id })
}
Expand All @@ -37,7 +38,7 @@ impl SigmaSerializable for DataInput {
#[cfg(test)]
mod tests {
use super::*;
use sigma_ser::test_helpers::*;
use crate::serialization::sigma_serialize_roundtrip;

proptest! {

Expand Down
12 changes: 6 additions & 6 deletions sigma-tree/src/chain/digest32.rs
@@ -1,14 +1,14 @@
use crate::chain::{Base16DecodedBytes, Base16EncodedBytes};
use crate::{
chain::{Base16DecodedBytes, Base16EncodedBytes},
serialization::{sigma_byte_reader::SigmaByteRead, SerializationError, SigmaSerializable},
};
use blake2::digest::{Update, VariableOutput};
use blake2::VarBlake2b;
#[cfg(test)]
use proptest_derive::Arbitrary;
#[cfg(feature = "with-serde")]
use serde::{Deserialize, Serialize};
use sigma_ser::{
serializer::{SerializationError, SigmaSerializable},
vlq_encode,
};
use sigma_ser::vlq_encode;
use std::convert::TryFrom;
use std::convert::TryInto;
use std::io;
Expand Down Expand Up @@ -76,7 +76,7 @@ impl SigmaSerializable for Digest32 {
w.write_all(self.0.as_ref())?;
Ok(())
}
fn sigma_parse<R: vlq_encode::ReadSigmaVlqExt>(r: &mut R) -> Result<Self, SerializationError> {
fn sigma_parse<R: SigmaByteRead>(r: &mut R) -> Result<Self, SerializationError> {
let mut bytes = [0; Digest32::SIZE];
r.read_exact(&mut bytes)?;
Ok(Self(bytes.into()))
Expand Down

0 comments on commit 00a93e1

Please sign in to comment.