Skip to main content

airbender_crypto/bls12_381/curves/
g2.rs

1use core::ops::Neg;
2
3use ark_ec::{
4    bls12,
5    bls12::Bls12Config,
6    hashing::curve_maps::wb::{IsogenyMap, WBConfig},
7    models::CurveConfig,
8    scalar_mul::glv::GLVConfig,
9    short_weierstrass::{Affine, Projective, SWCurveConfig},
10    AffineRepr, CurveGroup, PrimeGroup,
11};
12use ark_ff::{AdditiveGroup, Field, PrimeField, Zero};
13use ark_serialize::{Compress, SerializationError};
14
15#[cfg(any(
16    all(target_arch = "riscv32", feature = "bigint_ops"),
17    test,
18    feature = "proving"
19))]
20use crate::ark_ff_delegation::{BigIntMacro as BigInt, MontFp};
21#[cfg(not(any(
22    all(target_arch = "riscv32", feature = "bigint_ops"),
23    test,
24    feature = "proving"
25)))]
26use ark_ff::{BigInt, MontFp};
27
28use super::{
29    g2_swu_iso,
30    util::{serialize_fq, EncodingFlags, G2_SERIALIZED_SIZE},
31};
32use crate::bls12_381::{
33    util::{read_g2_compressed, read_g2_uncompressed},
34    *,
35};
36
37pub type G2Affine = bls12::G2Affine<crate::bls12_381::curves::Config>;
38pub type G2Projective = bls12::G2Projective<crate::bls12_381::curves::Config>;
39
40#[derive(Clone, Default, PartialEq, Eq)]
41pub struct Config;
42
43impl CurveConfig for Config {
44    type BaseField = Fq2;
45    type ScalarField = Fr;
46
47    /// COFACTOR = (x^8 - 4 x^7 + 5 x^6) - (4 x^4 + 6 x^3 - 4 x^2 - 4 x + 13) //
48    /// 9
49    /// = 305502333931268344200999753193121504214466019254188142667664032982267604182971884026507427359259977847832272839041616661285803823378372096355777062779109
50    #[rustfmt::skip]
51    const COFACTOR: &'static [u64] = &[
52        0xcf1c38e31c7238e5,
53        0x1616ec6e786f0c70,
54        0x21537e293a6691ae,
55        0xa628f1cb4d9e82ef,
56        0xa68a205b2e5a7ddf,
57        0xcd91de4547085aba,
58        0x91d50792876a202,
59        0x5d543a95414e7f1,
60    ];
61
62    /// COFACTOR_INV = COFACTOR^{-1} mod r
63    /// 26652489039290660355457965112010883481355318854675681319708643586776743290055
64    const COFACTOR_INV: Fr =
65        MontFp!("26652489039290660355457965112010883481355318854675681319708643586776743290055");
66}
67
68impl SWCurveConfig for Config {
69    /// COEFF_A = [0, 0]
70    const COEFF_A: Fq2 = Fq2::new(g1::Config::COEFF_A, g1::Config::COEFF_A);
71
72    /// COEFF_B = [4, 4]
73    const COEFF_B: Fq2 = Fq2::new(g1::Config::COEFF_B, g1::Config::COEFF_B);
74
75    /// AFFINE_GENERATOR_COEFFS = (G2_GENERATOR_X, G2_GENERATOR_Y)
76    const GENERATOR: G2Affine = G2Affine::new_unchecked(G2_GENERATOR_X, G2_GENERATOR_Y);
77
78    #[inline(always)]
79    fn mul_by_a(_: Self::BaseField) -> Self::BaseField {
80        Self::BaseField::zero()
81    }
82
83    fn is_in_correct_subgroup_assuming_on_curve(point: &G2Affine) -> bool {
84        // Algorithm from Section 4 of https://eprint.iacr.org/2021/1130.
85        //
86        // Checks that [p]P = [X]P
87
88        let mut x_times_point = point.mul_bigint(crate::bls12_381::curves::Config::X);
89        if crate::bls12_381::curves::Config::X_IS_NEGATIVE {
90            x_times_point = -x_times_point;
91        }
92
93        let p_times_point = p_power_endomorphism(point);
94
95        x_times_point.eq(&p_times_point)
96    }
97
98    #[inline]
99    fn clear_cofactor(p: &G2Affine) -> G2Affine {
100        // Based on Section 4.1 of https://eprint.iacr.org/2017/419.pdf
101        // [h(ψ)]P = [x^2 − x − 1]P + [x − 1]ψ(P) + (ψ^2)(2P)
102
103        // x = -15132376222941642752
104        // When multiplying, use -c1 instead, and then negate the result. That's much
105        // more efficient, since the scalar -c1 has less limbs and a much lower Hamming
106        // weight.
107        let x: &'static [u64] = crate::bls12_381::curves::Config::X;
108        let p_projective = p.into_group();
109
110        // [x]P
111        let x_p = Config::mul_affine(p, &x).neg();
112        // ψ(P)
113        let psi_p = p_power_endomorphism(&p);
114        // (ψ^2)(2P)
115        let mut psi2_p2 = double_p_power_endomorphism(&p_projective.double());
116
117        // tmp = [x]P + ψ(P)
118        let mut tmp = x_p.clone();
119        tmp += &psi_p;
120
121        // tmp2 = [x^2]P + [x]ψ(P)
122        let mut tmp2: Projective<Config> = tmp;
123        tmp2 = tmp2.mul_bigint(x).neg();
124
125        // add up all the terms
126        psi2_p2 += tmp2;
127        psi2_p2 -= x_p;
128        psi2_p2 += &-psi_p;
129        (psi2_p2 - p_projective).into_affine()
130    }
131
132    fn deserialize_with_mode<R: ark_serialize::Read>(
133        mut reader: R,
134        compress: ark_serialize::Compress,
135        validate: ark_serialize::Validate,
136    ) -> Result<Affine<Self>, ark_serialize::SerializationError> {
137        let p = if compress == ark_serialize::Compress::Yes {
138            read_g2_compressed(&mut reader)?
139        } else {
140            read_g2_uncompressed(&mut reader)?
141        };
142
143        if validate == ark_serialize::Validate::Yes && !p.is_in_correct_subgroup_assuming_on_curve()
144        {
145            return Err(SerializationError::InvalidData);
146        }
147        Ok(p)
148    }
149
150    fn serialize_with_mode<W: ark_serialize::Write>(
151        item: &Affine<Self>,
152        mut writer: W,
153        compress: ark_serialize::Compress,
154    ) -> Result<(), SerializationError> {
155        let encoding = EncodingFlags {
156            is_compressed: compress == ark_serialize::Compress::Yes,
157            is_infinity: item.is_zero(),
158            is_lexographically_largest: item.y > -item.y,
159        };
160        let mut p = *item;
161        if encoding.is_infinity {
162            p = G2Affine::zero();
163        }
164
165        let mut x_bytes = [0u8; G2_SERIALIZED_SIZE];
166        let c1_bytes = serialize_fq(p.x.c1);
167        let c0_bytes = serialize_fq(p.x.c0);
168        x_bytes[0..48].copy_from_slice(&c1_bytes[..]);
169        x_bytes[48..96].copy_from_slice(&c0_bytes[..]);
170        if encoding.is_compressed {
171            let mut bytes: [u8; G2_SERIALIZED_SIZE] = x_bytes;
172
173            encoding.encode_flags(&mut bytes);
174            writer.write_all(&bytes)?;
175        } else {
176            let mut bytes = [0u8; 2 * G2_SERIALIZED_SIZE];
177
178            let mut y_bytes = [0u8; G2_SERIALIZED_SIZE];
179            let c1_bytes = serialize_fq(p.y.c1);
180            let c0_bytes = serialize_fq(p.y.c0);
181            y_bytes[0..48].copy_from_slice(&c1_bytes[..]);
182            y_bytes[48..96].copy_from_slice(&c0_bytes[..]);
183            bytes[0..G2_SERIALIZED_SIZE].copy_from_slice(&x_bytes);
184            bytes[G2_SERIALIZED_SIZE..].copy_from_slice(&y_bytes);
185
186            encoding.encode_flags(&mut bytes);
187            writer.write_all(&bytes)?;
188        };
189
190        Ok(())
191    }
192
193    fn serialized_size(compress: ark_serialize::Compress) -> usize {
194        if compress == Compress::Yes {
195            G2_SERIALIZED_SIZE
196        } else {
197            2 * G2_SERIALIZED_SIZE
198        }
199    }
200}
201
202impl GLVConfig for Config {
203    const ENDO_COEFFS: &'static[Self::BaseField] = &[
204        Fq2::new(
205            MontFp!("793479390729215512621379701633421447060886740281060493010456487427281649075476305620758731620350"),
206            Fq::ZERO
207        )
208    ];
209
210    const LAMBDA: Self::ScalarField = MontFp!("228988810152649578064853576960394133503");
211
212    const SCALAR_DECOMP_COEFFS: [(bool, <Self::ScalarField as PrimeField>::BigInt); 4] = [
213        (false, BigInt!("228988810152649578064853576960394133503")),
214        (true, BigInt!("1")),
215        (false, BigInt!("1")),
216        (false, BigInt!("228988810152649578064853576960394133504")),
217    ];
218
219    fn endomorphism(p: &Projective<Self>) -> Projective<Self> {
220        let mut res = (*p).clone();
221        res.x *= Self::ENDO_COEFFS[0];
222        res
223    }
224
225    fn endomorphism_affine(p: &Affine<Self>) -> Affine<Self> {
226        let mut res = (*p).clone();
227        res.x *= Self::ENDO_COEFFS[0];
228        res
229    }
230}
231
232pub const G2_GENERATOR_X: Fq2 = Fq2::new(G2_GENERATOR_X_C0, G2_GENERATOR_X_C1);
233pub const G2_GENERATOR_Y: Fq2 = Fq2::new(G2_GENERATOR_Y_C0, G2_GENERATOR_Y_C1);
234
235/// G2_GENERATOR_X_C0 =
236/// 352701069587466618187139116011060144890029952792775240219908644239793785735715026873347600343865175952761926303160
237pub const G2_GENERATOR_X_C0: Fq = MontFp!("352701069587466618187139116011060144890029952792775240219908644239793785735715026873347600343865175952761926303160");
238
239/// G2_GENERATOR_X_C1 =
240/// 3059144344244213709971259814753781636986470325476647558659373206291635324768958432433509563104347017837885763365758
241pub const G2_GENERATOR_X_C1: Fq = MontFp!("3059144344244213709971259814753781636986470325476647558659373206291635324768958432433509563104347017837885763365758");
242
243/// G2_GENERATOR_Y_C0 =
244/// 1985150602287291935568054521177171638300868978215655730859378665066344726373823718423869104263333984641494340347905
245pub const G2_GENERATOR_Y_C0: Fq = MontFp!("1985150602287291935568054521177171638300868978215655730859378665066344726373823718423869104263333984641494340347905");
246
247/// G2_GENERATOR_Y_C1 =
248/// 927553665492332455747201965776037880757740193453592970025027978793976877002675564980949289727957565575433344219582
249pub const G2_GENERATOR_Y_C1: Fq = MontFp!("927553665492332455747201965776037880757740193453592970025027978793976877002675564980949289727957565575433344219582");
250
251// PSI_X = 1/(u+1)^((p-1)/3)
252const P_POWER_ENDOMORPHISM_COEFF_0 : Fq2 = Fq2::new(
253    Fq::ZERO,
254    MontFp!(
255                "4002409555221667392624310435006688643935503118305586438271171395842971157480381377015405980053539358417135540939437"
256    )
257);
258
259// PSI_Y = 1/(u+1)^((p-1)/2)
260const P_POWER_ENDOMORPHISM_COEFF_1: Fq2 = Fq2::new(
261    MontFp!(
262                "2973677408986561043442465346520108879172042883009249989176415018091420807192182638567116318576472649347015917690530"),
263    MontFp!(
264       "1028732146235106349975324479215795277384839936929757896155643118032610843298655225875571310552543014690878354869257")
265);
266
267// PSI_2_X = (u+1)^((1-p^2)/3)
268const DOUBLE_P_POWER_ENDOMORPHISM_COEFF_0: Fq2 = Fq2::new(
269    MontFp!("4002409555221667392624310435006688643935503118305586438271171395842971157480381377015405980053539358417135540939436"),
270    Fq::ZERO
271);
272
273/// psi(P) is the untwist-Frobenius-twist endomorphism on E'(Fq2)
274fn p_power_endomorphism(p: &Affine<Config>) -> Affine<Config> {
275    // The p-power endomorphism for G2 is defined as follows:
276    // 1. Note that G2 is defined on curve E': y^2 = x^3 + 4(u+1).
277    //    To map a point (x, y) in E' to (s, t) in E,
278    //    set s = x / ((u+1) ^ (1/3)), t = y / ((u+1) ^ (1/2)),
279    //    because E: y^2 = x^3 + 4.
280    // 2. Apply the Frobenius endomorphism (s, t) => (s', t'),
281    //    another point on curve E, where s' = s^p, t' = t^p.
282    // 3. Map the point from E back to E'; that is,
283    //    set x' = s' * ((u+1) ^ (1/3)), y' = t' * ((u+1) ^ (1/2)).
284    //
285    // To sum up, it maps
286    // (x,y) -> (x^p / ((u+1)^((p-1)/3)), y^p / ((u+1)^((p-1)/2)))
287    // as implemented in the code as follows.
288
289    let mut res = *p;
290    res.x.frobenius_map_in_place(1);
291    res.y.frobenius_map_in_place(1);
292
293    let tmp_x = res.x.clone();
294    res.x.c0 = -P_POWER_ENDOMORPHISM_COEFF_0.c1 * &tmp_x.c1;
295    res.x.c1 = P_POWER_ENDOMORPHISM_COEFF_0.c1 * &tmp_x.c0;
296    res.y *= P_POWER_ENDOMORPHISM_COEFF_1;
297
298    res
299}
300
301/// For a p-power endomorphism psi(P), compute psi(psi(P))
302fn double_p_power_endomorphism(p: &Projective<Config>) -> Projective<Config> {
303    let mut res = *p;
304
305    res.x *= DOUBLE_P_POWER_ENDOMORPHISM_COEFF_0;
306    res.y = res.y.neg();
307
308    res
309}
310
311// Parameters from the [IETF draft v16, section E.3](https://www.ietf.org/archive/id/draft-irtf-cfrg-hash-to-curve-16.html#name-3-isogeny-map-for-bls12-381).
312impl WBConfig for Config {
313    type IsogenousCurve = g2_swu_iso::SwuIsoConfig;
314
315    const ISOGENY_MAP: IsogenyMap<'static, Self::IsogenousCurve, Self> =
316        g2_swu_iso::ISOGENY_MAP_TO_G2;
317}