Skip to content

Commit

Permalink
Update rust unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
panleone authored and Duddino Gatto committed Aug 11, 2023
1 parent 4c77aed commit aefa4d3
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 131 deletions.
9 changes: 3 additions & 6 deletions src/rust/src/rustzcash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use blake2s_simd::Params as Blake2sParams;
use bls12_381::{Bls12, Scalar};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use ff::{Field, PrimeField};
use group::GroupEncoding;
use jubjub::Fr;
use libc::{c_char, c_uchar, size_t};
use pairing::group::GroupEncoding;
use rand_core::OsRng;
use std::{
ffi::CStr,
Expand Down Expand Up @@ -337,11 +337,8 @@ fn test_gen_r() {
assert_ne!(r1, r2);

// Verify r values are valid in the field
let mut repr = FrRepr::default();
repr.read_le(&r1[..]).expect("length is not 32 bytes");
let _ = Fr::from_repr(repr).unwrap();
repr.read_le(&r2[..]).expect("length is not 32 bytes");
let _ = Fr::from_repr(repr).unwrap();
let _ = jubjub::Scalar::from_bytes(&r1).unwrap();
let _ = jubjub::Scalar::from_bytes(&r2).unwrap();
}

/// Return 32 byte random scalar, uniformly.
Expand Down
21 changes: 8 additions & 13 deletions src/rust/src/tests/key_agreement.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use ff::{PrimeField, PrimeFieldRepr};
use pairing::bls12_381::Bls12;
use group::Group;
use rand_core::{OsRng, RngCore};
use zcash_primitives::jubjub::{edwards, JubjubBls12};
use zcash_primitives::primitives::{Diversifier, ViewingKey};
use zcash_primitives::sapling::{Diversifier, NullifierDerivingKey, ViewingKey};

use crate::{
librustzcash_sapling_generate_r, librustzcash_sapling_ka_agree,
Expand All @@ -11,29 +9,27 @@ use crate::{

#[test]
fn test_key_agreement() {
let params = JubjubBls12::new();
let mut rng = OsRng;

// Create random viewing key
let vk = ViewingKey::<Bls12> {
ak: edwards::Point::rand(&mut rng, &params).mul_by_cofactor(&params),
nk: edwards::Point::rand(&mut rng, &params).mul_by_cofactor(&params),
let vk = ViewingKey {
ak: jubjub::SubgroupPoint::random(&mut rng),
nk: NullifierDerivingKey(jubjub::SubgroupPoint::random(&mut rng)),
};

// Create a random address with the viewing key
let addr = loop {
let mut d = [0; 11];
rng.fill_bytes(&mut d);
match vk.to_payment_address(Diversifier(d), &params) {
match vk.to_payment_address(Diversifier(d)) {
Some(a) => break a,
None => {}
}
};

// Grab ivk from our viewing key in serialized form
let ivk = vk.ivk();
let mut ivk_serialized = [0u8; 32];
ivk.into_repr().write_le(&mut ivk_serialized[..]).unwrap();
let ivk_serialized = ivk.to_repr();

// Create random esk
let mut esk = [0u8; 32];
Expand All @@ -45,8 +41,7 @@ fn test_key_agreement() {
let mut shared_secret_sender = [0u8; 32];

// Serialize pk_d for the call to librustzcash_sapling_ka_agree
let mut addr_pk_d = [0u8; 32];
addr.pk_d().write(&mut addr_pk_d[..]).unwrap();
let addr_pk_d = addr.to_bytes()[11..].try_into().unwrap();

assert!(librustzcash_sapling_ka_agree(
&addr_pk_d,
Expand Down
64 changes: 17 additions & 47 deletions src/rust/src/tests/key_components.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
use ff::{PrimeField, PrimeFieldRepr};
use pairing::bls12_381::Bls12;
use group::GroupEncoding;
use zcash_primitives::{
jubjub::{fs::FsRepr, FixedGenerators, JubjubEngine, JubjubParams},
primitives::{Diversifier, ProofGenerationKey},
constants::SPENDING_KEY_GENERATOR,
sapling::{Diversifier, Nullifier, ProofGenerationKey, Rseed},
};

use super::JUBJUB;

use crate::{
librustzcash_ask_to_ak, librustzcash_check_diversifier, librustzcash_crh_ivk,
librustzcash_ivk_to_pkd, librustzcash_nsk_to_nk,
Expand All @@ -30,7 +27,7 @@ fn key_components() {
note_cm: [u8; 32],
note_pos: u64,
note_nf: [u8; 32],
};
}

// From https://github.com/zcash-hackworks/zcash-test-vectors/blob/master/sapling_key_components.py
let test_vectors = vec![
Expand Down Expand Up @@ -657,44 +654,27 @@ fn key_components() {
];

for tv in test_vectors {
let mut ask_repr = FsRepr::default();
let mut nsk_repr = FsRepr::default();
ask_repr.read_le(&tv.ask[..]).unwrap();
nsk_repr.read_le(&tv.nsk[..]).unwrap();
let nsk = <Bls12 as JubjubEngine>::Fs::from_repr(nsk_repr).unwrap();
let ask = jubjub::Scalar::from_bytes(&tv.ask).unwrap();
let nsk = jubjub::Scalar::from_bytes(&tv.nsk).unwrap();

let ak = JUBJUB
.generator(FixedGenerators::SpendingKeyGenerator)
.mul(ask_repr.clone(), &JUBJUB);
{
let mut vec = Vec::new();
ak.write(&mut vec).unwrap();
assert_eq!(&vec, &tv.ak);
}
let ak = SPENDING_KEY_GENERATOR * ask;
assert_eq!(&ak.to_bytes(), &tv.ak);
{
let mut ak = [0u8; 32];
librustzcash_ask_to_ak(&tv.ask, &mut ak);
assert_eq!(&ak, &tv.ak);
}

let pgk = ProofGenerationKey { ak, nsk };
let fvk = pgk.to_viewing_key(&JUBJUB);
{
let mut vec = Vec::new();
fvk.nk.write(&mut vec).unwrap();
assert_eq!(&vec, &tv.nk);
}
let fvk = pgk.to_viewing_key();
assert_eq!(&fvk.nk.0.to_bytes(), &tv.nk);
{
let mut nk = [0u8; 32];
librustzcash_nsk_to_nk(&tv.nsk, &mut nk);
assert_eq!(&nk, &tv.nk);
}

{
let mut vec = Vec::new();
fvk.ivk().into_repr().write_le(&mut vec).unwrap();
assert_eq!(&vec, &tv.ivk);
}
assert_eq!(&fvk.ivk().to_repr(), &tv.ivk);
{
let mut ivk = [0u8; 32];
librustzcash_crh_ivk(&tv.ak, &tv.nk, &mut ivk);
Expand All @@ -704,28 +684,18 @@ fn key_components() {
let diversifier = Diversifier(tv.default_d);
assert!(librustzcash_check_diversifier(&tv.default_d));

let addr = fvk.to_payment_address(diversifier, &JUBJUB).unwrap();
{
let mut vec = Vec::new();
addr.pk_d().write(&mut vec).unwrap();
assert_eq!(&vec, &tv.default_pk_d);
}
let addr = fvk.to_payment_address(diversifier).unwrap();
assert_eq!(&addr.to_bytes()[11..], &tv.default_pk_d);
{
let mut default_pk_d = [0u8; 32];
librustzcash_ivk_to_pkd(&tv.ivk, &tv.default_d, &mut default_pk_d);
assert_eq!(&default_pk_d, &tv.default_pk_d);
}

let mut note_r_repr = FsRepr::default();
note_r_repr.read_le(&tv.note_r[..]).unwrap();
let note_r = <Bls12 as JubjubEngine>::Fs::from_repr(note_r_repr).unwrap();
let note = addr.create_note(tv.note_v, note_r, &JUBJUB).unwrap();
{
let mut vec = Vec::new();
note.cm(&JUBJUB).into_repr().write_le(&mut vec).unwrap();
assert_eq!(&vec, &tv.note_cm);
}
let note_r = jubjub::Scalar::from_bytes(&tv.note_r).unwrap();
let note = addr.create_note(tv.note_v, Rseed::BeforeZip212(note_r));
assert_eq!(&note.cmu().to_bytes(), &tv.note_cm);

assert_eq!(note.nf(&fvk, tv.note_pos, &JUBJUB), tv.note_nf);
assert_eq!(note.nf(&fvk.nk, tv.note_pos), Nullifier(tv.note_nf));
}
}
73 changes: 28 additions & 45 deletions src/rust/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use zcash_primitives::jubjub::{FixedGenerators, JubjubParams};

use super::JUBJUB;
use group::GroupEncoding;
use zcash_primitives::constants::{
NOTE_COMMITMENT_RANDOMNESS_GENERATOR, NULLIFIER_POSITION_GENERATOR,
PROOF_GENERATION_KEY_GENERATOR, SPENDING_KEY_GENERATOR, VALUE_COMMITMENT_RANDOMNESS_GENERATOR,
VALUE_COMMITMENT_VALUE_GENERATOR,
};

mod key_agreement;
mod key_components;
Expand All @@ -16,7 +19,7 @@ fn sapling_generators() {
wprb: [u8; 32],
vcvb: [u8; 32],
vcrb: [u8; 32],
};
}

// From https://github.com/zcash-hackworks/zcash-test-vectors/blob/master/sapling_generators.py
let sapling_generators = SaplingGenerators {
Expand Down Expand Up @@ -52,45 +55,25 @@ fn sapling_generators() {
],
};

{
let mut vec = Vec::new();
let p = JUBJUB.generator(FixedGenerators::SpendingKeyGenerator);
p.write(&mut vec).unwrap();
assert_eq!(&vec, &sapling_generators.skb);
}

{
let mut vec = Vec::new();
let p = JUBJUB.generator(FixedGenerators::ProofGenerationKey);
p.write(&mut vec).unwrap();
assert_eq!(&vec, &sapling_generators.pkb);
}

{
let mut vec = Vec::new();
let p = JUBJUB.generator(FixedGenerators::NullifierPosition);
p.write(&mut vec).unwrap();
assert_eq!(&vec, &sapling_generators.npb);
}

{
let mut vec = Vec::new();
let p = JUBJUB.generator(FixedGenerators::NoteCommitmentRandomness);
p.write(&mut vec).unwrap();
assert_eq!(&vec, &sapling_generators.wprb);
}

{
let mut vec = Vec::new();
let p = JUBJUB.generator(FixedGenerators::ValueCommitmentValue);
p.write(&mut vec).unwrap();
assert_eq!(&vec, &sapling_generators.vcvb);
}

{
let mut vec = Vec::new();
let p = JUBJUB.generator(FixedGenerators::ValueCommitmentRandomness);
p.write(&mut vec).unwrap();
assert_eq!(&vec, &sapling_generators.vcrb);
}
assert_eq!(&SPENDING_KEY_GENERATOR.to_bytes(), &sapling_generators.skb);
assert_eq!(
&PROOF_GENERATION_KEY_GENERATOR.to_bytes(),
&sapling_generators.pkb
);
assert_eq!(
&NULLIFIER_POSITION_GENERATOR.to_bytes(),
&sapling_generators.npb
);
assert_eq!(
&NOTE_COMMITMENT_RANDOMNESS_GENERATOR.to_bytes(),
&sapling_generators.wprb
);
assert_eq!(
&VALUE_COMMITMENT_VALUE_GENERATOR.to_bytes(),
&sapling_generators.vcvb
);
assert_eq!(
&VALUE_COMMITMENT_RANDOMNESS_GENERATOR.to_bytes(),
&sapling_generators.vcrb
);
}
2 changes: 1 addition & 1 deletion src/rust/src/tests/notes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn notes() {
note_cm: [u8; 32],
note_pos: u64,
note_nf: [u8; 32],
};
}

// From https://github.com/zcash-hackworks/zcash-test-vectors/blob/master/sapling_key_components.py
let test_vectors = vec![
Expand Down
34 changes: 15 additions & 19 deletions src/rust/src/tests/signatures.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use ff::{PrimeField, PrimeFieldRepr};
use pairing::bls12_381::Bls12;
use zcash_primitives::jubjub::{FixedGenerators, JubjubEngine};
use zcash_primitives::redjubjub::{PrivateKey, PublicKey, Signature};

use super::JUBJUB;
use zcash_primitives::{
constants::SPENDING_KEY_GENERATOR,
sapling::redjubjub::{PrivateKey, PublicKey, Signature},
};

#[test]
fn redjubjub_signatures() {
Expand All @@ -16,7 +14,7 @@ fn redjubjub_signatures() {
m: [u8; 32],
sig: [u8; 64],
rsig: [u8; 64],
};
}

// From https://github.com/zcash-hackworks/zcash-test-vectors/blob/master/sapling_signatures.py
let test_vectors = vec![
Expand Down Expand Up @@ -483,32 +481,30 @@ fn redjubjub_signatures() {
];

for tv in test_vectors {
let sk = PrivateKey::<Bls12>::read(&tv.sk[..]).unwrap();
let vk = PublicKey::<Bls12>::read(&tv.vk[..], &JUBJUB).unwrap();
let rvk = PublicKey::<Bls12>::read(&tv.rvk[..], &JUBJUB).unwrap();
let sk = PrivateKey::read(&tv.sk[..]).unwrap();
let vk = PublicKey::read(&tv.vk[..]).unwrap();
let rvk = PublicKey::read(&tv.rvk[..]).unwrap();
let sig = Signature::read(&tv.sig[..]).unwrap();
let rsig = Signature::read(&tv.rsig[..]).unwrap();

let mut alpha_repr = <<Bls12 as JubjubEngine>::Fs as PrimeField>::Repr::default();
alpha_repr.read_le(&tv.alpha[..]).unwrap();
let alpha = <Bls12 as JubjubEngine>::Fs::from_repr(alpha_repr).unwrap();
let alpha = jubjub::Scalar::from_bytes(&tv.alpha).unwrap();

{
let mut vec = Vec::new();
sk.randomize(alpha.clone()).write(&mut vec).unwrap();
sk.randomize(alpha).write(&mut vec).unwrap();
assert_eq!(&vec, &tv.rsk);
}
{
let mut vec = Vec::new();
vk.randomize(alpha, FixedGenerators::SpendingKeyGenerator, &JUBJUB)
vk.randomize(alpha, SPENDING_KEY_GENERATOR)
.write(&mut vec)
.unwrap();
assert_eq!(&vec, &tv.rvk);
}

assert!(vk.verify(&tv.m, &sig, FixedGenerators::SpendingKeyGenerator, &JUBJUB));
assert!(rvk.verify(&tv.m, &rsig, FixedGenerators::SpendingKeyGenerator, &JUBJUB));
assert!(!vk.verify(&tv.m, &rsig, FixedGenerators::SpendingKeyGenerator, &JUBJUB));
assert!(!rvk.verify(&tv.m, &sig, FixedGenerators::SpendingKeyGenerator, &JUBJUB));
assert!(vk.verify(&tv.m, &sig, SPENDING_KEY_GENERATOR));
assert!(rvk.verify(&tv.m, &rsig, SPENDING_KEY_GENERATOR));
assert!(!vk.verify(&tv.m, &rsig, SPENDING_KEY_GENERATOR));
assert!(!rvk.verify(&tv.m, &sig, SPENDING_KEY_GENERATOR));
}
}

0 comments on commit aefa4d3

Please sign in to comment.