X-Git-Url: https://git.distorted.org.uk/~mdw/secnet/blobdiff_plain/1047c205103e6da9fc6a317f41583147dbc11aa3..a1a6042e24c9873aa6abf668bcb68d39d0eb4190:/scaf.h diff --git a/scaf.h b/scaf.h new file mode 100644 index 0000000..dc0f336 --- /dev/null +++ b/scaf.h @@ -0,0 +1,168 @@ +/* -*-c-*- + * + * Simple scalar fields + * + * (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_SCAF_H +#define CATACOMB_SCAF_H + +#ifdef __cplusplus + extern "C" { +#endif + +/*----- Header files ------------------------------------------------------*/ + +#include "fake-mLib-bits.h" + +/*----- Type selection ----------------------------------------------------*/ + + typedef uint32 scaf_piece; + typedef uint64 scaf_dblpiece; + +#define SCAF_NPIECE(bits, piecewd) (((bits) + piecewd - 1)/piecewd) + +/*----- Functions provided -----------------------------------------------*/ + +/* --- @scaf_load@ --- * + * + * Arguments: @scaf_piece *z@ = where to write the result + * @const octet *b@ = source buffer to read + * @size_t sz@ = size of the source buffer + * @size_t npiece@ = number of pieces to read + * @unsigned piecewd@ = nominal width of pieces in bits + * + * Returns: --- + * + * Use: Loads a little-endian encoded scalar into a vector @z@ of + * single-precision pieces. + */ + +extern void scaf_load(scaf_piece */*z*/, const octet */*b*/, size_t /*sz*/, + size_t /*npiece*/, unsigned /*piecewd*/); + +/* --- @scaf_loaddbl@ --- * + * + * Arguments: @scaf_dblpiece *z@ = where to write the result + * @const octet *b@ = source buffer to read + * @size_t sz@ = size of the source buffer + * @size_t npiece@ = number of pieces to read + * @unsigned piecewd@ = nominal width of pieces in bits + * + * Returns: --- + * + * Use: Loads a little-endian encoded scalar into a vector @z@ of + * double-precision pieces. + */ + +extern void scaf_loaddbl(scaf_dblpiece */*z*/, const octet */*b*/, + size_t /*sz*/, size_t /*npiece*/, + unsigned /*piecewd*/); + +/* --- @scaf_store@ --- * + * + * Arguments: @octet *b@ = buffer to fill in + * @size_t sz@ = size of the buffer + * @const scaf_piece *x@ = scalar to store + * @size_t npiece@ = number of pieces in @x@ + * @unsigned piecewd@ = nominal width of pieces in bits + * + * Returns: --- + * + * Use: Stores a scalar in a vector of single-precison pieces as a + * little-endian vector of bytes. + */ + +extern void scaf_store(octet */*b*/, size_t /*sz*/, const scaf_piece */*x*/, + size_t /*npiece*/, unsigned /*piecewd*/); + +/* --- @scaf_mul@ --- * + * + * Arguments: @scaf_dblpiece *z@ = where to put the answer + * @const scaf_piece *x, *y@ = the operands + * @size_t npiece@ = the length of the operands + * + * Returns: --- + * + * Use: Multiply two scalars. The destination must have space for + * @2*npiece@ pieces (though the last one will always be zero). + * The result is not reduced. + */ + +extern void scaf_mul(scaf_dblpiece */*z*/, const scaf_piece */*x*/, + const scaf_piece */*y*/, size_t /*npiece*/); + +/* --- @scaf_reduce@ --- * + * + * Arguments: @scaf_piece *z@ = where to write the result + * @const scaf_dblpiece *x@ = the operand to reduce + * @const scaf_piece *l@ = the modulus, in internal format + * @const scaf_piece *mu@ = scaled approximation to @1/l@ + * @size_t npiece@ = number of pieces in @l@ + * @unsigned piecewd@ = nominal width of a piece in bits + * @scaf_piece *scratch@ = @3*npiece@ scratch pieces + * + * Returns: --- + * + * Use: Reduce @x@ (a vector of @2*npiece@ double-precision pieces) + * modulo @l@ (a vector of @npiece@ single-precision pieces), + * writing the result to @z@. + * + * Write @n = npiece@, @w = piecewd@, and %$B = 2^w$%. The + * operand @mu@ must contain %$\lfloor B^{2n}/l \rfloor$%, in + * @npiece + 1@ pieces. Furthermore, we must have + * %$3 l < B^n$%. (Fiddle with %$w$% if necessary.) + */ + +extern void scaf_reduce(scaf_piece */*z*/, const scaf_dblpiece */*x*/, + const scaf_piece */*l*/, const scaf_piece */*mu*/, + size_t /*npiece*/, unsigned /*piecewd*/, + scaf_piece */*scratch*/); + +/*----- That's all, folks -------------------------------------------------*/ + +#ifdef __cplusplus + } +#endif + +#endif