Skip to content

Commit

Permalink
zcash_primitives: Add MockSpendProver and MockOutputProver
Browse files Browse the repository at this point in the history
These are analogues of `MockTxProver` in that they implement the
corresponding Sapling prover traits.
  • Loading branch information
str4d committed Oct 6, 2023
1 parent 46903fa commit 04aa5a0
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 2 deletions.
2 changes: 2 additions & 0 deletions zcash_primitives/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ and this library adheres to Rust's notion of
- `circuit` module (moved from `zcash_proofs::circuit::sapling`).
- `constants` module.
- `prover::{SpendProver, OutputProver}`
- Test helpers, behind the `test-dependencies` feature flag:
- `zcash_primitives::prover::mock::{MockSpendProver, MockOutputProver}`

### Removed
- `zcash_primitives::constants`:
Expand Down
88 changes: 86 additions & 2 deletions zcash_primitives/src/sapling/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,20 +133,104 @@ pub trait TxProver {

#[cfg(any(test, feature = "test-dependencies"))]
pub mod mock {
use ff::Field;
use rand_core::OsRng;

use super::TxProver;
use super::{OutputProver, SpendProver, TxProver};
use crate::{
sapling::{
self,
circuit::ValueCommitmentOpening,
constants::SPENDING_KEY_GENERATOR,
redjubjub::{PublicKey, Signature},
value::{NoteValue, ValueCommitTrapdoor, ValueCommitment},
Diversifier, PaymentAddress, ProofGenerationKey, Rseed,
},
transaction::components::{Amount, GROTH_PROOF_SIZE},
transaction::components::{sapling::GrothProofBytes, Amount, GROTH_PROOF_SIZE},
};

pub struct MockSpendProver;

impl SpendProver for MockSpendProver {
type Proof = GrothProofBytes;

fn prepare_circuit(

Check warning on line 157 in zcash_primitives/src/sapling/prover.rs

View check run for this annotation

Codecov / codecov/patch

zcash_primitives/src/sapling/prover.rs#L157

Added line #L157 was not covered by tests
proof_generation_key: ProofGenerationKey,
diversifier: Diversifier,
_rseed: Rseed,
value: NoteValue,
alpha: jubjub::Fr,
rcv: ValueCommitTrapdoor,
anchor: bls12_381::Scalar,
_merkle_path: sapling::MerklePath,
) -> Option<sapling::circuit::Spend> {
let payment_address = proof_generation_key

Check warning on line 167 in zcash_primitives/src/sapling/prover.rs

View check run for this annotation

Codecov / codecov/patch

zcash_primitives/src/sapling/prover.rs#L167

Added line #L167 was not covered by tests
.to_viewing_key()
.ivk()
.to_payment_address(diversifier);
Some(sapling::circuit::Spend {
value_commitment_opening: Some(ValueCommitmentOpening {
value: value.inner(),
randomness: rcv.inner(),

Check warning on line 174 in zcash_primitives/src/sapling/prover.rs

View check run for this annotation

Codecov / codecov/patch

zcash_primitives/src/sapling/prover.rs#L170-L174

Added lines #L170 - L174 were not covered by tests
}),
proof_generation_key: Some(proof_generation_key),
payment_address,
commitment_randomness: Some(jubjub::Scalar::ZERO),
ar: Some(alpha),
auth_path: vec![],
anchor: Some(anchor),

Check warning on line 181 in zcash_primitives/src/sapling/prover.rs

View check run for this annotation

Codecov / codecov/patch

zcash_primitives/src/sapling/prover.rs#L176-L181

Added lines #L176 - L181 were not covered by tests
})
}

fn create_proof<R: rand_core::RngCore>(

Check warning on line 185 in zcash_primitives/src/sapling/prover.rs

View check run for this annotation

Codecov / codecov/patch

zcash_primitives/src/sapling/prover.rs#L185

Added line #L185 was not covered by tests
&self,
_circuit: sapling::circuit::Spend,
_rng: &mut R,
) -> Self::Proof {
[0u8; GROTH_PROOF_SIZE]

Check warning on line 190 in zcash_primitives/src/sapling/prover.rs

View check run for this annotation

Codecov / codecov/patch

zcash_primitives/src/sapling/prover.rs#L190

Added line #L190 was not covered by tests
}

fn encode_proof(proof: Self::Proof) -> GrothProofBytes {
proof

Check warning on line 194 in zcash_primitives/src/sapling/prover.rs

View check run for this annotation

Codecov / codecov/patch

zcash_primitives/src/sapling/prover.rs#L193-L194

Added lines #L193 - L194 were not covered by tests
}
}

pub struct MockOutputProver;

impl OutputProver for MockOutputProver {
type Proof = GrothProofBytes;

fn prepare_circuit(

Check warning on line 203 in zcash_primitives/src/sapling/prover.rs

View check run for this annotation

Codecov / codecov/patch

zcash_primitives/src/sapling/prover.rs#L203

Added line #L203 was not covered by tests
esk: jubjub::Fr,
payment_address: PaymentAddress,
rcm: jubjub::Fr,
value: NoteValue,
rcv: ValueCommitTrapdoor,
) -> sapling::circuit::Output {
sapling::circuit::Output {
value_commitment_opening: Some(ValueCommitmentOpening {

Check warning on line 211 in zcash_primitives/src/sapling/prover.rs

View check run for this annotation

Codecov / codecov/patch

zcash_primitives/src/sapling/prover.rs#L211

Added line #L211 was not covered by tests
value: value.inner(),
randomness: rcv.inner(),
}),
payment_address: Some(payment_address),
commitment_randomness: Some(rcm),
esk: Some(esk),

Check warning on line 217 in zcash_primitives/src/sapling/prover.rs

View check run for this annotation

Codecov / codecov/patch

zcash_primitives/src/sapling/prover.rs#L215-L217

Added lines #L215 - L217 were not covered by tests
}
}

fn create_proof<R: rand_core::RngCore>(

Check warning on line 221 in zcash_primitives/src/sapling/prover.rs

View check run for this annotation

Codecov / codecov/patch

zcash_primitives/src/sapling/prover.rs#L221

Added line #L221 was not covered by tests
&self,
_circuit: sapling::circuit::Output,
_rng: &mut R,
) -> Self::Proof {
[0u8; GROTH_PROOF_SIZE]

Check warning on line 226 in zcash_primitives/src/sapling/prover.rs

View check run for this annotation

Codecov / codecov/patch

zcash_primitives/src/sapling/prover.rs#L226

Added line #L226 was not covered by tests
}

fn encode_proof(proof: Self::Proof) -> GrothProofBytes {
proof

Check warning on line 230 in zcash_primitives/src/sapling/prover.rs

View check run for this annotation

Codecov / codecov/patch

zcash_primitives/src/sapling/prover.rs#L229-L230

Added lines #L229 - L230 were not covered by tests
}
}

pub struct MockTxProver;

impl TxProver for MockTxProver {
Expand Down

0 comments on commit 04aa5a0

Please sign in to comment.