3 * Arithmetic in the Goldilocks field GF(2^448 - 2^224 - 1)
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_FGOLDI_H
29 #define CATACOMB_FGOLDI_H
35 /*----- Header files ------------------------------------------------------*/
37 #include <mLib/bits.h>
39 #ifndef CATACOMB_QFARITH_H
43 /*----- Data structures ---------------------------------------------------*/
50 #if !defined(FGOLDI_IMPL) && defined(HAVE_INT64)
51 # define FGOLDI_IMPL 28
55 # define FGOLDI_IMPL 12
59 typedef int32 fgoldi_piece;
62 typedef int16 fgoldi_piece;
65 /*----- Functions provided ------------------------------------------------*/
67 /* --- @fgoldi_load@ --- *
69 * Arguments: @fgoldi *z@ = where to store the result
70 * @const octet xv[56]@ = source to read
74 * Use: Reads an element of %$\gf{2^{448} - 2^{224} - 19}$% in
75 * external representation from @xv@ and stores it in @z@.
77 * External representation is little-endian base-256. Some
78 * elements have multiple encodings, which are not produced by
79 * correct software; use of noncanonical encodings is not an
80 * error, and toleration of them is considered a performance
84 extern void fgoldi_load(fgoldi */*z*/, const octet /*xv*/[56]);
86 /* --- @fgoldi_store@ --- *
88 * Arguments: @octet zv[56]@ = where to write the result
89 * @const fgoldi *x@ = the field element to write
93 * Use: Stores a field element in the given octet vector in external
94 * representation. A canonical encoding is always stored.
97 extern void fgoldi_store(octet /*zv*/[56], const fgoldi */*x*/);
99 /* --- @fgoldi_set@ --- *
101 * Arguments: @fgoldi *z@ = where to write the result
102 * @int a@ = a small-ish constant
106 * Use: Sets @z@ to equal @a@.
109 extern void fgoldi_set(fgoldi */*x*/, int /*a*/);
111 /* --- @fgoldi_add@ --- *
113 * Arguments: @fgoldi *z@ = where to put the result (may alias @x@ or @y@)
114 * @const fgoldi *x, *y@ = two operands
118 * Use: Set @z@ to the sum %$x + y$%.
121 extern void fgoldi_add(fgoldi */*z*/,
122 const fgoldi */*x*/, const fgoldi */*y*/);
124 /* --- @fgoldi_sub@ --- *
126 * Arguments: @fgoldi *z@ = where to put the result (may alias @x@ or @y@)
127 * @const fgoldi *x, *y@ = two operands
131 * Use: Set @z@ to the difference %$x - y$%.
134 extern void fgoldi_sub(fgoldi */*z*/,
135 const fgoldi */*x*/, const fgoldi */*y*/);
137 /* --- @fgoldi_condswap@ --- *
139 * Arguments: @fgoldi *x, *y@ = two operands
140 * @uint32 m@ = a mask
144 * Use: If @m@ is zero, do nothing; if @m@ is all-bits-set, then
145 * exchange @x@ and @y@. If @m@ has some other value, then
146 * scramble @x@ and @y@ in an unhelpful way.
149 extern void fgoldi_condswap(fgoldi */*x*/, fgoldi */*y*/, uint32 /*m*/);
151 /* --- @fgoldi_mulconst@ --- *
153 * Arguments: @fgoldi *z@ = where to put the result (may alias @x@)
154 * @const fgoldi *x@ = an operand
155 * @long a@ = a small-ish constant; %$|a| < 2^{20}$%.
159 * Use: Set @z@ to the product %$a x$%.
162 extern void fgoldi_mulconst(fgoldi */*z*/, const fgoldi */*x*/, long /*a*/);
164 /* --- @fgoldi_mul@ --- *
166 * Arguments: @fgoldi *z@ = where to put the result (may alias @x@ or @y@)
167 * @const fgoldi *x, *y@ = two operands
171 * Use: Set @z@ to the product %$x y$%.
174 extern void fgoldi_mul(fgoldi */*z*/,
175 const fgoldi */*x*/, const fgoldi */*y*/);
177 /* --- @fgoldi_sqr@ --- *
179 * Arguments: @fgoldi *z@ = where to put the result (may alias @x@ or @y@)
180 * @const fgoldi *x@ = an operand
184 * Use: Set @z@ to the square %$x^2$%.
187 extern void fgoldi_sqr(fgoldi */*z*/, const fgoldi */*x*/);
189 /* --- @fgoldi_inv@ --- *
191 * Arguments: @fgoldi *z@ = where to put the result (may alias @x@)
192 * @const fgoldi *x@ = an operand
196 * Use: Stores in @z@ the multiplicative inverse %$x^{-1}$%. If
197 * %$x = 0$% then @z@ is set to zero. This is considered a
201 extern void fgoldi_inv(fgoldi */*z*/, const fgoldi */*x*/);
203 /*----- That's all, folks -------------------------------------------------*/