forked from microsoft/FourQlib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_extras.c
137 lines (116 loc) · 4.67 KB
/
test_extras.c
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/***********************************************************************************
* FourQlib: a high-performance crypto library based on the elliptic curve FourQ
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* Abstract: utility functions for tests
************************************************************************************/
#include "../FourQ_internal.h"
#include "test_extras.h"
#if (OS_TARGET == OS_LINUX) && (TARGET == TARGET_ARM)
#include <time.h>
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int64_t cpu_nseconds(void)
{ // Access system counter for benchmarking
struct timespec time;
clock_gettime(CLOCK_REALTIME, &time);
return (int64_t)(time.tv_sec*1e9 + time.tv_nsec);
}
int fp2compare64(uint64_t* a, uint64_t* b)
{ // Comparing uint64_t digits of two quadratic extension field elements, ai=bi? : (0) equal, (1) unequal
// NOTE: this function does not have constant-time execution. TO BE USED FOR TESTING ONLY.
unsigned int i;
for (i = 0; i < (2*NWORDS64_FIELD); i++) {
if (a[i] != b[i]) return 1;
}
return 0;
}
void random_scalar_test(uint64_t* a)
{ // Generating a pseudo-random scalar value in [0, 2^256-1]
// NOTE: distribution is not fully uniform. TO BE USED FOR TESTING ONLY.
unsigned char* string = (unsigned char*)&a[0];
unsigned int i;
for (i = 0; i < (sizeof(uint64_t)*NWORDS64_ORDER); i++) {
string[i] = (unsigned char)rand();
}
}
void fp2random1271_test(f2elm_t a)
{ // Generating a pseudo-random GF(p^2) element a+b*i, where a,b in [0, 2^127-1]
// NOTE: distribution is not fully uniform. TO BE USED FOR TESTING ONLY.
digit_t mask_7fff = (digit_t)-1 >> 1;
random_scalar_test((uint64_t*)&a[0]);
a[0][NWORDS_FIELD-1] &= mask_7fff;
a[1][NWORDS_FIELD-1] &= mask_7fff;
}
bool verify_mLSB_recoding(uint64_t* scalar, int* digits)
{ // Verification of the mLSB-set's recoding algorithm used in fixed-base scalar multiplication
unsigned int j, l = L_FIXEDBASE, d = D_FIXEDBASE;
uint64_t temp, temp2, carry, borrow, generated_scalar[NWORDS64_ORDER] = {0};
int i, digit;
for (i = (l-1); i >= 0; i--)
{
// Shift generated scalar to the left by 1 (multiply by 2)
temp = ((generated_scalar[0] >> (RADIX64-1)) & 1) ;
generated_scalar[0] = generated_scalar[0] << 1;
for (j = 1; j < NWORDS64_ORDER; j++) {
temp2 = ((generated_scalar[j] >> (RADIX64-1)) & 1) ;
generated_scalar[j] = (generated_scalar[j] << 1) | temp;
temp = temp2;
}
// generated scalar + digit_i
if (i < (int)d) {
digit = digits[i] | 1;
if (digit >= 0) {
generated_scalar[0] = generated_scalar[0] + digit;
carry = (generated_scalar[0] < (unsigned int)digit);
for (j = 1; j < NWORDS64_ORDER; j++)
{
generated_scalar[j] = generated_scalar[j] + carry;
carry = (generated_scalar[j] < carry);
}
} else {
borrow = 0;
temp = (uint64_t)(-digit);
for (j = 0; j < NWORDS64_ORDER; j++)
{
temp2 = generated_scalar[j] - temp;
carry = (generated_scalar[j] < temp);
generated_scalar[j] = temp2 - borrow;
borrow = carry || (temp2 < borrow);
temp = 0;
}
}
} else {
digit = digits[i]*(digits[i-(i/d)*d] | 1);
if (digit >= 0) {
generated_scalar[0] = generated_scalar[0] + digit;
carry = (generated_scalar[0] < (unsigned int)digit);
for (j = 1; j < NWORDS64_ORDER; j++)
{
generated_scalar[j] = generated_scalar[j] + carry;
carry = (generated_scalar[j] < carry);
}
} else {
borrow = 0;
temp = (uint64_t)(-digit);
for (j = 0; j < NWORDS64_ORDER; j++)
{
temp2 = generated_scalar[j] - temp;
carry = (generated_scalar[j] < temp);
generated_scalar[j] = temp2 - borrow;
borrow = carry || (temp2 < borrow);
temp = 0;
}
}
}
}
for (j = 0; j < NWORDS64_ORDER; j++)
{
if (scalar[j] != generated_scalar[j])
return false;
}
return true;
}