Skip to content

Commit 2312661

Browse files
Generalized bdd into an add (now slower) & more tests
1 parent 9eca876 commit 2312661

35 files changed

+1342
-491
lines changed

compile.sh

+7
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ if [ "$isDbg" -eq 1 ] ; then
9797
preprocFlags="${preprocFlags} -DDBG_ROWINFO=${dbgLvl}"
9898
preprocFlags="${preprocFlags} -DDBG_WRAPPER=${dbgLvl}"
9999
preprocFlags="${preprocFlags} -DDBG_MEM=${dbgLvl}"
100+
preprocFlags="${preprocFlags} -DDBG_ADDCORE=${dbgLvl}"
101+
preprocFlags="${preprocFlags} -DDBG_ADDOP=${dbgLvl}"
100102
dbgLvl="DBG_LVL_TOFIX"
101103
preprocFlags="${preprocFlags} -DDBG_HASHMAP=${dbgLvl}"
102104
preprocFlags="${preprocFlags} -DDBG_ROWHASHED=${dbgLvl}"
@@ -112,6 +114,8 @@ elif [ "$isCheck" -eq 1 ] ; then
112114
preprocFlags="${preprocFlags} -DDBG_HASHMAP=DBG_LVL_TOFIX"
113115
preprocFlags="${preprocFlags} -DDBG_MEM=DBG_LVL_TOFIX"
114116
preprocFlags="${preprocFlags} -DDBG_HASH=DBG_LVL_TOFIX"
117+
preprocFlags="${preprocFlags} -DDBG_ADDCORE=DBG_LVL_TOFIX"
118+
preprocFlags="${preprocFlags} -DDBG_ADDOP=DBG_LVL_TOFIX"
115119
preprocFlags="${preprocFlags} -DDBG_BDD=DBG_LVL_TOFIX"
116120
preprocFlags="${preprocFlags} -DDBG_HASHCACHE=DBG_LVL_TOFIX"
117121
preprocFlags="${preprocFlags} -DDBG_BITARRAY=DBG_LVL_TOFIX"
@@ -125,6 +129,8 @@ else
125129
preprocFlags="${preprocFlags} -DDBG_HASHMAP=DBG_LVL_NONE"
126130
preprocFlags="${preprocFlags} -DDBG_MEM=DBG_LVL_NONE"
127131
preprocFlags="${preprocFlags} -DDBG_HASH=DBG_LVL_NONE"
132+
preprocFlags="${preprocFlags} -DDBG_ADDCORE=DBG_LVL_NONE"
133+
preprocFlags="${preprocFlags} -DDBG_ADDOP=DBG_LVL_NONE"
128134
preprocFlags="${preprocFlags} -DDBG_BDD=DBG_LVL_NONE"
129135
preprocFlags="${preprocFlags} -DDBG_HASHCACHE=DBG_LVL_NONE"
130136
preprocFlags="${preprocFlags} -DDBG_BITARRAY=DBG_LVL_NONE"
@@ -152,6 +158,7 @@ warningFlag="${warningFlag} -Werror"
152158
standardFlag="--std=gnu2x"
153159

154160
libFlag=""
161+
libFlag="${libFlag} -lgmp"
155162
if [ $numThreads -gt 0 ] ; then
156163
libFlag="${libFlag} -lpthread"
157164
fi

generate_multi_out.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ function execGadget(){
7676

7777
echo $name - ${maxAllowedCoeff_rps} - ${maxAllowedCoeff_rpc} = ${maxCoeff} - ${d}
7878

79-
for op in $(echo -e "rpsCor3\nrpsCor2\nrpsCor1") ; do
79+
for op in $(echo -e "rpsVraps\nrpsCor3\nrpsCor2\nrpsCor1") ; do
8080
findCoeffAndSaveResults "$file" "--$op" "$mainDir/$name/$op" "$maxCoeff" "${maxAllowedCoeff_rps}"
8181
done
8282

8383
local t=$[d / 2] # ignore the others.
84-
for op in $(echo -e "rpcCor2\nrpcCor1") ; do
84+
for op in $(echo -e "rpcVraps\nrpcCor2\nrpcCor1") ; do
8585
findCoeffAndSaveResults "$file" "--${op} -t $t" "$mainDir/$name/${op}__$t" "$maxCoeff" "${maxAllowedCoeff_rpc}"
8686
done
8787
}

generate_out.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ function execGadget(){
6868

6969
echo $name - ${maxAllowedCoeff_rps} - ${maxAllowedCoeff_rpc} = ${maxCoeff} - ${d}
7070

71-
for op in $(echo -e "rpsCor3\nrpsCor2\nrpsCor1") ; do
71+
for op in $(echo -e "rpsVraps\nrpsCor3\nrpsCor2\nrpsCor1") ; do
7272
findCoeffAndSaveResults "$file" "--$op" "$mainDir/$name/$op" "$maxCoeff" "${maxAllowedCoeff_rps}"
7373
done
7474

7575
local t=$[d / 2] # ignore the others.
76-
for op in $(echo -e "rpcCor2\nrpcCor1") ; do
76+
for op in $(echo -e "rpcVraps\nrpcCor2\nrpcCor1") ; do
7777
findCoeffAndSaveResults "$file" "--${op} -t $t" "$mainDir/$name/${op}__$t" "$maxCoeff" "${maxAllowedCoeff_rpc}"
7878
done
7979
}

