1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0.

//! WeDPR traits definitions for replaceable algorithms.

use crate::error::WedprError;

/// Trait of a replaceable hash algorithm.
pub trait Hash {
    /// Generates a fixed length hash bytes vector from a bytes array of any
    /// length.
    fn hash<T: ?Sized + AsRef<[u8]>>(&self, input: &T) -> Vec<u8>;
}

/// Trait of a replaceable coder algorithm.
pub trait Coder {
    /// Converts bytes to an encoded string.
    fn encode<T: ?Sized + AsRef<[u8]>>(&self, input: &T) -> String;
    /// Decodes an encoded string to a bytes vector.
    fn decode(&self, input: &str) -> Result<Vec<u8>, WedprError>;
}

/// Trait of a replaceable elliptic curve integrated encryption scheme (ECIES)
/// algorithm.
pub trait Ecies {
    /// Encrypts a message by ECIES with a public key.
    fn encrypt<T: ?Sized + AsRef<[u8]>>(
        &self,
        public_key: &T,
        message: &T,
    ) -> Result<Vec<u8>, WedprError>;

    /// Decrypts a ciphertext by ECIES with a private key.
    fn decrypt<T: ?Sized + AsRef<[u8]>>(
        &self,
        private_key: &T,
        ciphertext: &T,
    ) -> Result<Vec<u8>, WedprError>;
}

/// Trait of a replaceable signature algorithm.
pub trait Signature {
    /// Signs a message hash with the private key.
    fn sign<T: ?Sized + AsRef<[u8]>>(
        &self,
        private_key: &T,
        msg_hash: &T,
    ) -> Result<Vec<u8>, WedprError>;

    /// Verifies a message hash with the public key.
    fn verify<T: ?Sized + AsRef<[u8]>>(
        &self,
        public_key: &T,
        msg_hash: &T,
        signature: &T,
    ) -> bool;

    /// Generates a new key pair for signature algorithm,
    /// where the first part is public key,
    /// the second part is private key.
    // TODO: Replace output list with a struct.
    fn generate_keypair(&self) -> (Vec<u8>, Vec<u8>);
}

/// Trait of a replaceable verifiable random function (VRF) algorithm, which is
/// the public-key version of a keyed cryptographic hash.
pub trait Vrf {
    /// Encodes a VRF proof to bytes.
    fn encode_proof(&self) -> Vec<u8>;

    /// Decode a VRF proof from bytes.
    fn decode_proof<T: ?Sized + AsRef<[u8]>>(
        proof: &T,
    ) -> Result<Self, WedprError>
    where Self: Sized;

    /// Proves a keyed VRF hash with a message and a private key.
    fn prove<T: ?Sized + AsRef<[u8]>>(
        private_key: &T,
        message: &T,
    ) -> Result<Self, WedprError>
    where
        Self: Sized;

    /// Proves a keyed VRF hash with a message faster with both the private and
    /// public keys.
    fn prove_fast<T: ?Sized + AsRef<[u8]>>(
        private_key: &T,
        public_key: &T,
        message: &T,
    ) -> Result<Self, WedprError>
    where
        Self: Sized;

    /// Verifies a keyed VRF hash with a message and its public key.
    fn verify<T: ?Sized + AsRef<[u8]>>(
        &self,
        public_key: &T,
        message: &T,
    ) -> bool;

    /// Derives a VRF public key from a private key.
    fn derive_public_key<T: ?Sized + AsRef<[u8]>>(private_key: &T) -> Vec<u8>;

    /// Hashes a VRF proof to bytes.
    fn proof_to_hash(&self) -> Result<Vec<u8>, WedprError>;

    /// Checks the validity of a VRF public key.
    fn is_valid_public_key<T: ?Sized + AsRef<[u8]>>(public_key: &T) -> bool;
}

/// Trait of a replaceable block cipher algorithm.
pub trait BlockCipher {
    /// Encrypts a message with a symmetric key and an initialization vector
    /// (IV).
    fn encrypt<T: ?Sized + AsRef<[u8]>>(
        &self,
        message: &T,
        key: &T,
        iv: &T,
    ) -> Result<Vec<u8>, WedprError>;

    /// Decrypts a cipher with a symmetric key and an initialization vector
    /// (IV).
    fn decrypt<T: ?Sized + AsRef<[u8]>>(
        &self,
        ciphertext: &T,
        key: &T,
        iv: &T,
    ) -> Result<Vec<u8>, WedprError>;

    /// Generates a new key for block cipher algorithm.
    fn generate_key(&self) -> Vec<u8>;

    /// Generates a new random initialization vector.
    fn generate_iv(&self) -> Vec<u8>;
}