Skip to content

Commit

Permalink
refactor: use generator() instead of random point in the curve
Browse files Browse the repository at this point in the history
Signed-off-by: pedro bufulin <[email protected]>
  • Loading branch information
pedrohba1 committed Dec 18, 2024
1 parent 0dccaf6 commit 6b9f4e5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
2 changes: 1 addition & 1 deletion h2s2/src/holographic_homomorphic_signature_scheme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub trait HolographicHomomorphicSignatureScheme<P: Pairing, D: Digest + Send + S
type AggregatedSignature;

/// Generate one G2 element and `n` G1 elements
fn setup<R: Rng>(rng: &mut R, n: usize) -> Result<Self::Parameters, Box<dyn Error>>;
fn setup(n: usize) -> Result<Self::Parameters, Box<dyn Error>>;

/// Generate hash aggregate (H_a) with `tag` and `n` lanes, and a
/// allocation_id as a ScalarField
Expand Down
33 changes: 15 additions & 18 deletions h2s2/src/ncs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::{error::Error, marker::PhantomData};
use crate::holographic_homomorphic_signature_scheme::HolographicHomomorphicSignatureScheme;
use ark_ec::pairing::Pairing;
use ark_ec::AffineRepr;
use ark_ec::PrimeGroup;
use ark_ff::PrimeField;
use ark_ff::{BigInteger, UniformRand, Zero};
use ark_std::rand::Rng;
Expand Down Expand Up @@ -69,27 +70,25 @@ impl<P: Pairing, D: Digest + Send + Sync> HolographicHomomorphicSignatureScheme<
type AggregatedSignature = AggregatedSignature<P>;

// n represents the max_lanes amount
fn setup<R: Rng>(rng: &mut R, n: usize) -> Result<Self::Parameters, Box<dyn Error>> {
// Generate the G2 generator
let g2_generator = P::G2::rand(rng);
fn setup(n: usize) -> Result<Self::Parameters, Box<dyn Error>> {
// Use the hardcoded G2 generator from the Pairing trait
let g2_generator = P::G2::generator();

// Generate a deterministic set of G1 generators based on the hardcoded G1 generator
let g1_base_generator = P::G1::generator();
let g1_generators: Vec<P::G1> = (0..=n)
.map(|i| g1_base_generator.mul(&P::ScalarField::from(i as u64)))
.collect();

// Prepare the parameters without the secret/public keys
let g1_generators: Vec<P::G1> = (0..=n).map(|_| P::G1::rand(rng)).collect();
let mut pp: H2S2Parameters<P> = H2S2Parameters {
// Initialize parameters without secret/public keys
let pp: H2S2Parameters<P> = H2S2Parameters {
g1_generators,
g2_generator,
secret_key: Some(P::ScalarField::zero()), // Temporary placeholder
public_key: P::G2::zero(), // Temporary placeholder
max_lanes: n,
};

// Use the keygen function to generate the secret/public key pair
let (public_key, secret_key) = Self::keygen(&pp, rng)?;

// Update the parameters with the generated keys
pp.secret_key = Some(secret_key);
pp.public_key = public_key;

Ok(pp)
}

Expand Down Expand Up @@ -201,7 +200,7 @@ impl<P: Pairing, D: Digest + Send + Sync> HolographicHomomorphicSignatureScheme<
for (sig, &wt) in signatures.iter().zip(weights.iter()) {
let weight_scalar = P::ScalarField::from(wt as u64);
aggregate_signature += sig.signature.mul(weight_scalar);
total_value += weight_scalar * sig.value;
total_value += weight_scalar.mul(sig.value);
}

Ok(AggregatedSignature {
Expand All @@ -221,10 +220,8 @@ mod tests {

static N: usize = 10; // Define the number of generators

static PARAMS: Lazy<H2S2Parameters<Bn254>> = Lazy::new(|| {
let mut rng = test_rng();
NCS::<Bn254, Blake2b512>::setup(&mut rng, N).expect("Setup failed")
});
static PARAMS: Lazy<H2S2Parameters<Bn254>> =
Lazy::new(|| NCS::<Bn254, Blake2b512>::setup(N).expect("Setup failed"));

#[test]
fn test_setup() {
Expand Down

0 comments on commit 6b9f4e5

Please sign in to comment.