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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
// Copyright 2018 The Exonum Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Utilities for managing the secret state of a wallet.

use exonum::crypto::{gen_keypair, CryptoHash, PublicKey, SecretKey};

use std::fmt;

use super::CONFIG;
use crypto::{enc, Commitment, Opening, SimpleRangeProof};
use storage::WalletInfo;
use transactions::{Accept, CreateWallet, Transfer};

lazy_static! {
    /// Opening to a minimum transfer amount.
    static ref MIN_TRANSFER_OPENING: Opening = Opening::with_no_blinding(CONFIG.min_transfer_amount);
}

encoding_struct! {
    /// Encrypted information embedded into transfers.
    ///
    /// # Implementation note
    ///
    /// Using byte slices for `nonce` and `encrypted_data` is a simplification; they both
    /// have known constant byte size, so it could make sense to be more type-safe.
    struct EncryptedData {
        /// Cryptographic nonce for the `box` routine from `libsodium`.
        nonce: &[u8],
        /// Data encrypted with the `box` routine from `libsodium`.
        encrypted_data: &[u8],
    }
}

impl EncryptedData {
    /// Encrypts data based on sender’s private encryption key
    /// and the receiver’s public one.
    fn seal(message: &[u8], receiver: &enc::PublicKey, sender_sk: &enc::SecretKey) -> Self {
        let nonce = enc::gen_nonce();
        let encrypted_data = enc::seal(message, &nonce, receiver, sender_sk);

        EncryptedData::new(nonce.as_ref(), &encrypted_data)
    }

    /// Decrypts data based on sender’s public encryption key
    /// and the receiver’s secret one.
    fn open(&self, sender: &enc::PublicKey, receiver_sk: &enc::SecretKey) -> Option<Vec<u8>> {
        let nonce = enc::Nonce::from_slice(self.nonce())?;
        enc::open(self.encrypted_data(), &nonce, sender, receiver_sk).ok()
    }

    /// Decrypts data based on sender’s private encryption key
    /// and the receiver’s public one.
    // This is possible as `box` uses Diffie-Hellman key exchange to derive a shared secret
    // for encryption with a symmetric cipher. It’s enough to know one secret key to restore
    // this shared secret.
    fn open_as_sender(
        &self,
        receiver: &enc::PublicKey,
        sender_sk: &enc::SecretKey,
    ) -> Option<Vec<u8>> {
        let nonce = enc::Nonce::from_slice(self.nonce())?;
        let precomputed_key = enc::precompute(receiver, sender_sk);
        enc::open_precomputed(self.encrypted_data(), &nonce, &precomputed_key).ok()
    }
}

/// Secret state of an account owner.
///
/// # Usage
///
/// `SecretState` can be used to create transactions originating from the wallet.
/// The state also should be updated based on the wallet history, which can be retrieved
/// with [HTTP API]. Each transaction in the history should be applied to the state
/// exactly once.
///
/// [HTTP API]: ::api::Api::wallet()
pub struct SecretState {
    encryption_sk: enc::SecretKey,
    signing_key: SecretKey,

    // We save verifying key for efficiency reasons.
    verifying_key: PublicKey,

    // This `Opening` is why `SecretState` is needed: we need to be able to open
    // the commitment to the wallet balance, which is stored in the blockchain,
    // in order to produce `Transfer`s and possibly for other tasks (such as proving
    // bounds on the balance to off-chain parties). If the opening is lost,
    // the wallet owner can no longer perform these tasks. Fortunately, with the given
    // design, it’s always possible (and quite easy) to restore the opening from scratch
    // provided that the owner knows the secret key to the wallet; indeed, it’s enough
    // to download wallet history anew and replay it.
    balance_opening: Opening,

    history_len: u64,
}

impl fmt::Debug for SecretState {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter
            .debug_struct("SecretState")
            .field("verifying_key", &self.verifying_key)
            .finish()
    }
}

/// Information about an incoming transfer successfully verified w.r.t. the `SecretState`
/// of the receiver’s wallet.
#[derive(Debug)]
pub struct VerifiedTransfer {
    /// Opening for the transferred amount.
    pub opening: Opening,
    /// `Accept` transaction for the transfer.
    pub accept: Accept,
}

impl VerifiedTransfer {
    /// Gets the transferred amount in plaintext.
    pub fn value(&self) -> u64 {
        self.opening.value
    }
}

impl SecretState {
    /// Creates an uninitialized state. The keypair for cryptographic operations
    /// is generated randomly.
    pub fn with_random_keypair() -> Self {
        let (verifying_key, signing_key) = gen_keypair();
        Self::from_keypair(verifying_key, signing_key)
    }

