Skip to content

Commit

Permalink
implement Display for all Literal variants;
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat committed Aug 18, 2023
1 parent 3110470 commit 4ecb9fb
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 5 deletions.
9 changes: 9 additions & 0 deletions ergo-chain-types/src/ec_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ impl std::fmt::Debug for EcPoint {
}
}

#[allow(clippy::unwrap_used)]
impl std::fmt::Display for EcPoint {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.write_str(&base16::encode_lower(
&self.scorex_serialize_bytes().unwrap(),
))
}
}

impl EcPoint {
/// Number of bytes to represent any group element as byte array
pub const GROUP_SIZE: usize = 33;
Expand Down
6 changes: 3 additions & 3 deletions ergotree-ir/src/mir/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl std::fmt::Display for Literal {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Literal::Coll(CollKind::NativeColl(NativeColl::CollByte(i8_bytes))) => {
write!(f, "Coll[Byte](");
write!(f, "Coll[Byte](")?;
for (i, b) in i8_bytes.iter().enumerate() {
if i > 0 {
write!(f, ", ")?;
Expand Down Expand Up @@ -169,8 +169,8 @@ impl std::fmt::Display for Literal {
Literal::BigInt(v) => v.fmt(f),
Literal::SigmaProp(v) => v.fmt(f),
Literal::GroupElement(v) => v.fmt(f),
Literal::AvlTree(v) => v.fmt(f),
Literal::CBox(v) => v.fmt(f),
Literal::AvlTree(v) => write!(f, "AvlTree({:?})", v),
Literal::CBox(v) => write!(f, "ErgoBox({:?})", v),
}
}
}
Expand Down
42 changes: 40 additions & 2 deletions ergotree-ir/src/sigma_protocol/sigma_boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::serialization::SigmaSerializable;
use ergo_chain_types::EcPoint;
use std::convert::TryFrom;
use std::convert::TryInto;
use std::fmt::Formatter;

extern crate derive_more;
use bounded_vec::BoundedVec;
Expand Down Expand Up @@ -53,6 +54,12 @@ impl From<EcPoint> for ProveDlog {
}
}

impl std::fmt::Display for ProveDlog {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "proveDlog({})", self.h)
}
}

/// Construct a new SigmaProp value representing public key of Diffie Hellman signature protocol.
/// Used in a proof that of equality of discrete logarithms (i.e., a proof of a Diffie-Hellman tuple):
/// given group elements g, h, u, v, the proof convinces a verifier that the prover knows `w` such
Expand Down Expand Up @@ -86,8 +93,18 @@ impl ProveDhTuple {
}
}

impl std::fmt::Display for ProveDhTuple {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"ProveDhTuple(g: {}, h: {}, u: {}, v: {})",
self.g, self.h, self.u, self.v
)
}
}

/// Sigma proposition
#[derive(PartialEq, Eq, Debug, Clone, From)]
#[derive(PartialEq, Eq, Debug, Clone, From, derive_more::Display)]
pub enum SigmaProofOfKnowledgeTree {
/// public key of Diffie Hellman signature protocol
ProveDhTuple(ProveDhTuple),
Expand Down Expand Up @@ -125,6 +142,16 @@ impl HasOpCode for SigmaConjecture {
}
}

impl std::fmt::Display for SigmaConjecture {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
SigmaConjecture::Cand(c) => write!(f, "{}", c),
SigmaConjecture::Cor(c) => write!(f, "{}", c),
SigmaConjecture::Cthreshold(c) => write!(f, "{}", c),
}
}
}

/// Algebraic data type of sigma proposition expressions
/// Values of this type are used as values of SigmaProp type
#[derive(PartialEq, Eq, Debug, Clone, From, TryInto)]
Expand Down Expand Up @@ -246,8 +273,18 @@ impl From<Cthreshold> for SigmaBoolean {
}
}

impl std::fmt::Display for SigmaBoolean {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
SigmaBoolean::TrivialProp(b) => write!(f, "sigmaProp({})", b),
SigmaBoolean::ProofOfKnowledge(kt) => write!(f, "{}", kt),
SigmaBoolean::SigmaConjecture(sc) => write!(f, "{}", sc),
}
}
}

/// Proposition which can be proven and verified by sigma protocol.
#[derive(PartialEq, Eq, Debug, Clone, From, Into)]
#[derive(PartialEq, Eq, Debug, Clone, From, Into, derive_more::Display)]
pub struct SigmaProp(SigmaBoolean);

impl SigmaProp {
Expand Down Expand Up @@ -295,6 +332,7 @@ impl From<ProveDhTuple> for SigmaProp {
))
}
}

/// Arbitrary impl for ProveDlog
#[cfg(feature = "arbitrary")]
#[allow(clippy::unwrap_used)]
Expand Down
13 changes: 13 additions & 0 deletions ergotree-ir/src/sigma_protocol/sigma_boolean/cand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ impl Cand {
}
}

impl std::fmt::Display for Cand {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("(")?;
for (i, item) in self.items.iter().enumerate() {
if i > 0 {
f.write_str(" && ")?;
}
item.fmt(f)?;
}
f.write_str(")")
}
}

impl SigmaSerializable for Cand {
fn sigma_serialize<W: SigmaByteWrite>(&self, w: &mut W) -> SigmaSerializeResult {
w.put_u16(self.items.len() as u16)?;
Expand Down
13 changes: 13 additions & 0 deletions ergotree-ir/src/sigma_protocol/sigma_boolean/cor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ impl Cor {
}
}

impl std::fmt::Display for Cor {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("(")?;
for (i, item) in self.items.iter().enumerate() {
if i > 0 {
f.write_str(" || ")?;
}
item.fmt(f)?;
}
f.write_str(")")
}
}

impl SigmaSerializable for Cor {
fn sigma_serialize<W: SigmaByteWrite>(&self, w: &mut W) -> SigmaSerializeResult {
w.put_u16(self.items.len() as u16)?;
Expand Down
15 changes: 15 additions & 0 deletions ergotree-ir/src/sigma_protocol/sigma_boolean/cthreshold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,21 @@ impl Cthreshold {
}
}

impl std::fmt::Display for Cthreshold {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("atLeast(")?;
f.write_str(self.k.to_string().as_str())?;
f.write_str(", (")?;
for (i, item) in self.children.iter().enumerate() {
if i > 0 {
f.write_str(", ")?;
}
item.fmt(f)?;
}
f.write_str(")")
}
}

impl HasStaticOpCode for Cthreshold {
const OP_CODE: OpCode = OpCode::ATLEAST;
}
Expand Down

0 comments on commit 4ecb9fb

Please sign in to comment.