3 * Constant-time operations
5 * (c) 2013 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,
35 /*----- Header files ------------------------------------------------------*/
37 #include <mLib/bits.h>
39 /*----- Functions provided ------------------------------------------------*/
41 /* --- @ct_inteq@ --- *
43 * Arguments: @uint32 x, y@ = two 32-bit unsigned integers
45 * Returns: One if @x@ and @y@ are equal, zero if they differ.
47 * Use: Answers whether two integers are equal, in constant time.
50 extern int ct_inteq(uint32 /*x*/, uint32 /*y*/);
52 /* --- @ct_intle@ --- *
54 * Arguments: @uint32 x, y@ = two 32-bit unsigned integers
56 * Returns: One if %$x \le y$% are equal, zero if @x@ is greater.
58 * Use: Answers whether two integers are ordered, in constant time.
61 extern int ct_intle(uint32 /*x*/, uint32 /*y*/);
63 /* --- @ct_pick@ --- *
65 * Arguments: @uint32 a@ = a switch, either zero or one
66 * @uint32 x0, x1@ = two 32-bit unsigned integers
68 * Returns: @x0@ if @a@ is zero; @x1@ if @a@ is one. Other values of @a@
69 * will give you unhelpful results.
71 * Use: Picks one of two results according to a switch variable, in
75 extern int ct_pick(uint32 /*a*/, uint32 /*x0*/, uint32 /*x1*/);
77 /* --- @ct_condcopy@ --- *
79 * Arguments: @uint32 a@ = a switch, either zero or one
80 * @void *d@ = destination pointer
81 * @const void *s@ = source pointer
82 * @size_t n@ amount to copy
86 * Use: If @a@ is one then copy the @n@ bytes starting at @s@ to
87 * @d@; if @a@ is zero then leave @d@ unchanged (but it will
88 * still be written). All of this is done in constant time.
91 extern void ct_condcopy(uint32 /*a*/,
92 void */*d*/, const void */*s*/, size_t /*n*/);
96 * Arguments: @const void *p, *q@ = two pointers to buffers
97 * @size_t n@ = the (common) size of the buffers
99 * Returns: One if the two buffers are equal, zero if they aren't.
101 * Use: Compares two chunks of memory, in constant time.
104 extern int ct_memeq(const void */*p*/, const void */*q*/, size_t /*n*/);
106 /*----- That's all, folks -------------------------------------------------*/