Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce duplicate code across different curve cycle providers #255

Merged
merged 3 commits into from
Nov 20, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor: generalize curve test
ashWhiteHat committed Nov 17, 2023
commit 6f9990efe8f83ed2c391f194914951562af7bc3f
34 changes: 0 additions & 34 deletions src/provider/bn256_grumpkin.rs
Original file line number Diff line number Diff line change
@@ -69,37 +69,3 @@ impl_traits!(
"30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47",
"30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001"
);

#[cfg(test)]
mod tests {
use super::*;
type G = bn256::Point;

fn from_label_serial(label: &'static [u8], n: usize) -> Vec<Bn256Affine> {
let mut shake = Shake256::default();
shake.update(label);
let mut reader = shake.finalize_xof();
let mut ck = Vec::new();
for _ in 0..n {
let mut uniform_bytes = [0u8; 32];
reader.read_exact(&mut uniform_bytes).unwrap();
let hash = bn256::Point::hash_to_curve("from_uniform_bytes");
ck.push(hash(&uniform_bytes).to_affine());
}
ck
}

#[test]
fn test_from_label() {
let label = b"test_from_label";
for n in [
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 1021,
] {
let ck_par = <G as DlogGroup>::from_label(label, n);
let ck_ser = from_label_serial(label, n);
assert_eq!(ck_par.len(), n);
assert_eq!(ck_ser.len(), n);
assert_eq!(ck_par, ck_ser);
}
}
}
52 changes: 50 additions & 2 deletions src/provider/mod.rs
Original file line number Diff line number Diff line change
@@ -393,11 +393,44 @@ mod tests {
use crate::provider::{
bn256_grumpkin::{bn256, grumpkin},
secp_secq::{secp256k1, secq256k1},
DlogGroup,
};
use group::{ff::Field, Group};
use halo2curves::CurveAffine;
use digest::{ExtendableOutput, Update};
use group::{ff::Field, Curve, Group};
use halo2curves::{CurveAffine, CurveExt};
use pasta_curves::{pallas, vesta};
use rand_core::OsRng;
use sha3::Shake256;
use std::io::Read;

macro_rules! impl_cycle_pair_test {
($curve:ident) => {
fn from_label_serial(label: &'static [u8], n: usize) -> Vec<$curve::Affine> {
let mut shake = Shake256::default();
shake.update(label);
let mut reader = shake.finalize_xof();
(0..n)
.map(|_| {
let mut uniform_bytes = [0u8; 32];
reader.read_exact(&mut uniform_bytes).unwrap();
let hash = $curve::Point::hash_to_curve("from_uniform_bytes");
hash(&uniform_bytes).to_affine()
})
.collect()
}

let label = b"test_from_label";
for n in [
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 1021,
] {
let ck_par = <$curve::Point as DlogGroup>::from_label(label, n);
let ck_ser = from_label_serial(label, n);
assert_eq!(ck_par.len(), n);
assert_eq!(ck_ser.len(), n);
assert_eq!(ck_par, ck_ser);
}
};
}

fn test_msm_with<F: Field, A: CurveAffine<ScalarExt = F>>() {
let n = 8;
@@ -425,4 +458,19 @@ mod tests {
test_msm_with::<secp256k1::Scalar, secp256k1::Affine>();
test_msm_with::<secq256k1::Scalar, secq256k1::Affine>();
}

#[test]
fn test_bn256_from_label() {
impl_cycle_pair_test!(bn256);
}

#[test]
fn test_pallas_from_label() {
impl_cycle_pair_test!(pallas);
}

#[test]
fn test_secp256k1_from_label() {
impl_cycle_pair_test!(secp256k1);
}
}
34 changes: 0 additions & 34 deletions src/provider/pasta.rs
Original file line number Diff line number Diff line change
@@ -199,37 +199,3 @@ impl_traits!(
"40000000000000000000000000000000224698fc094cf91b992d30ed00000001",
"40000000000000000000000000000000224698fc0994a8dd8c46eb2100000001"
);

#[cfg(test)]
mod tests {
use super::*;
type G = <PallasEngine as Engine>::GE;

fn from_label_serial(label: &'static [u8], n: usize) -> Vec<EpAffine> {
let mut shake = Shake256::default();
shake.update(label);
let mut reader = shake.finalize_xof();
let mut ck = Vec::new();
for _ in 0..n {
let mut uniform_bytes = [0u8; 32];
reader.read_exact(&mut uniform_bytes).unwrap();
let hash = Ep::hash_to_curve("from_uniform_bytes");
ck.push(hash(&uniform_bytes).to_affine());
}
ck
}

#[test]
fn test_from_label() {
let label = b"test_from_label";
for n in [
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 1021,
] {
let ck_par = <G as DlogGroup>::from_label(label, n);
let ck_ser = from_label_serial(label, n);
assert_eq!(ck_par.len(), n);
assert_eq!(ck_ser.len(), n);
assert_eq!(ck_par, ck_ser);
}
}
}
34 changes: 0 additions & 34 deletions src/provider/secp_secq.rs
Original file line number Diff line number Diff line change
@@ -66,37 +66,3 @@ impl_traits!(
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f",
"fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"
);

#[cfg(test)]
mod tests {
use super::*;
type G = secp256k1::Point;

fn from_label_serial(label: &'static [u8], n: usize) -> Vec<Secp256k1Affine> {
let mut shake = Shake256::default();
shake.update(label);
let mut reader = shake.finalize_xof();
let mut ck = Vec::new();
for _ in 0..n {
let mut uniform_bytes = [0u8; 32];
reader.read_exact(&mut uniform_bytes).unwrap();
let hash = secp256k1::Point::hash_to_curve("from_uniform_bytes");
ck.push(hash(&uniform_bytes).to_affine());
}
ck
}

#[test]
fn test_from_label() {
let label = b"test_from_label";
for n in [
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 1021,
] {
let ck_par = <G as DlogGroup>::from_label(label, n);
let ck_ser = from_label_serial(label, n);
assert_eq!(ck_par.len(), n);
assert_eq!(ck_ser.len(), n);
assert_eq!(ck_par, ck_ser);
}
}
}