Skip to content

Commit

Permalink
Testing macros
Browse files Browse the repository at this point in the history
  • Loading branch information
davxy committed May 30, 2024
1 parent af0ce78 commit 22c7bf6
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 85 deletions.
93 changes: 32 additions & 61 deletions src/suites/bandersnatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ pub mod weierstrass {

suite_types!(BandersnatchSha512);

#[cfg(test)]
suite_tests!(BandersnatchSha512, true);

impl Suite for BandersnatchSha512 {
const SUITE_ID: u8 = CUSTOM_SUITE_ID_FLAG | 0x03;
const CHALLENGE_LEN: usize = 32;
Expand Down Expand Up @@ -111,56 +114,6 @@ pub mod weierstrass {
pub type Verifier = ring::Verifier<BandersnatchSha512>;
pub type Proof = ring::Proof<BandersnatchSha512>;
}

// sage: q = 52435875175126190479447740508185965837690552500527637822603658699938581184513
// sage: Fq = GF(q)
// sage: MONT_A = 29978822694968839326280996386011761570173833766074948509196803838190355340952
// sage: MONT_B = 25465760566081946422412445027709227188579564747101592991722834452325077642517
// sage: MONT_A/Fq(3) = 9992940898322946442093665462003920523391277922024982836398934612730118446984
// sage: Fq(1)/MONT_B = 41180284393978236561320365279764246793818536543197771097409483252169927600582
impl MapConfig for ark_ed_on_bls12_381_bandersnatch::BandersnatchConfig {
const MONT_A_OVER_THREE: ark_ed_on_bls12_381_bandersnatch::Fq =
MontFp!("9992940898322946442093665462003920523391277922024982836398934612730118446984");
const MONT_B_INV: ark_ed_on_bls12_381_bandersnatch::Fq = MontFp!(
"41180284393978236561320365279764246793818536543197771097409483252169927600582"
);
}

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

// TODO: use macro to build all tests
#[test]
fn ietf_prove_verify() {
testing::ietf_prove_verify::<BandersnatchSha512>();
}

#[test]
fn prove_verify_pedersen() {
testing::pedersen_prove_verify::<BandersnatchSha512>();
}

#[cfg(feature = "ring")]
#[test]
fn ring_prove_verify() {
testing::ring_prove_verify::<BandersnatchSha512>()
}

#[test]
fn sw_to_te_roundtrip() {
use crate::{testing, utils::ark_next};
use ark_ed_on_bls12_381_bandersnatch::{BandersnatchConfig, SWAffine};

let org_point = testing::random_val::<SWAffine>(None);

let te_point = ark_next::map_sw_to_te::<BandersnatchConfig>(&org_point).unwrap();
assert!(te_point.is_on_curve());

let sw_point = ark_next::map_te_to_sw::<BandersnatchConfig>(&te_point).unwrap();
assert!(sw_point.is_on_curve());
}
}
}

