From patchwork Mon Mar 21 13:26:07 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolai Stange X-Patchwork-Id: 8632901 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Original-To: patchwork-linux-crypto@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 2EF58C0553 for ; Mon, 21 Mar 2016 13:27:09 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 4C1F720304 for ; Mon, 21 Mar 2016 13:27:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 4E7B520306 for ; Mon, 21 Mar 2016 13:27:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755839AbcCUN0s (ORCPT ); Mon, 21 Mar 2016 09:26:48 -0400 Received: from mail-wm0-f65.google.com ([74.125.82.65]:34728 "EHLO mail-wm0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755468AbcCUN0n (ORCPT ); Mon, 21 Mar 2016 09:26:43 -0400 Received: by mail-wm0-f65.google.com with SMTP id p65so22326609wmp.1; Mon, 21 Mar 2016 06:26:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=0yv8iEDJshx971+jdOBaMPf2ZUrtOnhFMF8tf8Fzwgs=; b=f+EV5lwjQ6rNwU8GhyIiI4QOPxVRkMwWhK2WmiboYysR4YK1V/E2fLGyiAEbgEGGHm 0F5hVfOHxv3RAsLlROiwKziB77cwvSZAD6upkEwgj6QwYgKc/9500yBTM3y7AHHMU8sG TPMVZNqquaR0Y+1klPPfnTBrs7uyysf46BCdS7cX8izsk9UcpBqIQEQzYKd+KYm1rOXM 8GntiIcBrTeJ84izXa0Yzjn097SPSAh9nEsEexaNJGWGrda3cBisWWgXMxNJMztX4O7m Qn1rIL4nwPSlqXz3H1MxjDd1DKLgA/+koLQ9BW4KZqkxhjsezC9fob5Oy+HknvCbr9P+ iIGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=0yv8iEDJshx971+jdOBaMPf2ZUrtOnhFMF8tf8Fzwgs=; b=B4janqdnNleqFJjxgGco9svHphwXGoHil93OMzjQpu/4jPMYlw4tFVApohXS2rPLrb 6GyF1kHQmqdBfQBGUughdcjHa+t4vSjNi112HFMrwlDhtK4NvuoGzC2X1gGTI2Y1Lgrf s/RyitAU/ACycXq14SvhUnKQIjLBlr2m/77xEEMm5xKn6Nnsqkc1sd50Pvl1uCb6ETRa nV5Arw9a8Ma+R94PrYla5eWwqUePh0JBqESF5wpqckQEf97+R7JHo8IVX6symUI3vIkP EoV55W7QytYfp7iJqtquy5zDS1sEHlKOowi+pc6amZeg1AWaUkHs+iszif8bCMKIxo9i glAQ== X-Gm-Message-State: AD7BkJKFtrha8tBFmjsDg7FL3iHJ45gNQYWyD9elV0yQox5BYrNveCQN4d3zdLPwRaEgIw== X-Received: by 10.194.92.107 with SMTP id cl11mr34310090wjb.21.1458566802399; Mon, 21 Mar 2016 06:26:42 -0700 (PDT) Received: from localhost (x55b18fc0.dyn.telefonica.de. [85.177.143.192]) by smtp.gmail.com with ESMTPSA id b1sm25363122wjy.0.2016.03.21.06.26.41 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 21 Mar 2016 06:26:42 -0700 (PDT) From: Nicolai Stange To: Herbert Xu , "David S. Miller" Cc: Tadeusz Struk , Michal Marek , Andrzej Zaborowski , Stephan Mueller , Arnd Bergmann , linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, Nicolai Stange Subject: [PATCH RESEND v2 06/14] lib/mpi: mpi_read_buffer(): optimize skipping of leading zero limbs Date: Mon, 21 Mar 2016 14:26:07 +0100 Message-Id: <1458566775-5239-7-git-send-email-nicstange@gmail.com> X-Mailer: git-send-email 2.7.3 In-Reply-To: <1458566775-5239-1-git-send-email-nicstange@gmail.com> References: <1458566775-5239-1-git-send-email-nicstange@gmail.com> Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Currently, if the number of leading zeros is greater than fits into a complete limb, mpi_read_buffer() skips them by iterating over them limb-wise. Instead of skipping the high order zero limbs within the loop as shown above, adjust the copying loop's bounds. Signed-off-by: Nicolai Stange --- lib/mpi/mpicoder.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/lib/mpi/mpicoder.c b/lib/mpi/mpicoder.c index 623439e..2fd8d41 100644 --- a/lib/mpi/mpicoder.c +++ b/lib/mpi/mpicoder.c @@ -184,7 +184,9 @@ int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes, p = buf; *nbytes = n - lzeros; - for (i = a->nlimbs - 1; i >= 0; i--) { + for (i = a->nlimbs - 1 - lzeros / BYTES_PER_MPI_LIMB, + lzeros %= BYTES_PER_MPI_LIMB; + i >= 0; i--) { alimb = a->d[i]; #if BYTES_PER_MPI_LIMB == 4 *p++ = alimb >> 24; @@ -205,15 +207,11 @@ int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes, #endif if (lzeros > 0) { - if (lzeros >= sizeof(alimb)) { - p -= sizeof(alimb); - } else { - mpi_limb_t *limb1 = (void *)p - sizeof(alimb); - mpi_limb_t *limb2 = (void *)p - sizeof(alimb) - + lzeros; - *limb1 = *limb2; - p -= lzeros; - } + mpi_limb_t *limb1 = (void *)p - sizeof(alimb); + mpi_limb_t *limb2 = (void *)p - sizeof(alimb) + + lzeros; + *limb1 = *limb2; + p -= lzeros; lzeros -= sizeof(alimb); } }