X-Git-Url: https://git.distorted.org.uk/~mdw/secnet/blobdiff_plain/ae8cd973b817f81c075ab20e84d3239125146f24..8a7654a695e8ecdc8c14dced445839a7a7e6a8ca:/fgoldi.h diff --git a/fgoldi.h b/fgoldi.h new file mode 100644 index 0000000..cac2ff1 --- /dev/null +++ b/fgoldi.h @@ -0,0 +1,212 @@ +/* -*-c-*- + * + * Arithmetic in the Goldilocks field GF(2^448 - 2^224 - 1) + * + * (c) 2017 Straylight/Edgeware + */ + +/*----- Licensing notice --------------------------------------------------* + * + * This file is part of secnet. + * See README for full list of copyright holders. + * + * secnet is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version d of the License, or + * (at your option) any later version. + * + * secnet is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * version 3 along with secnet; if not, see + * https://www.gnu.org/licenses/gpl.html. + * + * This file was originally part of Catacomb, but has been automatically + * modified for incorporation into secnet: see `import-catacomb-crypto' + * for details. + * + * Catacomb is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * Catacomb is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with Catacomb; if not, write to the Free + * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, + * MA 02111-1307, USA. + */ + +#ifndef CATACOMB_FGOLDI_H +#define CATACOMB_FGOLDI_H + +#ifdef __cplusplus + extern "C" { +#endif + +/*----- Header files ------------------------------------------------------*/ + +#include "fake-mLib-bits.h" + +#ifndef CATACOMB_QFARITH_H +# include "qfarith.h" +#endif + +/*----- Data structures ---------------------------------------------------*/ + +typedef union { + int32 p28[16]; +} fgoldi; + +/*----- Functions provided ------------------------------------------------*/ + +/* --- @fgoldi_load@ --- * + * + * Arguments: @fgoldi *z@ = where to store the result + * @const octet xv[56]@ = source to read + * + * Returns: --- + * + * Use: Reads an element of %$\gf{2^{448} - 2^{224} - 19}$% in + * external representation from @xv@ and stores it in @z@. + * + * External representation is little-endian base-256. Some + * elements have multiple encodings, which are not produced by + * correct software; use of noncanonical encodings is not an + * error, and toleration of them is considered a performance + * feature. + */ + +extern void fgoldi_load(fgoldi */*z*/, const octet /*xv*/[56]); + +/* --- @fgoldi_store@ --- * + * + * Arguments: @octet zv[56]@ = where to write the result + * @const fgoldi *x@ = the field element to write + * + * Returns: --- + * + * Use: Stores a field element in the given octet vector in external + * representation. A canonical encoding is always stored. + */ + +extern void fgoldi_store(octet /*zv*/[56], const fgoldi */*x*/); + +/* --- @fgoldi_set@ --- * + * + * Arguments: @fgoldi *z@ = where to write the result + * @int a@ = a small-ish constant + * + * Returns: --- + * + * Use: Sets @z@ to equal @a@. + */ + +extern void fgoldi_set(fgoldi */*x*/, int /*a*/); + +/* --- @fgoldi_add@ --- * + * + * Arguments: @fgoldi *z@ = where to put the result (may alias @x@ or @y@) + * @const fgoldi *x, *y@ = two operands + * + * Returns: --- + * + * Use: Set @z@ to the sum %$x + y$%. + */ + +extern void fgoldi_add(fgoldi */*z*/, + const fgoldi */*x*/, const fgoldi */*y*/); + +/* --- @fgoldi_sub@ --- * + * + * Arguments: @fgoldi *z@ = where to put the result (may alias @x@ or @y@) + * @const fgoldi *x, *y@ = two operands + * + * Returns: --- + * + * Use: Set @z@ to the difference %$x - y$%. + */ + +extern void fgoldi_sub(fgoldi */*z*/, + const fgoldi */*x*/, const fgoldi */*y*/); + +/* --- @fgoldi_condswap@ --- * + * + * Arguments: @fgoldi *x, *y@ = two operands + * @uint32 m@ = a mask + * + * Returns: --- + * + * Use: If @m@ is zero, do nothing; if @m@ is all-bits-set, then + * exchange @x@ and @y@. If @m@ has some other value, then + * scramble @x@ and @y@ in an unhelpful way. + */ + +extern void fgoldi_condswap(fgoldi */*x*/, fgoldi */*y*/, uint32 /*m*/); + +/* --- @fgoldi_mulconst@ --- * + * + * Arguments: @fgoldi *z@ = where to put the result (may alias @x@) + * @const fgoldi *x@ = an operand + * @long a@ = a small-ish constant; %$|a| < 2^{20}$%. + * + * Returns: --- + * + * Use: Set @z@ to the product %$a x$%. + */ + +extern void fgoldi_mulconst(fgoldi */*z*/, const fgoldi */*x*/, long /*a*/); + +/* --- @fgoldi_mul@ --- * + * + * Arguments: @fgoldi *z@ = where to put the result (may alias @x@ or @y@) + * @const fgoldi *x, *y@ = two operands + * + * Returns: --- + * + * Use: Set @z@ to the product %$x y$%. + */ + +extern void fgoldi_mul(fgoldi */*z*/, + const fgoldi */*x*/, const fgoldi */*y*/); + +/* --- @fgoldi_sqr@ --- * + * + * Arguments: @fgoldi *z@ = where to put the result (may alias @x@ or @y@) + * @const fgoldi *x@ = an operand + * + * Returns: --- + * + * Use: Set @z@ to the square %$x^2$%. + */ + +extern void fgoldi_sqr(fgoldi */*z*/, const fgoldi */*x*/); + +/* --- @fgoldi_inv@ --- * + * + * Arguments: @fgoldi *z@ = where to put the result (may alias @x@) + * @const fgoldi *x@ = an operand + * + * Returns: --- + * + * Use: Stores in @z@ the multiplicative inverse %$x^{-1}$%. If + * %$x = 0$% then @z@ is set to zero. This is considered a + * feature. + */ + +extern void fgoldi_inv(fgoldi */*z*/, const fgoldi */*x*/); + +/*----- That's all, folks -------------------------------------------------*/ + +#ifdef __cplusplus + } +#endif + +#endif