|
1 | 1 | use blst::min_pk::{AggregatePublicKey as RawAggregatePublicKey, PublicKey as RawPublicKey}; |
2 | 2 | use derive_more::From; |
3 | 3 |
|
4 | | -use crate::{Error, PublicKeyBytes}; |
| 4 | +use bls_core::{error::Error, traits::PublicKey as PublicKeyTrait}; |
| 5 | + |
| 6 | +use super::public_key_bytes::PublicKeyBytes; |
5 | 7 |
|
6 | 8 | #[derive(Clone, Copy, PartialEq, Eq, Default, Debug, From)] |
7 | 9 | pub struct PublicKey(RawPublicKey); |
8 | 10 |
|
9 | | -impl From<PublicKey> for PublicKeyBytes { |
10 | | - #[inline] |
11 | | - fn from(public_key: PublicKey) -> Self { |
12 | | - Self(public_key.as_raw().compress()) |
13 | | - } |
14 | | -} |
15 | | - |
16 | 11 | impl TryFrom<PublicKeyBytes> for PublicKey { |
17 | 12 | type Error = Error; |
18 | 13 |
|
19 | 14 | #[inline] |
20 | 15 | fn try_from(bytes: PublicKeyBytes) -> Result<Self, Self::Error> { |
21 | | - let raw = RawPublicKey::uncompress(bytes.as_bytes())?; |
| 16 | + let raw = |
| 17 | + RawPublicKey::uncompress(bytes.as_bytes()).map_err(|_| Error::InvalidPublicKey)?; |
22 | 18 |
|
23 | 19 | // This is needed to pass `fast_aggregate_verify` tests. |
24 | 20 | // See the following for more information: |
25 | 21 | // - <https://github.com/supranational/blst/issues/11> |
26 | 22 | // - <https://github.com/ethereum/consensus-specs/releases/tag/v1.0.0> |
27 | | - raw.validate()?; |
| 23 | + raw.validate().map_err(|_| Error::InvalidPublicKey)?; |
28 | 24 |
|
29 | 25 | Ok(Self(raw)) |
30 | 26 | } |
31 | 27 | } |
32 | 28 |
|
33 | | -impl PublicKey { |
34 | | - /// [`eth_aggregate_pubkeys`](https://github.com/ethereum/consensus-specs/blob/86fb82b221474cc89387fa6436806507b3849d88/specs/altair/bls.md#eth_aggregate_pubkeys) |
35 | | - pub fn aggregate_nonempty(public_keys: impl IntoIterator<Item = Self>) -> Result<Self, Error> { |
36 | | - public_keys |
37 | | - .into_iter() |
38 | | - .reduce(Self::aggregate) |
39 | | - .ok_or(Error::NoPublicKeysToAggregate) |
40 | | - } |
41 | | - |
42 | | - #[inline] |
43 | | - #[must_use] |
44 | | - pub fn aggregate(mut self, other: Self) -> Self { |
45 | | - self.aggregate_in_place(other); |
46 | | - self |
47 | | - } |
| 29 | +impl PublicKeyTrait for PublicKey { |
| 30 | + type PublicKeyBytes = PublicKeyBytes; |
48 | 31 |
|
49 | 32 | #[inline] |
50 | | - pub fn aggregate_in_place(&mut self, other: Self) { |
| 33 | + fn aggregate_in_place(&mut self, other: Self) { |
51 | 34 | let mut self_aggregate = RawAggregatePublicKey::from_public_key(self.as_raw()); |
52 | 35 | let other_aggregate = RawAggregatePublicKey::from_public_key(other.as_raw()); |
53 | 36 | self_aggregate.add_aggregate(&other_aggregate); |
54 | 37 | self.0 = self_aggregate.to_public_key(); |
55 | 38 | } |
| 39 | +} |
56 | 40 |
|
| 41 | +impl PublicKey { |
57 | 42 | pub(crate) const fn as_raw(&self) -> &RawPublicKey { |
58 | 43 | &self.0 |
59 | 44 | } |
|
0 commit comments