Skip to content

Commit

Permalink
circuit struct serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
jowparks committed Oct 18, 2024
1 parent 2394333 commit f9a8d34
Show file tree
Hide file tree
Showing 8 changed files with 596 additions and 56 deletions.
66 changes: 18 additions & 48 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion ironfish-zkp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,3 @@ lazy_static = "1.4.0"
rand = "0.8.5"
zcash_primitives = { git = "https://github.com/iron-fish/librustzcash.git", branch = "blstrs", package = "zcash_primitives" }
zcash_proofs = { git = "https://github.com/iron-fish/librustzcash.git", branch = "blstrs", package = "zcash_proofs" }
hex = "0.4.3"
86 changes: 86 additions & 0 deletions ironfish-zkp/src/circuits/mint_asset.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::{borrow::Borrow, io::{Read, Write}};
use bellperson::{
gadgets::{blake2s, boolean},
Circuit,
Expand All @@ -12,6 +13,9 @@ use crate::{
constants::{proof::PUBLIC_KEY_GENERATOR, CRH_IVK_PERSONALIZATION},
ProofGenerationKey,
};
use byteorder::{ReadBytesExt, WriteBytesExt};

use super::util::Reader;

pub struct MintAsset {
/// Key required to construct proofs for a particular spending key
Expand All @@ -22,6 +26,40 @@ pub struct MintAsset {
pub public_key_randomness: Option<jubjub::Fr>,
}


impl MintAsset {
pub fn write<W: Write>(&self, mut writer: W) -> std::io::Result<()> {
if let Some(proof_generation_key) = self.proof_generation_key.borrow() {
writer.write_u8(1)?;
writer.write_all(proof_generation_key.to_bytes().as_ref())?;
} else {
writer.write_u8(0)?;
}
if let Some(public_key_randomness) = self.public_key_randomness.borrow() {
writer.write_u8(1)?;
writer.write_all(public_key_randomness.to_bytes().as_ref())?;
} else {
writer.write_u8(0)?;
}
Ok(())
}

pub fn read<R: Read>(mut reader: R) -> std::io::Result<MintAsset> {
let mut proof_generation_key = None;
if reader.read_u8()? == 1 {
proof_generation_key = Some(ProofGenerationKey::read(&mut reader)?);
}
let mut public_key_randomness = None;
if reader.read_u8()? == 1 {
public_key_randomness = Some(jubjub::Fr::read(&mut reader)?);
}
Ok(MintAsset {
proof_generation_key,
public_key_randomness,
})
}
}

impl Circuit<blstrs::Scalar> for MintAsset {
fn synthesize<CS: bellperson::ConstraintSystem<blstrs::Scalar>>(
self,
Expand Down Expand Up @@ -181,4 +219,52 @@ mod test {
// Sanity check
assert!(cs.verify(&public_inputs));
}

#[test]
fn test_mint_asset_read_write() {
// Seed a fixed RNG for determinism in the test
let mut rng = StdRng::seed_from_u64(0);

// Create a MintAsset instance with random data
let proof_generation_key = ProofGenerationKey::new(
jubjub::SubgroupPoint::random(&mut rng),
jubjub::Fr::random(&mut rng),
);
let public_key_randomness = jubjub::Fr::random(&mut rng);

let mint_asset = MintAsset {
proof_generation_key: Some(proof_generation_key.clone()),
public_key_randomness: Some(public_key_randomness),
};

let mut buffer = vec![];
mint_asset.write(&mut buffer).unwrap();

let deserialized_mint_asset = MintAsset::read(&buffer[..]).unwrap();

assert_eq!(
mint_asset.proof_generation_key.is_some(),
deserialized_mint_asset.proof_generation_key.is_some()
);
assert_eq!(
mint_asset.public_key_randomness.is_some(),
deserialized_mint_asset.public_key_randomness.is_some()
);

if let (Some(pk1), Some(pk2)) = (
&mint_asset.proof_generation_key,
&deserialized_mint_asset.proof_generation_key,
) {
assert_eq!(pk1.ak, pk2.ak);
assert_eq!(pk1.nsk, pk2.nsk);

}

if let (Some(r1), Some(r2)) = (
&mint_asset.public_key_randomness,
&deserialized_mint_asset.public_key_randomness,
) {
assert_eq!(r1, r2);
}
}
}
Loading

0 comments on commit f9a8d34

Please sign in to comment.