arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-backend/src/cpu_spqlios/spqlios-arithmetic/spqlios/q120/q120_common.h
poulpy-backend/src/cpu_spqlios/spqlios-arithmetic/spqlios/q120/q120_common.hRaw
 1#ifndef SPQLIOS_Q120_COMMON_H
 2#define SPQLIOS_Q120_COMMON_H
 3
 4#include <stdint.h>
 5
 6#if !defined(SPQLIOS_Q120_USE_29_BIT_PRIMES) && !defined(SPQLIOS_Q120_USE_30_BIT_PRIMES) && \
 7    !defined(SPQLIOS_Q120_USE_31_BIT_PRIMES)
 8#define SPQLIOS_Q120_USE_30_BIT_PRIMES
 9#endif
10
11/**
12 * 29-bit primes and 2*2^16 roots of unity
13 */
14#ifdef SPQLIOS_Q120_USE_29_BIT_PRIMES
15#define Q1 ((1u << 29) - 2 * (1u << 17) + 1)
16#define OMEGA1 78289835
17#define Q1_CRT_CST 301701286  // (Q2*Q3*Q4)^-1 mod Q1
18
19#define Q2 ((1u << 29) - 5 * (1u << 17) + 1)
20#define OMEGA2 178519192
21#define Q2_CRT_CST 536020447  // (Q1*Q3*Q4)^-1 mod Q2
22
23#define Q3 ((1u << 29) - 26 * (1u << 17) + 1)
24#define OMEGA3 483889678
25#define Q3_CRT_CST 86367873  // (Q1*Q2*Q4)^-1 mod Q3
26
27#define Q4 ((1u << 29) - 35 * (1u << 17) + 1)
28#define OMEGA4 239808033
29#define Q4_CRT_CST 147030781  // (Q1*Q2*Q3)^-1 mod Q4
30#endif
31
32/**
33 * 30-bit primes and 2*2^16 roots of unity
34 */
35#ifdef SPQLIOS_Q120_USE_30_BIT_PRIMES
36#define Q1 ((1u << 30) - 2 * (1u << 17) + 1)
37#define OMEGA1 1070907127
38#define Q1_CRT_CST 43599465  // (Q2*Q3*Q4)^-1 mod Q1
39
40#define Q2 ((1u << 30) - 17 * (1u << 17) + 1)
41#define OMEGA2 315046632
42#define Q2_CRT_CST 292938863  // (Q1*Q3*Q4)^-1 mod Q2
43
44#define Q3 ((1u << 30) - 23 * (1u << 17) + 1)
45#define OMEGA3 309185662
46#define Q3_CRT_CST 594011630  // (Q1*Q2*Q4)^-1 mod Q3
47
48#define Q4 ((1u << 30) - 42 * (1u << 17) + 1)
49#define OMEGA4 846468380
50#define Q4_CRT_CST 140177212  // (Q1*Q2*Q3)^-1 mod Q4
51#endif
52
53/**
54 * 31-bit primes and 2*2^16 roots of unity
55 */
56#ifdef SPQLIOS_Q120_USE_31_BIT_PRIMES
57#define Q1 ((1u << 31) - 1 * (1u << 17) + 1)
58#define OMEGA1 1615402923
59#define Q1_CRT_CST 1811422063  // (Q2*Q3*Q4)^-1 mod Q1
60
61#define Q2 ((1u << 31) - 4 * (1u << 17) + 1)
62#define OMEGA2 1137738560
63#define Q2_CRT_CST 2093150204  // (Q1*Q3*Q4)^-1 mod Q2
64
65#define Q3 ((1u << 31) - 11 * (1u << 17) + 1)
66#define OMEGA3 154880552
67#define Q3_CRT_CST 164149010  // (Q1*Q2*Q4)^-1 mod Q3
68
69#define Q4 ((1u << 31) - 23 * (1u << 17) + 1)
70#define OMEGA4 558784885
71#define Q4_CRT_CST 225197446  // (Q1*Q2*Q3)^-1 mod Q4
72#endif
73
74static const uint32_t PRIMES_VEC[4] = {Q1, Q2, Q3, Q4};
75static const uint32_t OMEGAS_VEC[4] = {OMEGA1, OMEGA2, OMEGA3, OMEGA4};
76
77#define MAX_ELL 10000
78
79// each number x mod Q120 is represented by uint64_t[4] with (non-unique) values (x mod q1, x mod q2,x mod q3,x mod q4),
80// each between [0 and 2^32-1]
81typedef struct _q120a q120a;
82
83// each number x mod Q120 is represented by uint64_t[4] with (non-unique) values (x mod q1, x mod q2,x mod q3,x mod q4),
84// each between [0 and 2^64-1]
85typedef struct _q120b q120b;
86
87// each number x mod Q120 is represented by uint32_t[8] with values (x mod q1, 2^32x mod q1, x mod q2, 2^32.x mod q2, x
88// mod q3, 2^32.x mod q3, x mod q4, 2^32.x mod q4) each between [0 and 2^32-1]
89typedef struct _q120c q120c;
90
91typedef struct _q120x2b q120x2b;
92typedef struct _q120x2c q120x2c;
93
94#endif  // SPQLIOS_Q120_COMMON_H