    /// Creates an uninitialized state from the specified Ed25519 keypair.
    pub fn from_keypair(verifying_key: PublicKey, signing_key: SecretKey) -> Self {
        let (_, encryption_sk) = enc::keypair_from_ed25519(verifying_key, signing_key.clone());
        SecretState {
            verifying_key,
            signing_key,
            encryption_sk,
            balance_opening: Opening::with_no_blinding(0),
            history_len: 0,
        }
    }

    /// Gets the public key of the wallet (aka verifying Ed25519 key for digital signatures).
    pub fn public_key(&self) -> &PublicKey {
        &self.verifying_key
    }

    /// Gets the current wallet balance.
    pub fn balance(&self) -> u64 {
        self.balance_opening.value
    }

    /// Produces a `CreateWallet` transaction for this wallet.
    pub fn create_wallet(&self) -> CreateWallet {
        CreateWallet::new(&self.verifying_key, &self.signing_key)
    }

    /// Produces a `Transfer` transaction from this wallet to the specified receiver.
    ///
    /// # Panics
    ///
    /// This method will panic if the transfer violates constraints imposed by the transaction
    /// logic of the service:
    ///
    /// - `amount` is out of bounds specified by service [`CONFIG`]
    /// - `receiver` is same as the sender
    /// - `rollback_delay` is not within acceptable range
    ///
    /// [`CONFIG`]: ::CONFIG
    pub fn create_transfer(
        &self,
        amount: u64,
        receiver: &PublicKey,
        rollback_delay: u32,
    ) -> Transfer {
        Transfer::create(amount, receiver, rollback_delay, self).expect("creating transfer failed")
    }

    /// Initializes the state.
    ///
    /// # Safety
    ///
    /// This method should be called after `CreateWallet` transaction is committed. It should
    /// only be called once.
    pub fn initialize(&mut self) {
        assert_eq!(self.history_len, 0);
        debug_assert_eq!(self.balance_opening, Opening::with_no_blinding(0));
        self.balance_opening = Opening::with_no_blinding(CONFIG.initial_balance);
        self.history_len = 1;
    }

    /// Verifies an incoming transfer.
    ///
    /// # Return value
    ///
    /// Returns the decrypted opening for the transferred amount, or `None` if it cannot
    /// be decrypted from the transfer.
    pub fn verify_transfer(&self, transfer: &Transfer) -> Option<VerifiedTransfer> {
        if self.verifying_key == *transfer.to() {
            let sender = enc::pk_from_ed25519(*transfer.from());
            let opening = transfer
                .encrypted_data()
                .open(&sender, &self.encryption_sk)?;

            let accept = Accept::new(&self.verifying_key, &transfer.hash(), &self.signing_key);
            Some(VerifiedTransfer {
                opening: Opening::from_slice(&opening)?,
                accept,
            })
        } else {
            None
        }
    }

    /// Updates the state according to a `Transfer` transaction.
    ///
    /// # Safety
    ///
    /// The transfer is assumed to be previously [verified] or originating from self.
    /// It is also assumed to be sourced from the blockchain (i.e., verified according
    /// to the blockchain rules).
    ///
    /// [verified]: #method.verify
    pub fn transfer(&mut self, transfer: &Transfer) {
        if self.verifying_key == *transfer.from() {
            let receiver = enc::pk_from_ed25519(*transfer.to());
            let opening = transfer
                .encrypted_data()
                .open_as_sender(&receiver, &self.encryption_sk)
                .expect("cannot decrypt own message");
            let opening = Opening::from_slice(&opening).expect("cannot parse own message");
            self.balance_opening -= opening;
        } else if self.verifying_key == *transfer.to() {
            let sender = enc::pk_from_ed25519(*transfer.from());
            let opening = transfer
                .encrypted_data()
                .open(&sender, &self.encryption_sk)
                .expect("cannot decrypt message");
            let opening = Opening::from_slice(&opening).expect("cannot parse message");
            self.balance_opening += opening;
        } else {
            panic!("unrelated transfer");
        }

        self.history_len += 1;
    }

    /// Rolls back a previously committed transfer.
    ///
    /// # Safety
    ///
    /// The transfer is assumed to be originating from the blockchain and rolled back
    /// according to the wallet history.
    pub fn rollback(&mut self, transfer: &Transfer) {
        if self.verifying_key == *transfer.from() {
            let receiver = enc::pk_from_ed25519(*transfer.to());
            let opening = transfer
                .encrypted_data()
                .open_as_sender(&receiver, &self.encryption_sk)
                .expect("cannot decrypt own message");
            let opening = Opening::from_slice(&opening).expect("cannot parse own message");
            self.balance_opening += opening;
        } else {
            panic!("unrelated transfer");
        }
        self.history_len += 1;
    }

    /// Checks if this state corresponds to the supplied public info about a `Wallet`.
    pub fn corresponds_to(&self, wallet: &WalletInfo) -> bool {
        wallet.public_key == self.verifying_key && wallet.balance.verify(&self.balance_opening)
    }

