Skip to content

Commit

Permalink
Testing refactory (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
davxy authored Jul 27, 2024
1 parent 183e444 commit 352bca6
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 223 deletions.
6 changes: 2 additions & 4 deletions src/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,8 @@ pub fn scalar_decode<S: Suite>(buf: &[u8]) -> ScalarField<S> {

#[cfg(test)]
mod tests {
use crate::testing::{
suite::{Public, Secret},
TEST_SEED,
};
use crate::suites::testing::{Public, Secret};
use crate::testing::TEST_SEED;
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};

#[test]
Expand Down
61 changes: 23 additions & 38 deletions src/ietf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,29 @@ pub mod testing {
use super::*;
use crate::testing as common;

pub fn prove_verify<S: IetfSuite>() {
use ietf::{Prover, Verifier};

let secret = Secret::<S>::from_seed(common::TEST_SEED);
let public = secret.public();
let input = Input::from(common::random_val(None));
let output = secret.output(input);

let proof = secret.prove(input, output, b"foo");
let result = public.verify(input, output, b"foo", &proof);
assert!(result.is_ok());
}

#[macro_export]
macro_rules! ietf_suite_tests {
($suite:ident) => {
#[test]
fn ietf_prove_verify() {
$crate::ietf::testing::prove_verify::<$suite>();
}
};
}

pub struct TestVector<S: IetfSuite> {
pub base: common::TestVector<S>,
pub c: ScalarField<S>,
Expand Down Expand Up @@ -210,41 +233,3 @@ pub mod testing {
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::testing::{
random_val,
suite::{AffinePoint, Input, ScalarField, Secret, TestSuite},
TEST_SEED,
};

#[test]
fn prove_verify_works() {
let secret = Secret::from_seed(TEST_SEED);
let public = secret.public();
let input = Input::from(random_val::<AffinePoint>(None));
let output = secret.output(input);

let proof = secret.prove(input, output, b"foo");

let result = public.verify(input, output, b"foo", &proof);
assert!(result.is_ok());
}

#[test]
fn proof_encode_decode() {
let c = hex::decode("d091c00b0f5c3619d10ecea44363b5a5").unwrap();
let c = ScalarField::from_be_bytes_mod_order(&c[..]);
let s = hex::decode("99cadc5b2957e223fec62e81f7b4825fc799a771a3d7334b9186bdbee87316b1")
.unwrap();
let s = ScalarField::from_be_bytes_mod_order(&s[..]);

let proof = Proof::<TestSuite> { c, s };

let mut buf = Vec::new();
proof.serialize_compressed(&mut buf).unwrap();
assert_eq!(buf.len(), TestSuite::CHALLENGE_LEN + 32);
}
}
7 changes: 2 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,8 @@ impl<S: Suite> Output<S> {

#[cfg(test)]
mod tests {
use crate::testing::{
random_val,
suite::{Input, Secret},
TEST_SEED,
};
use crate::suites::testing::{Input, Secret};
use crate::testing::{random_val, TEST_SEED};

#[test]
fn vrf_output_check() {
Expand Down
43 changes: 15 additions & 28 deletions src/pedersen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,49 +142,36 @@ impl<S: PedersenSuite> Verifier<S> for Public<S> {
}

#[cfg(test)]
mod tests {
pub(crate) mod testing {
use super::*;
use crate::testing::{
random_val,
suite::{AffinePoint, BaseField, Input, Secret, TestSuite},
TEST_SEED,
};
use ark_ff::MontFp;
use crate::testing::{self as common, random_val, TEST_SEED};

impl PedersenSuite for TestSuite {
const BLINDING_BASE: AffinePoint = {
const X: BaseField = MontFp!(
"1181072390894490040170698195029164902368238760122173135634802939739986120753"
);
const Y: BaseField = MontFp!(
"16819438535150625131748701663066892288775529055803151482550035706857354997714"
);
AffinePoint::new_unchecked(X, Y)
};
}
pub fn prove_verify<S: PedersenSuite>() {
use pedersen::{Prover, Verifier};

#[test]
fn prove_verify_works() {
let secret = Secret::from_seed(TEST_SEED);
let secret = Secret::<S>::from_seed(TEST_SEED);
let input = Input::from(random_val(None));
let output = secret.output(input);

let (proof, blinding) = secret.prove(input, output, b"foo");

let result = Public::verify(input, output, b"foo", &proof);
assert!(result.is_ok());

assert_eq!(
proof.key_commitment(),
secret.public().0 + TestSuite::BLINDING_BASE * blinding
(secret.public().0 + S::BLINDING_BASE * blinding).into()
);
}
}

#[cfg(test)]
pub mod testing {
use super::*;
use crate::testing as common;
#[macro_export]
macro_rules! pedersen_suite_tests {
($suite:ident) => {
#[test]
fn pedersen_prove_verify() {
$crate::pedersen::testing::prove_verify::<$suite>();
}
};
}

pub struct TestVector<S: PedersenSuite> {
pub base: common::TestVector<S>,
Expand Down
66 changes: 66 additions & 0 deletions src/ring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,69 @@ where
self.pcs_params.check()
}
}

#[cfg(test)]
pub(crate) mod testing {
use super::*;
use crate::testing::*;

pub fn prove_verify<S: RingSuite>()
where
BaseField<S>: ark_ff::PrimeField,
CurveConfig<S>: ark_ec::short_weierstrass::SWCurveConfig + Clone,
AffinePoint<S>: utils::te_sw_map::SWMapping<CurveConfig<S>>,
{
let rng = &mut ark_std::test_rng();
let ring_ctx = RingContext::<S>::from_rand(512, rng);

let secret = Secret::<S>::from_seed(TEST_SEED);
let public = secret.public();
let input = Input::from(random_val(Some(rng)));
let output = secret.output(input);

let ring_size = ring_ctx.max_ring_size();

let prover_idx = 3;
let mut pks = random_vec::<AffinePoint<S>>(ring_size, Some(rng));
pks[prover_idx] = public.0;

let prover_key = ring_ctx.prover_key(&pks);
let prover = ring_ctx.prover(prover_key, prover_idx);
let proof = secret.prove(input, output, b"foo", &prover);

let verifier_key = ring_ctx.verifier_key(&pks);
let verifier = ring_ctx.verifier(verifier_key);
let result = Public::verify(input, output, b"foo", &proof, &verifier);
assert!(result.is_ok());
}

pub fn check_complement_point<S: RingSuite>()
where
BaseField<S>: ark_ff::PrimeField,
CurveConfig<S>: ark_ec::short_weierstrass::SWCurveConfig + Clone,
AffinePoint<S>: utils::te_sw_map::SWMapping<CurveConfig<S>>,
{
use utils::te_sw_map::SWMapping;
let pt = S::COMPLEMENT_POINT.into_sw();
assert!(pt.is_on_curve());
assert!(!pt.is_in_correct_subgroup_assuming_on_curve());
}

#[macro_export]
macro_rules! ring_suite_tests {
($suite:ident, true) => {
#[cfg(feature = "ring")]
#[test]
fn ring_prove_verify() {
$crate::ring::testing::prove_verify::<$suite>()
}

#[cfg(feature = "ring")]
#[test]
fn check_complement_point() {
$crate::ring::testing::check_complement_point::<$suite>()
}
};
($suite:ident, false) => {};
}
}
3 changes: 3 additions & 0 deletions src/suites/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[cfg(test)]
pub(crate) mod testing;

#[cfg(feature = "ed25519")]
pub mod ed25519;

Expand Down
35 changes: 35 additions & 0 deletions src/suites/testing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//! Suite for testing

use crate::testing as common;
use crate::{pedersen::PedersenSuite, *};
use ark_ff::MontFp;

#[derive(Debug, Copy, Clone, PartialEq)]
pub struct TestSuite;

impl Suite for TestSuite {
const SUITE_ID: &'static [u8] = b"ark-ec-vrfs-testing";
const CHALLENGE_LEN: usize = 16;

type Affine = ark_ed25519::EdwardsAffine;
type Hasher = sha2::Sha256;
type Codec = codec::ArkworksCodec;

fn nonce(_sk: &ScalarField, _pt: Input) -> ScalarField {
common::random_val(None)
}
}

impl PedersenSuite for TestSuite {
const BLINDING_BASE: AffinePoint = {
const X: BaseField =
MontFp!("1181072390894490040170698195029164902368238760122173135634802939739986120753");
const Y: BaseField = MontFp!(
"16819438535150625131748701663066892288775529055803151482550035706857354997714"
);
AffinePoint::new_unchecked(X, Y)
};
}

suite_types!(TestSuite);
suite_tests!(TestSuite);
Loading

0 comments on commit 352bca6

Please sign in to comment.