Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Small performance gain in allocations #7054

Merged
merged 1 commit into from
Nov 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 2 additions & 7 deletions ethstore/src/account/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

use std::iter::repeat;
use std::str;
use ethkey::Secret;
use {json, Error, crypto};
Expand Down Expand Up @@ -90,9 +89,7 @@ impl Crypto {
// preallocated (on-stack in case of `Secret`) buffer to hold cipher
// length = length(plain) as we are using CTR-approach
let plain_len = plain.len();
let mut ciphertext: SmallVec<[u8; 32]> = SmallVec::new();
ciphertext.grow(plain_len);
ciphertext.extend(repeat(0).take(plain_len));
let mut ciphertext: SmallVec<[u8; 32]> = SmallVec::from_vec(vec![0; plain_len]);

// aes-128-ctr with initial vector of iv
crypto::aes::encrypt(&derived_left_bits, &iv, plain, &mut *ciphertext);
Expand Down Expand Up @@ -143,9 +140,7 @@ impl Crypto {
return Err(Error::InvalidPassword);
}

let mut plain: SmallVec<[u8; 32]> = SmallVec::new();
plain.grow(expected_len);
plain.extend(repeat(0).take(expected_len));
let mut plain: SmallVec<[u8; 32]> = SmallVec::from_vec(vec![0; expected_len]);

match self.cipher {
Cipher::Aes128Ctr(ref params) => {
Expand Down
15 changes: 8 additions & 7 deletions rpc/src/v1/helpers/secretstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

use std::iter::repeat;
use rand::{Rng, OsRng};
use ethkey::{Public, Secret, math};
use crypto;
Expand All @@ -32,10 +31,13 @@ pub fn encrypt_document(key: Bytes, document: Bytes) -> Result<Bytes, Error> {

// use symmetric encryption to encrypt document
let iv = initialization_vector();
let mut encrypted_document = Vec::with_capacity(document.len() + iv.len());
encrypted_document.extend(repeat(0).take(document.len()));
crypto::aes::encrypt(&key, &iv, &document, &mut encrypted_document);
encrypted_document.extend_from_slice(&iv);
let mut encrypted_document = vec![0; document.len() + iv.len()];
{
let (mut encryption_buffer, iv_buffer) = encrypted_document.split_at_mut(document.len());

crypto::aes::encrypt(&key, &iv, &document, &mut encryption_buffer);
iv_buffer.copy_from_slice(&iv);
}

Ok(encrypted_document)
}
Expand All @@ -53,8 +55,7 @@ pub fn decrypt_document(key: Bytes, mut encrypted_document: Bytes) -> Result<Byt

// use symmetric decryption to decrypt document
let iv = encrypted_document.split_off(encrypted_document_len - INIT_VEC_LEN);
let mut document = Vec::with_capacity(encrypted_document_len - INIT_VEC_LEN);
document.extend(repeat(0).take(encrypted_document_len - INIT_VEC_LEN));
let mut document = vec![0; encrypted_document_len - INIT_VEC_LEN];
crypto::aes::decrypt(&key, &iv, &encrypted_document, &mut document);

Ok(document)
Expand Down