Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp596063ybt; Fri, 10 Jul 2020 07:43:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw4wd/ktjrXrdyGJONEh9oAjZ1AlCokHQGYdAXsq9GOS5BlLj5wVaZqPY+wHObr17HQfAuy X-Received: by 2002:a17:906:f2c1:: with SMTP id gz1mr63780260ejb.88.1594392206104; Fri, 10 Jul 2020 07:43:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594392206; cv=none; d=google.com; s=arc-20160816; b=k9/hmAyOG6k+hrddNxWKnpfScJ9DXr1S4wajzGZWBN0Ye2p1qyHTEqYg7BL4L/oOju lqtmpNmj8yDFEKvzOALn1xGmTS9Ecu4CwhutxX6Av/3cVGxDrLKrhVFnZfVd0uUAvfIg NDbDpzMJ75T0qBkkkfCn3Y1tuWm5PvWqs/tUMfO6QfCtJBtwTss7BY9Ka4VLRffPfwvA 0hSjYrvhQN+XRSfo7R8SouRejdy7tFDH486vKJ/UrGIWMekDPkNB1fwZOgCPjlIp9ddd z87asamVaBDVQKmwmdEKJd7c2fwmf2GO1fVB8/Bum/UD5BK/b2jvFogmrOQWm8ROhG2Y W63w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=My4ZAHOVkD9+xihO6Xm9vQ4lQUc7IhhbhWPJ0Dx44hM=; b=j5wL+SBw9DL5Mv+74z9UlBuO/ArUDTJgk/fo9rz71QJL+emQJmcpiB59siA3ZkmnU1 LOULudpDDojYXaKUkz/ghXtQ8LzfMP/4KDr4IiuQjIQYWdeMDj+bOYoCKTP9UnkDQ9BZ lJSp52OO4aDAAscjhUIpX+Pm8yABGVTY7H7a8HZVzK/95EbMxfJDwdEQn/X4Qm8JW/fl OEz8cKuPu4EJGy62xrZFnJJjP6SvnmteKHvNpzLHeXPaG2rESrmBXc81tXWZHAgDMO3c vBpK21QhPNqY0R65wv5dr+SIcQ3Jc75B+el93R9V3Ef/YrKthRwafOKGskS7GxTZXEz8 EGbA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=vZsM5wnX; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v13si4011069ejw.696.2020.07.10.07.42.54; Fri, 10 Jul 2020 07:43:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=vZsM5wnX; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726920AbgGJOmw (ORCPT + 99 others); Fri, 10 Jul 2020 10:42:52 -0400 Received: from mail.kernel.org ([198.145.29.99]:37506 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726820AbgGJOmw (ORCPT ); Fri, 10 Jul 2020 10:42:52 -0400 Received: from mail-oi1-f169.google.com (mail-oi1-f169.google.com [209.85.167.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 86F97207D0 for ; Fri, 10 Jul 2020 14:42:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1594392171; bh=fCoShq35GT2+HVkpaCJ6q9szyI8+yYI5r4y2w0ro3/U=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=vZsM5wnXMo/Y4osi+ueGEkWqFpo3piibRcEAjWfWPwJeCeKMvFsZLI8s0rhTFh3QS vbAo7aDCtIuE4rAxW9q1J5fGJMPxhrrkDIx8fJYit2vZrETtrT6n4haSGTs2p/OEoA xigHQDz3Ydn/2T8iGpm1MjKZD7cyvT3CmSVgQjH4= Received: by mail-oi1-f169.google.com with SMTP id j11so4941227oiw.12 for ; Fri, 10 Jul 2020 07:42:51 -0700 (PDT) X-Gm-Message-State: AOAM531fjAezAi9v/euQTxbfzxHqBahRRIpwLdywbUQpsLjT81zQSGWv flklyn7FzMDZVP1NPbqj7ckm9UMynOYhXL5GWTE= X-Received: by 2002:aca:d643:: with SMTP id n64mr4464027oig.33.1594392170896; Fri, 10 Jul 2020 07:42:50 -0700 (PDT) MIME-Version: 1.0 References: <2543601.mvXUDI8C0e@positron.chronox.de> <4577235.31r3eYUQgx@positron.chronox.de> In-Reply-To: <4577235.31r3eYUQgx@positron.chronox.de> From: Ard Biesheuvel Date: Fri, 10 Jul 2020 17:42:39 +0300 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH 2/3] lib/mpi: Add mpi_sub_ui() To: =?UTF-8?Q?Stephan_M=C3=BCller?= Cc: Herbert Xu , Linux Crypto Mailing List , Marcelo Cerri , Tianjia Zhang Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org On Fri, 10 Jul 2020 at 13:16, Stephan M=C3=BCller wro= te: > > Add mpi_sub_ui() based on Gnu PG mpz_sub_ui() from mpz/aors_ui.h > adapting the code to the kernel's structures and coding style and also > removing the defines used to produce mpz_sub_ui() and mpz_add_ui() > from the same code. > Isn't GnuPG GPLv3 ? > Signed-off-by: Marcelo Henrique Cerri > Signed-off-by: Stephan Mueller > --- > include/linux/mpi.h | 3 +++ > lib/mpi/Makefile | 1 + > lib/mpi/mpi-sub-ui.c | 60 ++++++++++++++++++++++++++++++++++++++++++++ > 3 files changed, 64 insertions(+) > create mode 100644 lib/mpi/mpi-sub-ui.c > > diff --git a/include/linux/mpi.h b/include/linux/mpi.h > index 7bd6d8af0004..5d906dfbf3ed 100644 > --- a/include/linux/mpi.h > +++ b/include/linux/mpi.h > @@ -63,6 +63,9 @@ int mpi_powm(MPI res, MPI base, MPI exp, MPI mod); > int mpi_cmp_ui(MPI u, ulong v); > int mpi_cmp(MPI u, MPI v); > > +/*-- mpi-sub-ui.c --*/ > +int mpi_sub_ui(MPI w, MPI u, unsigned long vval); > + > /*-- mpi-bit.c --*/ > void mpi_normalize(MPI a); > unsigned mpi_get_nbits(MPI a); > diff --git a/lib/mpi/Makefile b/lib/mpi/Makefile > index d5874a7f5ff9..43b8fce14079 100644 > --- a/lib/mpi/Makefile > +++ b/lib/mpi/Makefile > @@ -16,6 +16,7 @@ mpi-y =3D \ > mpicoder.o \ > mpi-bit.o \ > mpi-cmp.o \ > + mpi-sub-ui.o \ > mpih-cmp.o \ > mpih-div.o \ > mpih-mul.o \ > diff --git a/lib/mpi/mpi-sub-ui.c b/lib/mpi/mpi-sub-ui.c > new file mode 100644 > index 000000000000..fa6b085bac36 > --- /dev/null > +++ b/lib/mpi/mpi-sub-ui.c > @@ -0,0 +1,60 @@ > +// SPDX-License-Identifier: GPL-2.0-or-later > +/* mpi-sub-ui.c - MPI functions > + * Copyright 1991, 1993, 1994, 1996, 1999-2002, 2004, 2012, 2013, 2= 015 > + * Free Software Foundation, Inc. > + * > + * This file is part of GnuPG. > + * > + * Note: This code is heavily based on the GNU MP Library. > + * Actually it's the same code with only minor changes in the > + * way the data is stored; this is to support the abstraction > + * of an optional secure memory allocation which may be used > + * to avoid revealing of sensitive data due to paging etc. > + * The GNU MP Library itself is published under the LGPL; > + * however I decided to publish this code under the plain GPL. > + */ > + > +#include "mpi-internal.h" > + > +int mpi_sub_ui(MPI w, MPI u, unsigned long vval) > +{ > + if (u->nlimbs =3D=3D 0) { > + if (mpi_resize(w, 1) < 0) > + return -ENOMEM; > + w->d[0] =3D vval; > + w->nlimbs =3D (vval !=3D 0); > + w->sign =3D (vval !=3D 0); > + return 0; > + } > + > + /* If not space for W (and possible carry), increase space. */ > + if (mpi_resize(w, u->nlimbs + 1)) > + return -ENOMEM; > + > + if (u->sign) { > + mpi_limb_t cy; > + > + cy =3D mpihelp_add_1(w->d, u->d, u->nlimbs, (mpi_limb_t) = vval); > + w->d[u->nlimbs] =3D cy; > + w->nlimbs =3D u->nlimbs + cy; > + w->sign =3D 1; > + } else { > + /* The signs are different. Need exact comparison to det= ermine > + * which operand to subtract from which. > + */ > + if (u->nlimbs =3D=3D 1 && u->d[0] < vval) { > + w->d[0] =3D vval - u->d[0]; > + w->nlimbs =3D 1; > + w->sign =3D 1; > + } else { > + mpihelp_sub_1(w->d, u->d, u->nlimbs, (mpi_limb_t)= vval); > + /* Size can decrease with at most one limb. */ > + w->nlimbs =3D (u->nlimbs - (w->d[u->nlimbs - 1] = =3D=3D 0)); > + w->sign =3D 0; > + } > + } > + > + mpi_normalize(w); > + return 0; > +} > +EXPORT_SYMBOL_GPL(mpi_sub_ui); > -- > 2.26.2 > > > >