Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dex: 🏒 SwapClaimProof::verify is fallible #3829

Merged
merged 4 commits into from
Feb 15, 2024
Merged
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
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ serde_json = {version = "1.0.96"}
serde_unit_struct = {version = "0.1"}
serde_with = {version = "3.5.1"}
sha2 = {version = "0.10"}
tap = "1.0.1"
tempfile = {version = "3.3.0"}
tendermint = {default-features = false, version = "0.34.0"}
tendermint-config = {version = "0.34.0"}
Expand Down
1 change: 1 addition & 0 deletions crates/core/component/dex/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ rand_core = {workspace = true}
regex = {workspace = true}
tokio = {workspace = true, features = ["full"], optional = true}
tonic = {workspace = true, optional = true}
tap = {workspace = true}

[dev-dependencies]
proptest = {workspace = true}
Expand Down
98 changes: 63 additions & 35 deletions crates/core/component/dex/src/swap_claim/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use penumbra_tct as tct;
use penumbra_tct::r1cs::StateCommitmentVar;

use penumbra_asset::{
asset::{self},
asset::{self, Id},
Value, ValueVar,
};
use penumbra_keys::keys::{Bip44Path, NullifierKey, NullifierKeyVar, SeedPhrase, SpendKey};
Expand All @@ -24,6 +24,8 @@ use penumbra_shielded_pool::{
note::{self, NoteVar},
Rseed,
};
use tap::Tap;
use tct::{Root, StateCommitment};

use crate::{
batch_swap_output_data::BatchSwapOutputDataVar,
Expand Down Expand Up @@ -349,6 +351,30 @@ impl DummyWitness for SwapClaimCircuit {
#[derive(Clone, Debug)]
pub struct SwapClaimProof(pub [u8; GROTH16_PROOF_LENGTH_BYTES]);

#[derive(Debug, thiserror::Error)]
pub enum VerificationError {
#[error("error deserializing compressed proof: {0:?}")]
ProofDeserialize(ark_serialize::SerializationError),
#[error("Fq types are Bls12-377 field members")]
Anchor,
#[error("nullifier is a Bls12-377 field member")]
Nullifier,
#[error("Fq types are Bls12-377 field members")]
ClaimFeeAmount,
#[error("asset_id is a Bls12-377 field member")]
ClaimFeeAssetId,
#[error("output_data is a Bls12-377 field member")]
OutputData,
#[error("note_commitment_1 is a Bls12-377 field member")]
NoteCommitment1,
#[error("note_commitment_2 is a Bls12-377 field member")]
NoteCommitment2,
#[error("error verifying proof: {0:?}")]
SynthesisError(ark_relations::r1cs::SynthesisError),
#[error("proof did not verify")]
InvalidProof,
}

impl SwapClaimProof {
#![allow(clippy::too_many_arguments)]
/// Generate an [`SwapClaimProof`] given the proving key, public inputs,
Expand Down Expand Up @@ -379,70 +405,72 @@ impl SwapClaimProof {
&self,
vk: &PreparedVerifyingKey<Bls12_377>,
public: SwapClaimProofPublic,
) -> anyhow::Result<()> {
let proof =
Proof::deserialize_compressed_unchecked(&self.0[..]).map_err(|e| anyhow::anyhow!(e))?;
) -> Result<(), VerificationError> {
let proof = Proof::deserialize_compressed_unchecked(&self.0[..])
.map_err(VerificationError::ProofDeserialize)?;

let mut public_inputs = Vec::new();

let SwapClaimProofPublic {
anchor: Root(anchor),
nullifier: Nullifier(nullifier),
claim_fee:
Fee(Value {
amount,
asset_id: Id(asset_id),
}),
output_data,
note_commitment_1: StateCommitment(note_commitment_1),
note_commitment_2: StateCommitment(note_commitment_2),
} = public;

public_inputs.extend(
Fq::from(public.anchor.0)
Fq::from(anchor)
.to_field_elements()
.expect("Fq types are Bls12-377 field members"),
.ok_or(VerificationError::Anchor)?,
);
public_inputs.extend(
public
.nullifier
.0
nullifier
.to_field_elements()
.expect("nullifier is a Bls12-377 field member"),
.ok_or(VerificationError::Nullifier)?,
);
public_inputs.extend(
Fq::from(public.claim_fee.0.amount)
Fq::from(amount)
.to_field_elements()
.expect("Fq types are Bls12-377 field members"),
.ok_or(VerificationError::ClaimFeeAmount)?,
);
public_inputs.extend(
public
.claim_fee
.0
.asset_id
.0
asset_id
.to_field_elements()
.expect("asset_id is a Bls12-377 field member"),
.ok_or(VerificationError::ClaimFeeAssetId)?,
);
public_inputs.extend(
public
.output_data
output_data
.to_field_elements()
.expect("output_data is a Bls12-377 field member"),
.ok_or(VerificationError::OutputData)?,
);
public_inputs.extend(
public
.note_commitment_1
.0
note_commitment_1
.to_field_elements()
.expect("note_commitment_1 is a Bls12-377 field member"),
.ok_or(VerificationError::NoteCommitment1)?,
);
public_inputs.extend(
public
.note_commitment_2
.0
note_commitment_2
.to_field_elements()
.expect("note_commitment_2 is a Bls12-377 field member"),
.ok_or(VerificationError::NoteCommitment2)?,
);

tracing::trace!(?public_inputs);
let start = std::time::Instant::now();
let proof_result = Groth16::<Bls12_377, LibsnarkReduction>::verify_with_processed_vk(
Groth16::<Bls12_377, LibsnarkReduction>::verify_with_processed_vk(
vk,
public_inputs.as_slice(),
&proof,
)
.map_err(|err| anyhow::anyhow!(err))?;
tracing::debug!(?proof_result, elapsed = ?start.elapsed());
proof_result
.then_some(())
.ok_or_else(|| anyhow::anyhow!("swapclaim proof did not verify"))
.map_err(VerificationError::SynthesisError)?
.tap(|proof_result| tracing::debug!(?proof_result, elapsed = ?start.elapsed()))
.then_some(())
.ok_or(VerificationError::InvalidProof)
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/test/mock-consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ license.workspace = true
anyhow = { workspace = true }
bytes = { workspace = true }
serde_json = { workspace = true }
tap = "1.0.1"
tap = { workspace = true }
tendermint = { workspace = true }
tower = { workspace = true, features = ["full"] }
tracing = { workspace = true }
Loading