Skip to content

Commit

Permalink
Add to_hex() and from_hex() to Nullifier struct
Browse files Browse the repository at this point in the history
  • Loading branch information
phklive authored Jan 12, 2024
1 parent 1841b99 commit 4e8fa1e
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 36 deletions.
11 changes: 4 additions & 7 deletions objects/src/accounts/account_id.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use core::fmt;

use miden_crypto::utils::{ByteReader, Deserializable, Serializable};
use vm_processor::DeserializationError;

use super::{
get_account_seed, Account, AccountError, Digest, Felt, FieldElement, Hasher, StarkField,
ToString, Vec, Word,
get_account_seed, Account, AccountError, ByteReader, Deserializable, DeserializationError,
Digest, Felt, FieldElement, Hasher, Serializable, StarkField, String, ToString, Vec, Word,
};
use crate::utils::string::String;
use crate::utils::hex_to_bytes;

// ACCOUNT ID
// ================================================================================================
Expand Down Expand Up @@ -212,7 +209,7 @@ impl AccountId {
/// Creates an Account Id from a hex string. Assumes the string starts with "0x" and
/// that the hexadecimal characters are big-endian encoded.
pub fn from_hex(hex_value: &str) -> Result<AccountId, AccountError> {
miden_crypto::utils::hex_to_bytes(hex_value)
hex_to_bytes(hex_value)
.map_err(|err| AccountError::HexParseError(err.to_string()))
.and_then(|mut bytes: [u8; 8]| {
// `bytes` ends up being parsed as felt, and the input to that is assumed to be little-endian
Expand Down
9 changes: 5 additions & 4 deletions objects/src/assets/mod.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use vm_processor::DeserializationError;

use super::{
accounts::{AccountId, AccountType},
utils::{collections::Vec, string::ToString},
utils::{
collections::Vec,
serde::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable},
string::ToString,
},
AssetError, Felt, Hasher, StarkField, Word, ZERO,
};

mod fungible;
pub use fungible::FungibleAsset;

mod nonfungible;
use miden_crypto::utils::{ByteReader, ByteWriter, Deserializable, Serializable};
pub use nonfungible::{NonFungibleAsset, NonFungibleAssetDetails};

mod token_symbol;
Expand Down
2 changes: 1 addition & 1 deletion objects/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub mod crypto {
}

pub mod utils {
pub use miden_crypto::utils::{format, vec};
pub use miden_crypto::utils::{bytes_to_hex_string, format, hex_to_bytes, vec, HexParseError};
pub use vm_core::utils::{collections, group_slice_elements, string, IntoBytes};

pub mod serde {
Expand Down
8 changes: 4 additions & 4 deletions objects/src/notes/assets.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use core::cell::OnceCell;

use miden_crypto::utils::{ByteReader, ByteWriter, Deserializable, Serializable};
use vm_processor::DeserializationError;

use super::{Asset, Digest, Felt, Hasher, NoteError, Vec, Word, WORD_SIZE, ZERO};
use super::{
Asset, ByteReader, ByteWriter, Deserializable, DeserializationError, Digest, Felt, Hasher,
NoteError, Serializable, Vec, Word, WORD_SIZE, ZERO,
};

// NOTE ASSETS
// ================================================================================================
Expand Down
7 changes: 4 additions & 3 deletions objects/src/notes/envelope.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use miden_crypto::utils::{ByteReader, ByteWriter, Deserializable, Serializable};
use vm_core::StarkField;
use vm_processor::DeserializationError;

use super::{Felt, Note, NoteId, NoteMetadata, Vec, Word};
use super::{
ByteReader, ByteWriter, Deserializable, DeserializationError, Felt, Note, NoteId, NoteMetadata,
Serializable, Vec, Word,
};

// NOTE ENVELOPE
// ================================================================================================
Expand Down
6 changes: 4 additions & 2 deletions objects/src/notes/inputs.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use core::cell::OnceCell;

use miden_crypto::utils::{ByteReader, ByteWriter, Deserializable, Serializable};
use vm_processor::DeserializationError;

use super::{Digest, Felt, Hasher, NoteError, Vec, ZERO};
use super::{
ByteReader, ByteWriter, Deserializable, Digest, Felt, Hasher, NoteError, Serializable, Vec,
ZERO,
};

// NOTE INPUTS
// ================================================================================================
Expand Down
5 changes: 3 additions & 2 deletions objects/src/notes/metadata.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use miden_crypto::utils::{ByteReader, ByteWriter, Deserializable, Serializable};
use vm_processor::DeserializationError;

use super::{AccountId, Felt, NoteError, Word};
use super::{
AccountId, ByteReader, ByteWriter, Deserializable, Felt, NoteError, Serializable, Word,
};

/// Represents metadata associated with a note. This includes the sender, tag, and number of assets.
/// - sender is the account which created the note.
Expand Down
9 changes: 5 additions & 4 deletions objects/src/notes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use core::cell::OnceCell;

use vm_processor::DeserializationError;

use super::{
accounts::AccountId,
assembly::{Assembler, AssemblyContext, ProgramAst},
assets::Asset,
utils::{collections::Vec, string::ToString},
utils::{
collections::Vec,
serde::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable},
string::{String, ToString},
},
vm::CodeBlock,
Digest, Felt, Hasher, NoteError, Word, WORD_SIZE, ZERO,
};
Expand All @@ -27,7 +29,6 @@ mod nullifier;
pub use nullifier::Nullifier;

mod origin;
use miden_crypto::utils::{ByteReader, ByteWriter, Deserializable, Serializable};
pub use origin::{NoteInclusionProof, NoteOrigin};

mod script;
Expand Down
37 changes: 34 additions & 3 deletions objects/src/notes/nullifier.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use super::{Digest, Felt, Hasher, Note, Word, WORD_SIZE, ZERO};
use crate::utils::serde::{
ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable,
use super::{
ByteReader, ByteWriter, Deserializable, DeserializationError, Digest, Felt, Hasher, Note,
Serializable, String, Word, WORD_SIZE, ZERO,
};
use crate::utils::{bytes_to_hex_string, hex_to_bytes, HexParseError};

// NULLIFIER
// ================================================================================================
Expand Down Expand Up @@ -43,6 +44,20 @@ impl Nullifier {
pub fn inner(&self) -> Digest {
self.0
}

/// Creates a Nullifier from a hex string. Assumes that the string starts with "0x" and
/// that the hexadecimal characters are big-endian encoded.
pub fn from_hex(hex_value: &str) -> Result<Self, HexParseError> {
hex_to_bytes(hex_value).and_then(|bytes: [u8; 32]| {
let digest = Digest::try_from(bytes)?;
Ok(digest.into())
})
}

/// Returns a big-endian, hex-encoded string.
pub fn to_hex(&self) -> String {
bytes_to_hex_string(self.0.as_bytes())
}
}

// CONVERSIONS INTO NULLIFIER
Expand Down Expand Up @@ -113,3 +128,19 @@ impl Deserializable for Nullifier {
Ok(Self(nullifier))
}
}

// TESTS
// ================================================================================================

#[cfg(test)]
mod tests {
use crate::notes::Nullifier;

#[test]
fn test_from_hex_and_back() {
let nullifier_hex = "0x41e7dbbc8ce63ec25cf2d76d76162f16ef8fd1195288171f5e5a3e178222f6d2";
let nullifier = Nullifier::from_hex(nullifier_hex).unwrap();

assert_eq!(nullifier_hex, nullifier.to_hex());
}
}
8 changes: 4 additions & 4 deletions objects/src/notes/origin.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use miden_crypto::utils::{ByteReader, ByteWriter, Deserializable, Serializable};
use vm_processor::DeserializationError;

use super::{Digest, NoteError, ToString, NOTE_TREE_DEPTH};
use super::{
ByteReader, ByteWriter, Deserializable, DeserializationError, Digest, NoteError, Serializable,
ToString, NOTE_TREE_DEPTH,
};
use crate::crypto::merkle::{MerklePath, NodeIndex};

/// Contains information about the origin of a note.
Expand Down
5 changes: 3 additions & 2 deletions objects/src/transaction/proven_tx.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use miden_crypto::utils::{ByteReader, ByteWriter, Deserializable, Serializable};
use miden_verifier::ExecutionProof;
use vm_processor::DeserializationError;

use super::{AccountId, Digest, InputNotes, NoteEnvelope, Nullifier, OutputNotes, TransactionId};
use crate::utils::serde::{
ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable,
};

// PROVEN TRANSACTION
// ================================================================================================
Expand Down

0 comments on commit 4e8fa1e

Please sign in to comment.