entropy_shared/
types.rs

1// Copyright (C) 2023 Entropy Cryptography Inc.
2// This program is free software: you can redistribute it and/or modify
3// it under the terms of the GNU Affero General Public License as published by
4// the Free Software Foundation, either version 3 of the License, or
5// (at your option) any later version.
6//
7// This program is distributed in the hope that it will be useful,
8// but WITHOUT ANY WARRANTY; without even the implied warranty of
9// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10// GNU Affero General Public License for more details.
11//
12// You should have received a copy of the GNU Affero General Public License
13// along with this program.  If not, see <https://www.gnu.org/licenses/>.
14
15#![allow(dead_code)]
16use super::constants::VERIFICATION_KEY_LENGTH;
17use blake2::{Blake2b512, Digest};
18#[cfg(not(feature = "wasm"))]
19use codec::alloc::vec::Vec;
20use codec::{Decode, Encode};
21use scale_info::TypeInfo;
22#[cfg(any(feature = "std", feature = "wasm"))]
23use serde::{Deserialize, Serialize};
24#[cfg(feature = "std")]
25use strum_macros::EnumIter;
26
27/// X25519 public key used by the client in non-interactive ECDH to authenticate/encrypt
28/// interactions with the threshold server (eg distributing threshold shares).
29pub type X25519PublicKey = [u8; 32];
30
31/// This should match the type found in `entropy-runtime`. We define it ourselves manually here
32/// since we don't want to pull that whole crate it just for a `u32`.
33pub type BlockNumber = u32;
34
35/// Information from the validators in signing party
36#[cfg_attr(not(feature = "wasm"), derive(sp_runtime::Serialize, sp_runtime::Deserialize))]
37#[derive(Clone, Encode, Decode, Debug, Eq, PartialEq, TypeInfo)]
38pub struct ValidatorInfo {
39    pub x25519_public_key: X25519PublicKey,
40    pub ip_address: codec::alloc::vec::Vec<u8>,
41    pub tss_account: codec::alloc::vec::Vec<u8>,
42}
43
44/// Offchain worker message for initiating the initial jumpstart DKG
45#[cfg(not(feature = "wasm"))]
46#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
47#[derive(Clone, Encode, Decode, Debug, Eq, PartialEq, TypeInfo)]
48pub struct OcwMessageDkg {
49    pub block_number: BlockNumber,
50    pub validators_info: Vec<ValidatorInfo>,
51}
52
53/// Offchain worker message for initiating a refresh
54#[cfg(not(feature = "wasm"))]
55#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
56#[derive(Clone, Encode, Decode, Debug, Eq, PartialEq, TypeInfo)]
57pub struct OcwMessageReshare {
58    // Stash address of new signer
59    pub new_signer: Vec<u8>,
60    pub block_number: BlockNumber,
61}
62
63/// Offchain worker message for initiating a proactive refresh
64#[cfg(not(feature = "wasm"))]
65#[derive(
66    Clone,
67    Encode,
68    Decode,
69    Debug,
70    Eq,
71    PartialEq,
72    TypeInfo,
73    sp_runtime::Serialize,
74    sp_runtime::Deserialize,
75)]
76pub struct OcwMessageProactiveRefresh {
77    pub block_number: BlockNumber,
78    /// Information of the validators to participate
79    pub validators_info: Vec<ValidatorInfo>,
80    /// Accounts to take part in the proactive refresh
81    pub proactive_refresh_keys: Vec<Vec<u8>>,
82}
83
84/// Offchain worker message for requesting a TDX attestation
85#[cfg(not(feature = "wasm"))]
86#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
87#[derive(Clone, Encode, Decode, Debug, Eq, PartialEq, TypeInfo)]
88pub struct OcwMessageAttestationRequest {
89    /// The account ids of all TSS servers who must submit an attestation this block
90    pub tss_account_ids: Vec<[u8; 32]>,
91}
92
93/// 256-bit hashing algorithms for deriving the point to be signed.
94#[cfg_attr(any(feature = "wasm", feature = "std"), derive(Serialize, Deserialize))]
95#[cfg_attr(feature = "std", derive(EnumIter))]
96#[derive(Clone, Debug, Eq, PartialEq)]
97#[cfg_attr(feature = "std", serde(rename = "hash"))]
98#[cfg_attr(feature = "std", serde(rename_all = "lowercase"))]
99#[non_exhaustive]
100pub enum HashingAlgorithm {
101    Sha1,
102    Sha2,
103    Sha3,
104    Keccak,
105    Blake2_256,
106    Custom(usize),
107}
108
109/// A compressed, serialized [synedrion::ecdsa::VerifyingKey<k256::Secp256k1>]
110pub type EncodedVerifyingKey = [u8; VERIFICATION_KEY_LENGTH as usize];
111
112#[cfg(not(feature = "wasm"))]
113pub type BoundedVecEncodedVerifyingKey =
114    sp_runtime::BoundedVec<u8, sp_runtime::traits::ConstU32<VERIFICATION_KEY_LENGTH>>;
115
116/// Input data to be included in a TDX attestation
117pub struct QuoteInputData(pub [u8; 64]);
118
119impl QuoteInputData {
120    pub fn new<T: Encode>(
121        tss_account_id: T,
122        x25519_public_key: X25519PublicKey,
123        nonce: [u8; 32],
124        block_number: u32,
125    ) -> Self {
126        let mut hasher = Blake2b512::new();
127        hasher.update(tss_account_id.encode());
128        hasher.update(x25519_public_key);
129        hasher.update(nonce);
130        hasher.update(block_number.to_be_bytes());
131        Self(hasher.finalize().into())
132    }
133}
134
135/// A trait used to get different stored keys for a given account ID.
136///
137/// Not every account ID will have an given key, in which case the implementer is expected to
138/// return `None`.
139pub trait KeyProvider<T> {
140    /// Get an X25519 public key, if any, for the given account ID.
141    fn x25519_public_key(account_id: &T) -> Option<X25519PublicKey>;
142
143    /// Get a provisioning certification key, if any, for the given account ID.
144    fn provisioning_key(account_id: &T) -> Option<EncodedVerifyingKey>;
145}
146
147/// A trait used to describe a queue of attestations.
148pub trait AttestationQueue<T> {
149    /// Indicate that a given attestation is ready to be moved from a pending state to a confirmed
150    /// state.
151    fn confirm_attestation(account_id: &T);
152
153    /// Request that an attestation get added to the queue for later processing.
154    fn push_pending_attestation(
155        signer: T,
156        tss_account: T,
157        x25519_public_key: X25519PublicKey,
158        endpoint: Vec<u8>,
159        provisioning_certification_key: EncodedVerifyingKey,
160    );
161
162    /// The list of pending (not processed) attestations.
163    fn pending_attestations() -> Vec<T>;
164}