TrustCore SDK NanoCrypto API reference  version 7.0
primefld25519.h
Go to the documentation of this file.
1 /*
2  * primefld25519.h
3  *
4  * Prime Field Header for the field with p = 2^255 - 19 elements;
5  *
6  * Copyright 2019-2024 DigiCert, Inc. All Rights Reserved.
7  * Proprietary and Confidential Material.
8  *
9  */
10 
28 /*------------------------------------------------------------------*/
29 
30 #ifndef __PRIMEFLD25519_HEADER__
31 #define __PRIMEFLD25519_HEADER__
32 
33 #include "../common/moptions.h"
34 #include "../common/mtypes.h"
35 #include "../common/merrors.h"
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
41 #define MOC_NUM_25519_BYTES 32
42 #define MOC_NUM_25519_UNITS 10
43 #define MOC_NUM_25519_ELEM_BYTES 40 /* 10 four byte units per element */
44 
45 /*
46  MACROS provided for addition and subtraction. These are for curve25519
47  specific operations and not for general purpose. The words of pA and pB
48  should be < 2^26 in absolute value. The resulting pResult will have words
49  < 2^27 in absolute value. No carries are needed.
50  */
51 #define PF_25519_add(pResult, pA, pB, i) \
52 for (i = 0; i < MOC_NUM_25519_UNITS; ++i) \
53 { \
54  pResult[i] = pA[i] + pB[i]; \
55 }
56 
57 #define PF_25519_subtract(pResult, pA, pB, i) \
58 for (i = 0; i < MOC_NUM_25519_UNITS; ++i) \
59 { \
60  pResult[i] = pA[i] - pB[i]; \
61 }
62 
63 #ifdef __PF_25519_TWOS_COMPLIMENT_OK__
64 #define PF_25519_additiveInvert(pA, i) \
65 for (i = 0; i < MOC_NUM_25519_UNITS; ++i) \
66 { \
67  pA[i] = (~(pA[i]))+1; \
68 }
69 #else
70 #define PF_25519_additiveInvert(pA, i) \
71 for (i = 0; i < MOC_NUM_25519_UNITS; ++i) \
72 { \
73 pA[i] = -1 * pA[i]; \
74 }
75 #endif
76 
88 MOC_EXTERN void PF_25519_multiply(sbyte4 *pResult, const sbyte4 *pA, const sbyte4 *pB);
89 
98 MOC_EXTERN void PF_25519_square(sbyte4 *pResult, const sbyte4 *pA);
99 
112 MOC_EXTERN MSTATUS PF_25519_specialExp(sbyte4 *pResult, const sbyte4 *pA, const byteBoolean isInverse);
113 
123 MOC_EXTERN byteBoolean PF_25519_match(const sbyte4 *pA, const sbyte4 *pB);
124 
136 MOC_EXTERN void PF_25519_to_bytes(ubyte *pResult, sbyte4 *pA);
137 
150 MOC_EXTERN MSTATUS PF_25519_from_bytes(sbyte4 *pResult, const ubyte *pInput, byteBoolean compareToThePrime);
151 
152 #ifdef __cplusplus
153 }
154 #endif
155 
156 #endif /* __PRIMEFLD25519_HEADER__ */
157 
MOC_EXTERN void PF_25519_square(sbyte4 *pResult, const sbyte4 *pA)
Squares a finite field elements.
MOC_EXTERN void PF_25519_to_bytes(ubyte *pResult, sbyte4 *pA)
Converts a finite field element stored as an sbyte4 word array into a Little Endian byte array repres...
MOC_EXTERN void PF_25519_multiply(sbyte4 *pResult, const sbyte4 *pA, const sbyte4 *pB)
Multiplies two finite field elements.
MOC_EXTERN MSTATUS PF_25519_specialExp(sbyte4 *pResult, const sbyte4 *pA, const byteBoolean isInverse)
Performs a finite field exponentiation that can be used to compute the inverse of an element or a par...
MOC_EXTERN byteBoolean PF_25519_match(const sbyte4 *pA, const sbyte4 *pB)
Tests if two finite field elements (encoded as sbyte4 word arrays) actually represent the same elemen...
MOC_EXTERN MSTATUS PF_25519_from_bytes(sbyte4 *pResult, const ubyte *pInput, byteBoolean compareToThePrime)
Converts a Little Endian byte array representing a finite field element into an element in sbyte4 wor...