5 * (c) 2017 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 #ifndef CATACOMB_SCAF_H
29 #define CATACOMB_SCAF_H
35 /*----- Header files ------------------------------------------------------*/
37 #include <mLib/bits.h>
39 /*----- Type selection ----------------------------------------------------*/
50 typedef uint32 scaf_piece;
51 typedef uint64 scaf_dblpiece;
54 typedef uint16 scaf_piece;
55 typedef uint32 scaf_dblpiece;
58 #define SCAF_NPIECE(bits, piecewd) (((bits) + piecewd - 1)/piecewd)
60 /*----- Functions provided -----------------------------------------------*/
62 /* --- @scaf_load@ --- *
64 * Arguments: @scaf_piece *z@ = where to write the result
65 * @const octet *b@ = source buffer to read
66 * @size_t sz@ = size of the source buffer
67 * @size_t npiece@ = number of pieces to read
68 * @unsigned piecewd@ = nominal width of pieces in bits
72 * Use: Loads a little-endian encoded scalar into a vector @z@ of
73 * single-precision pieces.
76 extern void scaf_load(scaf_piece */*z*/, const octet */*b*/, size_t /*sz*/,
77 size_t /*npiece*/, unsigned /*piecewd*/);
79 /* --- @scaf_loaddbl@ --- *
81 * Arguments: @scaf_dblpiece *z@ = where to write the result
82 * @const octet *b@ = source buffer to read
83 * @size_t sz@ = size of the source buffer
84 * @size_t npiece@ = number of pieces to read
85 * @unsigned piecewd@ = nominal width of pieces in bits
89 * Use: Loads a little-endian encoded scalar into a vector @z@ of
90 * double-precision pieces.
93 extern void scaf_loaddbl(scaf_dblpiece */*z*/, const octet */*b*/,
94 size_t /*sz*/, size_t /*npiece*/,
95 unsigned /*piecewd*/);
97 /* --- @scaf_store@ --- *
99 * Arguments: @octet *b@ = buffer to fill in
100 * @size_t sz@ = size of the buffer
101 * @const scaf_piece *x@ = scalar to store
102 * @size_t npiece@ = number of pieces in @x@
103 * @unsigned piecewd@ = nominal width of pieces in bits
107 * Use: Stores a scalar in a vector of single-precison pieces as a
108 * little-endian vector of bytes.
111 extern void scaf_store(octet */*b*/, size_t /*sz*/, const scaf_piece */*x*/,
112 size_t /*npiece*/, unsigned /*piecewd*/);
114 /* --- @scaf_mul@ --- *
116 * Arguments: @scaf_dblpiece *z@ = where to put the answer
117 * @const scaf_piece *x, *y@ = the operands
118 * @size_t npiece@ = the length of the operands
122 * Use: Multiply two scalars. The destination must have space for
123 * @2*npiece@ pieces (though the last one will always be zero).
124 * The result is not reduced.
127 extern void scaf_mul(scaf_dblpiece */*z*/, const scaf_piece */*x*/,
128 const scaf_piece */*y*/, size_t /*npiece*/);
130 /* --- @scaf_reduce@ --- *
132 * Arguments: @scaf_piece *z@ = where to write the result
133 * @const scaf_dblpiece *x@ = the operand to reduce
134 * @const scaf_piece *l@ = the modulus, in internal format
135 * @const scaf_piece *mu@ = scaled approximation to @1/l@
136 * @size_t npiece@ = number of pieces in @l@
137 * @unsigned piecewd@ = nominal width of a piece in bits
138 * @scaf_piece *scratch@ = @3*npiece + 1@ scratch pieces
142 * Use: Reduce @x@ (a vector of @2*npiece@ double-precision pieces)
143 * modulo @l@ (a vector of @npiece@ single-precision pieces),
144 * writing the result to @z@.
146 * Write @n = npiece@, @w = piecewd@, and %$B = 2^w$%. The
147 * operand @mu@ must contain %$\lfloor B^{2n}/l \rfloor$%, in
148 * @npiece + 1@ pieces. Furthermore, we must have
149 * %$3 l < B^n$%. (Fiddle with %$w$% if necessary.)
152 extern void scaf_reduce(scaf_piece */*z*/, const scaf_dblpiece */*x*/,
153 const scaf_piece */*l*/, const scaf_piece */*mu*/,
154 size_t /*npiece*/, unsigned /*piecewd*/,
155 scaf_piece */*scratch*/);
157 /*----- That's all, folks -------------------------------------------------*/