penumbra_sdk_keys/keys/
nullifier.rs

1use decaf377::Fq;
2
3use ark_r1cs_std::prelude::*;
4use ark_relations::r1cs::SynthesisError;
5use decaf377::r1cs::FqVar;
6
7pub const NK_LEN_BYTES: usize = 32;
8
9/// Allows deriving the nullifier associated with a positioned piece of state.
10#[derive(Clone, Copy, Debug, Eq, PartialEq)]
11pub struct NullifierKey(pub Fq);
12
13/// Represents the `NullifierKey` as a variable in an R1CS constraint system.
14pub struct NullifierKeyVar {
15    pub inner: FqVar,
16}
17
18impl AllocVar<NullifierKey, Fq> for NullifierKeyVar {
19    fn new_variable<T: std::borrow::Borrow<NullifierKey>>(
20        cs: impl Into<ark_relations::r1cs::Namespace<Fq>>,
21        f: impl FnOnce() -> Result<T, SynthesisError>,
22        mode: ark_r1cs_std::prelude::AllocationMode,
23    ) -> Result<Self, SynthesisError> {
24        let ns = cs.into();
25        let cs = ns.cs();
26        let inner: NullifierKey = *f()?.borrow();
27        match mode {
28            AllocationMode::Constant => unimplemented!(),
29            AllocationMode::Input => unimplemented!(),
30            AllocationMode::Witness => Ok(Self {
31                inner: FqVar::new_witness(cs, || Ok(inner.0))?,
32            }),
33        }
34    }
35}
36
37impl R1CSVar<Fq> for NullifierKeyVar {
38    type Value = NullifierKey;
39
40    fn cs(&self) -> ark_relations::r1cs::ConstraintSystemRef<Fq> {
41        self.inner.cs()
42    }
43
44    fn value(&self) -> Result<Self::Value, SynthesisError> {
45        let inner_fq = self.inner.value()?;
46        Ok(NullifierKey(inner_fq))
47    }
48}