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

feat: Add BLS signature verification for BN254 #89

Merged
Show file tree
Hide file tree
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
15 changes: 5 additions & 10 deletions halo2-ecc/src/bn254/bls_signature.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#![allow(non_snake_case)]

use super::pairing::PairingChip;
use super::{Fp12Chip, Fp2Chip, FpChip, FqPoint};
use super::{Fp12Chip, Fp2Chip, FpChip};
use crate::ecc::EccChip;
use crate::fields::FieldChip;
use crate::fields::PrimeField;
use crate::halo2_proofs::halo2curves::bn256::Fq12;
use crate::halo2_proofs::halo2curves::bn256::{G1Affine, G2Affine};
use halo2_base::Context;
use halo2_base::{AssignedValue, Context};

// To avoid issues with mutably borrowing twice (not allowed in Rust), we only store fp_chip and construct g2_chip and fp12_chip in scope when needed for temporary mutable borrows
pub struct BlsSignatureChip<'chip, F: PrimeField> {
Expand All @@ -31,7 +31,7 @@ impl<'chip, F: PrimeField> BlsSignatureChip<'chip, F> {
signatures: &[G2Affine],
pubkeys: &[G1Affine],
msghash: G2Affine,
) -> FqPoint<F> {
) -> AssignedValue<F> {
assert!(
signatures.len() == pubkeys.len(),
"signatures and pubkeys must be the same length"
Expand Down Expand Up @@ -73,12 +73,7 @@ impl<'chip, F: PrimeField> BlsSignatureChip<'chip, F> {
let result = fp12_chip.final_exp(ctx, multi_paired);

// Check signatures are verified
assert_eq!(
format!("{:?}", fp12_chip.get_assigned_value(&result.clone().into())),
format!("{:?}", Fq12::one()),
"Signatures do not match!"
);

result
let fp12_one = fp12_chip.load_constant(ctx, Fq12::one());
fp12_chip.is_equal(ctx, result, fp12_one)
}
}
18 changes: 7 additions & 11 deletions halo2-ecc/src/bn254/tests/bls_signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ use std::{
};

use super::*;
use crate::{
fields::{FieldChip, FpStrategy},
halo2_proofs::halo2curves::bn256::G2Affine,
};
use crate::{fields::FpStrategy, halo2_proofs::halo2curves::bn256::G2Affine};
use halo2_base::{
gates::{
builder::{
Expand All @@ -18,7 +15,7 @@ use halo2_base::{
},
halo2_proofs::{
halo2curves::{
bn256::{multi_miller_loop, G2Prepared},
bn256::{multi_miller_loop, G2Prepared, Gt},
pairing::MillerLoopResult,
},
poly::kzg::multiopen::{ProverGWC, VerifierGWC},
Expand Down Expand Up @@ -68,12 +65,11 @@ fn bls_signature_test<F: PrimeField>(
.final_exponentiation();

// Compare the 2 results
let fp12_chip = Fp12Chip::new(&fp_chip_1);
assert_eq!(
format!("Gt({:?})", fp12_chip.get_assigned_value(&result.into())),
format!("{actual_result:?}"),
"Signatures do not match!"
);
if *(result.value()) == 1.into() {
flyingnobita marked this conversation as resolved.
Show resolved Hide resolved
assert_eq!(actual_result, Gt::identity())
} else {
assert_ne!(actual_result, Gt::identity())
}
}

fn random_bls_signature_circuit(
Expand Down