Skip to content

Commit

Permalink
move proof generation key extension to ironfish-zkp
Browse files Browse the repository at this point in the history
  • Loading branch information
jowparks committed Oct 17, 2024
1 parent 62430e0 commit 3e00045
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 172 deletions.
3 changes: 1 addition & 2 deletions ironfish-rust/src/keys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use group::GroupEncoding;
use ironfish_zkp::constants::{
CRH_IVK_PERSONALIZATION, PROOF_GENERATION_KEY_GENERATOR, SPENDING_KEY_GENERATOR,
};
pub use ironfish_zkp::ProofGenerationKey;
use jubjub::SubgroupPoint;
use rand::prelude::*;

Expand All @@ -26,8 +27,6 @@ mod view_keys;
pub use view_keys::*;
mod util;
pub use util::*;
pub mod proof_generation_key;
pub use proof_generation_key::*;

#[cfg(test)]
mod test;
Expand Down
170 changes: 0 additions & 170 deletions ironfish-rust/src/keys/proof_generation_key.rs

This file was deleted.

1 change: 1 addition & 0 deletions ironfish-zkp/src/primitives/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
mod value_commitment;
pub mod proof_generation_key;
pub use value_commitment::ValueCommitment;
64 changes: 64 additions & 0 deletions ironfish-zkp/src/primitives/proof_generation_key.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use group::GroupEncoding;
use jubjub::{Fr, SubgroupPoint};
use zcash_primitives::sapling::ProofGenerationKey as ZcashProofGenerationKey;
use std::fmt;
use std::error::Error;
use std::ops::Deref;

#[derive(Debug)]
pub enum ProofGenerationKeyError {
InvalidAuthorizingKey,
InvalidNullifierDerivingKey,
HexConversionError,
}

impl fmt::Display for ProofGenerationKeyError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ProofGenerationKeyError::InvalidAuthorizingKey => write!(f, "Invalid authorizing key"),
ProofGenerationKeyError::InvalidNullifierDerivingKey => write!(f, "Invalid nullifier deriving key"),
ProofGenerationKeyError::HexConversionError => write!(f, "Hex conversion error"),
}
}
}

impl Error for ProofGenerationKeyError {}

pub struct ProofGenerationKey(ZcashProofGenerationKey);

impl ProofGenerationKey {
pub fn to_bytes(&self) -> [u8; 64] {
let mut proof_generation_key_bytes: [u8; 64] = [0; 64];
proof_generation_key_bytes[0..32].copy_from_slice(&self.0.ak.to_bytes());
proof_generation_key_bytes[32..].copy_from_slice(&self.0.nsk.to_bytes());
proof_generation_key_bytes
}

pub fn from_bytes(proof_generation_key_bytes: [u8; 64]) -> Result<Self, ProofGenerationKeyError> {
let mut ak_bytes: [u8; 32] = [0; 32];
let mut nsk_bytes: [u8; 32] = [0; 32];

ak_bytes[0..32].copy_from_slice(&proof_generation_key_bytes[0..32]);
nsk_bytes[0..32].copy_from_slice(&proof_generation_key_bytes[32..64]);

let ak = match SubgroupPoint::from_bytes(&ak_bytes).into() {
Some(ak) => ak,
None => return Err(ProofGenerationKeyError::InvalidAuthorizingKey),
};

let nsk = match Fr::from_bytes(&nsk_bytes).into() {
Some(nsk) => nsk,
None => return Err(ProofGenerationKeyError::InvalidNullifierDerivingKey),
};

Ok(ProofGenerationKey(ZcashProofGenerationKey { ak, nsk }))
}
}

impl Deref for ProofGenerationKey {
type Target = ZcashProofGenerationKey;

fn deref(&self) -> &Self::Target {
&self.0
}
}

0 comments on commit 3e00045

Please sign in to comment.