Skip to content

Commit

Permalink
Resolve merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
howardwu committed Sep 1, 2023
2 parents 848b13f + b87eb05 commit 981976d
Show file tree
Hide file tree
Showing 19 changed files with 96 additions and 107 deletions.
20 changes: 10 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ categories = [
include = [ "Cargo.toml", "vm", "README.md", "LICENSE.md" ]
license = "Apache-2.0"
edition = "2021"
rust-version = "1.66"
rust-version = "1.70"

[workspace]
members = [
Expand Down Expand Up @@ -203,8 +203,8 @@ version = "1.0.73"
optional = true

[dependencies.clap]
version = "4.3"
features = [ "derive", "color", "unstable-styles" ]
version = "4.4"
features = [ "derive", "color" ]
optional = true

[dependencies.colored]
Expand Down
2 changes: 1 addition & 1 deletion algorithms/src/fft/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub struct EvaluationDomain<F: FftField> {
pub group_gen: F,
/// Inverse of the generator of the subgroup.
pub group_gen_inv: F,
/// Multiplicative generator of the finite field.
/// Inverse of the multiplicative generator of the finite field.
pub generator_inv: F,
}

Expand Down
27 changes: 7 additions & 20 deletions console/collections/src/merkle_tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,28 +721,15 @@ impl<E: Environment, LH: LeafHash<Hash = PH::Hash>, PH: PathHash<Hash = Field<E>

/// Returns the depth of the tree, given the size of the tree.
#[inline]
#[allow(clippy::cast_possible_truncation)]
fn tree_depth<const DEPTH: u8>(tree_size: usize) -> Result<u8> {
let tree_size = u64::try_from(tree_size)?;
// Ensure the tree size is less than 2^52 (for casting to an f64).
let tree_depth = match tree_size < 4503599627370496_u64 {
// Compute the log2 of the tree size.
true => (tree_size as f64).log2(),
false => bail!("Tree size must be less than 2^52"),
};
// Ensure the tree depth is within a u8 range.
match tree_depth <= u8::MAX as f64 {
true => {
// Convert the tree depth to a u8.
let tree_depth = tree_depth as u8;
// Ensure the tree depth is within the depth bound.
match tree_depth <= DEPTH {
// Return the tree depth.
true => Ok(tree_depth),
false => bail!("Merkle tree cannot exceed depth {DEPTH}: attempted to reach depth {tree_depth}"),
}
}
false => bail!("Merkle tree depth ({tree_depth}) exceeds maximum size ({})", u8::MAX),
// Since we only allow tree sizes up to u64::MAX, the maximum possible depth is 63.
let tree_depth = u8::try_from(tree_size.checked_ilog2().unwrap_or(0))?;
// Ensure the tree depth is within the depth bound.
match tree_depth <= DEPTH {
// Return the tree depth.
true => Ok(tree_depth),
false => bail!("Merkle tree cannot exceed depth {DEPTH}: attempted to reach depth {tree_depth}"),
}
}

Expand Down
4 changes: 2 additions & 2 deletions console/program/src/data_types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ pub use record_type::{EntryType, RecordType};
mod register_type;
pub use register_type::RegisterType;

mod struct_;
pub use struct_::Struct;
mod struct_type;
pub use struct_type::StructType;

mod value_type;
pub use value_type::ValueType;
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@

use super::*;

impl<N: Network> FromBytes for Struct<N> {
/// Reads a struct from a buffer.
impl<N: Network> FromBytes for StructType<N> {
/// Reads a struct type from a buffer.
fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
// Read the name of the struct.
// Read the name of the struct type.
let name = Identifier::read_le(&mut reader)?;

// Read the number of members.
let num_members = u16::read_le(&mut reader)?;
// Ensure the number of members is within the maximum limit.
if num_members as usize > N::MAX_STRUCT_ENTRIES {
return Err(error(format!(
"Struct exceeds size: expected <= {}, found {num_members}",
"StructType exceeds size: expected <= {}, found {num_members}",
N::MAX_STRUCT_ENTRIES
)));
}
Expand All @@ -46,8 +46,8 @@ impl<N: Network> FromBytes for Struct<N> {
}
}

impl<N: Network> ToBytes for Struct<N> {
/// Writes the struct to a buffer.
impl<N: Network> ToBytes for StructType<N> {
/// Writes the struct type to a buffer.
fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
// Ensure the number of members is within the maximum limit.
if self.members.len() > N::MAX_STRUCT_ENTRIES {
Expand Down Expand Up @@ -80,8 +80,8 @@ mod tests {
#[test]
fn test_bytes() -> Result<()> {
let expected =
Struct::<CurrentNetwork>::from_str("struct message:\n first as field;\n second as field;")?;
let candidate = Struct::from_bytes_le(&expected.to_bytes_le().unwrap()).unwrap();
StructType::<CurrentNetwork>::from_str("struct message:\n first as field;\n second as field;")?;
let candidate = StructType::from_bytes_le(&expected.to_bytes_le().unwrap()).unwrap();
assert_eq!(expected, candidate);
Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,28 @@ use snarkvm_console_network::prelude::*;
use indexmap::IndexMap;

#[derive(Clone, PartialEq, Eq)]
pub struct Struct<N: Network> {
pub struct StructType<N: Network> {
/// The name of the struct.
name: Identifier<N>,
/// The name and type for the members of the struct.
members: IndexMap<Identifier<N>, PlaintextType<N>>,
}

impl<N: Network> Struct<N> {
/// Returns the name of the struct.
impl<N: Network> StructType<N> {
/// Returns the name of the struct type.
#[inline]
pub const fn name(&self) -> &Identifier<N> {
&self.name
}

/// Returns the members of the struct.
/// Returns the members of the struct type.
#[inline]
pub const fn members(&self) -> &IndexMap<Identifier<N>, PlaintextType<N>> {
&self.members
}
}

impl<N: Network> TypeName for Struct<N> {
impl<N: Network> TypeName for StructType<N> {
/// Returns the type name.
fn type_name() -> &'static str {
"struct"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use super::*;

impl<N: Network> Parser for Struct<N> {
impl<N: Network> Parser for StructType<N> {
/// Parses a struct as:
/// ```text
/// struct message:
Expand Down Expand Up @@ -74,7 +74,7 @@ impl<N: Network> Parser for Struct<N> {
}
}

impl<N: Network> FromStr for Struct<N> {
impl<N: Network> FromStr for StructType<N> {
type Err = Error;

/// Returns a struct from a string literal.
Expand All @@ -91,16 +91,16 @@ impl<N: Network> FromStr for Struct<N> {
}
}

impl<N: Network> Debug for Struct<N> {
/// Prints the struct as a string.
impl<N: Network> Debug for StructType<N> {
/// Prints the struct type as a string.
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
Display::fmt(self, f)
}
}

#[allow(clippy::format_push_string)]
impl<N: Network> Display for Struct<N> {
/// Prints the struct as a string.
impl<N: Network> Display for StructType<N> {
/// Prints the struct type as a string.
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
let mut output = format!("{} {}:\n", Self::type_name(), self.name);
for (identifier, plaintext_type) in &self.members {
Expand All @@ -120,7 +120,7 @@ mod tests {

#[test]
fn test_parse() -> Result<()> {
let expected = Struct::<CurrentNetwork> {
let expected = StructType::<CurrentNetwork> {
name: Identifier::from_str("message")?,
members: IndexMap::from_iter(
vec![
Expand All @@ -131,7 +131,7 @@ mod tests {
),
};

let (remainder, candidate) = Struct::<CurrentNetwork>::parse(
let (remainder, candidate) = StructType::<CurrentNetwork>::parse(
r"
struct message:
sender as address;
Expand All @@ -146,50 +146,52 @@ struct message:
#[test]
fn test_parse_fails() {
// Must be non-empty.
assert!(Struct::<CurrentNetwork>::parse("").is_err());
assert!(Struct::<CurrentNetwork>::parse("struct message:").is_err());
assert!(StructType::<CurrentNetwork>::parse("").is_err());
assert!(StructType::<CurrentNetwork>::parse("struct message:").is_err());

// Invalid characters.
assert!(Struct::<CurrentNetwork>::parse("{}").is_err());
assert!(Struct::<CurrentNetwork>::parse("_").is_err());
assert!(Struct::<CurrentNetwork>::parse("__").is_err());
assert!(Struct::<CurrentNetwork>::parse("___").is_err());
assert!(Struct::<CurrentNetwork>::parse("-").is_err());
assert!(Struct::<CurrentNetwork>::parse("--").is_err());
assert!(Struct::<CurrentNetwork>::parse("---").is_err());
assert!(Struct::<CurrentNetwork>::parse("*").is_err());
assert!(Struct::<CurrentNetwork>::parse("**").is_err());
assert!(Struct::<CurrentNetwork>::parse("***").is_err());
assert!(StructType::<CurrentNetwork>::parse("{}").is_err());
assert!(StructType::<CurrentNetwork>::parse("_").is_err());
assert!(StructType::<CurrentNetwork>::parse("__").is_err());
assert!(StructType::<CurrentNetwork>::parse("___").is_err());
assert!(StructType::<CurrentNetwork>::parse("-").is_err());
assert!(StructType::<CurrentNetwork>::parse("--").is_err());
assert!(StructType::<CurrentNetwork>::parse("---").is_err());
assert!(StructType::<CurrentNetwork>::parse("*").is_err());
assert!(StructType::<CurrentNetwork>::parse("**").is_err());
assert!(StructType::<CurrentNetwork>::parse("***").is_err());

// Must not start with a number.
assert!(Struct::<CurrentNetwork>::parse("1").is_err());
assert!(Struct::<CurrentNetwork>::parse("2").is_err());
assert!(Struct::<CurrentNetwork>::parse("3").is_err());
assert!(Struct::<CurrentNetwork>::parse("1foo").is_err());
assert!(Struct::<CurrentNetwork>::parse("12").is_err());
assert!(Struct::<CurrentNetwork>::parse("111").is_err());
assert!(StructType::<CurrentNetwork>::parse("1").is_err());
assert!(StructType::<CurrentNetwork>::parse("2").is_err());
assert!(StructType::<CurrentNetwork>::parse("3").is_err());
assert!(StructType::<CurrentNetwork>::parse("1foo").is_err());
assert!(StructType::<CurrentNetwork>::parse("12").is_err());
assert!(StructType::<CurrentNetwork>::parse("111").is_err());

// Must fit within the data capacity of a base field element.
let struct_ =
Struct::<CurrentNetwork>::parse("foo_bar_baz_qux_quux_quuz_corge_grault_garply_waldo_fred_plugh_xyzzy");
StructType::<CurrentNetwork>::parse("foo_bar_baz_qux_quux_quuz_corge_grault_garply_waldo_fred_plugh_xyzzy");
assert!(struct_.is_err());
}

#[test]
fn test_display() {
let expected = "struct message:\n first as field;\n second as field;";
let message = Struct::<CurrentNetwork>::parse(expected).unwrap().1;
let message = StructType::<CurrentNetwork>::parse(expected).unwrap().1;
assert_eq!(expected, format!("{message}"));
}

#[test]
fn test_display_fails() {
// Duplicate identifier.
let candidate = Struct::<CurrentNetwork>::parse("struct message:\n first as field;\n first as field;");
let candidate =
StructType::<CurrentNetwork>::parse("struct message:\n first as field;\n first as field;");
assert!(candidate.is_err());
// Visibility in plaintext type.
let candidate =
Struct::<CurrentNetwork>::parse("struct message:\n first as field.public;\n first as field.private;");
let candidate = StructType::<CurrentNetwork>::parse(
"struct message:\n first as field.public;\n first as field.private;",
);
assert!(candidate.is_err());
}

Expand All @@ -199,7 +201,7 @@ struct message:
for i in 0..CurrentNetwork::MAX_STRUCT_ENTRIES {
string += &format!(" member_{i} as field;\n");
}
assert!(Struct::<CurrentNetwork>::parse(&string).is_ok());
assert!(StructType::<CurrentNetwork>::parse(&string).is_ok());
}

#[test]
Expand All @@ -208,6 +210,6 @@ struct message:
for i in 0..=CurrentNetwork::MAX_STRUCT_ENTRIES {
string += &format!(" member_{i} as field;\n");
}
assert!(Struct::<CurrentNetwork>::parse(&string).is_err());
assert!(StructType::<CurrentNetwork>::parse(&string).is_err());
}
}
Loading

0 comments on commit 981976d

Please sign in to comment.