snarkvm_curves/templates/bls12/
g1.rs

1// Copyright (c) 2019-2025 Provable Inc.
2// This file is part of the snarkVM library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7
8// http://www.apache.org/licenses/LICENSE-2.0
9
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use crate::{
17    templates::{
18        bls12::Bls12Parameters,
19        short_weierstrass_jacobian::{Affine, Projective},
20    },
21    traits::AffineCurve,
22};
23use snarkvm_fields::Zero;
24use snarkvm_utilities::{FromBytes, ToBytes, serialize::*};
25
26use std::io::{Read, Result as IoResult, Write};
27
28pub type G1Affine<P> = Affine<<P as Bls12Parameters>::G1Parameters>;
29pub type G1Projective<P> = Projective<<P as Bls12Parameters>::G1Parameters>;
30
31#[derive(Clone, Debug, PartialEq, Eq, Hash, CanonicalSerialize, CanonicalDeserialize)]
32pub struct G1Prepared<P: Bls12Parameters>(pub G1Affine<P>);
33
34impl<P: Bls12Parameters> G1Prepared<P> {
35    pub fn is_zero(&self) -> bool {
36        self.0.is_zero()
37    }
38
39    pub fn from_affine(p: G1Affine<P>) -> Self {
40        G1Prepared(p)
41    }
42}
43
44impl<P: Bls12Parameters> Default for G1Prepared<P> {
45    fn default() -> Self {
46        G1Prepared(G1Affine::<P>::prime_subgroup_generator())
47    }
48}
49
50impl<P: Bls12Parameters> ToBytes for G1Prepared<P> {
51    fn write_le<W: Write>(&self, writer: W) -> IoResult<()> {
52        self.0.write_le(writer)
53    }
54}
55
56impl<P: Bls12Parameters> FromBytes for G1Prepared<P> {
57    fn read_le<R: Read>(reader: R) -> IoResult<Self> {
58        Ok(Self(G1Affine::<P>::read_le(reader)?))
59    }
60}