-
Notifications
You must be signed in to change notification settings - Fork 2
/
Scalar_arithmetic.h
82 lines (63 loc) · 1.41 KB
/
Scalar_arithmetic.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#ifndef _SCALAR_ARITHMETIC_H_
#define _SCALAR_ARITHMETIC_H_
#include <stdlib.h>
#include <stdint.h>
#include "Build_defs.h"
void S_init();
inline Scalar S_zero() {
return 0;
}
inline Scalar S_one() {
return 1;
}
#define PRIME_BOUND 256
#if 0
#define Scalar_assert(x) \
if (x > Prime ) { \
printf("WARNING: Scalar %d out of range.\n",x); \
exit(1); \
}
#else
#define Scalar_assert(x) {}
#endif
extern Scalar Prime;
extern Scalar Inverse_table[PRIME_BOUND];
extern uint16_t _d_;
extern uint32_t _c_;
// With Scalar an uint8_t, x can be stored in a uint16_t, but C has
// promoted uint8_t * uint8_t to int32_t
inline Scalar _modp(int32_t x) {
// return x % Prime;
uint32_t t = _c_ * x;
return ((__uint64_t) t * _d_) >> 32;
// return x % 251;
}
inline Scalar S_minus(Scalar x) {
Scalar_assert(x);
return _modp(Prime - x);
}
inline Scalar ConvertToScalar(int i) {
if (i > 0)
return _modp(i);
else
return S_minus(_modp(-i));
}
inline Scalar S_add(Scalar x, Scalar y) {
Scalar_assert(x);
Scalar_assert(y);
return _modp(x + y);
}
inline Scalar S_mul(Scalar x, Scalar y) {
Scalar_assert(x);
Scalar_assert(y);
return _modp(x * y);
}
inline Scalar S_inv(Scalar x) {
Scalar_assert(x);
if (x == 0) {
printf("WARNING: Division by 0 in S_inv.\n");
exit(1);
} else
return Inverse_table[x];
}
#endif