openvm_pairing_guest/bn254/
utils.rs
1use halo2curves_axiom::bn256::{Fq, Fq12, Fq2, G2Affine};
2use openvm_algebra_guest::{field::FieldExtension, IntMod};
3use openvm_ecc_guest::weierstrass::WeierstrassPoint;
4
5use super::{Fp, Fp12, Fp2};
6use crate::bn254::G2Affine as OpenVmG2Affine;
7
8pub(crate) fn convert_bn254_halo2_fq_to_fp(x: Fq) -> Fp {
9 let bytes = x.to_bytes();
10 Fp::from_le_bytes(&bytes)
11}
12
13pub(crate) fn convert_bn254_halo2_fq2_to_fp2(x: Fq2) -> Fp2 {
14 Fp2::new(
15 convert_bn254_halo2_fq_to_fp(x.c0),
16 convert_bn254_halo2_fq_to_fp(x.c1),
17 )
18}
19
20pub(crate) fn convert_bn254_halo2_fq12_to_fp12(x: Fq12) -> Fp12 {
21 Fp12 {
22 c: x.to_coeffs().map(convert_bn254_halo2_fq2_to_fp2),
23 }
24}
25
26pub(crate) fn convert_bn254_fp_to_halo2_fq(x: Fp) -> Fq {
27 Fq::from_bytes(&x.0).unwrap()
28}
29
30pub(crate) fn convert_bn254_fp2_to_halo2_fq2(x: Fp2) -> Fq2 {
31 Fq2 {
32 c0: convert_bn254_fp_to_halo2_fq(x.c0.clone()),
33 c1: convert_bn254_fp_to_halo2_fq(x.c1.clone()),
34 }
35}
36
37#[allow(unused)]
38pub(crate) fn convert_bn254_fp12_to_halo2_fq12(x: Fp12) -> Fq12 {
39 let c = x.to_coeffs();
40 Fq12::from_coeffs(c.map(convert_bn254_fp2_to_halo2_fq2))
41}
42
43#[allow(unused)]
44pub(crate) fn convert_g2_affine_halo2_to_openvm(p: G2Affine) -> OpenVmG2Affine {
45 OpenVmG2Affine::from_xy_unchecked(
46 convert_bn254_halo2_fq2_to_fp2(p.x),
47 convert_bn254_halo2_fq2_to_fp2(p.y),
48 )
49}