decaf377/ark_curve/
element.rs

1use ark_ec::{AffineRepr, CurveGroup, Group, ScalarMul, VariableBaseMSM};
2use ark_serialize::Valid;
3use ark_std::vec::Vec;
4
5use crate::{
6    ark_curve::{edwards::EdwardsAffine, Decaf377EdwardsConfig, EdwardsProjective},
7    Fq, Fr,
8};
9
10pub mod affine;
11pub mod projective;
12
13pub use affine::AffinePoint;
14pub use projective::Element;
15
16impl Valid for Element {
17    fn check(&self) -> Result<(), ark_serialize::SerializationError> {
18        Ok(())
19    }
20}
21
22impl ScalarMul for Element {
23    type MulBase = AffinePoint;
24
25    const NEGATION_IS_CHEAP: bool = true;
26
27    fn batch_convert_to_mul_base(bases: &[Self]) -> Vec<Self::MulBase> {
28        let bases_inner = bases.iter().map(|g| g.inner).collect::<Vec<_>>();
29        let result = EdwardsProjective::batch_convert_to_mul_base(&bases_inner[..]);
30        result
31            .into_iter()
32            .map(|g| AffinePoint { inner: g })
33            .collect::<Vec<_>>()
34    }
35}
36
37impl VariableBaseMSM for Element {}
38
39impl Group for Element {
40    type ScalarField = Fr;
41
42    fn double_in_place(&mut self) -> &mut Self {
43        let inner = *self.inner.double_in_place();
44        *self = Element { inner };
45        self
46    }
47
48    fn generator() -> Self {
49        Self::GENERATOR
50    }
51
52    fn mul_bigint(&self, other: impl AsRef<[u64]>) -> Self {
53        let inner = self.inner.mul_bigint(other);
54        Element { inner }
55    }
56}
57
58impl CurveGroup for Element {
59    // We implement `CurveGroup` as it is required by the `CurveVar`
60    // trait used in the R1CS feature. The `ProjectiveCurve` trait requires
61    // an affine representation of `Element` to be defined, and `AffineRepr`
62    // to be implemented on that type.
63    type Config = Decaf377EdwardsConfig;
64
65    type BaseField = Fq;
66
67    type Affine = AffinePoint;
68
69    // This type is supposed to represent an element of the entire elliptic
70    // curve group, not just the prime-order subgroup. Since this is decaf,
71    // this is just an `Element` again.
72    type FullGroup = AffinePoint;
73
74    fn normalize_batch(v: &[Self]) -> Vec<AffinePoint> {
75        let v_inner = v.iter().map(|g| g.inner).collect::<Vec<_>>();
76        let result = EdwardsProjective::normalize_batch(&v_inner[..]);
77        result
78            .into_iter()
79            .map(|g| AffinePoint { inner: g })
80            .collect::<Vec<_>>()
81    }
82
83    fn into_affine(self) -> Self::Affine {
84        self.into()
85    }
86}
87
88impl Valid for AffinePoint {
89    fn check(&self) -> Result<(), ark_serialize::SerializationError> {
90        Ok(())
91    }
92}
93
94impl AffineRepr for AffinePoint {
95    type Config = Decaf377EdwardsConfig;
96
97    type ScalarField = Fr;
98
99    type BaseField = Fq;
100
101    type Group = Element;
102
103    fn xy(&self) -> Option<(&Self::BaseField, &Self::BaseField)> {
104        self.inner.xy()
105    }
106
107    fn zero() -> Self {
108        AffinePoint {
109            inner: EdwardsAffine::zero(),
110        }
111    }
112
113    fn generator() -> Self {
114        Element::GENERATOR.into()
115    }
116
117    fn from_random_bytes(bytes: &[u8]) -> Option<Self> {
118        EdwardsAffine::from_random_bytes(bytes).map(|inner| AffinePoint { inner })
119    }
120
121    fn mul_bigint(&self, other: impl AsRef<[u64]>) -> Self::Group {
122        Element {
123            inner: self.inner.mul_bigint(other),
124        }
125    }
126
127    fn clear_cofactor(&self) -> Self {
128        // This is decaf so we're just returning the same point.
129        *self
130    }
131
132    fn mul_by_cofactor_to_group(&self) -> Self::Group {
133        self.into()
134    }
135}
136
137impl From<Element> for AffinePoint {
138    fn from(point: Element) -> Self {
139        Self {
140            inner: point.inner.into(),
141        }
142    }
143}
144
145impl From<AffinePoint> for Element {
146    fn from(point: AffinePoint) -> Self {
147        Self {
148            inner: point.inner.into(),
149        }
150    }
151}
152
153impl From<&Element> for AffinePoint {
154    fn from(point: &Element) -> Self {
155        Self {
156            inner: point.inner.into(),
157        }
158    }
159}
160
161impl From<&AffinePoint> for Element {
162    fn from(point: &AffinePoint) -> Self {
163        Self {
164            inner: point.inner.into(),
165        }
166    }
167}