• source navigation  • diff markup  • identifier search  • freetext search  •

Sources/usign/fprime.h

1 /* Arithmetic in prime fields
2  * Daniel Beer <dlbeer@gmail.com>, 10 Jan 2014
3  *
4  * This file is in the public domain.
5  */
6
7 #ifndef FPRIME_H_
8 #define FPRIME_H_
9
10 #include <stdint.h>
11 #include <string.h>
12
13 /* Maximum size of a field element (or a prime). Field elements are
14  * always manipulated and stored in normalized form, with 0 <= x < p.
15  * You can use normalize() to convert a denormalized bitstring to normal
16  * form.
17  *
18  * Operations are constant with respect to the value of field elements,
19  * but not with respect to the modulus.
20  *
21  * The modulus is a number p, such that 2p-1 fits in FPRIME_SIZE bytes.
22  */
23 #define FPRIME_SIZE             32
24
25 /* Load a large constant */
26 void fprime_from_bytes(uint8_t *x,
27                        const uint8_t *in, size_t len,
28                        const uint8_t *modulus);
29
30 /* Copy an element */
31 static inline void fprime_copy(uint8_t *x, const uint8_t *a)
32 {
33         memcpy(x, a, FPRIME_SIZE);
34 }
35
36 /* Compare two field points in constant time. Return one if equal, zero
37  * otherwise. This should be performed only on normalized values.
38  */
39 uint8_t fprime_eq(const uint8_t *x, const uint8_t *y);
40
41 /* Conditional copy. If condition == 0, then zero is copied to dst. If
42  * condition == 1, then one is copied to dst. Any other value results in
43  * undefined behaviour.
44  */
45 void fprime_select(uint8_t *dst,
46                    const uint8_t *zero, const uint8_t *one,
47                    uint8_t condition);
48
49 /* Add one value to another. The two pointers must be distinct. */
50 void fprime_add(uint8_t *r, const uint8_t *a, const uint8_t *modulus);
51
52 /* Multiply two values to get a third. r must be distinct from a and b */
53 void fprime_mul(uint8_t *r, const uint8_t *a, const uint8_t *b,
54                 const uint8_t *modulus);
55
56 #endif
57