Skip to content

Commit 56207ac

Browse files
Fix lint in pairing library
1 parent 0e55a56 commit 56207ac

File tree

21 files changed

+151
-130
lines changed

21 files changed

+151
-130
lines changed

extensions/pairing/guest/src/halo2curves_shims/bls12_381/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ pub use line::*;
99
#[cfg(test)]
1010
pub mod tests;
1111

12+
// Make public for use by tests in guest-libs/pairing/
13+
pub mod test_utils;
14+
1215
use halo2curves_axiom::bls12_381::{Fq, Fq12, Fq2};
1316
use openvm_algebra_guest::field::FieldExtension;
1417

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use core::mem::transmute;
2+
3+
use halo2curves_axiom::bls12_381::{Fq12, MillerLoopResult};
4+
use hex_literal::hex;
5+
use lazy_static::lazy_static;
6+
use num_bigint::BigUint;
7+
use num_traits::Pow;
8+
use openvm_algebra_guest::ExpBytes;
9+
10+
lazy_static! {
11+
pub static ref BLS12_381_MODULUS: BigUint = BigUint::from_bytes_be(&hex!(
12+
"1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab"
13+
));
14+
pub static ref BLS12_381_ORDER: BigUint = BigUint::from_bytes_be(&hex!(
15+
"73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001"
16+
));
17+
}
18+
19+
// Manual final exponentiation because halo2curves `MillerLoopResult` doesn't have constructor
20+
pub fn final_exp(f: Fq12) -> Fq12 {
21+
let p = BLS12_381_MODULUS.clone();
22+
let r = BLS12_381_ORDER.clone();
23+
let exp: BigUint = (p.pow(12u32) - BigUint::from(1u32)) / r;
24+
ExpBytes::exp_bytes(&f, true, &exp.to_bytes_be())
25+
}
26+
27+
// Gt(Fq12) is not public
28+
pub fn assert_miller_results_eq(a: MillerLoopResult, b: Fq12) {
29+
// [jpw] This doesn't work:
30+
// assert_eq!(a.final_exponentiation(), unsafe { transmute(final_exp(b)) });
31+
let a = unsafe { transmute::<MillerLoopResult, Fq12>(a) };
32+
assert_eq!(final_exp(a), final_exp(b));
33+
}

extensions/pairing/guest/src/halo2curves_shims/bls12_381/tests/mod.rs

+1-33
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
use alloc::vec::Vec;
2-
use core::mem::transmute;
32

4-
use halo2curves_axiom::bls12_381::{Fq, Fq12, Fq2, G1Affine, G2Affine, MillerLoopResult};
5-
use hex_literal::hex;
3+
use halo2curves_axiom::bls12_381::{Fq, Fq2, G1Affine, G2Affine};
64
use itertools::izip;
7-
use lazy_static::lazy_static;
8-
use num_bigint::BigUint;
9-
use num_traits::Pow;
10-
use openvm_algebra_guest::ExpBytes;
115
use openvm_ecc_guest::AffinePoint;
126
use rand::{rngs::StdRng, SeedableRng};
137

@@ -19,32 +13,6 @@ mod test_line;
1913
mod test_miller_loop;
2014

