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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use super::{
    hash,
    poly::PolySize,
    pke::{Pke, PublicKey, SecretKey, Parameter},
};
use core::marker::PhantomData;
use rac::{
    Concat, LineValid,
    generic_array::{GenericArray, typenum::U32},
};
use sha3::digest::{Update, ExtendableOutput};
use pq_kem::Kem;

pub struct Cpa<N>(PhantomData<N>)
where
    N: PolySize;

#[derive(Clone)]
pub struct PublicKeyCpa<N>
where
    N: PolySize,
{
    pk: PublicKey<N>,
    parameter: Parameter<N>,
    seed: GenericArray<u8, U32>,
}

#[derive(Clone)]
pub struct SecretKeyCpa<N>
where
    N: PolySize,
{
    sk: SecretKey<N>,
}

#[derive(Clone)]
pub struct CipherTextCpa<N>
where
    N: PolySize,
{
    pk: PublicKey<N>,
    ct: GenericArray<u8, N::CompressedLength>,
}

impl<N, D> Kem<D> for Cpa<N>
where
    D: Default + Update + ExtendableOutput,
    N: PolySize,
    PublicKeyCpa<N>: LineValid,
    SecretKeyCpa<N>: LineValid,
    CipherTextCpa<N>: LineValid,
    Parameter<N>: Pke<
        Seed = U32,
        GenerationSeed = U32,
        Plain = U32,
        Cipher = N::CompressedLength,
        PublicKey = PublicKey<N>,
        SecretKey = SecretKey<N>,
    >,
{
    type PublicKey = PublicKeyCpa<N>;
    type SecretKey = SecretKeyCpa<N>;
    type CipherText = CipherTextCpa<N>;
    type PairSeedLength = U32;
    type PublicKeyHashLength = U32;
    type EncapsulationSeedLength = U32;
    type SharedSecretLength = U32;

    fn generate_pair(
        seed: &GenericArray<u8, Self::PairSeedLength>,
    ) -> (Self::PublicKey, Self::SecretKey) {
        let Concat(public_seed, noise_seed) = hash::h::<D, _, _>(&Concat(hash::B(1), seed.clone()));

        let parameter = Parameter::new(&public_seed);
        let (pk, sk) = parameter.generate(&noise_seed);
        (
            PublicKeyCpa {
                pk: pk,
                parameter: parameter,
                seed: seed.clone(),
            },
            SecretKeyCpa { sk: sk },
        )
    }

    fn encapsulate(
        seed: &GenericArray<u8, Self::EncapsulationSeedLength>,
        public_key: &Self::PublicKey,
        public_key_hash: &GenericArray<u8, Self::PublicKeyHashLength>,
    ) -> (Self::CipherText, GenericArray<u8, Self::SharedSecretLength>) {
        let _ = public_key_hash;
        let Concat(message, noise_seed) = hash::h::<D, _, _>(&Concat(hash::B(2), seed.clone()));
        let (pk, cipher) = public_key
            .parameter
            .encrypt(&noise_seed, &public_key.pk, &message);
        (CipherTextCpa { pk: pk, ct: cipher }, hash::h::<D, _, _>(&message))
    }

    fn decapsulate(
        secret_key: &Self::SecretKey,
        public_key_hash: &GenericArray<u8, Self::PublicKeyHashLength>,
        cipher_text: &Self::CipherText,
    ) -> GenericArray<u8, Self::SharedSecretLength> {
        let _ = public_key_hash;
        let message = Parameter::decrypt(&cipher_text.pk, &secret_key.sk, &cipher_text.ct);
        hash::h::<D, _, _>(&message)
    }
}

mod codable {
    #[rustfmt::skip]
    use super::{
        PolySize, Parameter, Pke,
        PublicKeyCpa, PublicKey,
        SecretKeyCpa, SecretKey,
        CipherTextCpa,
    };
    use rac::{
        LineValid, Concat,
        generic_array::{GenericArray, typenum::U32},
    };

    type PkBytes<N> = Concat<PublicKey<N>, GenericArray<u8, <Parameter<N> as Pke>::Seed>>;
    type CtBytes<N> = Concat<PublicKey<N>, GenericArray<u8, <N as PolySize>::CompressedLength>>;

    impl<N> LineValid for PublicKeyCpa<N>
    where
        N: PolySize,
        Parameter<N>: Pke<Seed = U32>,
        PublicKey<N>: Clone + LineValid,
        PkBytes<N>: LineValid,
    {
        type Length = <PkBytes<N> as LineValid>::Length;

        fn try_clone_array(a: &GenericArray<u8, Self::Length>) -> Result<Self, ()> {
            PkBytes::try_clone_array(a).map(|Concat(pk, seed)| PublicKeyCpa {
                pk: pk,
                parameter: Parameter::new(&seed),
                seed: seed,
            })
        }

        fn clone_line(&self) -> GenericArray<u8, Self::Length> {
            Concat(self.pk.clone(), self.seed.clone()).clone_line()
        }
    }

    impl<N> LineValid for SecretKeyCpa<N>
    where
        N: PolySize,
        SecretKey<N>: LineValid,
    {
        type Length = <SecretKey<N> as LineValid>::Length;

        fn try_clone_array(a: &GenericArray<u8, Self::Length>) -> Result<Self, ()> {
            SecretKey::try_clone_array(a).map(|sk| SecretKeyCpa { sk: sk })
        }

        fn clone_line(&self) -> GenericArray<u8, Self::Length> {
            self.sk.clone_line()
        }
    }

    impl<N> LineValid for CipherTextCpa<N>
    where
        N: PolySize,
        PublicKey<N>: Clone + LineValid,
        CtBytes<N>: LineValid,
    {
        type Length = <CtBytes<N> as LineValid>::Length;

        fn try_clone_array(a: &GenericArray<u8, Self::Length>) -> Result<Self, ()> {
            CtBytes::try_clone_array(a).map(|Concat(pk, ct)| CipherTextCpa { pk: pk, ct: ct })
        }

        fn clone_line(&self) -> GenericArray<u8, Self::Length> {
            Concat(self.pk.clone(), self.ct.clone()).clone_line()
        }
    }
}