|
| 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 | +} |
0 commit comments