2115
#[cfg(not(target_os = "zkvm"))]
22-
23-
lazy_static! {
24-
pub static ref BLS12_381_MODULUS: BigUint = BigUint::from_bytes_be(&hex!(
25-
"1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab"
26-
));
27-
pub static ref BLS12_381_ORDER: BigUint = BigUint::from_bytes_be(&hex!(
28-
"73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001"
29-
));
30-
}
31-
32-
// Manual final exponentiation because halo2curves `MillerLoopResult` doesn't have constructor
33-
pub fn final_exp(f: Fq12) -> Fq12 {
34-
let p = BLS12_381_MODULUS.clone();
35-
let r = BLS12_381_ORDER.clone();
36-
let exp: BigUint = (p.pow(12u32) - BigUint::from(1u32)) / r;
37-
ExpBytes::exp_bytes(&f, true, &exp.to_bytes_be())
38-
}
39-
40-
// Gt(Fq12) is not public
41-
pub fn assert_miller_results_eq(a: MillerLoopResult, b: Fq12) {
42-
// [jpw] This doesn't work:
43-
// assert_eq!(a.final_exponentiation(), unsafe { transmute(final_exp(b)) });
44-
let a = unsafe { transmute::<MillerLoopResult, Fq12>(a) };
45-
assert_eq!(final_exp(a), final_exp(b));
46-
}
47-
4816
#[allow(non_snake_case)]
4917
#[allow(clippy::type_complexity)]
5018
pub fn generate_test_points_bls12_381(

extensions/pairing/guest/src/halo2curves_shims/bls12_381/tests/test_miller_loop.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use subtle::ConditionallySelectable;
88
use super::generate_test_points_bls12_381;
99
use crate::{
1010
halo2curves_shims::bls12_381::{
11-
tests::{assert_miller_results_eq, final_exp},
11+
test_utils::{assert_miller_results_eq, final_exp},
1212
Bls12_381,
1313
},
1414
pairing::{Evaluatable, LineMulMType, MillerStep, MultiMillerLoop},

extensions/pairing/guest/src/halo2curves_shims/bn254/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ pub use line::*;
99
#[cfg(test)]
1010
pub mod tests;
1111

12+
// Make public for use by tests in guest-libs/pairing/
13+
pub mod test_utils;
14+
1215
use halo2curves_axiom::bn256::{Fq, Fq12, Fq2};
1316
use openvm_algebra_guest::field::FieldExtension;
1417

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use core::mem::transmute;
2+
3+
use halo2curves_axiom::{
4+
bn256::{Fq12, Gt},
5+
pairing::MillerLoopResult,
6+
};
7+
use hex_literal::hex;
8+
use lazy_static::lazy_static;
9+
use num_bigint::BigUint;
10+
use num_traits::Pow;
11+
use openvm_algebra_guest::ExpBytes;
12+
13+
lazy_static! {
14+
pub static ref BN254_MODULUS: BigUint = BigUint::from_bytes_be(&hex!(
15+
"30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47"
16+
));
17+
pub static ref BN254_ORDER: BigUint = BigUint::from_bytes_be(&hex!(
18+
"30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001"
19+
));
20+
}
21+
22+
// Manual final exponentiation because halo2curves `MillerLoopResult` doesn't have constructor
23+
pub fn final_exp(f: Fq12) -> Fq12 {
24+
let p = BN254_MODULUS.clone();
25+
let r = BN254_ORDER.clone();
26+
let exp: BigUint = (p.pow(12u32) - BigUint::from(1u32)) / r;
27+
ExpBytes::exp_bytes(&f, true, &exp.to_bytes_be())
28+
}
29+
30+
// Gt(Fq12) is not public
31+
pub fn assert_miller_results_eq(a: Gt, b: Fq12) {
32+
let a = a.final_exponentiation();
33+
let b = final_exp(b);
34+
assert_eq!(unsafe { transmute::<Gt, Fq12>(a) }, b);
35+
}

extensions/pairing/guest/src/halo2curves_shims/bn254/tests/mod.rs

+1-34
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
11
use alloc::vec::Vec;
2-
use core::mem::transmute;
32

4-
use halo2curves_axiom::{
5-
bn256::{Fq, Fq12, Fq2, G1Affine, G2Affine, Gt},
6-
pairing::MillerLoopResult,
7-
};
8-
use hex_literal::hex;
3+
use halo2curves_axiom::bn256::{Fq, Fq2, G1Affine, G2Affine};
94
use itertools::izip;
10-
use lazy_static::lazy_static;
11-
use num_bigint::BigUint;
12-
use num_traits::Pow;
13-
use openvm_algebra_guest::ExpBytes;
145
use openvm_ecc_guest::AffinePoint;
156
use rand::{rngs::StdRng, SeedableRng};
167

@@ -21,30 +12,6 @@ mod test_line;
2112
#[cfg(test)]
2213
mod test_miller_loop;
2314

24-
lazy_static! {
25-
pub static ref BN254_MODULUS: BigUint = BigUint::from_bytes_be(&hex!(
26-
"30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47"
27-
));
28-
pub static ref BN254_ORDER: BigUint = BigUint::from_bytes_be(&hex!(
29-
"30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001"
30-
));
31-
}
32-
33-
// Manual final exponentiation because halo2curves `MillerLoopResult` doesn't have constructor
34-
pub fn final_exp(f: Fq12) -> Fq12 {
35-
let p = BN254_MODULUS.clone();
36-
let r = BN254_ORDER.clone();
37-
let exp: BigUint = (p.pow(12u32) - BigUint::from(1u32)) / r;
38-
ExpBytes::exp_bytes(&f, true, &exp.to_bytes_be())
39-
}
40-
41-
// Gt(Fq12) is not public
42-
pub fn assert_miller_results_eq(a: Gt, b: Fq12) {
43-
let a = a.final_exponentiation();
44-
let b = final_exp(b);
45-
assert_eq!(unsafe { transmute::<Gt, Fq12>(a) }, b);
46-
}
47-
4815
#[allow(non_snake_case)]
4916
#[allow(clippy::type_complexity)]
5017
pub fn generate_test_points_bn254(

extensions/pairing/guest/src/halo2curves_shims/bn254/tests/test_miller_loop.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ use alloc::vec::Vec;
22

33
use halo2curves_axiom::bn256::G2Prepared;
44

5-
use super::{assert_miller_results_eq, generate_test_points_bn254};
6-
use crate::{halo2curves_shims::bn254::Bn254, pairing::MultiMillerLoop};
5+
use super::generate_test_points_bn254;
6+
use crate::{
7+
halo2curves_shims::bn254::{test_utils::assert_miller_results_eq, Bn254},
8+
pairing::MultiMillerLoop,
9+
};
710

811
#[allow(non_snake_case)]
912
fn run_miller_loop_test(rand_seeds: &[u64]) {

guest-libs/ff_derive/guest/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ impl ReprEndianness {
4444
}
4545
}
4646

47+
// Clippy things methods named from_* don't take self as a parameter
48+
#[allow(clippy::wrong_self_convention)]
4749
fn from_repr(&self, name: &syn::Ident, limbs: usize) -> proc_macro2::TokenStream {
4850
let read_repr = match self {
4951
ReprEndianness::Big => quote! {

guest-libs/k256/guest/src/internal.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ impl IntrinsicCurve for Secp256k1 {
7474

7575
impl Secp256k1Point {
7676
pub fn x_be_bytes(&self) -> [u8; 32] {
77-
<Self as WeierstrassPoint>::x(&self).to_be_bytes()
77+
<Self as WeierstrassPoint>::x(self).to_be_bytes()
7878
}
7979

8080
pub fn y_be_bytes(&self) -> [u8; 32] {
81-
<Self as WeierstrassPoint>::y(&self).to_be_bytes()
81+
<Self as WeierstrassPoint>::y(self).to_be_bytes()
8282
}
8383
}

guest-libs/k256/guest/src/point.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ impl FromEncodedPoint<Secp256k1> for Secp256k1Point {
225225
match openvm_ecc_guest::ecdsa::VerifyingKey::<Secp256k1>::from_sec1_bytes(
226226
encoded_point.as_bytes(),
227227
) {
228-
Ok(verifying_key) => CtOption::new(verifying_key.as_affine().clone(), 1.into()),
228+
Ok(verifying_key) => CtOption::new(*verifying_key.as_affine(), 1.into()),
229229
Err(_) => CtOption::new(Secp256k1Point::default(), 0.into()),
230230
}
231231
}

guest-libs/p256/guest/src/internal.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ impl IntrinsicCurve for P256 {
7474

7575
impl P256Point {
7676
pub fn x_be_bytes(&self) -> [u8; 32] {
77-
<Self as WeierstrassPoint>::x(&self).to_be_bytes()
77+
<Self as WeierstrassPoint>::x(self).to_be_bytes()
7878
}
7979

8080
pub fn y_be_bytes(&self) -> [u8; 32] {
81-
<Self as WeierstrassPoint>::y(&self).to_be_bytes()
81+
<Self as WeierstrassPoint>::y(self).to_be_bytes()
8282
}
8383
}

guest-libs/p256/guest/src/point.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ impl FromEncodedPoint<P256> for P256Point {
220220
match openvm_ecc_guest::ecdsa::VerifyingKey::<P256>::from_sec1_bytes(
221221
encoded_point.as_bytes(),
222222
) {
223-
Ok(verifying_key) => CtOption::new(verifying_key.as_affine().clone(), 1.into()),
223+
Ok(verifying_key) => CtOption::new(*verifying_key.as_affine(), 1.into()),
224224
Err(_) => CtOption::new(P256Point::default(), 0.into()),
225225
}
226226
}

guest-libs/pairing/guest/src/bls12_381/mod.rs

-4
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ pub(crate) mod utils;
1515
pub use fp12::*;
1616
pub use fp2::*;
1717
use hex_literal::hex;
18-
#[cfg(not(target_os = "zkvm"))]
19-
use lazy_static::lazy_static;
20-
#[cfg(not(target_os = "zkvm"))]
21-
use num_bigint::BigUint;
2218
use openvm_ecc_sw_macros::sw_declare;
2319
use openvm_pairing_guest::pairing::PairingIntrinsics;
2420

guest-libs/pairing/guest/src/bls12_381/tests.rs

+14-11
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@ use num_bigint::BigUint;
66
use num_traits::One;
77
use openvm_algebra_guest::{field::FieldExtension, IntMod};
88
use openvm_ecc_guest::{weierstrass::WeierstrassPoint, AffinePoint};
9+
use openvm_pairing_guest::{
10+
bls12_381::{BLS12_381_MODULUS, BLS12_381_ORDER},
11+
pairing::{FinalExp, MultiMillerLoop, PairingCheck, PairingIntrinsics},
12+
};
913
use rand::{rngs::StdRng, SeedableRng};
1014

11-
use super::{Fp, Fp12, Fp2, BLS12_381_MODULUS, BLS12_381_ORDER};
15+
use super::{Fp, Fp12, Fp2};
1216
use crate::{
1317
bls12_381::{
1418
utils::{
1519
convert_bls12381_fp12_to_halo2_fq12, convert_bls12381_halo2_fq12_to_fp12,
1620
convert_bls12381_halo2_fq2_to_fp2, convert_bls12381_halo2_fq_to_fp,
1721
convert_g2_affine_halo2_to_openvm,
1822
},
19-
Bls12_381, G2Affine as OpenVmG2Affine, BLS12_381_PSEUDO_BINARY_ENCODING,
20-
BLS12_381_SEED_ABS,
21-
},
22-
pairing::{
23-
fp2_invert_assign, fp6_invert_assign, fp6_square_assign, FinalExp, MultiMillerLoop,
24-
PairingCheck, PairingIntrinsics,
23+
Bls12_381, G2Affine as OpenVmG2Affine,
2524
},
25+
operations::{fp2_invert_assign, fp6_invert_assign, fp6_square_assign},
2626
};
2727

2828
#[test]
@@ -158,7 +158,7 @@ fn test_fp_one() {
158158
// Gt(Fq12) is not public
159159
fn assert_miller_results_eq(a: MillerLoopResult, b: Fp12) {
160160
let b = convert_bls12381_fp12_to_halo2_fq12(b);
161-
crate::halo2curves_shims::bls12_381::tests::assert_miller_results_eq(a, b);
161+
openvm_pairing_guest::halo2curves_shims::bls12_381::test_utils::assert_miller_results_eq(a, b);
162162
}
163163

164164
#[test]
@@ -289,9 +289,12 @@ fn test_bls12381_pairing_check_hint_host() {
289289
y: h2c_q.y,
290290
};
291291

292-
let f_cmp =
293-
crate::halo2curves_shims::bls12_381::Bls12_381::multi_miller_loop(&[p_cmp], &[q_cmp]);
294-
let (c_cmp, s_cmp) = crate::halo2curves_shims::bls12_381::Bls12_381::final_exp_hint(&f_cmp);
292+
let f_cmp = openvm_pairing_guest::halo2curves_shims::bls12_381::Bls12_381::multi_miller_loop(
293+
&[p_cmp],
294+
&[q_cmp],
295+
);
296+
let (c_cmp, s_cmp) =
297+
openvm_pairing_guest::halo2curves_shims::bls12_381::Bls12_381::final_exp_hint(&f_cmp);
295298
let c_cmp = convert_bls12381_halo2_fq12_to_fp12(c_cmp);
296299
let s_cmp = convert_bls12381_halo2_fq12_to_fp12(s_cmp);
297300

guest-libs/pairing/guest/src/bn254/mod.rs

-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ extern crate alloc;
33
use core::ops::{Add, Neg};
44

55
use hex_literal::hex;
6-
#[cfg(not(target_os = "zkvm"))]
7-
use lazy_static::lazy_static;
8-
#[cfg(not(target_os = "zkvm"))]
9-
use num_bigint::BigUint;
106
use openvm_algebra_guest::{Field, IntMod};
117
use openvm_algebra_moduli_macros::moduli_declare;
128
use openvm_ecc_guest::{

guest-libs/pairing/guest/src/bn254/tests.rs

+19-13
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,23 @@ use num_bigint::BigUint;
77
use num_traits::One;
88
use openvm_algebra_guest::{field::FieldExtension, IntMod};
99
use openvm_ecc_guest::{weierstrass::WeierstrassPoint, AffinePoint};
10-
use openvm_pairing_guest::pairing::{
11-
fp2_invert_assign, fp6_invert_assign, fp6_square_assign, FinalExp, MultiMillerLoop,
12-
PairingCheck, PairingIntrinsics,
10+
use openvm_pairing_guest::{
11+
bn254::{BN254_MODULUS, BN254_ORDER},
12+
pairing::{FinalExp, MultiMillerLoop, PairingCheck, PairingIntrinsics},
1313
};
1414
use rand::{rngs::StdRng, SeedableRng};
1515

1616
use super::{Fp, Fp12, Fp2};
17-
use crate::bn254::{
18-
utils::{
19-
convert_bn254_fp12_to_halo2_fq12, convert_bn254_halo2_fq12_to_fp12,
20-
convert_bn254_halo2_fq2_to_fp2, convert_bn254_halo2_fq_to_fp,
21-
convert_g2_affine_halo2_to_openvm,
17+
use crate::{
18+
bn254::{
19+
utils::{
20+
convert_bn254_fp12_to_halo2_fq12, convert_bn254_halo2_fq12_to_fp12,
21+
convert_bn254_halo2_fq2_to_fp2, convert_bn254_halo2_fq_to_fp,
22+
convert_g2_affine_halo2_to_openvm,
23+
},
24+
Bn254, G2Affine as OpenVmG2Affine,
2225
},
23-
Bn254, G2Affine as OpenVmG2Affine, BN254_MODULUS, BN254_ORDER, BN254_PSEUDO_BINARY_ENCODING,
24-
BN254_SEED,
26+
operations::{fp2_invert_assign, fp6_invert_assign, fp6_square_assign},
2527
};
2628

2729
#[test]
@@ -146,7 +148,7 @@ fn test_fp_one() {
146148
// Gt(Fq12) is not public
147149
fn assert_miller_results_eq(a: Gt, b: Fp12) {
148150
let b = convert_bn254_fp12_to_halo2_fq12(b);
149-
crate::halo2curves_shims::bn254::tests::assert_miller_results_eq(a, b);
151+
openvm_pairing_guest::halo2curves_shims::bn254::test_utils::assert_miller_results_eq(a, b);
150152
}
151153

152154
#[test]
@@ -274,8 +276,12 @@ fn test_bn254_pairing_check_hint_host() {
274276
y: h2c_q.y,
275277
};
276278

277-
let f_cmp = crate::halo2curves_shims::bn254::Bn254::multi_miller_loop(&[p_cmp], &[q_cmp]);
278-
let (c_cmp, u_cmp) = crate::halo2curves_shims::bn254::Bn254::final_exp_hint(&f_cmp);
279+
let f_cmp = openvm_pairing_guest::halo2curves_shims::bn254::Bn254::multi_miller_loop(
280+
&[p_cmp],
281+
&[q_cmp],
282+
);
283+
let (c_cmp, u_cmp) =
284+
openvm_pairing_guest::halo2curves_shims::bn254::Bn254::final_exp_hint(&f_cmp);
279285
let c_cmp = convert_bn254_halo2_fq12_to_fp12(c_cmp);
280286
let u_cmp = convert_bn254_halo2_fq12_to_fp12(u_cmp);
281287

0 commit comments

Comments
 (0)