TNum* fast_Ring<TNum,kdefs>::initElementsToExponentsTab(TNum erzeuger) { TNum * tElementsToExponentsTab=NULL; size_t tableSize = getMaxSingleIndex() + 1; tElementsToExponentsTab = new TNum[tableSize]; tElementsToExponentsTab[0]=TNum::Zero; TNum num=erzeuger; for (int i=1; i<getCharacteristic(); i++) { tElementsToExponentsTab[ num.getX() ] = i; num.setX( ( (int)num.getX() * (int)erzeuger.getX() ) % getCharacteristic()); } if (num!=erzeuger) { std::cerr << "num " << num << std::endl; std::cerr << "generator " << erzeuger << std::endl; num = erzeuger; for (int i=1; i<getCharacteristic(); i++) { std::cerr << "num " << (int)num.getX() * (int)erzeuger.getX() << std::endl; num.setX( ( (int)num.getX() * (int)erzeuger.getX() ) % getCharacteristic()); std::cerr << "num " << num << std::endl << std::endl; } assert( num==erzeuger ); } return tElementsToExponentsTab; }
inline TNum const fast_Ring< TNum, kdefs>::multByExpRef(const TNum & a, const TNum & b) const { if (a.isZero() || b.isZero() ) return TNum::Zero; register short res = a.getX()+b.getX(); if (res>=getCharacteristic()) return res-getCharacteristic(); return res; }
inline void fast_Ring< TNum, kdefs>::multByExpInPlace( TNum & a, const TNum b) const { if (a.isZero() || b.isZero() ) a=TNum::Zero; register short res = a.getX()+b.getX(); if (res>=getCharacteristic()) a=res-getCharacteristic(); a=res; }
inline void fast_Ring< TNum, kdefs>::multByExpInPlaceRef( TNum & a, const TNum & b) const { if (a.isZero() ||b.isZero()) a=TNum::Zero; register unsigned short res = a.getX() + b.getX(); if (res>getCharacteristic()-1) a= res-getCharacteristic(); return; }
inline int fast_Ring<TNum,kdefs>::ConvertScalarSpec(const int a) const { #ifdef SAFE assert(a>=0); #endif return a % getCharacteristic(); }
inline int fast_Ring<TNum,kdefs>::ConvertScalar(const int a) const { int res = a; while (res<0) { res += getCharacteristic(); } if ( res >= getCharacteristic() ) { res %= getCharacteristic(); } // Alternativ zur Modulo-Rechnung: //while (res >=getCharacteristic()<) //{ // res -= getCharacteristic(); //} return res; }
TNum fast_Ring<TNum,kdefs>::getGenerator() { //std::cerr << " getGenerator () " << std::endl; if (getEpsPrecision()>0) { std::cerr << " Warning: getGenerator() is not implemented for epsPrecision>0 !"; std::cerr << std::endl; return TNum::Zero; } for (int m=1; m<getCharacteristic(); m++) { TNum erzeuger = m; TNum tmp = erzeuger; assert (tmp==erzeuger); bool erz=true; for (int n=0; n<getCharacteristic()-1; n++) { tmp.setX( (unsigned int ) ( (unsigned int )tmp.getX() * (unsigned int )erzeuger.getX() ) % getCharacteristic() ); if ( (tmp.getX()==erzeuger.getX()) && (n<( getCharacteristic() - 2 )) ) { erz=false; } } if (erz) { tmp = erzeuger; return erzeuger; } } std::cerr << " Error: kein Erzeuger gefunden ! - da gibt es einen Fehler! " << std::endl; exit(0); return TNum::Zero; }
TNum* fast_Ring<TNum,kdefs>::createFastAdditionTable() { TNum * tfastAdditionTable=NULL; if (getEpsPrecision()==0) { tfastAdditionTable = new TNum[ getCharacteristic()*2 ]; for (int m=0; m<getCharacteristic(); m++) { tfastAdditionTable[m]=m; } for (int m=0; m<getCharacteristic(); m++) { tfastAdditionTable[m + getCharacteristic() ]=m; } } return tfastAdditionTable; }
bool fast_Ring<TNum,kdefs>::isGenerator(const TNum & _generator) const { TNum tmp = _generator; assert (tmp==_generator); bool bIsGenerator=true; for (int n=0; n<getCharacteristic()-1; n++) { tmp.setX( (unsigned int ) ( (unsigned int )tmp.getX() * (unsigned int )_generator.getX() ) % getCharacteristic() ); if ( (tmp.getX()==_generator.getX()) && (n<( getCharacteristic() - 2 )) ) { bIsGenerator = false; } } return bIsGenerator; }
TNum* fast_Ring<TNum,kdefs>::initExponentsToElementTab(TNum erzeuger) { TNum * tExponentsToElementTab=NULL; long tableSize=0; tableSize = getMaxSingleIndex() + 1; tExponentsToElementTab = new TNum[tableSize]; tExponentsToElementTab[0] = TNum::Zero; TNum num = erzeuger; for (int i=1; i<getCharacteristic(); i++) { //exponentsToElementTab[ TNum::getSingleIndex( i ) ] = num; tExponentsToElementTab[ i ] = num; num.setX( ( (int)num.getX() * (int)erzeuger.getX() ) % getCharacteristic() ); } assert( num==erzeuger ); return tExponentsToElementTab; }
typename fast_Ring<TNum,kdefs>::sqrtInf_t* fast_Ring<TNum,kdefs>::createSqrtTable() { typename fast_Ring<TNum,kdefs>::sqrtInf_t * tSqrtTable=NULL; long tableSize=0; tableSize = getMaxSingleIndex() + 1; tSqrtTable = new typename fast_Ring<TNum,kdefs>::sqrtInf_t[tableSize]; typename fast_Ring<TNum,kdefs>::sqrtInf_t entry(0,TNum::Zero); for (int i=0; i<getCharacteristic(); i++) { tSqrtTable[ i ] = entry; } for (int i=0; i<getCharacteristic(); i++) { int res= (i*i) % getCharacteristic(); if (tSqrtTable[ res ].solutions==0) tSqrtTable[ res ].sqrt =TNum(i); else { assert( getCharacteristic()-i ==tSqrtTable[ res ].sqrt.getX() ); } tSqrtTable[ res ].solutions ++; } for (int i=0; i<getCharacteristic(); i++) { assert( tSqrtTable[ i ].solutions==0 || tSqrtTable[ i ].solutions==2 || tSqrtTable[ i ].sqrt.getX()==0 || getCharacteristic()==2 ); // std::cerr<< "tSqrtTable["<< i <<" ].sqrt.getX()" << (int)tSqrtTable[ i ].sqrt.getX() << std::endl; if (tSqrtTable[ i ].solutions>0) assert( (tSqrtTable[ i ].sqrt.getX() * tSqrtTable[ i ].sqrt.getX() ) % getCharacteristic() == i ); assert( tSqrtTable[ i ].sqrt.getEps()==0); } return tSqrtTable; }
TNum* fast_Ring<TNum,kdefs>::createMultiplicativeInverseTable() { int i, j, k, l; size_t tableSize = getMaxSingleIndex() + 1; TNum * inverses1 = new TNum[ tableSize ]; for (i=0; i<getCharacteristic(); i++) for (j=0; j<((getCharacteristic()-1)*getEpsPrecision())+1; j++) { TNum z1(i, j); size_t index = getSingleIndex( z1 ); assert(index<tableSize && index>=0); inverses1[ index ] = TNum::Zero; for (k=0; k<getCharacteristic(); k++) for (l=0; l<((getCharacteristic()-1)*getEpsPrecision())+1; l++) { TNum z2(k, l); TNum z; z.setX( ((int)z1.getX() * (int)z2.getX()) % getCharacteristic() ); z.setEps ( ( (int)z1.getX() * (int)z2.getEps() + (int)z2.getX() * (int)z1.getEps() ) % getCharacteristic() ); if ( z == TNum::One ) { size_t index_2 = getSingleIndex( z1); assert( index_2<tableSize && index_2 >=0); inverses1 [index_2 ] = z2; break; } } } return inverses1; }
TNum* fast_Ring<TNum,kdefs>::createAdditiveInverseTable() { int i, j; size_t tableSize = getMaxSingleIndex() + 1; TNum* tadditiveInverseTable = new TNum[ tableSize ]; for (i=0; i<getCharacteristic(); i++) for (j=0; j<( ( getCharacteristic()-1 )*getEpsPrecision() )+1; j++) { TNum z1(i, j); size_t index = getSingleIndex( z1); assert(index<tableSize && index>=0); TNum z2( ( getCharacteristic() - i) % getCharacteristic(), (getCharacteristic() - j) % getCharacteristic() ); tadditiveInverseTable[ index ] = z2; } return tadditiveInverseTable; }
TNum * fast_Ring<TNum, kdefs>::createAdditionTable() { unsigned short i, j, k , l; // sollte nicht unb short sein, sondern vom basistyp abhaengen TNum * tAdditionTable=0; size_t tableSize = getMaxPairIndex() + 1; #ifdef DEBUG std::cerr << "createAdditionTable::tableSize = " << tableSize << std::endl; #endif tAdditionTable = new TNum[tableSize]; for (i=0; i<getCharacteristic(); i++) for (j=0; j<((getCharacteristic()-1)*getEpsPrecision())+1; j++) for (k=0; k<getCharacteristic(); k++) for (l=0; l<((getCharacteristic()-1)*getEpsPrecision())+1; l++) { TNum z1(i, j); TNum z2(k, l); size_t index = getPairIndex(z1, z2); assert(index < tableSize && index>=0); tAdditionTable[index].setX ( ( (int)z1.getX() + (int)z2.getX() ) % getCharacteristic() ); tAdditionTable[index].setEps ( ((int)z1.getEps() + (int)z2.getEps()) % getCharacteristic() ); } return tAdditionTable; }
inline size_t fast_Ring<TNum,kdefs>::getSingleIndex(const TNum z1) const { // 1. reicht das, wenn ich hier TNum::getPairIndex(z1,z2) einsetze, // oder muss ich ueberall TNum::getxxxIndex verwenden damit das Programm schnell laeuft? return TNum::getSingleIndex(z1, getCharacteristic()); }
/** * @brief Handle a GATTS server event. */ void BLEService::handleGATTServerEvent( esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param) { switch(event) { // ESP_GATTS_ADD_CHAR_EVT - Indicate that a characteristic was added to the service. // add_char: // - esp_gatt_status_t status // - uint16_t attr_handle // - uint16_t service_handle // - esp_bt_uuid_t char_uuid // If we have reached the correct service, then locate the characteristic and remember the handle // for that characteristic. case ESP_GATTS_ADD_CHAR_EVT: { if (m_handle == param->add_char.service_handle) { BLECharacteristic *pCharacteristic = getCharacteristic(BLEUUID(param->add_char.char_uuid)); if (pCharacteristic == nullptr) { ESP_LOGE(LOG_TAG, "Expected to find characteristic with UUID: %s, but didnt!", BLEUUID(param->add_char.char_uuid).toString().c_str()); dump(); m_semaphoreAddCharEvt.give(); break; } pCharacteristic->setHandle(param->add_char.attr_handle); m_characteristicMap.setByHandle(param->add_char.attr_handle, pCharacteristic); //ESP_LOGD(tag, "Characteristic map: %s", m_characteristicMap.toString().c_str()); m_semaphoreAddCharEvt.give(); break; } // Reached the correct service. break; } // ESP_GATTS_ADD_CHAR_EVT // ESP_GATTS_START_EVT // // start: // esp_gatt_status_t status // uint16_t service_handle case ESP_GATTS_START_EVT: { if (param->start.service_handle == getHandle()) { m_semaphoreStartEvt.give(); } break; } // ESP_GATTS_START_EVT // ESP_GATTS_CREATE_EVT // Called when a new service is registered as having been created. // // create: // * esp_gatt_status_t status // * uint16_t service_handle // * esp_gatt_srvc_id_t service_id // * - esp_gatt_id id // * - esp_bt_uuid uuid // * - uint8_t inst_id // * - bool is_primary // case ESP_GATTS_CREATE_EVT: { if (getUUID().equals(BLEUUID(param->create.service_id.id.uuid))) { setHandle(param->create.service_handle); m_semaphoreCreateEvt.give(); } break; } // ESP_GATTS_CREATE_EVT default: { break; } // Default } // Switch m_characteristicMap.handleGATTServerEvent(event, gatts_if, param); } // handleGATTServerEvent
/** * @brief Set the value of a characteristic. * @param [in] characteristicUuid The characteristic to set. * @param [in] value The value to set. * @throws BLEUuidNotFound */ void BLERemoteService::setValue(BLEUUID characteristicUuid, std::string value) { ESP_LOGD(LOG_TAG, ">> setValue: uuid: %s", characteristicUuid.toString().c_str()); getCharacteristic(characteristicUuid)->writeValue(value); ESP_LOGD(LOG_TAG, "<< setValue"); } // setValue
BLECharacteristic* BLEService::getCharacteristic(const char* uuid) { return getCharacteristic(BLEUUID(uuid)); }
TNum * fast_Ring<TNum, kdefs>::createMultiplicationTable() { int i, j, k, l; TNum * tMultiplicationTable = NULL; size_t tableSize = getMaxPairIndex() + 1; tMultiplicationTable = new TNum[tableSize]; for (i=0; i< getCharacteristic(); i++) for (j=0; j<( (getCharacteristic()-1)*getEpsPrecision() )+1; j++) for (k=0; k < getCharacteristic(); k++) for (l=0; l<( (getCharacteristic() - 1)* getEpsPrecision() ) + 1; l++) { TNum z1 (i,j); TNum z2 (k,l); size_t index = getPairIndex(z1, z2); assert(index < tableSize && index>=0 ); TNum result; result.setX ( ((int) z1.getX() * (int)z2.getX() ) % getCharacteristic() ); result.setEps( ( (int)z1.getX() * (int)z2.getEps() + (int)z2.getX() * (int)z1.getEps() ) % getCharacteristic() ); if (j==0 && l==0 && i==k ) { if (result.getX()==getCharacteristic()-1) { bContainsImagNum_m=true; imagNum_m=z1; } } tMultiplicationTable[ index ] = result; } /*for (i=0; i< getCharacteristic(); i++) for (k=0; k < getCharacteristic(); k++) { TNum z1 (i,0); TNum z2 (k,0); size_t index = getPairIndex(z1, z2); assert(index < tableSize && index>=0 ); TNum result; result.setX ( ((int) z1.getX() * (int)z2.getX() ) % getCharacteristic() ); assert(tMultiplicationTable[ index ] == result); }*/ return tMultiplicationTable; }
inline size_t fast_Ring<TNum,kdefs>::getMaxPairIndex() const { return TNum::getMaxPairIndex( getCharacteristic() ); }
inline int fast_Ring<TNum,kdefs>::FastConvertScalar(const int a) const { return ( a + getCharacteristic() ) % getCharacteristic(); }
/** * @brief Read the value of a characteristic associated with this service. */ std::string BLERemoteService::getValue(BLEUUID characteristicUuid) { ESP_LOGD(LOG_TAG, ">> readValue: uuid: %s", characteristicUuid.toString().c_str()); std::string ret = getCharacteristic(characteristicUuid)->readValue(); ESP_LOGD(LOG_TAG, "<< readValue"); return ret; } // readValue
inline size_t fast_Ring<TNum,kdefs>::getPairIndex(const TNum z1, const TNum z2) const { return TNum::getPairIndex(z1, z2, getCharacteristic() ); }
/** * @brief Get the remote characteristic object for the characteristic UUID. * @param [in] uuid Remote characteristic uuid. * @return Reference to the remote characteristic object. * @throws BLEUuidNotFoundException */ BLERemoteCharacteristic* BLERemoteService::getCharacteristic(const char* uuid) { return getCharacteristic(BLEUUID(uuid)); } // getCharacteristic