src/addCore.c

+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
//This file is part of the program Random Probing Security Checker, which checks the random probing security properties of a given gadget
2+
//Copyright (C) 2022 Giuseppe Manzoni
3+
//This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
4+
//This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
5+
//You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
6+
7+
#include <string.h>
8+
9+
#include "addCore.h"
10+
#include "mem.h"
11+
#include "hashSet.h"
12+
#include "hashMap.h"
13+
#include "hashCache.h"
14+
15+
16+
17+
//#define NUM_THREADS
18+
19+
20+
21+
22+
#define DBG_FILE "addCore"
23+
#define DBG_LVL DBG_ADDCORE
24+
25+
26+
27+
// -- storageless
28+
29+
30+
31+
// 63: input | flags | hash :0
32+
#define ADD_FLAG__NEG (((addCore_arr_t) 1) << HASH_WIDTH)
33+
#define add_getHash(v) ((hash_t){ (v) & MASK_OF(HASH_WIDTH) })
34+
#define CONST_MAGIC_INPUT MASK_OF(MAX_NUM_TOT_INS__LOG2)
35+
36+
#define add_isPos(x) (((x) & ADD_FLAG__NEG) == 0)
37+
#define add_getInput(x) ((x) >> (HASH_WIDTH + ADD_FLAGS))
38+
#define add_toInput(in) (((addCore_arr_t) (in)) << (HASH_WIDTH + ADD_FLAGS))
39+
40+
41+
T__THREAD_SAFE addCore_arr_t addCore_neg(addCore_arr_t p){
42+
return p ^ ADD_FLAG__NEG;
43+
}
44+
45+
T__THREAD_SAFE bool addCore_isLeafElseNode(addCore_arr_t p){
46+
return add_getInput(p) == CONST_MAGIC_INPUT;
47+
}
48+
49+
T__THREAD_SAFE addCore_arr_t addCore_leaf(hash_t val, bool isPos){
50+
// addCore_arr_t ret = ((addCore_arr_t)val) | add_toInput(CONST_MAGIC_INPUT) | (isNegated ? ADD_FLAG__NEG : 0);
51+
addCore_arr_t ret = val.v;
52+
ret |= add_toInput(CONST_MAGIC_INPUT);
53+
ret |= isPos ? 0 : ADD_FLAG__NEG;
54+
return ret;
55+
}
56+
57+
T__THREAD_SAFE void addCore_getLeaf(addCore_arr_t p, hash_t *val, bool *isPos){
58+
if(val) *val = add_getHash(p);
59+
if(isPos) *isPos = add_isPos(p);
60+
}
61+
62+
63+
64+
65+
// -- storage & dbg
66+
67+
68+
69+
typedef struct{ addCore_arr_t v[2]; } node_t;
70+
#define A2S(pub) ((hashSet_t) { ((addCore_t) (pub)).addCore })
71+
72+
T__THREAD_SAFE addCore_t addCore_new(void){
73+
hashSet_t ret = hashSet_new(sizeof(node_t), "ADD, the storage");
74+
return (addCore_t){ ret.hashSet };
75+
}
76+
77+
void addCore_delete(addCore_t s){ hashSet_delete(A2S(s)); }
78+
double addCore_dbg_storageFill(addCore_t s){ return hashSet_dbg_fill(A2S(s)); }
79+
double addCore_dbg_hashConflictRate(addCore_t s){ return hashSet_dbg_hashConflictRate(A2S(s)); }
80+
81+
82+
83+
// -- getNode
84+
85+
86+
T__THREAD_SAFE void addCore_getNode(addCore_t s, addCore_arr_t p, wire_t *inputBit, addCore_arr_t *val0, addCore_arr_t *val1){
87+
node_t ret = *(node_t*)hashSet_getKey(A2S(s), add_getHash(p));
88+
if(!add_isPos(p)){
89+
ret.v[0] = addCore_neg(ret.v[0]);
90+
ret.v[1] = addCore_neg(ret.v[1]);
91+
}
92+
if(val0) *val0 = ret.v[0];
93+
if(val1) *val1 = ret.v[1];
94+
if(inputBit) *inputBit = add_getInput(p);
95+
}
96+
97+
98+
// -- add_node
99+
100+
101+
addCore_arr_t addCore_node(addCore_t s, wire_t inputBit, addCore_arr_t sub0, addCore_arr_t sub1){
102+
if(sub0 == sub1) return sub0; // simplify
103+
if(!add_isPos(sub0)) return addCore_neg(addCore_node(s, inputBit, addCore_neg(sub0), addCore_neg(sub1))); // ensure it's unique: have the sub0 always positive
104+
105+
hash_t hash;
106+
{
107+
node_t key;
108+
memset(&key, 0, sizeof(node_t));
109+
key.v[0] = sub0;
110+
key.v[1] = sub1;
111+
hash = hashSet_add(A2S(s), &key);
112+
}
113+
return ((addCore_arr_t)hash.v) | add_toInput(inputBit);
114+
}
115+
116+
117+
// -- add_flatten
118+
119+
T__THREAD_SAFE static void flattenR(addCore_t s, addCore_arr_t p, wire_t maxDepth, wire_t currDepth, hash_t *ret_hash, bool *ret_isPos){
120+
if(currDepth > maxDepth) FAIL("addCore_flatten: currDepth > maxDepth\n");
121+
122+
size_t blockSize = 1ull<< (maxDepth-currDepth);
123+
124+
if(addCore_isLeafElseNode(p)){
125+
for(size_t i = 0; i < blockSize; i++){
126+
if(ret_hash) ret_hash[i] = add_getHash(p);
127+
if(ret_isPos) ret_isPos[i] = add_isPos(p);
128+
}
129+
}else{
130+
size_t h = blockSize>>1;
131+
hash_t *next_hash = ret_hash ? ret_hash + h : NULL;
132+
bool *next_isPos = ret_isPos ? ret_isPos + h : NULL;
133+
134+
if(add_getInput(p) == currDepth){
135+
addCore_arr_t sub[2];
136+
addCore_getNode(s, p, NULL, &sub[0], &sub[1]);
137+
138+
flattenR(s, sub[0], maxDepth, currDepth+1, ret_hash, ret_isPos);
139+
flattenR(s, sub[1], maxDepth, currDepth+1, next_hash, next_isPos);
140+
}else{
141+
flattenR(s, p, maxDepth, currDepth+1, ret_hash, ret_isPos);
142+
flattenR(s, p, maxDepth, currDepth+1, next_hash, next_isPos);
143+
}
144+
}
145+
}
146+
147+
T__THREAD_SAFE void addCore_flattenR(addCore_t s, addCore_arr_t p, wire_t maxDepth, hash_t *ret_hash, bool *ret_isPos){
148+
flattenR(s, p, maxDepth, 0, ret_hash, ret_isPos);
149+
}

