Skip to content

Commit

Permalink
Merge branch 'mainnet-staging' into fix/stored-parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
howardwu authored May 23, 2024
2 parents fa04451 + f6ace91 commit a68e2a1
Show file tree
Hide file tree
Showing 10 changed files with 337 additions and 101 deletions.
21 changes: 21 additions & 0 deletions Cargo.lock

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

28 changes: 20 additions & 8 deletions circuit/program/src/data/plaintext/to_bits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ impl<A: Aleo> ToBits for Plaintext<A> {
Self::Literal(literal, bits_le) => {
// Compute the bits of the literal.
let bits = bits_le.get_or_init(|| {
let mut bits_le = vec![Boolean::constant(false), Boolean::constant(false)]; // Variant bit.
let mut bits_le = Vec::new();
bits_le.extend([Boolean::constant(false), Boolean::constant(false)]); // Variant bit.
literal.variant().write_bits_le(&mut bits_le);
literal.size_in_bits().write_bits_le(&mut bits_le);
literal.write_bits_le(&mut bits_le);
bits_le.shrink_to_fit();
bits_le
});
// Extend the vector with the bits of the literal.
Expand All @@ -35,15 +37,17 @@ impl<A: Aleo> ToBits for Plaintext<A> {
Self::Struct(members, bits_le) => {
// Compute the bits of the struct.
let bits = bits_le.get_or_init(|| {
let mut bits_le = vec![Boolean::constant(false), Boolean::constant(true)]; // Variant bit.
let mut bits_le = Vec::new();
bits_le.extend([Boolean::constant(false), Boolean::constant(true)]); // Variant bit.
U8::constant(console::U8::new(members.len() as u8)).write_bits_le(&mut bits_le);
for (identifier, value) in members {
let value_bits = value.to_bits_le();
identifier.size_in_bits().write_bits_le(&mut bits_le);
identifier.write_bits_le(&mut bits_le);
U16::constant(console::U16::new(value_bits.len() as u16)).write_bits_le(&mut bits_le);
bits_le.extend_from_slice(&value_bits);
bits_le.extend(value_bits);
}
bits_le.shrink_to_fit();
bits_le
});
// Extend the vector with the bits of the struct.
Expand All @@ -52,13 +56,15 @@ impl<A: Aleo> ToBits for Plaintext<A> {
Self::Array(elements, bits_le) => {
// Compute the bits of the array.
let bits = bits_le.get_or_init(|| {
let mut bits_le = vec![Boolean::constant(true), Boolean::constant(false)]; // Variant bit.
let mut bits_le = Vec::new();
bits_le.extend([Boolean::constant(true), Boolean::constant(false)]); // Variant bit.
U32::constant(console::U32::new(elements.len() as u32)).write_bits_le(&mut bits_le);
for value in elements {
let value_bits = value.to_bits_le();
U16::constant(console::U16::new(value_bits.len() as u16)).write_bits_le(&mut bits_le);
bits_le.extend(value_bits);
}
bits_le.shrink_to_fit();
bits_le
});
// Extend the vector with the bits of the array.
Expand All @@ -73,10 +79,12 @@ impl<A: Aleo> ToBits for Plaintext<A> {
Self::Literal(literal, bits_be) => {
// Compute the bits of the literal.
let bits = bits_be.get_or_init(|| {
let mut bits_be = vec![Boolean::constant(false), Boolean::constant(false)]; // Variant bit.
let mut bits_be = Vec::new();
bits_be.extend([Boolean::constant(false), Boolean::constant(false)]); // Variant bit.
literal.variant().write_bits_be(&mut bits_be);
literal.size_in_bits().write_bits_be(&mut bits_be);
literal.write_bits_be(&mut bits_be);
bits_be.shrink_to_fit();
bits_be
});
// Extend the vector with the bits of the literal.
Expand All @@ -85,15 +93,17 @@ impl<A: Aleo> ToBits for Plaintext<A> {
Self::Struct(members, bits_be) => {
// Compute the bits of the struct.
let bits = bits_be.get_or_init(|| {
let mut bits_be = vec![Boolean::constant(false), Boolean::constant(true)]; // Variant bit.
let mut bits_be = Vec::new();
bits_be.extend([Boolean::constant(false), Boolean::constant(true)]); // Variant bit.
U8::constant(console::U8::new(members.len() as u8)).write_bits_be(&mut bits_be);
for (identifier, value) in members {
let value_bits = value.to_bits_be();
identifier.size_in_bits().write_bits_be(&mut bits_be);
identifier.write_bits_be(&mut bits_be);
U16::constant(console::U16::new(value_bits.len() as u16)).write_bits_be(&mut bits_be);
bits_be.extend_from_slice(&value_bits);
bits_be.extend(value_bits);
}
bits_be.shrink_to_fit();
bits_be
});
// Extend the vector with the bits of the struct.
Expand All @@ -102,13 +112,15 @@ impl<A: Aleo> ToBits for Plaintext<A> {
Self::Array(elements, bits_be) => {
// Compute the bits of the array.
let bits = bits_be.get_or_init(|| {
let mut bits_be = vec![Boolean::constant(true), Boolean::constant(false)]; // Variant bit.
let mut bits_be = Vec::new();
bits_be.extend([Boolean::constant(true), Boolean::constant(false)]); // Variant bit.
U32::constant(console::U32::new(elements.len() as u32)).write_bits_be(&mut bits_be);
for value in elements {
let value_bits = value.to_bits_be();
U16::constant(console::U16::new(value_bits.len() as u16)).write_bits_be(&mut bits_be);
bits_be.extend(value_bits);
}
bits_be.shrink_to_fit();
bits_be
});
// Extend the vector with the bits of the array.
Expand Down
3 changes: 3 additions & 0 deletions console/program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ version = "0.2"
[dependencies.enum_index_derive]
version = "0.2"

[dependencies.enum-iterator]
version = "2.1"

[dependencies.indexmap]
version = "2.0"

Expand Down
2 changes: 1 addition & 1 deletion console/program/src/data/identifier/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl<N: Network> FromStr for Identifier<N> {

// Ensure that the identifier is not a literal.
ensure!(
crate::LiteralType::from_str(identifier).is_err(),
!enum_iterator::all::<crate::LiteralType>().any(|lt| lt.type_name() == identifier),
"Identifier '{identifier}' is a reserved literal type"
);

Expand Down
5 changes: 3 additions & 2 deletions console/program/src/data_types/literal_type/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ use snarkvm_console_network::prelude::*;
use snarkvm_console_types::{prelude::*, Boolean};

use core::fmt::{self, Debug, Display};
use enum_iterator::Sequence;
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;

#[derive(Copy, Clone, PartialEq, Eq, Hash, FromPrimitive)]
#[derive(Copy, Clone, PartialEq, Eq, Hash, FromPrimitive, Sequence)]
pub enum LiteralType {
/// The Aleo address type.
Address,
Expand Down Expand Up @@ -66,7 +67,7 @@ pub enum LiteralType {

impl LiteralType {
/// Returns the literal type name.
pub fn type_name(&self) -> &str {
pub const fn type_name(&self) -> &str {
match self {
Self::Address => "address",
Self::Boolean => "boolean",
Expand Down
34 changes: 17 additions & 17 deletions console/program/src/data_types/literal_type/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,23 @@ impl Parser for LiteralType {
fn parse(string: &str) -> ParserResult<Self> {
// Parse the type from the string.
alt((
map(tag("address"), |_| Self::Address),
map(tag("boolean"), |_| Self::Boolean),
map(tag("field"), |_| Self::Field),
map(tag("group"), |_| Self::Group),
map(tag("i8"), |_| Self::I8),
map(tag("i16"), |_| Self::I16),
map(tag("i32"), |_| Self::I32),
map(tag("i64"), |_| Self::I64),
map(tag("i128"), |_| Self::I128),
map(tag("u8"), |_| Self::U8),
map(tag("u16"), |_| Self::U16),
map(tag("u32"), |_| Self::U32),
map(tag("u64"), |_| Self::U64),
map(tag("u128"), |_| Self::U128),
map(tag("scalar"), |_| Self::Scalar),
map(tag("signature"), |_| Self::Signature),
map(tag("string"), |_| Self::String),
map(tag(Self::Address.type_name()), |_| Self::Address),
map(tag(Self::Boolean.type_name()), |_| Self::Boolean),
map(tag(Self::Field.type_name()), |_| Self::Field),
map(tag(Self::Group.type_name()), |_| Self::Group),
map(tag(Self::I8.type_name()), |_| Self::I8),
map(tag(Self::I16.type_name()), |_| Self::I16),
map(tag(Self::I32.type_name()), |_| Self::I32),
map(tag(Self::I64.type_name()), |_| Self::I64),
map(tag(Self::I128.type_name()), |_| Self::I128),
map(tag(Self::U8.type_name()), |_| Self::U8),
map(tag(Self::U16.type_name()), |_| Self::U16),
map(tag(Self::U32.type_name()), |_| Self::U32),
map(tag(Self::U64.type_name()), |_| Self::U64),
map(tag(Self::U128.type_name()), |_| Self::U128),
map(tag(Self::Scalar.type_name()), |_| Self::Scalar),
map(tag(Self::Signature.type_name()), |_| Self::Signature),
map(tag(Self::String.type_name()), |_| Self::String),
))(string)
}
}
Expand Down
4 changes: 4 additions & 0 deletions ledger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ package = "snarkvm-ledger-block"
path = "./block"
features = [ "test" ]

[dev-dependencies.ledger-test-helpers]
package = "snarkvm-ledger-test-helpers"
path = "./test-helpers"

[dev-dependencies.serde_json]
version = "1.0"
features = [ "preserve_order" ]
Loading

0 comments on commit a68e2a1

Please sign in to comment.