zcash_primitives: Support ovk = ⊥ in note encryption
Closes zcash/librustzcash#259.
This commit is contained in:
parent
72817b0edc
commit
40e135783c
|
@ -257,7 +257,7 @@ mod tests {
|
||||||
rseed,
|
rseed,
|
||||||
};
|
};
|
||||||
let encryptor = SaplingNoteEncryption::new(
|
let encryptor = SaplingNoteEncryption::new(
|
||||||
extfvk.fvk.ovk,
|
Some(extfvk.fvk.ovk),
|
||||||
note.clone(),
|
note.clone(),
|
||||||
to.clone(),
|
to.clone(),
|
||||||
Memo::default(),
|
Memo::default(),
|
||||||
|
|
|
@ -133,7 +133,7 @@ mod tests {
|
||||||
rseed,
|
rseed,
|
||||||
};
|
};
|
||||||
let encryptor = SaplingNoteEncryption::new(
|
let encryptor = SaplingNoteEncryption::new(
|
||||||
extfvk.fvk.ovk,
|
Some(extfvk.fvk.ovk),
|
||||||
note.clone(),
|
note.clone(),
|
||||||
to.clone(),
|
to.clone(),
|
||||||
Memo::default(),
|
Memo::default(),
|
||||||
|
@ -193,7 +193,7 @@ mod tests {
|
||||||
rseed,
|
rseed,
|
||||||
};
|
};
|
||||||
let encryptor = SaplingNoteEncryption::new(
|
let encryptor = SaplingNoteEncryption::new(
|
||||||
extfvk.fvk.ovk,
|
Some(extfvk.fvk.ovk),
|
||||||
note.clone(),
|
note.clone(),
|
||||||
to,
|
to,
|
||||||
Memo::default(),
|
Memo::default(),
|
||||||
|
@ -221,7 +221,7 @@ mod tests {
|
||||||
rseed,
|
rseed,
|
||||||
};
|
};
|
||||||
let encryptor = SaplingNoteEncryption::new(
|
let encryptor = SaplingNoteEncryption::new(
|
||||||
extfvk.fvk.ovk,
|
Some(extfvk.fvk.ovk),
|
||||||
note.clone(),
|
note.clone(),
|
||||||
change_addr,
|
change_addr,
|
||||||
Memo::default(),
|
Memo::default(),
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
//! Functions for creating transactions.
|
//! Functions for creating transactions.
|
||||||
|
|
||||||
use ff::PrimeField;
|
use ff::PrimeField;
|
||||||
use rand_core::{OsRng, RngCore};
|
use rand_core::OsRng;
|
||||||
use rusqlite::{types::ToSql, Connection, NO_PARAMS};
|
use rusqlite::{types::ToSql, Connection, NO_PARAMS};
|
||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
@ -148,16 +148,9 @@ pub fn create_to_address<P: AsRef<Path>>(
|
||||||
|
|
||||||
// Apply the outgoing viewing key policy.
|
// Apply the outgoing viewing key policy.
|
||||||
let ovk = match ovk_policy {
|
let ovk = match ovk_policy {
|
||||||
OvkPolicy::Sender => extfvk.fvk.ovk,
|
OvkPolicy::Sender => Some(extfvk.fvk.ovk),
|
||||||
OvkPolicy::Custom(ovk) => ovk,
|
OvkPolicy::Custom(ovk) => Some(ovk),
|
||||||
OvkPolicy::Discard => {
|
OvkPolicy::Discard => None,
|
||||||
// Generate a random outgoing viewing key that the caller does not know.
|
|
||||||
// The probability of this colliding with a legitimate outgoing viewing
|
|
||||||
// key is negligible.
|
|
||||||
let mut ovk = [0; 32];
|
|
||||||
OsRng.fill_bytes(&mut ovk);
|
|
||||||
OutgoingViewingKey(ovk)
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Target the next block, assuming we are up-to-date.
|
// Target the next block, assuming we are up-to-date.
|
||||||
|
|
|
@ -221,7 +221,7 @@ pub fn prf_ock(
|
||||||
/// let diversifier = Diversifier([0; 11]);
|
/// let diversifier = Diversifier([0; 11]);
|
||||||
/// let pk_d = diversifier.g_d().unwrap();
|
/// let pk_d = diversifier.g_d().unwrap();
|
||||||
/// let to = PaymentAddress::from_parts(diversifier, pk_d).unwrap();
|
/// let to = PaymentAddress::from_parts(diversifier, pk_d).unwrap();
|
||||||
/// let ovk = OutgoingViewingKey([0; 32]);
|
/// let ovk = Some(OutgoingViewingKey([0; 32]));
|
||||||
///
|
///
|
||||||
/// let value = 1000;
|
/// let value = 1000;
|
||||||
/// let rcv = jubjub::Fr::random(&mut rng);
|
/// let rcv = jubjub::Fr::random(&mut rng);
|
||||||
|
@ -233,29 +233,34 @@ pub fn prf_ock(
|
||||||
/// let note = to.create_note(value, Rseed::BeforeZip212(rcm)).unwrap();
|
/// let note = to.create_note(value, Rseed::BeforeZip212(rcm)).unwrap();
|
||||||
/// let cmu = note.cmu();
|
/// let cmu = note.cmu();
|
||||||
///
|
///
|
||||||
/// let enc = SaplingNoteEncryption::new(ovk, note, to, Memo::default(), &mut rng);
|
/// let mut enc = SaplingNoteEncryption::new(ovk, note, to, Memo::default(), &mut rng);
|
||||||
/// let encCiphertext = enc.encrypt_note_plaintext();
|
/// let encCiphertext = enc.encrypt_note_plaintext();
|
||||||
/// let outCiphertext = enc.encrypt_outgoing_plaintext(&cv.commitment().into(), &cmu);
|
/// let outCiphertext = enc.encrypt_outgoing_plaintext(&cv.commitment().into(), &cmu);
|
||||||
/// ```
|
/// ```
|
||||||
pub struct SaplingNoteEncryption {
|
pub struct SaplingNoteEncryption<R: RngCore + CryptoRng> {
|
||||||
epk: jubjub::SubgroupPoint,
|
epk: jubjub::SubgroupPoint,
|
||||||
esk: jubjub::Fr,
|
esk: jubjub::Fr,
|
||||||
note: Note,
|
note: Note,
|
||||||
to: PaymentAddress,
|
to: PaymentAddress,
|
||||||
memo: Memo,
|
memo: Memo,
|
||||||
ovk: OutgoingViewingKey,
|
/// `None` represents the `ovk = ⊥` case.
|
||||||
|
ovk: Option<OutgoingViewingKey>,
|
||||||
|
rng: R,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SaplingNoteEncryption {
|
impl<R: RngCore + CryptoRng> SaplingNoteEncryption<R> {
|
||||||
/// Creates a new encryption context for the given note.
|
/// Creates a new encryption context for the given note.
|
||||||
pub fn new<R: RngCore + CryptoRng>(
|
///
|
||||||
ovk: OutgoingViewingKey,
|
/// Setting `ovk` to `None` represents the `ovk = ⊥` case, where the note cannot be
|
||||||
|
/// recovered by the sender.
|
||||||
|
pub fn new(
|
||||||
|
ovk: Option<OutgoingViewingKey>,
|
||||||
note: Note,
|
note: Note,
|
||||||
to: PaymentAddress,
|
to: PaymentAddress,
|
||||||
memo: Memo,
|
memo: Memo,
|
||||||
rng: &mut R,
|
mut rng: R,
|
||||||
) -> SaplingNoteEncryption {
|
) -> Self {
|
||||||
let esk = note.generate_or_derive_esk(rng);
|
let esk = note.generate_or_derive_esk(&mut rng);
|
||||||
let epk = note.g_d * esk;
|
let epk = note.g_d * esk;
|
||||||
|
|
||||||
SaplingNoteEncryption {
|
SaplingNoteEncryption {
|
||||||
|
@ -265,6 +270,7 @@ impl SaplingNoteEncryption {
|
||||||
to,
|
to,
|
||||||
memo,
|
memo,
|
||||||
ovk,
|
ovk,
|
||||||
|
rng,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -317,16 +323,29 @@ impl SaplingNoteEncryption {
|
||||||
|
|
||||||
/// Generates `outCiphertext` for this note.
|
/// Generates `outCiphertext` for this note.
|
||||||
pub fn encrypt_outgoing_plaintext(
|
pub fn encrypt_outgoing_plaintext(
|
||||||
&self,
|
&mut self,
|
||||||
cv: &jubjub::ExtendedPoint,
|
cv: &jubjub::ExtendedPoint,
|
||||||
cmu: &bls12_381::Scalar,
|
cmu: &bls12_381::Scalar,
|
||||||
) -> [u8; OUT_CIPHERTEXT_SIZE] {
|
) -> [u8; OUT_CIPHERTEXT_SIZE] {
|
||||||
let ock = prf_ock(&self.ovk, &cv, &cmu, &self.epk);
|
let (ock, input) = if let Some(ovk) = &self.ovk {
|
||||||
|
let ock = prf_ock(ovk, &cv, &cmu, &self.epk);
|
||||||
|
|
||||||
let mut input = [0u8; OUT_PLAINTEXT_SIZE];
|
let mut input = [0u8; OUT_PLAINTEXT_SIZE];
|
||||||
input[0..32].copy_from_slice(&self.note.pk_d.to_bytes());
|
input[0..32].copy_from_slice(&self.note.pk_d.to_bytes());
|
||||||
input[32..OUT_PLAINTEXT_SIZE].copy_from_slice(self.esk.to_repr().as_ref());
|
input[32..OUT_PLAINTEXT_SIZE].copy_from_slice(self.esk.to_repr().as_ref());
|
||||||
|
|
||||||
|
(ock, input)
|
||||||
|
} else {
|
||||||
|
// ovk = ⊥
|
||||||
|
let mut ock = OutgoingCipherKey([0; 32]);
|
||||||
|
let mut input = [0u8; OUT_PLAINTEXT_SIZE];
|
||||||
|
|
||||||
|
self.rng.fill_bytes(&mut ock.0);
|
||||||
|
self.rng.fill_bytes(&mut input);
|
||||||
|
|
||||||
|
(ock, input)
|
||||||
|
};
|
||||||
|
|
||||||
let mut output = [0u8; OUT_CIPHERTEXT_SIZE];
|
let mut output = [0u8; OUT_CIPHERTEXT_SIZE];
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
ChachaPolyIetf::aead_cipher()
|
ChachaPolyIetf::aead_cipher()
|
||||||
|
@ -847,22 +866,13 @@ mod tests {
|
||||||
let cmu = note.cmu();
|
let cmu = note.cmu();
|
||||||
|
|
||||||
let ovk = OutgoingViewingKey([0; 32]);
|
let ovk = OutgoingViewingKey([0; 32]);
|
||||||
let ne = SaplingNoteEncryption::new(ovk, note, pa, Memo([0; 512]), &mut rng);
|
let mut ne = SaplingNoteEncryption::new(Some(ovk), note, pa, Memo([0; 512]), &mut rng);
|
||||||
let epk = ne.epk();
|
let epk = ne.epk().clone();
|
||||||
let enc_ciphertext = ne.encrypt_note_plaintext();
|
let enc_ciphertext = ne.encrypt_note_plaintext();
|
||||||
let out_ciphertext = ne.encrypt_outgoing_plaintext(&cv, &cmu);
|
let out_ciphertext = ne.encrypt_outgoing_plaintext(&cv, &cmu);
|
||||||
let ock = prf_ock(&ovk, &cv, &cmu, &epk);
|
let ock = prf_ock(&ovk, &cv, &cmu, &epk);
|
||||||
|
|
||||||
(
|
(ovk, ock, ivk, cv, cmu, epk, enc_ciphertext, out_ciphertext)
|
||||||
ovk,
|
|
||||||
ock,
|
|
||||||
ivk,
|
|
||||||
cv,
|
|
||||||
cmu,
|
|
||||||
epk.clone(),
|
|
||||||
enc_ciphertext,
|
|
||||||
out_ciphertext,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reencrypt_enc_ciphertext(
|
fn reencrypt_enc_ciphertext(
|
||||||
|
@ -1845,7 +1855,7 @@ mod tests {
|
||||||
// Test encryption
|
// Test encryption
|
||||||
//
|
//
|
||||||
|
|
||||||
let mut ne = SaplingNoteEncryption::new(ovk, note, to, Memo(tv.memo), &mut OsRng);
|
let mut ne = SaplingNoteEncryption::new(Some(ovk), note, to, Memo(tv.memo), OsRng);
|
||||||
// Swap in the ephemeral keypair from the test vectors
|
// Swap in the ephemeral keypair from the test vectors
|
||||||
ne.esk = esk;
|
ne.esk = esk;
|
||||||
ne.epk = epk;
|
ne.epk = epk;
|
||||||
|
|
|
@ -76,7 +76,8 @@ struct SpendDescriptionInfo {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct SaplingOutput {
|
pub struct SaplingOutput {
|
||||||
ovk: OutgoingViewingKey,
|
/// `None` represents the `ovk = ⊥` case.
|
||||||
|
ovk: Option<OutgoingViewingKey>,
|
||||||
to: PaymentAddress,
|
to: PaymentAddress,
|
||||||
note: Note,
|
note: Note,
|
||||||
memo: Memo,
|
memo: Memo,
|
||||||
|
@ -86,7 +87,7 @@ impl SaplingOutput {
|
||||||
pub fn new<R: RngCore + CryptoRng, P: consensus::Parameters>(
|
pub fn new<R: RngCore + CryptoRng, P: consensus::Parameters>(
|
||||||
height: u32,
|
height: u32,
|
||||||
rng: &mut R,
|
rng: &mut R,
|
||||||
ovk: OutgoingViewingKey,
|
ovk: Option<OutgoingViewingKey>,
|
||||||
to: PaymentAddress,
|
to: PaymentAddress,
|
||||||
value: Amount,
|
value: Amount,
|
||||||
memo: Option<Memo>,
|
memo: Option<Memo>,
|
||||||
|
@ -122,7 +123,7 @@ impl SaplingOutput {
|
||||||
ctx: &mut P::SaplingProvingContext,
|
ctx: &mut P::SaplingProvingContext,
|
||||||
rng: &mut R,
|
rng: &mut R,
|
||||||
) -> OutputDescription {
|
) -> OutputDescription {
|
||||||
let encryptor = SaplingNoteEncryption::new(
|
let mut encryptor = SaplingNoteEncryption::new(
|
||||||
self.ovk,
|
self.ovk,
|
||||||
self.note.clone(),
|
self.note.clone(),
|
||||||
self.to.clone(),
|
self.to.clone(),
|
||||||
|
@ -396,7 +397,7 @@ impl<P: consensus::Parameters, R: RngCore + CryptoRng> Builder<P, R> {
|
||||||
/// Adds a Sapling address to send funds to.
|
/// Adds a Sapling address to send funds to.
|
||||||
pub fn add_sapling_output(
|
pub fn add_sapling_output(
|
||||||
&mut self,
|
&mut self,
|
||||||
ovk: OutgoingViewingKey,
|
ovk: Option<OutgoingViewingKey>,
|
||||||
to: PaymentAddress,
|
to: PaymentAddress,
|
||||||
value: Amount,
|
value: Amount,
|
||||||
memo: Option<Memo>,
|
memo: Option<Memo>,
|
||||||
|
@ -502,7 +503,7 @@ impl<P: consensus::Parameters, R: RngCore + CryptoRng> Builder<P, R> {
|
||||||
return Err(Error::NoChangeAddress);
|
return Err(Error::NoChangeAddress);
|
||||||
};
|
};
|
||||||
|
|
||||||
self.add_sapling_output(change_address.0, change_address.1, change, None)?;
|
self.add_sapling_output(Some(change_address.0), change_address.1, change, None)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
@ -728,7 +729,7 @@ mod tests {
|
||||||
|
|
||||||
let mut builder = Builder::<TestNetwork, OsRng>::new(0);
|
let mut builder = Builder::<TestNetwork, OsRng>::new(0);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
builder.add_sapling_output(ovk, to, Amount::from_i64(-1).unwrap(), None),
|
builder.add_sapling_output(Some(ovk), to, Amount::from_i64(-1).unwrap(), None),
|
||||||
Err(Error::InvalidAmount)
|
Err(Error::InvalidAmount)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -840,7 +841,7 @@ mod tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
let extfvk = ExtendedFullViewingKey::from(&extsk);
|
let extfvk = ExtendedFullViewingKey::from(&extsk);
|
||||||
let ovk = extfvk.fvk.ovk;
|
let ovk = Some(extfvk.fvk.ovk);
|
||||||
let to = extfvk.default_address().unwrap().1;
|
let to = extfvk.default_address().unwrap().1;
|
||||||
|
|
||||||
// Fail if there is only a Sapling output
|
// Fail if there is only a Sapling output
|
||||||
|
|
Loading…
Reference in New Issue