jsonprooftoken/jwk/
curves.rs

1// Copyright 2023 Fondazione LINKS
2
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6
7//     http://www.apache.org/licenses/LICENSE-2.0
8
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use serde::{Deserialize, Serialize};
16use std::{fmt, str::FromStr};
17
18use crate::errors::CustomError;
19
20#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
21pub enum EllipticCurveTypes {
22    #[serde(rename = "P-256")]
23    P256,
24    #[serde(rename = "P-384")]
25    P384,
26    #[serde(rename = "P-521")]
27    P521,
28    Ed25519,
29    Ed448,
30    X25519,
31    X448,
32    #[serde(rename = "secp256k1")]
33    Secp256K1,
34
35    BLS12381G1,
36    BLS12381G2, //Only one supported
37    BLS48581G1,
38    BLS48581G2,
39}
40
41impl FromStr for EllipticCurveTypes {
42    type Err = CustomError;
43
44    fn from_str(s: &str) -> Result<Self, Self::Err> {
45        match s {
46            "BLS12381G2" => Ok(EllipticCurveTypes::BLS12381G2),
47            _ => Err(CustomError::CurveNotSupported),
48        }
49    }
50}
51
52impl fmt::Display for EllipticCurveTypes {
53    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54        let variant_str = match self {
55            EllipticCurveTypes::P256 => "P-256",
56            EllipticCurveTypes::P384 => "P-384",
57            EllipticCurveTypes::P521 => "P-521",
58            EllipticCurveTypes::Ed25519 => "Ed25519",
59            EllipticCurveTypes::Ed448 => "Ed448",
60            EllipticCurveTypes::X25519 => "X25519",
61            EllipticCurveTypes::X448 => "X448",
62            EllipticCurveTypes::Secp256K1 => "secp256k1",
63            EllipticCurveTypes::BLS12381G1 => "BLS12381G1",
64            EllipticCurveTypes::BLS12381G2 => "BLS12381G2",
65            EllipticCurveTypes::BLS48581G1 => "BLS48581G1",
66            EllipticCurveTypes::BLS48581G2 => "BLS48581G2",
67        };
68        write!(f, "{}", variant_str)
69    }
70}