Skip to content

Commit

Permalink
serialization and hash for NoteCommitment
Browse files Browse the repository at this point in the history
  • Loading branch information
bazzilic authored and mariari committed Aug 30, 2023
1 parent 7d9a13c commit c497f01
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 26 deletions.
2 changes: 1 addition & 1 deletion taiga_halo2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ name = "taiga_sudoku"
name = "tx_examples"

[features]
default = []
default = ["borsh", "serde"]
serde = ["dep:serde", "pasta_curves/serde"]
borsh = ["dep:borsh"]
51 changes: 50 additions & 1 deletion taiga_halo2/src/note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ use serde;
use borsh::{BorshDeserialize, BorshSerialize};

/// A commitment to a note.
#[derive(Copy, Debug, Clone)]
#[derive(Copy, Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct NoteCommitment(pallas::Point);

impl NoteCommitment {
Expand All @@ -54,6 +55,32 @@ impl Default for NoteCommitment {
}
}

#[cfg(feature = "borsh")]
impl BorshSerialize for NoteCommitment {
fn serialize<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
writer.write_all(&self.0.to_bytes())?;
Ok(())
}
}

#[cfg(feature = "borsh")]
impl BorshDeserialize for NoteCommitment {
fn deserialize_reader<R: std::io::Read>(reader: &mut R) -> std::io::Result<Self> {
let mut repr = [0u8; 32];
reader.read_exact(&mut repr)?;
let value = Option::from(pallas::Point::from_bytes(&repr)).ok_or_else(|| {
std::io::Error::new(std::io::ErrorKind::InvalidData, "Node value not in field")
})?;
Ok(Self(value))
}
}

impl Hash for NoteCommitment {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.to_bytes().as_ref().hash(state);
}
}

/// A note
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Expand Down Expand Up @@ -624,6 +651,8 @@ pub mod tests {
fn note_borsh_serialization_test() {
use borsh::{BorshDeserialize, BorshSerialize};
use rand::rngs::OsRng;

use crate::note::NoteCommitment;
let mut rng = OsRng;

let input_note = random_input_note(&mut rng);
Expand All @@ -643,5 +672,25 @@ pub mod tests {
let de_note: Note = BorshDeserialize::deserialize(&mut borsh.as_ref()).unwrap();
assert_eq!(output_note, de_note);
}

let icm = input_note.commitment();
{
// BorshSerialize
let borsh = icm.try_to_vec().unwrap();
// BorshDeserialize
let de_icm: NoteCommitment =
BorshDeserialize::deserialize(&mut borsh.as_ref()).unwrap();
assert_eq!(icm, de_icm);
}

let ocm = output_note.commitment();
{
// BorshSerialize
let borsh = ocm.try_to_vec().unwrap();
// BorshDeserialize
let de_ocm: NoteCommitment =
BorshDeserialize::deserialize(&mut borsh.as_ref()).unwrap();
assert_eq!(ocm, de_ocm);
}
}
}
31 changes: 7 additions & 24 deletions taiga_halo2/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,26 +351,6 @@ pub mod testing {

#[test]
fn test_halo2_transaction() {
use super::Transaction;
use rand::rngs::OsRng;

let rng = OsRng;

let (shielded_ptx_bundle, r_vec) = create_shielded_ptx_bundle(2);
// TODO: add transparent_ptx_bundle test
let transparent_ptx_bundle = None;
let tx = Transaction::build(
rng,
Some(shielded_ptx_bundle),
transparent_ptx_bundle,
r_vec,
);
let (_, _) = tx.execute().unwrap();
}

#[cfg(feature = "borsh")]
#[test]
fn test_halo2_transaction_borsh_serialize() {
use super::Transaction;
use borsh::{BorshDeserialize, BorshSerialize};
use rand::rngs::OsRng;
Expand All @@ -388,9 +368,12 @@ pub mod testing {
);
let (shielded_ret, _) = tx.execute().unwrap();

let borsh = tx.try_to_vec().unwrap();
let de_tx: Transaction = BorshDeserialize::deserialize(&mut borsh.as_ref()).unwrap();
let (de_shielded_ret, _) = de_tx.execute().unwrap();
assert_eq!(shielded_ret, de_shielded_ret);
#[cfg(feature = "borsh")]
{
let borsh = tx.try_to_vec().unwrap();
let de_tx: Transaction = BorshDeserialize::deserialize(&mut borsh.as_ref()).unwrap();
let (de_shielded_ret, _) = de_tx.execute().unwrap();
assert_eq!(shielded_ret, de_shielded_ret);
}
}
}

0 comments on commit c497f01

Please sign in to comment.