ark-r1cs-std/r1cs-std/src/fields/fp12.rs
r1cs-std/src/fields/fp12.rsRaw
1use r1cs_core::{ConstraintSystem, SynthesisError};
2
3use algebra::{
4 fields::{
5 fp12_2over3over2::{Fp12, Fp12Parameters},
6 fp6_3over2::{Fp6, Fp6Parameters},
7 Fp2Parameters,
8 },
9 BitIterator, PrimeField,
10};
11use core::{borrow::Borrow, marker::PhantomData};
12
13use crate::{prelude::*, Vec};
14
15type Fp2Gadget<P, ConstraintF> = super::fp2::Fp2Gadget<
16 <<P as Fp12Parameters>::Fp6Params as Fp6Parameters>::Fp2Params,
17 ConstraintF,
18>;
19type Fp6Gadget<P, ConstraintF> =
20 super::fp6_3over2::Fp6Gadget<<P as Fp12Parameters>::Fp6Params, ConstraintF>;
21type Fp6GadgetVariable<P, ConstraintF> = <Fp6Gadget<P, ConstraintF> as FieldGadget<
22 Fp6<<P as Fp12Parameters>::Fp6Params>,
23 ConstraintF,
24>>::Variable;
25
26#[derive(Derivative)]
27#[derivative(Debug(bound = "ConstraintF: PrimeField"))]
28#[must_use]
29pub struct Fp12Gadget<P, ConstraintF: PrimeField>
30where
31 P: Fp12Parameters,
32 <P::Fp6Params as Fp6Parameters>::Fp2Params: Fp2Parameters<Fp = ConstraintF>,
33{
34 pub c0: Fp6Gadget<P, ConstraintF>,
35 pub c1: Fp6Gadget<P, ConstraintF>,
36 #[derivative(Debug = "ignore")]
37 _params: PhantomData<P>,
38}
39
40impl<P, ConstraintF: PrimeField> Fp12Gadget<P, ConstraintF>
41where
42 P: Fp12Parameters,
43 <P::Fp6Params as Fp6Parameters>::Fp2Params: Fp2Parameters<Fp = ConstraintF>,
44{
45 #[inline]
46 pub fn new(c0: Fp6Gadget<P, ConstraintF>, c1: Fp6Gadget<P, ConstraintF>) -> Self {
47 Self {
48 c0,
49 c1,
50 _params: PhantomData,
51 }
52 }
53
54 /// Multiply by quadratic nonresidue v.
55 #[inline]
56 pub(crate) fn mul_fp6_by_nonresidue<CS: ConstraintSystem<ConstraintF>>(
57 cs: CS,
58 fe: &Fp6Gadget<P, ConstraintF>,
59 ) -> Result<Fp6Gadget<P, ConstraintF>, SynthesisError> {
60 let new_c0 = Fp6Gadget::<P, ConstraintF>::mul_fp2_gadget_by_nonresidue(cs, &fe.c2)?;
61 let new_c1 = fe.c0.clone();
62 let new_c2 = fe.c1.clone();
63 Ok(Fp6Gadget::<P, ConstraintF>::new(new_c0, new_c1, new_c2))
64 }
65
66 #[inline]
67 pub fn conjugate_in_place<CS: ConstraintSystem<ConstraintF>>(
68 &mut self,
69 cs: CS,
70 ) -> Result<&mut Self, SynthesisError> {
71 self.c1.negate_in_place(cs)?;
72 Ok(self)
73 }
74
75 /// Multiplies by an element of the form (c0 = (c0, c1, 0), c1 = (0, d1, 0))
76 #[inline]
77 pub fn mul_by_014<CS: ConstraintSystem<ConstraintF>>(
78 &self,
79 mut cs: CS,
80 c0: &Fp2Gadget<P, ConstraintF>,
81 c1: &Fp2Gadget<P, ConstraintF>,
82 d1: &Fp2Gadget<P, ConstraintF>,
83 ) -> Result<Self, SynthesisError> {
84 let v0 = self.c0.mul_by_c0_c1_0(cs.ns(|| "v0"), &c0, &c1)?;
85 let v1 = self.c1.mul_by_0_c1_0(cs.ns(|| "v1"), &d1)?;
86 let new_c0 = Self::mul_fp6_by_nonresidue(cs.ns(|| "first mul_by_nr"), &v1)?
87 .add(cs.ns(|| "v0 + nonresidue * v1"), &v0)?;
88
89 let c1 = {
90 let tmp = c1.add(cs.ns(|| "c1 + d1"), &d1)?;
91 let a0_plus_a1 = self.c0.add(cs.ns(|| "a0 + a1"), &self.c1)?;
92 a0_plus_a1
93 .mul_by_c0_c1_0(cs.ns(|| "(a0 + a1) * (b0 + b1)"), &c0, &tmp)?
94 .sub(cs.ns(|| "sub v0"), &v0)?
95 .sub(cs.ns(|| "sub v1"), &v1)?
96 };
97 Ok(Self::new(new_c0, c1))
98 }
99
100 /// Multiplies by an element of the form (c0 = (c0, 0, 0), c1 = (d0, d1, 0))
101 #[inline]
102 pub fn mul_by_034<CS: ConstraintSystem<ConstraintF>>(
103 &self,
104 mut cs: CS,
105 c0: &Fp2Gadget<P, ConstraintF>,
106 d0: &Fp2Gadget<P, ConstraintF>,
107 d1: &Fp2Gadget<P, ConstraintF>,
108 ) -> Result<Self, SynthesisError> {
109 let a0 = self.c0.c0.mul(cs.ns(|| "a0"), &c0)?;
110 let a1 = self.c0.c1.mul(cs.ns(|| "a1"), &c0)?;
111 let a2 = self.c0.c2.mul(cs.ns(|| "a2"), &c0)?;
112 let a = Fp6Gadget::<P, ConstraintF>::new(a0, a1, a2);
113 let b = self.c1.mul_by_c0_c1_0(cs.ns(|| "b"), &d0, &d1)?;
114
115 let c0 = c0.add(cs.ns(|| "c0 + d0"), &d0)?;
116 let c1 = d1;
117 let e = self
118 .c0
119 .add(cs.ns(|| "self.c0 + self.c1"), &self.c1)?
120 .mul_by_c0_c1_0(cs.ns(|| "compute e"), &c0, &c1)?;
121 let a_plus_b = a.add(cs.ns(|| "a + b"), &b)?;
122 let c1 = e.sub(cs.ns(|| "e - (a + b)"), &a_plus_b)?;
123 let c0 = Self::mul_fp6_by_nonresidue(cs.ns(|| "b *nr"), &b)?.add(cs.ns(|| "plus a"), &a)?;
124
125 Ok(Self::new(c0, c1))
126 }
127
128 pub fn cyclotomic_square<CS: ConstraintSystem<ConstraintF>>(
129 &self,
130 mut cs: CS,
131 ) -> Result<Self, SynthesisError> {
132 let mut result = Self::zero(cs.ns(|| "alloc result"))?;
133 let fp2_nr = <P::Fp6Params as Fp6Parameters>::NONRESIDUE;
134
135 let z0 = &self.c0.c0;
136 let z4 = &self.c0.c1;
137 let z3 = &self.c0.c2;
138 let z2 = &self.c1.c0;
139 let z1 = &self.c1.c1;
140 let z5 = &self.c1.c2;
141
142 // t0 + t1*y = (z0 + z1*y)^2 = a^2
143 let tmp = z0.mul(cs.ns(|| "first mul"), &z1)?;
144 let t0 = {
145 // (z0 + &z1) * &(z0 + &(fp2_nr * &z1)) - &tmp - &(tmp * &fp2_nr);
146 let mut cs = cs.ns(|| "t0");
147 let tmp1 = z0.add(cs.ns(|| "tmp1"), &z1)?;
148 let tmp2 = z1
149 .mul_by_constant(cs.ns(|| "tmp2.1"), &fp2_nr)?
150 .add(cs.ns(|| "tmp2.2"), &z0)?;
151 let tmp4 = tmp
152 .mul_by_constant(cs.ns(|| "tmp4.1"), &fp2_nr)?
153 .add(cs.ns(|| "tmp4.2"), &tmp)?;
154 tmp1.mul(cs.ns(|| "tmp3.1"), &tmp2)?
155 .sub(cs.ns(|| "tmp3.2"), &tmp4)?
156 };
157 let t1 = tmp.double(cs.ns(|| "t1"))?;
158
159 // t2 + t3*y = (z2 + z3*y)^2 = b^2
160 let tmp = z2.mul(cs.ns(|| "second mul"), &z3)?;
161 let t2 = {
162 // (z2 + &z3) * &(z2 + &(fp2_nr * &z3)) - &tmp - &(tmp * &fp2_nr);
163 let mut cs = cs.ns(|| "t2");
164 let tmp1 = z2.add(cs.ns(|| "tmp1"), &z3)?;
165 let tmp2 = z3
166 .mul_by_constant(cs.ns(|| "tmp2.1"), &fp2_nr)?
167 .add(cs.ns(|| "tmp2.2"), &z2)?;
168 let tmp4 = tmp
169 .mul_by_constant(cs.ns(|| "tmp4.1"), &fp2_nr)?
170 .add(cs.ns(|| "tmp4.2"), &tmp)?;
171 tmp1.mul(cs.ns(|| "tmp3.1"), &tmp2)?
172 .sub(cs.ns(|| "tmp3.2"), &tmp4)?
173 };
174 let t3 = tmp.double(cs.ns(|| "t3"))?;
175
176 // t4 + t5*y = (z4 + z5*y)^2 = c^2
177 let tmp = z4.mul(cs.ns(|| "third mul"), &z5)?;
178 let t4 = {
179 // (z4 + &z5) * &(z4 + &(fp2_nr * &z5)) - &tmp - &(tmp * &fp2_nr);
180 let mut cs = cs.ns(|| "t4");
181 let tmp1 = z4.add(cs.ns(|| "tmp1"), &z5)?;
182 let tmp2 = z5
183 .mul_by_constant(cs.ns(|| "tmp2.1"), &fp2_nr)?
184 .add(cs.ns(|| "tmp2.2"), &z4)?;
185 let tmp4 = tmp
186 .mul_by_constant(cs.ns(|| "tmp4.1"), &fp2_nr)?
187 .add(cs.ns(|| "tmp4.2"), &tmp)?;
188 tmp1.mul(cs.ns(|| "tmp3.1"), &tmp2)?
189 .sub(cs.ns(|| "tmp3.2"), &tmp4)?
190 };
191 let t5 = tmp.double(cs.ns(|| "t5"))?;
192
193 // for A
194
195 // z0 = 3 * t0 - 2 * z0
196 result.c0.c0 = {
197 let mut cs = cs.ns(|| "result.c0.c0");
198 t0.sub(cs.ns(|| "1"), &z0)?
199 .double(cs.ns(|| "2"))?
200 .add(cs.ns(|| "3"), &t0)?
201 };
202
203 // z1 = 3 * t1 + 2 * z1
204 result.c1.c1 = {
205 let mut cs = cs.ns(|| "result.c1.c1");
206 t1.add(cs.ns(|| "1"), &z1)?
207 .double(cs.ns(|| "2"))?
208 .add(cs.ns(|| "3"), &t1)?
209 };
210
211 // for B
212
213 // z2 = 3 * (xi * t5) + 2 * z2
214 result.c1.c0 = {
215 let mut cs = cs.ns(|| "result.c1.c0");
216 let tmp = t5.mul_by_constant(cs.ns(|| "1"), &fp2_nr)?;
217 z2.add(cs.ns(|| "2"), &tmp)?
218 .double(cs.ns(|| "3"))?
219 .add(cs.ns(|| "4"), &tmp)?
220 };
221
222 // z3 = 3 * t4 - 2 * z3
223 result.c0.c2 = {
224 let mut cs = cs.ns(|| "result.c0.c2");
225 t4.sub(cs.ns(|| "1"), &z3)?
226 .double(cs.ns(|| "2"))?
227 .add(cs.ns(|| "3"), &t4)?
228 };
229
230 // for C
231
232 // z4 = 3 * t2 - 2 * z4
233 result.c0.c1 = {
234 let mut cs = cs.ns(|| "result.c0.c1");
235 t2.sub(cs.ns(|| "1"), &z4)?
236 .double(cs.ns(|| "2"))?
237 .add(cs.ns(|| "3"), &t2)?
238 };
239
240 // z5 = 3 * t3 + 2 * z5
241 result.c1.c2 = {
242 let mut cs = cs.ns(|| "result.c1.c2");
243 t3.add(cs.ns(|| "1"), &z5)?
244 .double(cs.ns(|| "2"))?
245 .add(cs.ns(|| "3"), &t3)?
246 };
247
248 Ok(result)
249 }
250
251 #[inline]
252 pub fn cyclotomic_exp<CS: ConstraintSystem<ConstraintF>, S: AsRef<[u64]>>(
253 &self,
254 mut cs: CS,
255 exp: S,
256 ) -> Result<Self, SynthesisError> {
257 let mut res = Self::one(cs.ns(|| "one"))?;
258 let mut found_one = false;
259 for (j, i) in BitIterator::new(exp).enumerate() {
260 if found_one {
261 res = res.cyclotomic_square(cs.ns(|| format!("res_square_{:?}", j)))?;
262 } else {
263 found_one = i;
264 }
265 if i {
266 res.mul_in_place(cs.ns(|| format!("res_mul2_{:?}", j)), self)?;
267 }
268 }
269 Ok(res)
270 }
271}
272
273impl<P, ConstraintF: PrimeField> FieldGadget<Fp12<P>, ConstraintF> for Fp12Gadget<P, ConstraintF>
274where
275 P: Fp12Parameters,
276 <P::Fp6Params as Fp6Parameters>::Fp2Params: Fp2Parameters<Fp = ConstraintF>,
277{
278 type Variable = (
279 Fp6GadgetVariable<P, ConstraintF>,
280 Fp6GadgetVariable<P, ConstraintF>,
281 );
282
283 #[inline]
284 fn get_value(&self) -> Option<Fp12<P>> {
285 Some(Fp12::new(self.c0.get_value()?, self.c1.get_value()?))
286 }
287
288 #[inline]
289 fn get_variable(&self) -> Self::Variable {
290 (self.c0.get_variable(), self.c1.get_variable())
291 }
292
293 #[inline]
294 fn zero<CS: ConstraintSystem<ConstraintF>>(mut cs: CS) -> Result<Self, SynthesisError> {
295 let c0 = Fp6Gadget::<P, ConstraintF>::zero(cs.ns(|| "c0"))?;
296 let c1 = Fp6Gadget::<P, ConstraintF>::zero(cs.ns(|| "c1"))?;
297 Ok(Self::new(c0, c1))
298 }
299
300 #[inline]
301 fn one<CS: ConstraintSystem<ConstraintF>>(mut cs: CS) -> Result<Self, SynthesisError> {
302 let c0 = Fp6Gadget::<P, ConstraintF>::one(cs.ns(|| "c0"))?;
303 let c1 = Fp6Gadget::<P, ConstraintF>::zero(cs.ns(|| "c1"))?;
304 Ok(Self::new(c0, c1))
305 }
306
307 #[inline]
308 fn conditionally_add_constant<CS: ConstraintSystem<ConstraintF>>(
309 &self,
310 mut cs: CS,
311 bit: &Boolean,
312 coeff: Fp12<P>,
313 ) -> Result<Self, SynthesisError> {
314 let c0 = self
315 .c0
316 .conditionally_add_constant(cs.ns(|| "c0"), bit, coeff.c0)?;
317 let c1 = self
318 .c1
319 .conditionally_add_constant(cs.ns(|| "c1"), bit, coeff.c1)?;
320 Ok(Self::new(c0, c1))
321 }
322
323 #[inline]
324 fn add<CS: ConstraintSystem<ConstraintF>>(
325 &self,
326 mut cs: CS,
327 other: &Self,
328 ) -> Result<Self, SynthesisError> {
329 let c0 = self.c0.add(cs.ns(|| "c0"), &other.c0)?;
330 let c1 = self.c1.add(cs.ns(|| "c1"), &other.c1)?;
331 Ok(Self::new(c0, c1))
332 }
333
334 #[inline]
335 fn add_in_place<CS: ConstraintSystem<ConstraintF>>(
336 &mut self,
337 mut cs: CS,
338 other: &Self,
339 ) -> Result<&mut Self, SynthesisError> {
340 self.c0.add_in_place(cs.ns(|| "c0"), &other.c0)?;
341 self.c1.add_in_place(cs.ns(|| "c1"), &other.c1)?;
342 Ok(self)
343 }
344
345 #[inline]
346 fn sub<CS: ConstraintSystem<ConstraintF>>(
347 &self,
348 mut cs: CS,
349 other: &Self,
350 ) -> Result<Self, SynthesisError> {
351 let c0 = self.c0.sub(cs.ns(|| "c0"), &other.c0)?;
352 let c1 = self.c1.sub(cs.ns(|| "c1"), &other.c1)?;
353 Ok(Self::new(c0, c1))
354 }
355
356 #[inline]
357 fn sub_in_place<CS: ConstraintSystem<ConstraintF>>(
358 &mut self,
359 mut cs: CS,
360 other: &Self,
361 ) -> Result<&mut Self, SynthesisError> {
362 self.c0.sub_in_place(cs.ns(|| "c0"), &other.c0)?;
363 self.c1.sub_in_place(cs.ns(|| "c1"), &other.c1)?;
364 Ok(self)
365 }
366
367 #[inline]
368 fn negate<CS: ConstraintSystem<ConstraintF>>(
369 &self,
370 mut cs: CS,
371 ) -> Result<Self, SynthesisError> {
372 let c0 = self.c0.negate(cs.ns(|| "c0"))?;
373 let c1 = self.c1.negate(cs.ns(|| "c1"))?;
374 Ok(Self::new(c0, c1))
375 }
376
377 #[inline]
378 fn negate_in_place<CS: ConstraintSystem<ConstraintF>>(
379 &mut self,
380 mut cs: CS,
381 ) -> Result<&mut Self, SynthesisError> {
382 self.c0.negate_in_place(cs.ns(|| "c0"))?;
383 self.c1.negate_in_place(cs.ns(|| "c1"))?;
384 Ok(self)
385 }
386
387 #[inline]
388 fn mul<CS: ConstraintSystem<ConstraintF>>(
389 &self,
390 mut cs: CS,
391 other: &Self,
392 ) -> Result<Self, SynthesisError> {
393 // Karatsuba multiplication:
394 // v0 = A.c0 * B.c0
395 // v1 = A.c1 * B.c1
396 // result.c0 = v0 + non_residue * v1
397 // result.c1 = (A.c0 + A.c1) * (B.c0 + B.c1) - v0 - v1
398 //
399 // Enforced with 3 Fp3_mul_gadget's that ensure that:
400 // A.c1 * B.c1 = v1
401 // A.c0 * B.c0 = v0
402 // (A.c0+A.c1)*(B.c0+B.c1) = result.c1 + v0 + v1
403
404 let v0 = self.c0.mul(cs.ns(|| "v0"), &other.c0)?;
405 let v1 = self.c1.mul(cs.ns(|| "v1"), &other.c1)?;
406 let c0 = {
407 let non_residue_times_v1 =
408 Self::mul_fp6_by_nonresidue(cs.ns(|| "first mul_by_nr"), &v1)?;
409 v0.add(cs.ns(|| "v0 + beta * v1"), &non_residue_times_v1)?
410 };
411 let c1 = {
412 let a0_plus_a1 = self.c0.add(cs.ns(|| "a0 + a1"), &self.c1)?;
413 let b0_plus_b1 = other.c0.add(cs.ns(|| "b0 + b1"), &other.c1)?;
414 let a0_plus_a1_times_b0_plus_b1 =
415 a0_plus_a1.mul(&mut cs.ns(|| "(a0 + a1) * (b0 + b1)"), &b0_plus_b1)?;
416 a0_plus_a1_times_b0_plus_b1
417 .sub(cs.ns(|| "res - v0"), &v0)?
418 .sub(cs.ns(|| "res - v0 - v1"), &v1)?
419 };
420
421 Ok(Self::new(c0, c1))
422 }
423
424 fn square<CS: ConstraintSystem<ConstraintF>>(
425 &self,
426 mut cs: CS,
427 ) -> Result<Self, SynthesisError> {
428 // From Libsnark/fp2_gadget.tcc
429 // Complex multiplication for Fp2:
430 // v0 = A.c0 * A.c1
431 // result.c0 = (A.c0 + A.c1) * (A.c0 + non_residue * A.c1) - (1 +
432 // non_residue) * v0 result.c1 = 2 * v0
433 // Enforced with 2 constraints:
434 // (2*A.c0) * A.c1 = result.c1
435 // (A.c0 + A.c1) * (A.c0 + non_residue * A.c1) = result.c0 + result.c1 * (1
436 // + non_residue)/2 Reference:
437 // "Multiplication and Squaring on Pairing-Friendly Fields"
438 // Devegili, OhEigeartaigh, Scott, Dahab
439
440 let mut v0 = self.c0.mul(cs.ns(|| "v0"), &self.c1)?;
441 let a0_plus_a1 = self.c0.add(cs.ns(|| "a0 + a1"), &self.c1)?;
442
443 let non_residue_a1 = Self::mul_fp6_by_nonresidue(cs.ns(|| "non_residue * a1"), &self.c1)?;
444 let a0_plus_non_residue_a1 = self
445 .c0
446 .add(cs.ns(|| "a0 + non_residue * a1"), &non_residue_a1)?;
447 let one_plus_non_residue_v0 =
448 Self::mul_fp6_by_nonresidue(cs.ns(|| "non_residue * v0"), &v0)?
449 .add(cs.ns(|| "plus v0"), &v0)?;
450
451 let c0 = a0_plus_a1
452 .mul(
453 cs.ns(|| "(a0 + a1) * (a0 + non_residue * a1)"),
454 &a0_plus_non_residue_a1,
455 )?
456 .sub(cs.ns(|| "- (1 + non_residue) v0"), &one_plus_non_residue_v0)?;
457
458 v0.double_in_place(cs.ns(|| "2v0"))?;
459 let c1 = v0;
460
461 Ok(Self {
462 c0,
463 c1,
464 _params: PhantomData,
465 })
466 }
467
468 #[inline]
469 fn add_constant<CS: ConstraintSystem<ConstraintF>>(
470 &self,
471 mut cs: CS,
472 other: &Fp12<P>,
473 ) -> Result<Self, SynthesisError> {
474 let c0 = self.c0.add_constant(cs.ns(|| "c0"), &other.c0)?;
475 let c1 = self.c1.add_constant(cs.ns(|| "c1"), &other.c1)?;
476
477 Ok(Self::new(c0, c1))
478 }
479
480 #[inline]
481 fn add_constant_in_place<CS: ConstraintSystem<ConstraintF>>(
482 &mut self,
483 mut cs: CS,
484 other: &Fp12<P>,
485 ) -> Result<&mut Self, SynthesisError> {
486 self.c0.add_constant_in_place(cs.ns(|| "c0"), &other.c0)?;
487 self.c1.add_constant_in_place(cs.ns(|| "c1"), &other.c1)?;
488 Ok(self)
489 }
490
491 fn mul_by_constant<CS: ConstraintSystem<ConstraintF>>(
492 &self,
493 mut cs: CS,
494 other: &Fp12<P>,
495 ) -> Result<Self, SynthesisError> {
496 // Karatsuba multiplication (see mul above).
497 // Doesn't need any constraints; returns linear combinations of
498 // `self`'s variables.
499 //
500 // (The operations below are guaranteed to return linear combinations)
501 let (a0, a1) = (&self.c0, &self.c1);
502 let (b0, b1) = (other.c0, other.c1);
503 let mut v0 = a0.mul_by_constant(&mut cs.ns(|| "v0"), &b0)?;
504 let mut v1 = Self::mul_fp6_by_nonresidue(&mut cs.ns(|| "v1"), a1)?;
505 let beta_v1 = v1.mul_by_constant_in_place(&mut cs.ns(|| "beta * v1"), &b1)?;
506
507 v0.add_in_place(&mut cs.ns(|| "c0"), beta_v1)?;
508 let c0 = v0;
509
510 let mut a0b1 = a0.mul_by_constant(&mut cs.ns(|| "a0b1"), &b1)?;
511 let a1b0 = a1.mul_by_constant(&mut cs.ns(|| "a1b0"), &b0)?;
512 a0b1.add_in_place(&mut cs.ns(|| "c1"), &a1b0)?;
513 let c1 = a0b1;
514 Ok(Self::new(c0, c1))
515 }
516
517 fn frobenius_map<CS: ConstraintSystem<ConstraintF>>(
518 &self,
519 cs: CS,
520 power: usize,
521 ) -> Result<Self, SynthesisError> {
522 let mut res = self.clone();
523 res.frobenius_map_in_place(cs, power)?;
524 Ok(res)
525 }
526
527 fn frobenius_map_in_place<CS: ConstraintSystem<ConstraintF>>(
528 &mut self,
529 mut cs: CS,
530 power: usize,
531 ) -> Result<&mut Self, SynthesisError> {
532 self.c0
533 .frobenius_map_in_place(cs.ns(|| "frob_map1"), power)?;
534 self.c1
535 .frobenius_map_in_place(cs.ns(|| "frob_map2"), power)?;
536
537 self.c1
538 .c0
539 .mul_by_constant_in_place(cs.ns(|| "mul1"), &P::FROBENIUS_COEFF_FP12_C1[power % 12])?;
540 self.c1
541 .c1
542 .mul_by_constant_in_place(cs.ns(|| "mul2"), &P::FROBENIUS_COEFF_FP12_C1[power % 12])?;
543 self.c1
544 .c2
545 .mul_by_constant_in_place(cs.ns(|| "mul3"), &P::FROBENIUS_COEFF_FP12_C1[power % 12])?;
546 Ok(self)
547 }
548
549 fn mul_equals<CS: ConstraintSystem<ConstraintF>>(
550 &self,
551 mut cs: CS,
552 other: &Self,
553 result: &Self,
554 ) -> Result<(), SynthesisError> {
555 // Karatsuba multiplication for Fp2:
556 // v0 = A.c0 * B.c0
557 // v1 = A.c1 * B.c1
558 // result.c0 = v0 + non_residue * v1
559 // result.c1 = (A.c0 + A.c1) * (B.c0 + B.c1) - v0 - v1
560 // Enforced with 3 constraints:
561 // A.c1 * B.c1 = v1
562 // A.c0 * B.c0 = result.c0 - non_residue * v1
563 // (A.c0+A.c1)*(B.c0+B.c1) = result.c1 + result.c0 + (1 - non_residue) * v1
564 // Reference:
565 // "Multiplication and Squaring on Pairing-Friendly Fields"
566 // Devegili, OhEigeartaigh, Scott, Dahab
567 let mul_cs = &mut cs.ns(|| "mul");
568
569 // Compute v1
570 let v1 = self.c1.mul(mul_cs.ns(|| "v1"), &other.c1)?;
571
572 // Perform second check
573 let non_residue_times_v1 = Self::mul_fp6_by_nonresidue(mul_cs.ns(|| "nr * v1"), &v1)?;
574 let rhs = result
575 .c0
576 .sub(mul_cs.ns(|| "sub from result.c0"), &non_residue_times_v1)?;
577 self.c0
578 .mul_equals(mul_cs.ns(|| "second check"), &other.c0, &rhs)?;
579
580 // Last check
581 let a0_plus_a1 = self.c0.add(mul_cs.ns(|| "a0 + a1"), &self.c1)?;
582 let b0_plus_b1 = other.c0.add(mul_cs.ns(|| "b0 + b1"), &other.c1)?;
583 let one_minus_non_residue_v1 =
584 v1.sub(mul_cs.ns(|| "sub from v1"), &non_residue_times_v1)?;
585
586 let result_c1_plus_result_c0_plus_one_minus_non_residue_v1 = result
587 .c1
588 .add(mul_cs.ns(|| "c1 + c0"), &result.c0)?
589 .add(mul_cs.ns(|| "rest of stuff"), &one_minus_non_residue_v1)?;
590
591 a0_plus_a1.mul_equals(
592 mul_cs.ns(|| "third check"),
593 &b0_plus_b1,
594 &result_c1_plus_result_c0_plus_one_minus_non_residue_v1,
595 )?;
596
597 Ok(())
598 }
599
600 fn cost_of_mul() -> usize {
601 3 * Fp6Gadget::<P, ConstraintF>::cost_of_mul()
602 }
603
604 fn cost_of_mul_equals() -> usize {
605 Self::cost_of_mul()
606 }
607}
608
609impl<P, ConstraintF: PrimeField> PartialEq for Fp12Gadget<P, ConstraintF>
610where
611 P: Fp12Parameters,
612 <P::Fp6Params as Fp6Parameters>::Fp2Params: Fp2Parameters<Fp = ConstraintF>,
613{
614 fn eq(&self, other: &Self) -> bool {
615 self.c0 == other.c0 && self.c1 == other.c1
616 }
617}
618
619impl<P, ConstraintF: PrimeField> Eq for Fp12Gadget<P, ConstraintF>
620where
621 P: Fp12Parameters,
622 <P::Fp6Params as Fp6Parameters>::Fp2Params: Fp2Parameters<Fp = ConstraintF>,
623{
624}
625
626impl<P, ConstraintF: PrimeField> EqGadget<ConstraintF> for Fp12Gadget<P, ConstraintF>
627where
628 P: Fp12Parameters,
629 <P::Fp6Params as Fp6Parameters>::Fp2Params: Fp2Parameters<Fp = ConstraintF>,
630{
631}
632
633impl<P, ConstraintF: PrimeField> ConditionalEqGadget<ConstraintF> for Fp12Gadget<P, ConstraintF>
634where
635 P: Fp12Parameters,
636 <P::Fp6Params as Fp6Parameters>::Fp2Params: Fp2Parameters<Fp = ConstraintF>,
637{
638 #[inline]
639 fn conditional_enforce_equal<CS: ConstraintSystem<ConstraintF>>(
640 &self,
641 mut cs: CS,
642 other: &Self,
643 condition: &Boolean,
644 ) -> Result<(), SynthesisError> {
645 self.c0
646 .conditional_enforce_equal(&mut cs.ns(|| "c0"), &other.c0, condition)?;
647 self.c1
648 .conditional_enforce_equal(&mut cs.ns(|| "c1"), &other.c1, condition)?;
649 Ok(())
650 }
651
652 fn cost() -> usize {
653 2 * <Fp6Gadget<P, ConstraintF> as ConditionalEqGadget<ConstraintF>>::cost()
654 }
655}
656
657impl<P, ConstraintF: PrimeField> NEqGadget<ConstraintF> for Fp12Gadget<P, ConstraintF>
658where
659 P: Fp12Parameters,
660 <P::Fp6Params as Fp6Parameters>::Fp2Params: Fp2Parameters<Fp = ConstraintF>,
661{
662 #[inline]
663 fn enforce_not_equal<CS: ConstraintSystem<ConstraintF>>(
664 &self,
665 mut cs: CS,
666 other: &Self,
667 ) -> Result<(), SynthesisError> {
668 self.c0.enforce_not_equal(&mut cs.ns(|| "c0"), &other.c0)?;
669 self.c1.enforce_not_equal(&mut cs.ns(|| "c1"), &other.c1)?;
670 Ok(())
671 }
672
673 fn cost() -> usize {
674 2 * <Fp6Gadget<P, ConstraintF> as NEqGadget<ConstraintF>>::cost()
675 }
676}
677
678impl<P, ConstraintF: PrimeField> ToBitsGadget<ConstraintF> for Fp12Gadget<P, ConstraintF>
679where
680 P: Fp12Parameters,
681 <P::Fp6Params as Fp6Parameters>::Fp2Params: Fp2Parameters<Fp = ConstraintF>,
682{
683 fn to_bits<CS: ConstraintSystem<ConstraintF>>(
684 &self,
685 mut cs: CS,
686 ) -> Result<Vec<Boolean>, SynthesisError> {
687 let mut c0 = self.c0.to_bits(cs.ns(|| "c0"))?;
688 let mut c1 = self.c1.to_bits(cs.ns(|| "c1"))?;
689 c0.append(&mut c1);
690 Ok(c0)
691 }
692
693 fn to_non_unique_bits<CS: ConstraintSystem<ConstraintF>>(
694 &self,
695 mut cs: CS,
696 ) -> Result<Vec<Boolean>, SynthesisError> {
697 let mut c0 = self.c0.to_non_unique_bits(cs.ns(|| "c0"))?;
698 let mut c1 = self.c1.to_non_unique_bits(cs.ns(|| "c1"))?;
699 c0.append(&mut c1);
700 Ok(c0)
701 }
702}
703
704impl<P, ConstraintF: PrimeField> ToBytesGadget<ConstraintF> for Fp12Gadget<P, ConstraintF>
705where
706 P: Fp12Parameters,
707 <P::Fp6Params as Fp6Parameters>::Fp2Params: Fp2Parameters<Fp = ConstraintF>,
708{
709 fn to_bytes<CS: ConstraintSystem<ConstraintF>>(
710 &self,
711 mut cs: CS,
712 ) -> Result<Vec<UInt8>, SynthesisError> {
713 let mut c0 = self.c0.to_bytes(cs.ns(|| "c0"))?;
714 let mut c1 = self.c1.to_bytes(cs.ns(|| "c1"))?;
715 c0.append(&mut c1);
716 Ok(c0)
717 }
718
719 fn to_non_unique_bytes<CS: ConstraintSystem<ConstraintF>>(
720 &self,
721 mut cs: CS,
722 ) -> Result<Vec<UInt8>, SynthesisError> {
723 let mut c0 = self.c0.to_non_unique_bytes(cs.ns(|| "c0"))?;
724 let mut c1 = self.c1.to_non_unique_bytes(cs.ns(|| "c1"))?;
725 c0.append(&mut c1);
726 Ok(c0)
727 }
728}
729
730impl<P, ConstraintF: PrimeField> Clone for Fp12Gadget<P, ConstraintF>
731where
732 P: Fp12Parameters,
733 <P::Fp6Params as Fp6Parameters>::Fp2Params: Fp2Parameters<Fp = ConstraintF>,
734{
735 fn clone(&self) -> Self {
736 Self::new(self.c0.clone(), self.c1.clone())
737 }
738}
739
740impl<P, ConstraintF: PrimeField> CondSelectGadget<ConstraintF> for Fp12Gadget<P, ConstraintF>
741where
742 P: Fp12Parameters,
743 <P::Fp6Params as Fp6Parameters>::Fp2Params: Fp2Parameters<Fp = ConstraintF>,
744{
745 #[inline]
746 fn conditionally_select<CS: ConstraintSystem<ConstraintF>>(
747 mut cs: CS,
748 cond: &Boolean,
749 true_value: &Self,
750 false_value: &Self,
751 ) -> Result<Self, SynthesisError> {
752 let c0 = Fp6Gadget::<P, ConstraintF>::conditionally_select(
753 &mut cs.ns(|| "c0"),
754 cond,
755 &true_value.c0,
756 &false_value.c0,
757 )?;
758 let c1 = Fp6Gadget::<P, ConstraintF>::conditionally_select(
759 &mut cs.ns(|| "c1"),
760 cond,
761 &true_value.c1,
762 &false_value.c1,
763 )?;
764
765 Ok(Self::new(c0, c1))
766 }
767
768 fn cost() -> usize {
769 2 * <Fp6Gadget<P, ConstraintF> as CondSelectGadget<ConstraintF>>::cost()
770 }
771}
772
773impl<P, ConstraintF: PrimeField> TwoBitLookupGadget<ConstraintF> for Fp12Gadget<P, ConstraintF>
774where
775 P: Fp12Parameters,
776 <P::Fp6Params as Fp6Parameters>::Fp2Params: Fp2Parameters<Fp = ConstraintF>,
777{
778 type TableConstant = Fp12<P>;
779 fn two_bit_lookup<CS: ConstraintSystem<ConstraintF>>(
780 mut cs: CS,
781 b: &[Boolean],
782 c: &[Self::TableConstant],
783 ) -> Result<Self, SynthesisError> {
784 let c0s = c.iter().map(|f| f.c0).collect::<Vec<_>>();
785 let c1s = c.iter().map(|f| f.c1).collect::<Vec<_>>();
786 let c0 = Fp6Gadget::<P, ConstraintF>::two_bit_lookup(cs.ns(|| "Lookup c0"), b, &c0s)?;
787 let c1 = Fp6Gadget::<P, ConstraintF>::two_bit_lookup(cs.ns(|| "Lookup c1"), b, &c1s)?;
788 Ok(Self::new(c0, c1))
789 }
790
791 fn cost() -> usize {
792 2 * <Fp6Gadget<P, ConstraintF> as TwoBitLookupGadget<ConstraintF>>::cost()
793 }
794}
795
796impl<P, ConstraintF: PrimeField> ThreeBitCondNegLookupGadget<ConstraintF>
797 for Fp12Gadget<P, ConstraintF>
798where
799 P: Fp12Parameters,
800 <P::Fp6Params as Fp6Parameters>::Fp2Params: Fp2Parameters<Fp = ConstraintF>,
801{
802 type TableConstant = Fp12<P>;
803
804 fn three_bit_cond_neg_lookup<CS: ConstraintSystem<ConstraintF>>(
805 mut cs: CS,
806 b: &[Boolean],
807 b0b1: &Boolean,
808 c: &[Self::TableConstant],
809 ) -> Result<Self, SynthesisError> {
810 let c0s = c.iter().map(|f| f.c0).collect::<Vec<_>>();
811 let c1s = c.iter().map(|f| f.c1).collect::<Vec<_>>();
812 let c0 = Fp6Gadget::<P, ConstraintF>::three_bit_cond_neg_lookup(
813 cs.ns(|| "Lookup c0"),
814 b,
815 b0b1,
816 &c0s,
817 )?;
818 let c1 = Fp6Gadget::<P, ConstraintF>::three_bit_cond_neg_lookup(
819 cs.ns(|| "Lookup c1"),
820 b,
821 b0b1,
822 &c1s,
823 )?;
824 Ok(Self::new(c0, c1))
825 }
826
827 fn cost() -> usize {
828 2 * <Fp6Gadget<P, ConstraintF> as ThreeBitCondNegLookupGadget<ConstraintF>>::cost()
829 }
830}
831
832impl<P, ConstraintF: PrimeField> AllocGadget<Fp12<P>, ConstraintF> for Fp12Gadget<P, ConstraintF>
833where
834 P: Fp12Parameters,
835 <P::Fp6Params as Fp6Parameters>::Fp2Params: Fp2Parameters<Fp = ConstraintF>,
836{
837 #[inline]
838 fn alloc<F, T, CS: ConstraintSystem<ConstraintF>>(
839 mut cs: CS,
840 value_gen: F,
841 ) -> Result<Self, SynthesisError>
842 where
843 F: FnOnce() -> Result<T, SynthesisError>,
844 T: Borrow<Fp12<P>>,
845 {
846 let (c0, c1) = match value_gen() {
847 Ok(fe) => {
848 let fe = *fe.borrow();
849 (Ok(fe.c0), Ok(fe.c1))
850 },
851 Err(_) => (
852 Err(SynthesisError::AssignmentMissing),
853 Err(SynthesisError::AssignmentMissing),
854 ),
855 };
856
857 let c0 = Fp6Gadget::<P, ConstraintF>::alloc(&mut cs.ns(|| "c0"), || c0)?;
858 let c1 = Fp6Gadget::<P, ConstraintF>::alloc(&mut cs.ns(|| "c1"), || c1)?;
859 Ok(Self::new(c0, c1))
860 }
861
862 #[inline]
863 fn alloc_input<F, T, CS: ConstraintSystem<ConstraintF>>(
864 mut cs: CS,
865 value_gen: F,
866 ) -> Result<Self, SynthesisError>
867 where
868 F: FnOnce() -> Result<T, SynthesisError>,
869 T: Borrow<Fp12<P>>,
870 {
871 let (c0, c1) = match value_gen() {
872 Ok(fe) => {
873 let fe = *fe.borrow();
874 (Ok(fe.c0), Ok(fe.c1))
875 },
876 Err(_) => (
877 Err(SynthesisError::AssignmentMissing),
878 Err(SynthesisError::AssignmentMissing),
879 ),
880 };
881
882 let c0 = Fp6Gadget::<P, ConstraintF>::alloc_input(&mut cs.ns(|| "c0"), || c0)?;
883 let c1 = Fp6Gadget::<P, ConstraintF>::alloc_input(&mut cs.ns(|| "c1"), || c1)?;
884 Ok(Self::new(c0, c1))
885 }
886}