pub mod edwards {
Expand All @@ -171,6 +124,9 @@ pub mod edwards {

suite_types!(BandersnatchSha512Edwards);

#[cfg(test)]
suite_tests!(BandersnatchSha512Edwards);

impl Suite for BandersnatchSha512Edwards {
const SUITE_ID: u8 = CUSTOM_SUITE_ID_FLAG | 0x04;
const CHALLENGE_LEN: usize = 32;
Expand All @@ -190,19 +146,34 @@ pub mod edwards {
AffinePoint::new_unchecked(X, Y)
};
}
}

#[cfg(test)]
mod test {
use super::*;
// sage: q = 52435875175126190479447740508185965837690552500527637822603658699938581184513
// sage: Fq = GF(q)
// sage: MONT_A = 29978822694968839326280996386011761570173833766074948509196803838190355340952
// sage: MONT_B = 25465760566081946422412445027709227188579564747101592991722834452325077642517
// sage: MONT_A/Fq(3) = 9992940898322946442093665462003920523391277922024982836398934612730118446984
// sage: Fq(1)/MONT_B = 41180284393978236561320365279764246793818536543197771097409483252169927600582
impl MapConfig for ark_ed_on_bls12_381_bandersnatch::BandersnatchConfig {
const MONT_A_OVER_THREE: ark_ed_on_bls12_381_bandersnatch::Fq =
MontFp!("9992940898322946442093665462003920523391277922024982836398934612730118446984");
const MONT_B_INV: ark_ed_on_bls12_381_bandersnatch::Fq =
MontFp!("41180284393978236561320365279764246793818536543197771097409483252169927600582");
}

#[test]
fn ietf_prove_verify() {
testing::ietf_prove_verify::<BandersnatchSha512Edwards>();
}
#[cfg(test)]
mod tests {
use crate::{testing, utils::ark_next};
use ark_ed_on_bls12_381_bandersnatch::{BandersnatchConfig, SWAffine};

#[test]
fn prove_verify_pedersen() {
testing::pedersen_prove_verify::<BandersnatchSha512Edwards>();
}
#[test]
fn sw_to_te_roundtrip() {
let org_point = testing::random_val::<SWAffine>(None);

let te_point = ark_next::map_sw_to_te::<BandersnatchConfig>(&org_point).unwrap();
assert!(te_point.is_on_curve());

let sw_point = ark_next::map_te_to_sw::<BandersnatchConfig>(&te_point).unwrap();
assert!(sw_point.is_on_curve());
}
}
17 changes: 16 additions & 1 deletion src/suites/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,32 @@
//! Section 5.4.1.1, with interpret_hash_value_as_a_point(s) =
//! string_to_point(s[0]...s[31]).

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

#[derive(Copy, Clone)]
pub struct Ed25519Sha512;

suite_types!(Ed25519Sha512);

#[cfg(test)]
suite_tests!(Ed25519Sha512);

impl Suite for Ed25519Sha512 {
const SUITE_ID: u8 = CUSTOM_SUITE_ID_FLAG | 0x03;
const CHALLENGE_LEN: usize = 16;

type Affine = ark_ed25519::EdwardsAffine;
type Hasher = sha2::Sha512;
}

impl PedersenSuite for Ed25519Sha512 {
const BLINDING_BASE: AffinePoint = {
const X: BaseField =
MontFp!("1181072390894490040170698195029164902368238760122173135634802939739986120753");
const Y: BaseField = MontFp!(
"16819438535150625131748701663066892288775529055803151482550035706857354997714"
);
AffinePoint::new_unchecked(X, Y)
};
}
26 changes: 21 additions & 5 deletions src/suites/secp256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,17 @@
//! Section 5.4.1.1, with interpret_hash_value_as_a_point(s) =
//! string_to_point(0x02 || s).

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

#[derive(Copy, Clone)]
pub struct P256Sha256Tai;

suite_types!(P256Sha256Tai);

#[cfg(test)]
suite_tests!(P256Sha256Tai);

impl Suite for P256Sha256Tai {
const SUITE_ID: u8 = 0x01;
const CHALLENGE_LEN: usize = 16;
Expand Down Expand Up @@ -100,13 +104,25 @@ impl Suite for P256Sha256Tai {
}
}

impl PedersenSuite for P256Sha256Tai {
const BLINDING_BASE: AffinePoint = {
const X: BaseField = MontFp!(
"14043613715035732602742871684475452461130505690937359323850445130419175222977"
);
const Y: BaseField = MontFp!(
"56943419272466863994763824717057516408187649339843987947344693936486947084336"
);
AffinePoint::new_unchecked(X, Y)
};
}

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

#[test]
fn secp256_rfc_9381_test_vector_10() {
fn rfc_9381_10() {
let v = TestVector {
flags: TEST_FLAG_SKIP_PROOF_CHECK,
sk: "c9afa9d845ba75166b5c215767b1d6934e50c3db36e89b127b8a622b120f6721",
Expand All @@ -126,7 +142,7 @@ mod tests {
}

#[test]
fn secp256_rfc_9381_test_vector_11() {
fn rfc_9381_11() {
let v = TestVector {
flags: 0,
sk: "c9afa9d845ba75166b5c215767b1d6934e50c3db36e89b127b8a622b120f6721",
Expand All @@ -143,7 +159,7 @@ mod tests {
}

#[test]
fn secp256_rfc_9381_test_vector_12() {
fn rfc_9381_12() {
let v = TestVector {
flags: 0,
sk: "2ca1411a41b17b24cc8c3b089cfd033f1920202a6c0de8abb97df1498d50d2c8",
Expand Down
50 changes: 32 additions & 18 deletions src/testing.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(unused)]
#[cfg(not(feature = "std"))]
use ark_std::{vec, vec::Vec};

Expand All @@ -23,14 +24,12 @@ pub(crate) mod suite {
suite_types!(TestSuite);
}

#[allow(unused)]
pub fn random_vec<T: UniformRand>(n: usize, rng: Option<&mut dyn RngCore>) -> Vec<T> {
let mut local_rng = ark_std::test_rng();
let rng = rng.unwrap_or(&mut local_rng);
(0..n).map(|_| T::rand(rng)).collect()
}

#[allow(unused)]
pub fn random_val<T: UniformRand>(rng: Option<&mut dyn RngCore>) -> T {
let mut local_rng = ark_std::test_rng();
let rng = rng.unwrap_or(&mut local_rng);
Expand All @@ -45,15 +44,7 @@ pub fn ietf_prove_verify<S: crate::ietf::IetfSuite>() {
let input = Input::from(random_val(None));
let output = secret.output(input);

let mut buf = Vec::new();
public.serialize_compressed(&mut buf).unwrap();
println!("{}", buf.len());

let proof = secret.prove(input, output, b"foo");
let mut buf = Vec::new();
proof.serialize_compressed(&mut buf).unwrap();
println!("{}", buf.len());

let result = public.verify(input, output, b"foo", &proof);
assert!(result.is_ok());
}
Expand All @@ -66,14 +57,6 @@ pub fn pedersen_prove_verify<S: crate::pedersen::PedersenSuite>() {
let output = secret.output(input);

let (proof, blinding) = secret.prove(input, output, b"foo");
let mut buf = Vec::new();
secret.public().serialize_compressed(&mut buf).unwrap();
println!("{}", buf.len());

let mut buf = Vec::new();
proof.serialize_compressed(&mut buf).unwrap();
println!("{}", buf.len());

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

Expand Down Expand Up @@ -118,3 +101,34 @@ where
let result = Public::verify(input, output, b"foo", &proof, &verifier);
assert!(result.is_ok());
}

#[macro_export]
macro_rules! suite_tests {
($suite:ident, $build_ring:ident) => {
suite_tests!($suite);
ring_suite_tests!($build_ring);
};
($suite:ident) => {
#[test]
fn ietf_prove_verify() {
$crate::testing::ietf_prove_verify::<$suite>();
}

#[test]
fn pedersen_prove_verify() {
$crate::testing::pedersen_prove_verify::<$suite>();
}
};
}

#[macro_export]
macro_rules! ring_suite_tests {
(true) => {
#[cfg(feature = "ring")]
#[test]
fn ring_prove_verify() {
$crate::testing::ring_prove_verify::<BandersnatchSha512>()
}
};
(false) => {};
}

0 comments on commit 22c7bf6

Please sign in to comment.