|
1 | 1 | use ark_ec::{bn::BnConfig, short_weierstrass::SWCurveConfig, CurveGroup}; |
2 | 2 | use ark_ff::{AdditiveGroup, Field, Fp2Config}; |
3 | | -use crate::{bag::*, circuits::bn254::{fp254impl::Fp254Impl, fq::Fq, fq2::Fq2}}; |
| 3 | +use crate::{bag::*, circuits::bn254::{fp254impl::Fp254Impl, fq::Fq, fq12::Fq12, fq2::Fq2}}; |
4 | 4 |
|
5 | 5 | pub fn double_in_place(r: &mut ark_bn254::G2Projective) -> (ark_bn254::Fq2, ark_bn254::Fq2, ark_bn254::Fq2) { |
6 | 6 | let half = ark_bn254::Fq::from(Fq::half_modulus()); |
@@ -222,6 +222,23 @@ pub fn ell(f: &mut ark_bn254::Fq12, coeffs: (ark_bn254::Fq2, ark_bn254::Fq2, ark |
222 | 222 | f.mul_by_034(&c0, &c1, &c2); |
223 | 223 | } |
224 | 224 |
|
| 225 | +pub fn ell_circuit(f: Wires, coeffs: (Wires, Wires, Wires), p: Wires) -> Circuit { |
| 226 | + let mut circuit = Circuit::empty(); |
| 227 | + let c0 = coeffs.0; |
| 228 | + let c1 = coeffs.1; |
| 229 | + let c2 = coeffs.2; |
| 230 | + |
| 231 | + let px = p[0..Fq::N_BITS].to_vec(); |
| 232 | + let py = p[Fq::N_BITS..2*Fq::N_BITS].to_vec(); |
| 233 | + |
| 234 | + let new_c0 = circuit.extend(Fq2::mul_by_fq(c0, py)); |
| 235 | + let new_c1 = circuit.extend(Fq2::mul_by_fq(c1, px)); |
| 236 | + let new_f = circuit.extend(Fq12::mul_by_034(f, new_c0, new_c1, c2)); |
| 237 | + |
| 238 | + circuit.add_wires(new_f); |
| 239 | + circuit |
| 240 | +} |
| 241 | + |
225 | 242 | pub fn miller_loop(p: ark_bn254::G1Projective, q: ark_bn254::G2Projective) -> ark_bn254::Fq12 { |
226 | 243 | let qell = ell_coeffs(q); |
227 | 244 | let mut q_ell = qell.iter(); |
@@ -253,7 +270,7 @@ mod tests { |
253 | 270 | use ark_std::rand::SeedableRng; |
254 | 271 | use ark_ec::pairing::Pairing; |
255 | 272 | use rand_chacha::ChaCha20Rng; |
256 | | - use crate::circuits::bn254::utils::{fq2_from_wires, wires_set_from_g2a, wires_set_from_g2p}; |
| 273 | + use crate::circuits::bn254::utils::{fq12_from_wires, fq2_from_wires, wires_set_from_fq12, wires_set_from_fq2, wires_set_from_g1p, wires_set_from_g2a, wires_set_from_g2p}; |
257 | 274 | use super::*; |
258 | 275 |
|
259 | 276 | #[test] |
@@ -324,6 +341,23 @@ mod tests { |
324 | 341 | assert_eq!(c1, coeffs.y); |
325 | 342 | } |
326 | 343 |
|
| 344 | + #[test] |
| 345 | + fn test_ell_circuit() { |
| 346 | + let mut prng = ChaCha20Rng::seed_from_u64(0); |
| 347 | + let mut f = ark_bn254::Fq12::rand(&mut prng); |
| 348 | + let coeffs = (ark_bn254::Fq2::rand(&mut prng), ark_bn254::Fq2::rand(&mut prng), ark_bn254::Fq2::rand(&mut prng)); |
| 349 | + let p = ark_bn254::G1Projective::rand(&mut prng); |
| 350 | + |
| 351 | + let circuit = ell_circuit(wires_set_from_fq12(f), (wires_set_from_fq2(coeffs.0), wires_set_from_fq2(coeffs.1), wires_set_from_fq2(coeffs.2)), wires_set_from_g1p(p)); |
| 352 | + circuit.print_gate_type_counts(); |
| 353 | + for mut gate in circuit.1 { |
| 354 | + gate.evaluate(); |
| 355 | + } |
| 356 | + let new_f = fq12_from_wires(circuit.0); |
| 357 | + ell(&mut f, coeffs, p); |
| 358 | + assert_eq!(f, new_f); |
| 359 | + } |
| 360 | + |
327 | 361 | #[test] |
328 | 362 | fn test_miller_loop() { |
329 | 363 | let mut prng = ChaCha20Rng::seed_from_u64(0); |
|
0 commit comments