Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions contracts/privacy_pool/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use soroban_sdk::{contract, contractimpl, Address, BytesN, Env};
use crate::core::{admin, deposit, initialize, view, withdraw};
use crate::types::errors::Error;
use crate::types::state::{
AnalyticsSnapshot, Denomination, PerformanceMetricKind, PoolConfig, PoolId, Proof, PublicInputs,
VerifyingKey,
AnalyticsSnapshot, Denomination, PerformanceMetricKind, PoolConfig, PoolId, Proof,
PublicInputsWithSchema, VerifyingKey,
};

#[contract]
Expand Down Expand Up @@ -58,7 +58,7 @@ impl PrivacyPool {
env: Env,
pool_id: PoolId,
proof: Proof,
pub_inputs: PublicInputs,
pub_inputs: PublicInputsWithSchema,
) -> Result<bool, Error> {
withdraw::execute(env, pool_id, proof, pub_inputs)
}
Expand Down
29 changes: 21 additions & 8 deletions contracts/privacy_pool/src/core/withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,55 @@ use crate::crypto::verifier;
use crate::storage::{analytics, config, nullifier};
use crate::types::errors::Error;
use crate::types::events::emit_withdraw;
use crate::types::state::{PoolId, Proof, PublicInputs};
use crate::types::state::{PoolId, Proof, PublicInputsWithSchema, EXPECTED_WITHDRAW_SCHEMA_VERSION};
use crate::utils::{address_decoder, validation};

const EXPECTED_CIRCUIT_VERSION: &str = "1.0.0";
const EXPECTED_MANIFEST_ID: &[u8; 32] = &[0; 32];


/// Execute a withdrawal from a specific shielded pool using a ZK proof.
pub fn execute(
env: Env,
pool_id: PoolId,
proof: Proof,
pub_inputs: PublicInputs,
pub_inputs_with_schema: PublicInputsWithSchema,
) -> Result<bool, Error> {
// Load and validate pool configuration
let pool_config = config::load_pool_config(&env, &pool_id)?;
validation::require_not_paused(&pool_config)?;

let denomination_amount = pool_config.denomination.amount();
let pub_inputs = pub_inputs_with_schema.inputs;

// Step 1: Validate schema version
verifier::validate_schema_version(
&pub_inputs_with_schema.schema_version,
EXPECTED_WITHDRAW_SCHEMA_VERSION,
)?;

// Step 1: Validate root is in pool history
// Step 2: Validate root is in pool history
validation::require_known_root(&env, &pool_id, &pub_inputs.root)?;

// Step 2: Check nullifier not already spent in this pool
// Step 3: Check nullifier not already spent in this pool
validation::require_nullifier_unspent(&env, &pool_id, &pub_inputs.nullifier_hash)?;

// Step 2.5: Validate pool-id and denomination binding
// Step 3.5: Validate pool-id and denomination binding
if pub_inputs.pool_id != pool_id.0 {
return Err(Error::InvalidPoolId);
}
if pub_inputs.denomination != pool_config.denomination.encode_as_field(&env) {
return Err(Error::InvalidDenomination);
}

// Step 3: Validate and decode fee
// Step 4: Validate and decode fee
let fee = validation::decode_and_validate_fee(&pub_inputs.fee, denomination_amount)?;

// Step 4: Verify Groth16 proof for this pool
// Step 5: Verify Groth16 proof for this pool
let vk = config::load_verifying_key(&env, &pool_id)?;
let proof_valid = verifier::verify_proof(&env, &vk, &proof, &pub_inputs)?;
let manifest_id_bytes = BytesN::from_array(&env, EXPECTED_MANIFEST_ID);
let proof_valid =
verifier::verify_proof(&env, &vk, &proof, &pub_inputs, EXPECTED_CIRCUIT_VERSION, &manifest_id_bytes)?;
if !proof_valid {
return Err(Error::InvalidProof);
}
Expand Down
34 changes: 34 additions & 0 deletions contracts/privacy_pool/src/crypto/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ pub fn verify_proof(
vk: &VerifyingKey,
proof: &Proof,
pub_inputs: &PublicInputs,
expected_circuit_version: &str,
expected_manifest_id: &BytesN<32>,
) -> Result<bool, Error> {
// Step 0: Validate VK identity
validate_vk_identity(vk, expected_circuit_version, expected_manifest_id)?;
let bn254 = env.crypto().bn254();

// Step 1: Compute vk_x (linear combination of public inputs)
Expand Down Expand Up @@ -175,3 +179,33 @@ pub fn validate_schema_version(

Ok(())
}
// ──────────────────────────────────────────────────────────────
// Verifying Key Identity Validation
// ──────────────────────────────────────────────────────────────

/// Validates the circuit version and manifest ID of the verifying key.
///
/// # Arguments
/// * `vk` - The verifying key to validate
/// * `expected_circuit_version` - The expected circuit version string
/// * `expected_manifest_id` - The expected manifest ID
///
/// # Returns
/// * `Ok(())` if the identity matches
/// * `Err(Error::CircuitVersionMismatch)` if the circuit version does not match
/// * `Err(Error::ManifestIdMismatch)` if the manifest ID does not match
pub fn validate_vk_identity(
vk: &VerifyingKey,
expected_circuit_version: &str,
expected_manifest_id: &BytesN<32>,
) -> Result<(), Error> {
let vk_circuit_version = vk.circuit_version.to_string();
if vk_circuit_version != expected_circuit_version {
return Err(Error::CircuitVersionMismatch);
}
if vk.manifest_id != *expected_manifest_id {
return Err(Error::ManifestIdMismatch);
}
Ok(())
}

4 changes: 4 additions & 0 deletions contracts/privacy_pool/src/types/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,8 @@ pub enum Error {
InvalidSchemaVersion = 80,
/// Proof schema version does not match expected version
SchemaVersionMismatch = 81,
/// Circuit version in the VK does not match the expected version
CircuitVersionMismatch = 82,
/// Manifest ID in the VK does not match the expected version
ManifestIdMismatch = 83,
}
4 changes: 4 additions & 0 deletions contracts/privacy_pool/src/types/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ pub struct VerifyingKey {
/// G1 points for public input combination: [IC_0, IC_1, ..., IC_8]
/// One per public input (pool_id, root, nullifier_hash, recipient, amount, relayer, fee, denomination) + IC_0
pub gamma_abc_g1: soroban_sdk::Vec<BytesN<64>>,
/// Circuit version identifier (e.g., "1.0.0")
pub circuit_version: String,
/// Manifest identifier (sha256 of the manifest)
pub manifest_id: BytesN<32>,
}

// ──────────────────────────────────────────────────────────────
Expand Down
2 changes: 2 additions & 0 deletions sdk/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export interface ZkArtifactManifestCircuit {
checksum?: string; // Legacy field for compatibility
name: string;
backend: string;
circuit_version?: string;
root_depth?: number;
public_input_schema?: string[];
}
Expand All @@ -53,6 +54,7 @@ export interface ZkArtifactManifestBackend {
}

export interface ZkArtifactManifest {
manifest_id?: string;
version: number | string;
backend: string | ZkArtifactManifestBackend;
circuits: Record<string, ZkArtifactManifestCircuit>;
Expand Down