From patchwork Tue Mar 22 12:12:40 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolai Stange X-Patchwork-Id: 8641781 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 40B19C0553 for ; Tue, 22 Mar 2016 12:14:38 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 5BB572037F for ; Tue, 22 Mar 2016 12:14:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 779CD2035B for ; Tue, 22 Mar 2016 12:14:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1758584AbcCVMOW (ORCPT ); Tue, 22 Mar 2016 08:14:22 -0400 Received: from mail-wm0-f68.google.com ([74.125.82.68]:35432 "EHLO mail-wm0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1758306AbcCVMOF (ORCPT ); Tue, 22 Mar 2016 08:14:05 -0400 Received: by mail-wm0-f68.google.com with SMTP id r129so17283723wmr.2; Tue, 22 Mar 2016 05:14:03 -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=4MwkVUQk15DHwLakt+aTiW0Km5+w0CqVwYcOBpHNhto=; b=skTzxelu9tp5Pdd8mohue4s9BhFaXYca21+gPFDPh9O4L+vIXrZFWTKd1tuKc3kdbv MELh9j5qjn2YquBYQ1pJHae/Uk4d5Jk+fyxqtDJo0I7QneKE+lB6OLeywseHQf5EFrbE Vye/ox/G4CyOV6DftBmO68LYllYkB/q90p/uXzZYQ0sauHHuKQL7s+rpGr7DkxsVAz0K oYJMLN15P88GQxGerVD+YFHGpoj6MNanmBF3IVy6C6UC+w/vk58QjsLtuDS3mn87zvx3 /z3PT9yuxf2iCUCOC28C1jAn84a2MHZQ4H7jOx+Z/zgVbUx+MdHhxhqN4EoJT/SyPtvu m7rA== 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=4MwkVUQk15DHwLakt+aTiW0Km5+w0CqVwYcOBpHNhto=; b=AfuY9cNVtV0CkhYK1nabfDWEBVSAgpz8L8MzWlXW1nrmPpcyTmCbdZW1DuHj5AQs9b 1iB8JPNkTX/ryqyHIdyNCxjKP1FXhnoN3jbJqFMqAEMrjX9UmxcJ8lCAWbB4xHoyEPpx v3j8vBIG1khzJXc0yXqGP2MjbKP2y+t6xyJJnlXz3+niTBmV2J9ZWB4J44TGp/bPavL3 4y7KnoxZHhmYwlXrtrAYnbhRzdUW6GpFLH03aesB/mqgXM4fnumNcAS4dXgf1hDd3MbQ TjizL0r/5r4yggZY00S75bah4AIANgnz4MpJEoazdYC82UOTrmH2qV1sX/yjeRtiQUal Mxgw== X-Gm-Message-State: AD7BkJK+c4fOJgaQJ73dPWd8MtrQnqihTGvhctOH38r7Gqema80tMzNZAplHfa3k7Li1hQ== X-Received: by 10.194.216.2 with SMTP id om2mr35785437wjc.164.1458648842958; Tue, 22 Mar 2016 05:14:02 -0700 (PDT) Received: from localhost (x55b18fc0.dyn.telefonica.de. [85.177.143.192]) by smtp.gmail.com with ESMTPSA id w15sm17047612wmd.10.2016.03.22.05.14.02 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 22 Mar 2016 05:14:02 -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 v3 06/14] lib/mpi: mpi_read_buffer(): optimize skipping of leading zero limbs Date: Tue, 22 Mar 2016 13:12:40 +0100 Message-Id: <1458648768-1469-7-git-send-email-nicstange@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1458648768-1469-1-git-send-email-nicstange@gmail.com> References: <1458648768-1469-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, T_DKIM_INVALID, T_RP_MATCHES_RCVD, 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); } }