jsonprooftoken/jpa/
algs.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 std::{fmt, str::FromStr};
16
17use serde::{Deserialize, Serialize};
18
19#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)]
20#[allow(non_camel_case_types)]
21pub enum ProofAlgorithm {
22    ///TODO: we need two algorithms (BLS12381_SHA256 and BLS12381_SHAKE256)
23    /// We should also distinguish between the BBS algorithm standard and the one extended
24    #[serde(rename = "BBS-BLS12381-SHA256")]
25    BLS12381_SHA256,
26    #[serde(rename = "BBS-BLS12381-SHAKE256")]
27    BLS12381_SHAKE256,
28
29    // #[serde(rename = "BBS-BLS12381-SHA256-PROOF")]
30    // BLS12381_SHA256_PROOF,
31    // #[serde(rename = "BBS-BLS12381-SHAKE256-PROOF")]
32    // BLS12381_SHAKE256_PROOF,
33    #[serde(rename = "SU-ES256")]
34    SU_ES256,
35    #[serde(rename = "MAC-H256")]
36    MAC_H256,
37    #[serde(rename = "MAC-H384")]
38    MAC_H384,
39    #[serde(rename = "MAC-H512")]
40    MAC_H512,
41    #[serde(rename = "MAC-K25519")]
42    MAC_K25519,
43    #[serde(rename = "MAC-K448")]
44    MAC_K448,
45    #[serde(rename = "MAC-H256K")]
46    MAC_H256K,
47}
48
49impl Into<PresentationProofAlgorithm> for ProofAlgorithm {
50    fn into(self) -> PresentationProofAlgorithm {
51        match self {
52            ProofAlgorithm::BLS12381_SHA256 => PresentationProofAlgorithm::BLS12381_SHA256_PROOF,
53            ProofAlgorithm::BLS12381_SHAKE256 => {
54                PresentationProofAlgorithm::BLS12381_SHAKE256_PROOF
55            }
56            ProofAlgorithm::SU_ES256 => PresentationProofAlgorithm::SU_ES256,
57            ProofAlgorithm::MAC_H256 => PresentationProofAlgorithm::MAC_H256,
58            ProofAlgorithm::MAC_H384 => PresentationProofAlgorithm::MAC_H384,
59            ProofAlgorithm::MAC_H512 => PresentationProofAlgorithm::MAC_H512,
60            ProofAlgorithm::MAC_K25519 => PresentationProofAlgorithm::MAC_K25519,
61            ProofAlgorithm::MAC_K448 => PresentationProofAlgorithm::MAC_K448,
62            ProofAlgorithm::MAC_H256K => PresentationProofAlgorithm::MAC_H256K,
63        }
64    }
65}
66
67impl fmt::Display for ProofAlgorithm {
68    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
69        let variant_str = match self {
70            ProofAlgorithm::BLS12381_SHA256 => "BBS-BLS12381-SHA256",
71            ProofAlgorithm::BLS12381_SHAKE256 => "BBS-BLS12381-SHAKE256",
72            ProofAlgorithm::SU_ES256 => "SU-ES256",
73            ProofAlgorithm::MAC_H256 => "MAC-H256",
74            ProofAlgorithm::MAC_H384 => "MAC-H384",
75            ProofAlgorithm::MAC_H512 => "MAC-H512",
76            ProofAlgorithm::MAC_K25519 => "MAC-K25519",
77            ProofAlgorithm::MAC_K448 => "MAC-K448",
78            ProofAlgorithm::MAC_H256K => "MAC-H256K",
79        };
80        write!(f, "{}", variant_str)
81    }
82}
83
84impl FromStr for ProofAlgorithm {
85    type Err = &'static str;
86
87    fn from_str(s: &str) -> Result<Self, Self::Err> {
88        match s {
89            "BBS-BLS12381-SHA256" => Ok(ProofAlgorithm::BLS12381_SHA256),
90            "BBS-BLS12381-SHAKE256" => Ok(ProofAlgorithm::BLS12381_SHAKE256),
91            "SU-ES256" => Ok(ProofAlgorithm::SU_ES256),
92            "MAC-H256" => Ok(ProofAlgorithm::MAC_H256),
93            "MAC-H384" => Ok(ProofAlgorithm::MAC_H384),
94            "MAC-H512" => Ok(ProofAlgorithm::MAC_H512),
95            "MAC-K25519" => Ok(ProofAlgorithm::MAC_K25519),
96            "MAC-K448" => Ok(ProofAlgorithm::MAC_K448),
97            "MAC-H256K" => Ok(ProofAlgorithm::MAC_H256K),
98            _ => Err("Invalid proof algorithm"),
99        }
100    }
101}
102
103#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)]
104#[allow(non_camel_case_types)]
105pub enum PresentationProofAlgorithm {
106    #[serde(rename = "BBS-BLS12381-SHA256-PROOF")]
107    BLS12381_SHA256_PROOF,
108    #[serde(rename = "BBS-BLS12381-SHAKE256-PROOF")]
109    BLS12381_SHAKE256_PROOF,
110
111    #[serde(rename = "SU-ES256")]
112    SU_ES256,
113    #[serde(rename = "MAC-H256")]
114    MAC_H256,
115    #[serde(rename = "MAC-H384")]
116    MAC_H384,
117    #[serde(rename = "MAC-H512")]
118    MAC_H512,
119    #[serde(rename = "MAC-K25519")]
120    MAC_K25519,
121    #[serde(rename = "MAC-K448")]
122    MAC_K448,
123    #[serde(rename = "MAC-H256K")]
124    MAC_H256K,
125}
126
127impl fmt::Display for PresentationProofAlgorithm {
128    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
129        let variant_str = match self {
130            PresentationProofAlgorithm::BLS12381_SHA256_PROOF => "BBS-BLS12381-SHA256-PROOF",
131            PresentationProofAlgorithm::BLS12381_SHAKE256_PROOF => "BBS-BLS12381-SHAKE256-PROOF",
132            PresentationProofAlgorithm::SU_ES256 => "SU-ES256",
133            PresentationProofAlgorithm::MAC_H256 => "MAC-H256",
134            PresentationProofAlgorithm::MAC_H384 => "MAC-H384",
135            PresentationProofAlgorithm::MAC_H512 => "MAC-H512",
136            PresentationProofAlgorithm::MAC_K25519 => "MAC-K25519",
137            PresentationProofAlgorithm::MAC_K448 => "MAC-K448",
138            PresentationProofAlgorithm::MAC_H256K => "MAC-H256K",
139        };
140        write!(f, "{}", variant_str)
141    }
142}
143
144impl FromStr for PresentationProofAlgorithm {
145    type Err = &'static str;
146
147    fn from_str(s: &str) -> Result<Self, Self::Err> {
148        match s {
149            "BBS-BLS12381-SHA256-PROOF" => Ok(PresentationProofAlgorithm::BLS12381_SHA256_PROOF),
150            "BBS-BLS12381-SHAKE256-PROOF" => {
151                Ok(PresentationProofAlgorithm::BLS12381_SHAKE256_PROOF)
152            }
153            "SU-ES256" => Ok(PresentationProofAlgorithm::SU_ES256),
154            "MAC-H256" => Ok(PresentationProofAlgorithm::MAC_H256),
155            "MAC-H384" => Ok(PresentationProofAlgorithm::MAC_H384),
156            "MAC-H512" => Ok(PresentationProofAlgorithm::MAC_H512),
157            "MAC-K25519" => Ok(PresentationProofAlgorithm::MAC_K25519),
158            "MAC-K448" => Ok(PresentationProofAlgorithm::MAC_K448),
159            "MAC-H256K" => Ok(PresentationProofAlgorithm::MAC_H256K),
160            _ => Err("Invalid proof algorithm"),
161        }
162    }
163}