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

Proof-Setup: Implement linking between phases, test integration with the rest of the codebase #2982

Merged
merged 12 commits into from
Sep 8, 2023
Merged
8 changes: 8 additions & 0 deletions Cargo.lock

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

19 changes: 7 additions & 12 deletions crates/core/component/dex/src/batch_swap_output_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,12 +297,12 @@ impl TryFrom<BatchSwapOutputDataResponse> for BatchSwapOutputData {

#[cfg(test)]
mod tests {
use ark_groth16::{r1cs_to_qap::LibsnarkReduction, Groth16, ProvingKey, VerifyingKey};
use ark_groth16::{r1cs_to_qap::LibsnarkReduction, Groth16};
use ark_relations::r1cs::ConstraintSynthesizer;
use ark_snark::SNARK;
use decaf377::Bls12_377;
use penumbra_asset::asset;
use penumbra_proof_params::ParameterSetup;
use penumbra_proof_params::{generate_test_parameters, DummyWitness};
use rand_core::OsRng;

use super::*;
Expand Down Expand Up @@ -371,8 +371,8 @@ mod tests {
}
}

impl ParameterSetup for ProRataOutputCircuit {
fn generate_test_parameters() -> (ProvingKey<Bls12_377>, VerifyingKey<Bls12_377>) {
impl DummyWitness for ProRataOutputCircuit {
fn with_dummy_witness() -> Self {
let trading_pair = TradingPair {
asset_1: asset::Cache::with_known_assets()
.get_unit("upenumbra")
Expand All @@ -383,7 +383,7 @@ mod tests {
.unwrap()
.id(),
};
let circuit = ProRataOutputCircuit {
Self {
delta_1_i: Amount::from(1u32),
delta_2_i: Amount::from(1u32),
lambda_1_i: Amount::from(1u32),
Expand All @@ -399,12 +399,7 @@ mod tests {
trading_pair,
epoch_starting_height: 1,
},
};
let (pk, vk) = Groth16::<Bls12_377, LibsnarkReduction>::circuit_specific_setup(
circuit, &mut OsRng,
)
.expect("can perform circuit specific setup");
(pk, vk)
}
}
}

Expand Down Expand Up @@ -441,8 +436,8 @@ mod tests {
bsod,
};

let (pk, vk) = ProRataOutputCircuit::generate_test_parameters();
let mut rng = OsRng;
let (pk, vk) = generate_test_parameters::<ProRataOutputCircuit>(&mut rng);

let proof = Groth16::<Bls12_377, LibsnarkReduction>::prove(&pk, circuit, &mut rng)
.expect("should be able to form proof");
Expand Down
23 changes: 10 additions & 13 deletions crates/core/component/dex/src/swap/proof.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use ark_ff::ToConstraintField;
use ark_groth16::{
r1cs_to_qap::LibsnarkReduction, Groth16, PreparedVerifyingKey, Proof, ProvingKey, VerifyingKey,
r1cs_to_qap::LibsnarkReduction, Groth16, PreparedVerifyingKey, Proof, ProvingKey,
};
use ark_r1cs_std::prelude::*;
use ark_relations::r1cs::{ConstraintSynthesizer, ConstraintSystemRef};
Expand All @@ -14,7 +14,6 @@ use penumbra_fee::Fee;
use penumbra_proto::{core::crypto::v1alpha1 as pb, DomainType, TypeUrl};
use penumbra_tct as tct;
use penumbra_tct::r1cs::StateCommitmentVar;
use rand_core::OsRng;

use penumbra_asset::{
asset,
Expand All @@ -29,7 +28,7 @@ use crate::{
TradingPair,
};

use penumbra_proof_params::{ParameterSetup, GROTH16_PROOF_LENGTH_BYTES};
use penumbra_proof_params::{DummyWitness, GROTH16_PROOF_LENGTH_BYTES};

pub struct SwapCircuit {
/// The swap plaintext.
Expand Down Expand Up @@ -102,8 +101,8 @@ impl ConstraintSynthesizer<Fq> for SwapCircuit {
}
}

impl ParameterSetup for SwapCircuit {
fn generate_test_parameters() -> (ProvingKey<Bls12_377>, VerifyingKey<Bls12_377>) {
impl DummyWitness for SwapCircuit {
fn with_dummy_witness() -> Self {
let a = asset::Cache::with_known_assets()
.get_unit("upenumbra")
.unwrap();
Expand Down Expand Up @@ -134,17 +133,13 @@ impl ParameterSetup for SwapCircuit {
rseed: Rseed([1u8; 32]),
};

let circuit = SwapCircuit {
Self {
swap_plaintext: swap_plaintext.clone(),
fee_blinding: Fr::from(1),
swap_commitment: swap_plaintext.swap_commitment(),
fee_commitment: balance::Commitment(decaf377::basepoint()),
balance_commitment: balance::Commitment(decaf377::basepoint()),
};
let (pk, vk) =
Groth16::<Bls12_377, LibsnarkReduction>::circuit_specific_setup(circuit, &mut OsRng)
.expect("can perform circuit specific setup");
(pk, vk)
}
}
}

Expand Down Expand Up @@ -251,7 +246,9 @@ mod tests {
use penumbra_asset::{Balance, Value};
use penumbra_keys::keys::{SeedPhrase, SpendKey};
use penumbra_num::Amount;
use penumbra_proof_params::generate_prepared_test_parameters;
use proptest::prelude::*;
use rand_core::OsRng;

fn fr_strategy() -> BoxedStrategy<Fr> {
any::<[u8; 32]>()
Expand All @@ -263,9 +260,9 @@ mod tests {
#![proptest_config(ProptestConfig::with_cases(2))]
#[test]
fn swap_proof_happy_path(fee_blinding in fr_strategy(), value1_amount in 2..200u64) {
let (pk, vk) = SwapCircuit::generate_prepared_test_parameters();

let mut rng = OsRng;
let (pk, vk) = generate_prepared_test_parameters::<SwapCircuit>(&mut rng);


let seed_phrase = SeedPhrase::generate(&mut rng);
let sk_recipient = SpendKey::from_seed_phrase(seed_phrase, 0);
Expand Down
26 changes: 11 additions & 15 deletions crates/core/component/dex/src/swap_claim/proof.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use ark_ff::ToConstraintField;
use ark_groth16::{
r1cs_to_qap::LibsnarkReduction, Groth16, PreparedVerifyingKey, Proof, ProvingKey, VerifyingKey,
r1cs_to_qap::LibsnarkReduction, Groth16, PreparedVerifyingKey, Proof, ProvingKey,
};
use ark_r1cs_std::prelude::*;
use ark_relations::r1cs::{ConstraintSynthesizer, ConstraintSystemRef};
Expand All @@ -11,7 +11,6 @@ use penumbra_fee::Fee;
use penumbra_proto::{core::crypto::v1alpha1 as pb, DomainType, TypeUrl};
use penumbra_tct as tct;
use penumbra_tct::r1cs::StateCommitmentVar;
use rand_core::OsRng;

use penumbra_asset::{
asset::{self},
Expand All @@ -31,7 +30,7 @@ use crate::{
BatchSwapOutputData, TradingPair,
};

use penumbra_proof_params::{ParameterSetup, GROTH16_PROOF_LENGTH_BYTES};
use penumbra_proof_params::{DummyWitness, GROTH16_PROOF_LENGTH_BYTES};

/// SwapClaim consumes an existing Swap NFT so they are most similar to Spend operations,
/// however the note commitment proof needs to be for a specific block due to clearing prices
Expand Down Expand Up @@ -204,8 +203,8 @@ impl ConstraintSynthesizer<Fq> for SwapClaimCircuit {
}
}

impl ParameterSetup for SwapClaimCircuit {
fn generate_test_parameters() -> (ProvingKey<Bls12_377>, VerifyingKey<Bls12_377>) {
impl DummyWitness for SwapClaimCircuit {
fn with_dummy_witness() -> Self {
let trading_pair = TradingPair {
asset_1: asset::Cache::with_known_assets()
.get_unit("upenumbra")
Expand Down Expand Up @@ -264,7 +263,7 @@ impl ParameterSetup for SwapClaimCircuit {
let note_commitment_2 = tct::StateCommitment(Fq::from(2));
let (lambda_1, lambda_2) = output_data.pro_rata_outputs((delta_1_i, delta_2_i));

let circuit = SwapClaimCircuit {
Self {
swap_plaintext,
state_commitment_proof,
anchor,
Expand All @@ -278,11 +277,7 @@ impl ParameterSetup for SwapClaimCircuit {
note_blinding_2,
note_commitment_1,
note_commitment_2,
};
let (pk, vk) =
Groth16::<Bls12_377, LibsnarkReduction>::circuit_specific_setup(circuit, &mut OsRng)
.expect("can perform circuit specific setup");
(pk, vk)
}
}
}

Expand Down Expand Up @@ -406,15 +401,17 @@ mod tests {
use ark_ff::UniformRand;
use penumbra_keys::keys::{SeedPhrase, SpendKey};
use penumbra_num::Amount;
use penumbra_proof_params::generate_prepared_test_parameters;
use proptest::prelude::*;
use rand_core::OsRng;

proptest! {
#![proptest_config(ProptestConfig::with_cases(2))]
#[test]
fn swap_claim_proof_happy_path_filled(seed_phrase_randomness in any::<[u8; 32]>(), value1_amount in 2..200u64) {
let (pk, vk) = SwapClaimCircuit::generate_prepared_test_parameters();

let mut rng = OsRng;
let (pk, vk) = generate_prepared_test_parameters::<SwapClaimCircuit>(&mut rng);


let seed_phrase = SeedPhrase::from_randomness(&seed_phrase_randomness);
let sk_recipient = SpendKey::from_seed_phrase(seed_phrase, 0);
Expand Down Expand Up @@ -500,9 +497,8 @@ mod tests {

#[test]
fn swap_claim_proof_happy_path_unfilled() {
let (pk, vk) = SwapClaimCircuit::generate_prepared_test_parameters();

let mut rng = OsRng;
let (pk, vk) = generate_prepared_test_parameters::<SwapClaimCircuit>(&mut rng);

let seed_phrase = SeedPhrase::generate(rng);
let sk_recipient = SpendKey::from_seed_phrase(seed_phrase, 0);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Seeds for failure cases proptest has generated in the past. It is
# automatically read and these particular cases re-run before any
# novel cases are generated.
#
# It is recommended to check this file in to source control so that
# everyone who runs the test benefits from these saved cases.
cc 26bb258b706f59489cba08e6eb9724f26b3c062baf6fb3d97f662f4892bed416 # shrinks to seed_phrase_randomness = [13, 16, 140, 52, 135, 171, 74, 227, 99, 253, 177, 7, 160, 177, 69, 45, 9, 54, 15, 250, 71, 192, 136, 62, 225, 240, 158, 59, 43, 90, 232, 192], spend_auth_randomizer = BigInt([13101869489432526996, 5587207668677778290, 13142428231073505608, 232443455065964708]), value_amount = 1637377837, num_commitments = 1951
23 changes: 10 additions & 13 deletions crates/core/component/governance/src/delegator_vote/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,22 @@ use decaf377::FieldExt;
use decaf377::{r1cs::FqVar, Bls12_377, Fq, Fr};

use ark_ff::ToConstraintField;
use ark_groth16::{Groth16, PreparedVerifyingKey, Proof, ProvingKey, VerifyingKey};
use ark_groth16::{Groth16, PreparedVerifyingKey, Proof, ProvingKey};
use ark_r1cs_std::prelude::AllocVar;
use ark_relations::r1cs::{ConstraintSynthesizer, ConstraintSystemRef};
use ark_snark::SNARK;
use decaf377_rdsa::{SpendAuth, VerificationKey};
use penumbra_proto::{core::crypto::v1alpha1 as pb, DomainType, TypeUrl};
use penumbra_tct as tct;
use penumbra_tct::r1cs::StateCommitmentVar;
use rand_core::OsRng;
use tct::r1cs::PositionVar;

use penumbra_asset::{balance, balance::commitment::BalanceCommitmentVar, Value};
use penumbra_keys::keys::{
AuthorizationKeyVar, IncomingViewingKeyVar, NullifierKey, NullifierKeyVar,
RandomizedVerificationKey, SeedPhrase, SpendAuthRandomizerVar, SpendKey,
};
use penumbra_proof_params::{ParameterSetup, VerifyingKeyExt, GROTH16_PROOF_LENGTH_BYTES};
use penumbra_proof_params::{DummyWitness, VerifyingKeyExt, GROTH16_PROOF_LENGTH_BYTES};
use penumbra_sct::{Nullifier, NullifierVar};
use penumbra_shielded_pool::{note, Note, Rseed};

Expand Down Expand Up @@ -184,8 +183,8 @@ impl ConstraintSynthesizer<Fq> for DelegatorVoteCircuit {
}
}

impl ParameterSetup for DelegatorVoteCircuit {
fn generate_test_parameters() -> (ProvingKey<Bls12_377>, VerifyingKey<Bls12_377>) {
impl DummyWitness for DelegatorVoteCircuit {
fn with_dummy_witness() -> Self {
let seed_phrase = SeedPhrase::from_randomness(&[b'f'; 32]);
let sk_sender = SpendKey::from_seed_phrase(seed_phrase, 0);
let fvk_sender = sk_sender.full_viewing_key();
Expand All @@ -212,7 +211,7 @@ impl ParameterSetup for DelegatorVoteCircuit {
let state_commitment_proof = sct.witness(note_commitment).unwrap();
let start_position = state_commitment_proof.position();

let circuit = DelegatorVoteCircuit {
Self {
state_commitment_proof,
note,
v_blinding,
Expand All @@ -224,11 +223,7 @@ impl ParameterSetup for DelegatorVoteCircuit {
nullifier,
rk,
start_position,
};
let (pk, vk) =
Groth16::<Bls12_377, LibsnarkReduction>::circuit_specific_setup(circuit, &mut OsRng)
.expect("can perform circuit specific setup");
(pk, vk)
}
}
}

Expand Down Expand Up @@ -364,8 +359,10 @@ mod tests {
use decaf377::{Fq, Fr};
use penumbra_asset::{asset, Value};
use penumbra_keys::keys::{SeedPhrase, SpendKey};
use penumbra_proof_params::generate_prepared_test_parameters;
use penumbra_sct::Nullifier;
use proptest::prelude::*;
use rand_core::OsRng;

fn fr_strategy() -> BoxedStrategy<Fr> {
any::<[u8; 32]>()
Expand All @@ -377,8 +374,8 @@ mod tests {
#![proptest_config(ProptestConfig::with_cases(1))]
#[test]
fn delegator_vote_happy_path(seed_phrase_randomness in any::<[u8; 32]>(), spend_auth_randomizer in fr_strategy(), value_amount in 1..2000000000u64, num_commitments in 0..2000u64) {
let (pk, vk) = DelegatorVoteCircuit::generate_prepared_test_parameters();
let mut rng = OsRng;
let (pk, vk) = generate_prepared_test_parameters::<DelegatorVoteCircuit>(&mut rng);

let seed_phrase = SeedPhrase::from_randomness(&seed_phrase_randomness);
let sk_sender = SpendKey::from_seed_phrase(seed_phrase, 0);
Expand Down Expand Up @@ -448,8 +445,8 @@ mod tests {
#[test]
#[should_panic]
fn delegator_vote_invalid_start_position(seed_phrase_randomness in any::<[u8; 32]>(), spend_auth_randomizer in fr_strategy(), value_amount in 1..2000000000u64, num_commitments in 1000..2000u64) {
let (pk, vk) = DelegatorVoteCircuit::generate_prepared_test_parameters();
let mut rng = OsRng;
let (pk, vk) = generate_prepared_test_parameters::<DelegatorVoteCircuit>(&mut rng);

let seed_phrase = SeedPhrase::from_randomness(&seed_phrase_randomness);
let sk_sender = SpendKey::from_seed_phrase(seed_phrase, 0);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Seeds for failure cases proptest has generated in the past. It is
# automatically read and these particular cases re-run before any
# novel cases are generated.
#
# It is recommended to check this file in to source control so that
# everyone who runs the test benefits from these saved cases.
cc 012a287e1f34e2196d0936bf83eebf318ed7e32b5770ca09fbf3c5a42532a7d9 # shrinks to seed_phrase_randomness = [0, 147, 120, 164, 112, 216, 214, 148, 86, 42, 58, 15, 58, 68, 159, 66, 185, 30, 143, 239, 67, 91, 135, 64, 3, 229, 245, 127, 95, 253, 45, 222], incorrect_seed_phrase_randomness = [2, 213, 138, 24, 182, 233, 46, 0, 14, 87, 160, 137, 130, 75, 91, 220, 38, 17, 219, 177, 185, 25, 15, 48, 127, 232, 65, 170, 107, 241, 10, 238], spend_auth_randomizer = BigInt([11157411907499961512, 3695312772271047997, 1336443116240752099, 89537063999159638]), value_amount = 57, v_blinding = BigInt([4043089063310689772, 3831438451173389786, 16670315344678707725, 206829689043777334])
Loading