1use std::ops::{Add, Neg, Sub};
2
3use bn::{Fr, Group, G1, G2};
4use rand::Rng;
5
6use crate::{
7 error::{Error, Result},
8 utils,
9};
10
11pub struct PrivateKey(pub Fr);
13
14impl PrivateKey {
15 pub fn random<R>(rng: &mut R) -> Self
17 where
18 R: Rng,
19 {
20 let private_key = Fr::random(rng);
22
23 Self(private_key)
24 }
25
26 pub fn to_bytes(&self) -> Result<Vec<u8>> {
28 utils::fr_to_bytes(self.into())
29 }
30}
31
32impl TryFrom<&[u8]> for PrivateKey {
33 type Error = Error;
34
35 fn try_from(private_key: &[u8]) -> Result<Self, Self::Error> {
36 Ok(PrivateKey(Fr::from_slice(private_key)?))
37 }
38}
39
40impl From<PrivateKey> for Fr {
41 fn from(private_key: PrivateKey) -> Self {
42 private_key.0
43 }
44}
45
46impl From<&PrivateKey> for Fr {
47 fn from(private_key: &PrivateKey) -> Self {
48 private_key.0
49 }
50}
51
52#[derive(Copy, Clone, Debug)]
54pub struct PublicKey(pub G2);
55
56impl PublicKey {
57 pub fn from_private_key(private_key: &PrivateKey) -> Self {
59 Self(G2::one() * private_key.into())
60 }
61
62 pub fn from_compressed<T: AsRef<[u8]>>(bytes: T) -> Result<Self> {
65 Ok(Self(G2::from_compressed(bytes.as_ref())?))
66 }
67
68 pub fn from_uncompressed<T: AsRef<[u8]>>(bytes: T) -> Result<Self> {
71 Ok(Self(utils::from_uncompressed_to_g2(bytes.as_ref())?))
72 }
73
74 pub fn to_compressed(&self) -> Result<Vec<u8>> {
77 utils::g2_to_compressed(self.into())
78 }
79
80 pub fn to_uncompressed(&self) -> Result<Vec<u8>> {
83 utils::g2_to_uncompressed(self.into())
84 }
85}
86
87impl From<PublicKey> for G2 {
88 fn from(public_key: PublicKey) -> Self {
89 public_key.0
90 }
91}
92
93impl From<&PublicKey> for G2 {
94 fn from(public_key: &PublicKey) -> Self {
95 public_key.0
96 }
97}
98
99impl Add for PublicKey {
100 type Output = Self;
101
102 fn add(self, other: Self) -> Self::Output {
103 Self(self.0.add(other.0))
104 }
105}
106
107impl Sub for PublicKey {
108 type Output = Self;
109
110 fn sub(self, other: Self) -> Self {
111 Self(self.0 - other.0)
112 }
113}
114
115impl Neg for PublicKey {
116 type Output = Self;
117
118 fn neg(self) -> Self {
119 Self(-self.0)
120 }
121}
122
123pub struct PublicKeyG1(pub G1);
125
126impl PublicKeyG1 {
127 pub fn from_private_key(private_key: &PrivateKey) -> Self {
129 Self(G1::one() * private_key.into())
130 }
131
132 pub fn to_compressed(&self) -> Result<Vec<u8>> {
135 utils::g1_to_compressed(self.0)
136 }
137
138 pub fn from_compressed<T: AsRef<[u8]>>(bytes: T) -> Result<Self> {
141 Ok(Self(G1::from_compressed(bytes.as_ref())?))
142 }
143}
144
145impl From<PublicKeyG1> for G1 {
146 fn from(public_key: PublicKeyG1) -> Self {
147 public_key.0
148 }
149}
150
151impl From<&PublicKeyG1> for G1 {
152 fn from(public_key: &PublicKeyG1) -> Self {
153 public_key.0
154 }
155}
156
157impl Add for PublicKeyG1 {
158 type Output = Self;
159
160 fn add(self, other: Self) -> Self::Output {
161 Self(self.0.add(other.0))
162 }
163}
164
165impl Sub for PublicKeyG1 {
166 type Output = Self;
167
168 fn sub(self, other: Self) -> Self {
169 Self(self.0 - other.0)
170 }
171}
172
173impl Neg for PublicKeyG1 {
174 type Output = Self;
175
176 fn neg(self) -> Self {
177 Self(-self.0)
178 }
179}
180
181#[derive(Copy, Clone, Debug)]
183pub struct Signature(pub G1);
184
185impl Signature {
186 pub fn to_compressed(&self) -> Result<Vec<u8>> {
189 utils::g1_to_compressed(self.0)
190 }
191
192 pub fn from_compressed<T: AsRef<[u8]>>(bytes: T) -> Result<Self> {
195 let uncompressed = G1::from_compressed(bytes.as_ref())?;
196
197 Ok(Self(uncompressed))
198 }
199}
200
201impl From<Signature> for G1 {
202 fn from(signature: Signature) -> Self {
203 signature.0
204 }
205}
206
207impl From<&Signature> for G1 {
208 fn from(signature: &Signature) -> Self {
209 signature.0
210 }
211}
212
213impl Add for Signature {
214 type Output = Self;
215
216 fn add(self, other: Self) -> Self::Output {
217 Self(self.0.add(other.0))
218 }
219}
220
221impl Sub for Signature {
222 type Output = Self;
223
224 fn sub(self, other: Self) -> Self {
225 Self(self.0 - other.0)
226 }
227}
228
229impl Neg for Signature {
230 type Output = Self;
231
232 fn neg(self) -> Self {
233 Self(-self.0)
234 }
235}