    /// Produces a public info about the state.
    pub fn to_public(&self) -> WalletInfo {
        WalletInfo {
            public_key: self.verifying_key,
            balance: Commitment::from_opening(&self.balance_opening),
        }
    }
}

impl Transfer {
    /// Creates a new transfer.
    fn create(
        amount: u64,
        receiver: &PublicKey,
        rollback_delay: u32,
        sender_secrets: &SecretState,
    ) -> Option<Self> {
        assert!(CONFIG.rollback_delay_bounds.start <= rollback_delay);
        assert!(rollback_delay < CONFIG.rollback_delay_bounds.end);
        assert!(amount >= CONFIG.min_transfer_amount);
        assert!(sender_secrets.balance_opening.value >= amount);
        assert_ne!(receiver, sender_secrets.public_key());

        let (committed_amount, opening) = Commitment::new(amount);
        let amount_proof = SimpleRangeProof::prove(&(&opening - &MIN_TRANSFER_OPENING))?;
        let remaining_balance = &sender_secrets.balance_opening - &opening;
        let sufficient_balance_proof = SimpleRangeProof::prove(&remaining_balance)?;
        let encrypted_data = EncryptedData::seal(
            &opening.to_bytes(),
            &enc::pk_from_ed25519(*receiver),
            &sender_secrets.encryption_sk,
        );

        Some(Transfer::new(
            &sender_secrets.verifying_key,
            receiver,
            rollback_delay,
            sender_secrets.history_len,
            committed_amount,
            amount_proof,
            sufficient_balance_proof,
            encrypted_data,
            &sender_secrets.signing_key,
        ))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use exonum::blockchain::Transaction;

    fn gen_wallet(balance: u64) -> SecretState {
        let mut secrets = SecretState::with_random_keypair();
        secrets.balance_opening = Opening::with_no_blinding(balance);
        secrets
    }

    #[test]
    fn can_open_encrypted_data() {
        const MSG: &[u8] = b"hello";

        let sender = gen_wallet(100);
        let sender_pk = sender.to_public().encryption_key();
        let receiver = gen_wallet(100);
        let receiver_pk = receiver.to_public().encryption_key();

        let encrypted_data = EncryptedData::seal(MSG, &receiver_pk, &sender.encryption_sk);
        assert_eq!(
            encrypted_data.open(&sender_pk, &receiver.encryption_sk),
            Some(MSG.to_vec())
        );
        assert_eq!(
            encrypted_data.open_as_sender(&receiver_pk, &sender.encryption_sk),
            Some(MSG.to_vec())
        );
    }

    #[test]
    fn transfer_verifies() {
        let sender_sec = gen_wallet(100);
        let sender = sender_sec.to_public();
        let receiver_sec = gen_wallet(50);
        let receiver = receiver_sec.to_public();

        let transfer =
            Transfer::create(42, &receiver.public_key, 10, &sender_sec).expect("transfer");
        assert!(transfer.verify_stateless());
        assert!(transfer.verify_stateful(&sender.balance));

        let opening = transfer
            .encrypted_data()
            .open(&sender.encryption_key(), &receiver_sec.encryption_sk)
            .expect("decrypt");
        let opening = Opening::from_slice(&opening).expect("opening");
        assert_eq!(opening.value, 42);
        assert!(transfer.amount().verify(&opening));

        let opening = transfer
            .encrypted_data()
            .open_as_sender(&receiver.encryption_key(), &sender_sec.encryption_sk)
            .expect("decrypt");
        let opening = Opening::from_slice(&opening).expect("opening");
        assert_eq!(opening.value, 42);
        assert!(transfer.amount().verify(&opening));
    }

    #[test]
    fn transfer_with_small_amount_does_not_verify() {
        let sender_sec = gen_wallet(100);
        let (receiver, _) = gen_keypair();
        let (committed_amount, opening) = Commitment::new(0);

        // This intentionally deviates from the proper procedure - we don’t subtract
        // `MIN_AMOUNT_OPENING` from the `opening`.
        let amount_proof = SimpleRangeProof::prove(&opening).expect("prove amount");

        let remaining_balance = &sender_sec.balance_opening - &opening;
        let sufficient_balance_proof =
            SimpleRangeProof::prove(&remaining_balance).expect("prove balance");
        let encrypted_data = EncryptedData::seal(
            &opening.to_bytes(),
            &enc::pk_from_ed25519(receiver),
            &sender_sec.encryption_sk,
        );

        let transfer = Transfer::new(
            &sender_sec.verifying_key,
            &receiver,
            10, // rollback delay
            1,  // history length
            committed_amount,
            amount_proof,
            sufficient_balance_proof,
            encrypted_data,
            &sender_sec.signing_key,
        );
        assert!(!transfer.verify());
    }
}