src/addCore.h

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//This file is part of the program Random Probing Security Checker, which checks the random probing security properties of a given gadget
2+
//Copyright (C) 2022 Giuseppe Manzoni
3+
//This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
4+
//This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
5+
//You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
6+
7+
#ifndef _ADDCORE_H_
8+
#define _ADDCORE_H_
9+
10+
#include <stdint.h>
11+
#include "types.h"
12+
#include "hash.h"
13+
14+
15+
//#define MAX_NUM_TOT_INS
16+
17+
18+
#define ADD_FLAGS 1
19+
20+
#define MAX_NUM_TOT_INS__LOG2 (64 - HASH_WIDTH - ADD_FLAGS)
21+
22+
// HASH_WIDTH = 48 means log2(MAX_NUM_TOT_INS+1) = 64 -48 - ADD_FLAGS = 15, which means it can handle MAX_NUM_TOT_INS = 32767 wires.
23+
// lowering HASH_WIDTH increases the log2(MAX_NUM_TOT_INS+1), which allows to support more wires.
24+
#if HASH_WIDTH >= 48
25+
#error "unsupported 48 used of indexes, a computer with that much memory is outside the current target."
26+
#endif
27+
#if MAX_NUM_TOT_INS >= 32767
28+
#error "unsupported MAX_NUM_TOT_INS >= 32767"
29+
#endif
30+
31+
32+
typedef struct { void *addCore; } addCore_t; // the storage of the add
33+
typedef uint64_t addCore_arr_t; // an array of values
34+
35+
36+
T__THREAD_SAFE addCore_t addCore_new(void);
37+
void addCore_delete(addCore_t storage);
38+
39+
40+
T__THREAD_SAFE addCore_arr_t addCore_leaf(hash_t val, bool isPos);
41+
addCore_arr_t addCore_node(addCore_t storage, wire_t inputBit, addCore_arr_t val0, addCore_arr_t val1); // val0 and val1 must have a higher inputBit, or be a const.
42+
43+
T__THREAD_SAFE bool addCore_isLeafElseNode(addCore_arr_t p);
44+
T__THREAD_SAFE void addCore_getLeaf(addCore_arr_t p, hash_t *ret_val, bool *ret_isPos); // if a ret_ is null, it's not set
45+
T__THREAD_SAFE void addCore_getNode(addCore_t storage, addCore_arr_t p, wire_t *ret_inputBit, addCore_arr_t *ret_val0, addCore_arr_t *ret_val1); // if a ret_ is null, it's not set
46+
47+
T__THREAD_SAFE addCore_arr_t addCore_neg(addCore_arr_t p);
48+
49+
T__THREAD_SAFE void addCore_flattenR(addCore_t s, addCore_arr_t p, wire_t maxDepth, hash_t *ret_hash, bool *ret_isPos);
50+
51+
T__THREAD_SAFE double addCore_dbg_storageFill(addCore_t storage);
52+
T__THREAD_SAFE double addCore_dbg_hashConflictRate(addCore_t storage);
53+
54+
55+
56+
#endif // _ADDCORE_H_

0 commit comments

Comments
 (0)