follower.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. package main
  2. //#cgo CFLAGS: -fopenmp -O2
  3. //#cgo LDFLAGS: -lcrypto -lm -fopenmp
  4. //#include "../c/dpf.h"
  5. //#include "../c/okv.h"
  6. //#include "../c/dpf.c"
  7. //#include "../c/okv.c"
  8. import "C"
  9. //ssssssssssssss
  10. import (
  11. "2PPS/lib"
  12. "crypto/rand"
  13. "crypto/rsa"
  14. "crypto/sha256"
  15. "crypto/tls"
  16. "crypto/x509"
  17. "crypto/x509/pkix"
  18. "encoding/pem"
  19. "fmt"
  20. "math/big"
  21. "net"
  22. "strconv"
  23. "sync"
  24. "time"
  25. "unsafe"
  26. "golang.org/x/crypto/nacl/box"
  27. )
  28. const leader string = "127.0.0.1:4442"
  29. //needs to be changed at leader/follower/client at the same time
  30. const neededSubscriptions = 1
  31. //this stores all neccessary information for each client
  32. type clientKeys struct {
  33. SharedSecret [32]byte
  34. PirQuery [][]byte
  35. }
  36. //uses clients publicKey as key
  37. var clientData = make(map[[32]byte]clientKeys)
  38. var topicList []byte
  39. var topicAmount int
  40. var followerPrivateKey *[32]byte
  41. var followerPublicKey *[32]byte
  42. var leaderPublicKey *[32]byte
  43. var numThreads int = 12
  44. //has to be dividable by 32
  45. var dataLength int = 32
  46. var numRows int = 2
  47. var round int = 1
  48. var startTime time.Time
  49. var maxTimePerRound time.Duration = 5 * time.Second
  50. func main() {
  51. generatedPublicKey, generatedPrivateKey, err := box.GenerateKey(rand.Reader)
  52. if err != nil {
  53. panic(err)
  54. }
  55. followerPrivateKey = generatedPrivateKey
  56. followerPublicKey = generatedPublicKey
  57. /*
  58. if len(os.Args) != 4 {
  59. fmt.Println("try again: numThreads, dataLength, numRows")
  60. return
  61. }
  62. numThreads, _ = strconv.Atoi(os.Args[2])
  63. dataLength, _ = strconv.Atoi(os.Args[3])
  64. numRows, _ = strconv.Atoi(os.Args[4])
  65. */
  66. C.initializeServer(C.int(numThreads))
  67. followerConnectionPrivateKey, err := rsa.GenerateKey(rand.Reader, 2048)
  68. if err != nil {
  69. panic(err)
  70. }
  71. // Generate a pem block with the private key
  72. keyPem := pem.EncodeToMemory(&pem.Block{
  73. Type: "RSA PRIVATE KEY",
  74. Bytes: x509.MarshalPKCS1PrivateKey(followerConnectionPrivateKey),
  75. })
  76. tml := x509.Certificate{
  77. // you can add any attr that you need
  78. NotBefore: time.Now(),
  79. NotAfter: time.Now().AddDate(5, 0, 0),
  80. // you have to generate a different serial number each execution
  81. SerialNumber: big.NewInt(123123),
  82. Subject: pkix.Name{
  83. CommonName: "New Name",
  84. Organization: []string{"New Org."},
  85. },
  86. BasicConstraintsValid: true,
  87. }
  88. cert, err := x509.CreateCertificate(rand.Reader, &tml, &tml, &followerConnectionPrivateKey.PublicKey, followerConnectionPrivateKey)
  89. if err != nil {
  90. panic(err)
  91. }
  92. // Generate a pem block with the certificate
  93. certPem := pem.EncodeToMemory(&pem.Block{
  94. Type: "CERTIFICATE",
  95. Bytes: cert,
  96. })
  97. tlsCert, err := tls.X509KeyPair(certPem, keyPem)
  98. if err != nil {
  99. panic(err)
  100. }
  101. config := &tls.Config{Certificates: []tls.Certificate{tlsCert}}
  102. fmt.Println("start leader")
  103. //listens for leader
  104. lnLeader, err := tls.Listen("tcp", ":4443", config)
  105. if err != nil {
  106. panic(err)
  107. }
  108. defer lnLeader.Close()
  109. leaderConnection, err := lnLeader.Accept()
  110. if err != nil {
  111. panic(err)
  112. }
  113. //send publicKey to leader
  114. _, err = leaderConnection.Write(followerPublicKey[:])
  115. if err != nil {
  116. panic(err)
  117. }
  118. //receives leader PublicKey
  119. var tmpLeaderPubKey [32]byte
  120. _, err = leaderConnection.Read(tmpLeaderPubKey[:])
  121. if err != nil {
  122. panic(err)
  123. }
  124. leaderPublicKey = &tmpLeaderPubKey
  125. //setup ends here
  126. //locks access to DB
  127. var m sync.Mutex
  128. wg := &sync.WaitGroup{}
  129. for {
  130. //phase1
  131. fmt.Println("phase1")
  132. //create write db for this round
  133. for i := 0; i < numRows; i++ {
  134. C.createDb(C.int(0), C.int(dataLength))
  135. }
  136. for i := 0; i < numThreads; i++ {
  137. wg.Add(1)
  138. leaderConnection, err := lnLeader.Accept()
  139. if err != nil {
  140. panic(err)
  141. }
  142. leaderConnection.SetDeadline(time.Time{})
  143. startTime = time.Now()
  144. go phase1(i, leaderConnection, m, wg)
  145. }
  146. wg.Wait()
  147. //phase2
  148. fmt.Println("phase2")
  149. leaderConnection, err := lnLeader.Accept()
  150. if err != nil {
  151. panic(err)
  152. }
  153. leaderConnection.SetDeadline(time.Time{})
  154. phase2(leaderConnection)
  155. //phase3
  156. fmt.Println("phase3")
  157. if round == 1 {
  158. //addTestTweets()
  159. }
  160. //no tweets -> continue to phase 1 and mb get tweets
  161. topicList, topicAmount = lib.GetTopicList(0)
  162. if len(topicList) == 0 {
  163. continue
  164. }
  165. for i := 0; i < numThreads; i++ {
  166. wg.Add(1)
  167. leaderConnection, err := lnLeader.Accept()
  168. if err != nil {
  169. panic(err)
  170. }
  171. leaderConnection.SetDeadline(time.Time{})
  172. startTime = time.Now()
  173. go phase3(leaderConnection, wg)
  174. }
  175. wg.Wait()
  176. lib.CleanUpdbR(round)
  177. round++
  178. }
  179. }
  180. func phase1(id int, leaderWorkerConnection net.Conn, m sync.Mutex, wg *sync.WaitGroup) {
  181. gotClient := make([]byte, 1)
  182. for {
  183. _, err := leaderWorkerConnection.Read(gotClient)
  184. if err != nil {
  185. panic(err)
  186. }
  187. //this worker is done
  188. if gotClient[0] == 0 {
  189. wg.Done()
  190. return
  191. }
  192. //setup the worker-specific db
  193. dbSize := int(C.dbSize)
  194. db := make([][]byte, dbSize)
  195. for i := 0; i < dbSize; i++ {
  196. db[i] = make([]byte, int(C.db[i].dataSize))
  197. }
  198. //gets clients publicKey
  199. var clientPublicKey *[32]byte
  200. var tmpClientPublicKey [32]byte
  201. _, err = leaderWorkerConnection.Read(tmpClientPublicKey[:])
  202. if err != nil {
  203. panic(err)
  204. }
  205. clientPublicKey = &tmpClientPublicKey
  206. //gets dpfQuery from leader
  207. dpfLengthBytes := make([]byte, 4)
  208. _, err = leaderWorkerConnection.Read(dpfLengthBytes)
  209. if err != nil {
  210. panic(err)
  211. }
  212. dpfLength := byteToInt(dpfLengthBytes)
  213. dpfQueryBEncrypted := make([]byte, dpfLength)
  214. _, err = leaderWorkerConnection.Read(dpfQueryBEncrypted)
  215. if err != nil {
  216. panic(err)
  217. }
  218. //auditing starts here
  219. //gets seeed from leader
  220. seed := make([]byte, 16)
  221. _, err = leaderWorkerConnection.Read(seed)
  222. if err != nil {
  223. panic(err)
  224. }
  225. //receive client audit result
  226. auditLengthBytes := make([]byte, 4)
  227. _, err = leaderWorkerConnection.Read(auditLengthBytes)
  228. if err != nil {
  229. panic(err)
  230. }
  231. auditLength := 200 //byteToInt(auditLengthBytes)
  232. clientAuditEncrypted := make([]byte, auditLength)
  233. _, err = leaderWorkerConnection.Read(clientAuditEncrypted)
  234. if err != nil {
  235. panic(err)
  236. }
  237. //decrypts the clients audit result
  238. var decryptNonce [24]byte
  239. copy(decryptNonce[:], clientAuditEncrypted[:24])
  240. clientAuditB, ok := box.Open(nil, clientAuditEncrypted[24:], &decryptNonce, clientPublicKey, followerPrivateKey)
  241. if !ok {
  242. panic("clientAuditB decryption not ok")
  243. }
  244. //decrypt dpfQueryB for sorting into db
  245. copy(decryptNonce[:], dpfQueryBEncrypted[:24])
  246. dpfQueryB, ok := box.Open(nil, dpfQueryBEncrypted[24:], &decryptNonce, clientPublicKey, followerPrivateKey)
  247. if !ok {
  248. panic("dpfQueryB decryption not ok")
  249. }
  250. vector := make([]byte, dbSize*16)
  251. //run dpf, xor into local db
  252. for i := 0; i < dbSize; i++ {
  253. ds := int(C.db[i].dataSize)
  254. dataShare := make([]byte, ds)
  255. pos := C.getUint128_t(C.int(i))
  256. //fmt.Println(i, pos)
  257. v := C.evalDPF(C.ctx[id], (*C.uchar)(&dpfQueryB[0]), pos, C.int(ds), (*C.uchar)(&dataShare[0]))
  258. fmt.Println("")
  259. fmt.Println("v", i, v)
  260. copy(vector[i*16:(i+1)*16], C.GoBytes(unsafe.Pointer(&v), 16))
  261. for j := 0; j < ds; j++ {
  262. db[i][j] = db[i][j] ^ dataShare[j]
  263. }
  264. }
  265. //prepare for audit
  266. mVal := make([]byte, 16)
  267. cVal := make([]byte, 16)
  268. C.serverSetupProof(C.ctx[id], (*C.uchar)(&seed[0]), C.dbSize, (*C.uchar)(&vector[0]), (*C.uchar)(&mVal[0]), (*C.uchar)(&cVal[0]))
  269. //compute audit query
  270. auditResultB := make([]byte, 96)
  271. C.serverComputeQuery(C.ctx[id], (*C.uchar)(&seed[0]), (*C.uchar)(&mVal[0]), (*C.uchar)(&cVal[0]), (*C.uchar)(&clientAuditB[0]), (*C.uchar)(&auditResultB[0]))
  272. //encrypt follower audit result
  273. var nonce [24]byte
  274. //fill nonce with randomness
  275. _, err = rand.Read(nonce[:])
  276. if err != nil {
  277. panic("couldn't get randomness for nonce!")
  278. }
  279. auditResultBEncrypted := box.Seal(nonce[:], auditResultB, &nonce, leaderPublicKey, followerPrivateKey)
  280. //gets audit result from leader
  281. auditResultAEncryptedLengthBytes := make([]byte, 4)
  282. _, err = leaderWorkerConnection.Read(auditResultAEncryptedLengthBytes)
  283. if err != nil {
  284. panic(err)
  285. }
  286. auditResultAEncryptedLength := byteToInt(auditResultAEncryptedLengthBytes)
  287. auditResultAEncrypted := make([]byte, auditResultAEncryptedLength)
  288. _, err = leaderWorkerConnection.Read(auditResultAEncrypted)
  289. if err != nil {
  290. panic(err)
  291. }
  292. //send follower audit result to leader
  293. auditResultBEncryptedLengthBytes := intToByte(len(auditResultBEncrypted))
  294. _, err = leaderWorkerConnection.Write(auditResultBEncryptedLengthBytes)
  295. if err != nil {
  296. panic(err)
  297. }
  298. _, err = leaderWorkerConnection.Write(auditResultBEncrypted)
  299. if err != nil {
  300. panic(err)
  301. }
  302. //decrypts the audit result from leader
  303. copy(decryptNonce[:], auditResultAEncrypted[:24])
  304. auditResultA, ok := box.Open(nil, auditResultAEncrypted[24:], &decryptNonce, leaderPublicKey, followerPrivateKey)
  305. if !ok {
  306. panic("auditResultA decryption not ok")
  307. }
  308. //compute audit result
  309. auditResult := int(C.serverVerifyProof((*C.uchar)(&auditResultA[0]), (*C.uchar)(&auditResultB[0])))
  310. if auditResult == 0 {
  311. //fmt.Println("audit failed")
  312. } /*else {
  313. fmt.Println("audit passed")
  314. }
  315. */
  316. //xor the worker's DB into the main DB
  317. for i := 0; i < dbSize; i++ {
  318. m.Lock()
  319. C.xorIn(C.int(i), (*C.uchar)(&db[i][0]))
  320. m.Unlock()
  321. }
  322. }
  323. }
  324. func phase2(leaderWorkerConnection net.Conn) {
  325. //gets current seed
  326. seedFollower := make([]byte, 16)
  327. C.readSeed((*C.uchar)(&seedFollower[0]))
  328. //get data
  329. dbSize := int(C.dbSize)
  330. tmpdbFollower := make([][]byte, dbSize)
  331. for i := range tmpdbFollower {
  332. tmpdbFollower[i] = make([]byte, dataLength)
  333. }
  334. for i := 0; i < dbSize; i++ {
  335. C.readData(C.int(i), (*C.uchar)(&tmpdbFollower[i][0]))
  336. }
  337. //receive seed from leader
  338. seedLeader := make([]byte, 16)
  339. _, err := leaderWorkerConnection.Read(seedLeader)
  340. if err != nil {
  341. panic(err)
  342. }
  343. //receive data from leader
  344. tmpdbLeader := make([][]byte, dbSize)
  345. for i := range tmpdbLeader {
  346. tmpdbLeader[i] = make([]byte, dataLength)
  347. }
  348. for i := 0; i < dbSize; i++ {
  349. _, err = leaderWorkerConnection.Read(tmpdbLeader[i])
  350. if err != nil {
  351. panic(err)
  352. }
  353. }
  354. //writes seed to leader
  355. _, err = leaderWorkerConnection.Write(seedFollower)
  356. if err != nil {
  357. panic(err)
  358. }
  359. //write data to leader
  360. for i := 0; i < dbSize; i++ {
  361. _, err = leaderWorkerConnection.Write(tmpdbFollower[i])
  362. if err != nil {
  363. panic(err)
  364. }
  365. }
  366. //put together the db
  367. tmpdb := make([][]byte, dbSize)
  368. for i := range tmpdb {
  369. tmpdb[i] = make([]byte, dataLength)
  370. }
  371. //get own Ciphers
  372. ciphersFollowers := make([]*C.uchar, dbSize)
  373. for i := 0; i < dbSize; i++ {
  374. ciphersFollowers[i] = (*C.uchar)(C.malloc(16))
  375. }
  376. for i := 0; i < dbSize; i++ {
  377. C.getCipher(0, C.int(i), ciphersFollowers[i])
  378. }
  379. //receive ciphers from leader
  380. ciphersLeader := make([]byte, dbSize*16)
  381. for i := 0; i < dbSize; i++ {
  382. _, err = leaderWorkerConnection.Read(ciphersLeader[i*16:])
  383. if err != nil {
  384. panic(err)
  385. }
  386. }
  387. //send own Ciphers to leader
  388. for i := 0; i < dbSize; i++ {
  389. _, err = leaderWorkerConnection.Write(C.GoBytes(unsafe.Pointer(ciphersFollowers[i]), 16))
  390. if err != nil {
  391. panic(err)
  392. }
  393. }
  394. //put in ciphers from leader
  395. for i := 0; i < dbSize; i++ {
  396. C.putCipher(0, C.int(i), (*C.uchar)(&ciphersLeader[i*16]))
  397. }
  398. for i := 0; i < dbSize; i++ {
  399. C.decryptRow(C.int(i), (*C.uchar)(&tmpdb[i][0]), (*C.uchar)(&tmpdbLeader[i][0]), (*C.uchar)(&tmpdbFollower[i][0]), (*C.uchar)(&seedLeader[0]), (*C.uchar)(&seedFollower[0]))
  400. }
  401. var tweets []lib.Tweet
  402. for i := 0; i < dbSize; i++ {
  403. //discard cover message
  404. if tmpdb[i][0] == 0 {
  405. continue
  406. } else {
  407. //reconstruct tweet
  408. var position int = 0
  409. var topics []string
  410. var topic string
  411. var text string
  412. for _, letter := range tmpdb[i] {
  413. if string(letter) == ";" {
  414. if topic != "" {
  415. topics = append(topics, topic)
  416. topic = ""
  417. }
  418. position++
  419. } else {
  420. if position == 0 {
  421. if string(letter) == "," {
  422. topics = append(topics, topic)
  423. topic = ""
  424. } else {
  425. topic = topic + string(letter)
  426. }
  427. } else if position == 1 {
  428. text = text + string(letter)
  429. }
  430. }
  431. }
  432. tweet := lib.Tweet{"", -1, topics, text, round}
  433. tweets = append(tweets, tweet)
  434. }
  435. }
  436. //fmt.Println("tweets recovered: ", tweets)
  437. //sort into read db
  438. lib.NewEntries(tweets, 0)
  439. //reset write db after the tweets were moved to read db
  440. C.resetDb()
  441. }
  442. func addTestTweets() {
  443. //creates test tweets
  444. tweets := make([]lib.Tweet, 5)
  445. for i := range tweets {
  446. j := i
  447. if i == 1 {
  448. j = 0
  449. }
  450. text := "Text " + strconv.Itoa(i)
  451. var topics []string
  452. topics = append(topics, "Topic "+strconv.Itoa(j))
  453. tweets[i] = lib.Tweet{"", -1, topics, text, i}
  454. }
  455. lib.NewEntries(tweets, 0)
  456. }
  457. func phase3(leaderWorkerConnection net.Conn, wg *sync.WaitGroup) {
  458. gotClient := make([]byte, 1)
  459. for {
  460. _, err := leaderWorkerConnection.Read(gotClient)
  461. if err != nil {
  462. panic(err)
  463. }
  464. //this worker is done
  465. if gotClient[0] == 0 {
  466. wg.Done()
  467. return
  468. }
  469. subPhase := make([]byte, 1)
  470. _, err = leaderWorkerConnection.Read(subPhase)
  471. if err != nil {
  472. panic(err)
  473. }
  474. var clientPublicKey [32]byte
  475. _, err = leaderWorkerConnection.Read(clientPublicKey[:])
  476. if err != nil {
  477. panic(err)
  478. }
  479. //gets the client data
  480. clientKeys := clientData[clientPublicKey]
  481. if subPhase[0] == 0 || subPhase[0] == 1 {
  482. clientKeys, _ = handlePirQuery(clientKeys, leaderWorkerConnection, int(subPhase[0]), clientPublicKey)
  483. }
  484. getSendTweets(clientKeys, nil, leaderWorkerConnection)
  485. wantsArchive := make([]byte, 1)
  486. _, err = leaderWorkerConnection.Read(wantsArchive)
  487. if err != nil {
  488. panic(err)
  489. }
  490. if wantsArchive[0] == 1 {
  491. _, archiveQuerys := handlePirQuery(clientKeys, leaderWorkerConnection, -1, clientPublicKey)
  492. getSendTweets(clientKeys, archiveQuerys, leaderWorkerConnection)
  493. }
  494. //saves clientKeys
  495. clientData[clientPublicKey] = clientKeys
  496. }
  497. }
  498. func getSendTweets(clientKeys clientKeys, archiveQuerys [][]byte, leaderWorkerConnection net.Conn) {
  499. tmpNeededSubscriptions := neededSubscriptions
  500. if archiveQuerys != nil {
  501. tmpNeededSubscriptions = len(archiveQuerys)
  502. }
  503. for i := 0; i < tmpNeededSubscriptions; i++ {
  504. //gets all requested tweets
  505. var tweets []byte
  506. if archiveQuerys == nil {
  507. tweets = lib.GetTweets(clientKeys.PirQuery[i], dataLength, 0)
  508. } else {
  509. tweets = lib.GetTweets(archiveQuerys[i], dataLength, 1)
  510. }
  511. //expand sharedSecret so it is of right length
  512. expandBy := len(tweets) / 32
  513. var expandedSharedSecret []byte
  514. for i := 0; i < expandBy; i++ {
  515. expandedSharedSecret = append(expandedSharedSecret, clientKeys.SharedSecret[:]...)
  516. }
  517. fmt.Println(expandedSharedSecret)
  518. //Xor's sharedSecret with all tweets
  519. lib.Xor(expandedSharedSecret[:], tweets)
  520. lib.Xor(tweets, expandedSharedSecret[:])
  521. //sends tweets to leader
  522. tweetsLengthBytes := intToByte(len(tweets))
  523. _, err := leaderWorkerConnection.Write(tweetsLengthBytes)
  524. if err != nil {
  525. panic(err)
  526. }
  527. _, err = leaderWorkerConnection.Write(tweets)
  528. if err != nil {
  529. panic(err)
  530. }
  531. }
  532. }
  533. func handlePirQuery(clientKeys clientKeys, leaderWorkerConnection net.Conn, subPhase int, clientPublicKey [32]byte) (clientKeys, [][]byte) {
  534. archiveNeededSubscriptions := make([]byte, 4)
  535. if subPhase == -1 {
  536. _, err := leaderWorkerConnection.Read(archiveNeededSubscriptions)
  537. if err != nil {
  538. panic(err)
  539. }
  540. }
  541. //gets the msg length
  542. msgLengthBytes := make([]byte, 4)
  543. _, err := leaderWorkerConnection.Read(msgLengthBytes)
  544. if err != nil {
  545. panic(err)
  546. }
  547. msgLength := byteToInt(msgLengthBytes)
  548. message := make([]byte, msgLength)
  549. //gets the message
  550. _, err = leaderWorkerConnection.Read(message)
  551. if err != nil {
  552. panic(err)
  553. }
  554. var decryptNonce [24]byte
  555. copy(decryptNonce[:], message[:24])
  556. decrypted, ok := box.Open(nil, message[24:], &decryptNonce, &clientPublicKey, followerPrivateKey)
  557. if !ok {
  558. panic("pirQuery decryption not ok")
  559. }
  560. //gets sharedSecret
  561. if subPhase == 0 {
  562. //bs!
  563. var newSharedSecret [32]byte
  564. for index := 0; index < 32; index++ {
  565. newSharedSecret[index] = decrypted[index]
  566. }
  567. clientKeys.SharedSecret = newSharedSecret
  568. decrypted = decrypted[32:]
  569. //follower updates sharedSecret
  570. } else if subPhase == 1 {
  571. sharedSecret := clientKeys.SharedSecret
  572. sharedSecret = sha256.Sum256(sharedSecret[:])
  573. clientKeys.SharedSecret = sharedSecret
  574. }
  575. //follower expects pirQuery
  576. //transforms byteArray to ints of wanted topics
  577. pirQueryFlattened := decrypted
  578. tmpNeededSubscriptions := neededSubscriptions
  579. tmpTopicAmount := topicAmount
  580. if subPhase == -1 {
  581. tmpNeededSubscriptions = byteToInt(archiveNeededSubscriptions)
  582. _, tmpTopicAmount = lib.GetTopicList(1)
  583. }
  584. pirQuerys := make([][]byte, tmpNeededSubscriptions)
  585. for i := range pirQuerys {
  586. pirQuerys[i] = make([]byte, tmpTopicAmount)
  587. }
  588. for i := 0; i < tmpNeededSubscriptions; i++ {
  589. pirQuerys[i] = pirQueryFlattened[i*tmpTopicAmount : (i+1)*tmpTopicAmount]
  590. }
  591. //sets the pirQuery for the client in case whe are not archiving
  592. if subPhase != -1 {
  593. clientKeys.PirQuery = pirQuerys
  594. }
  595. return clientKeys, pirQuerys
  596. }
  597. func transformBytesToStringArray(topicsAsBytes []byte) []string {
  598. var topics []string
  599. var topic string
  600. var position int = 0
  601. for _, letter := range topicsAsBytes {
  602. if string(letter) == "," {
  603. topics[position] = topic
  604. topic = ""
  605. position++
  606. } else {
  607. topic = topic + string(letter)
  608. }
  609. }
  610. return topics
  611. }
  612. func byteToInt(myBytes []byte) (x int) {
  613. x = int(myBytes[3])<<24 + int(myBytes[2])<<16 + int(myBytes[1])<<8 + int(myBytes[0])
  614. return
  615. }
  616. func intToByte(myInt int) (retBytes []byte) {
  617. retBytes = make([]byte, 4)
  618. retBytes[3] = byte((myInt >> 24) & 0xff)
  619. retBytes[2] = byte((myInt >> 16) & 0xff)
  620. retBytes[1] = byte((myInt >> 8) & 0xff)
  621. retBytes[0] = byte(myInt & 0xff)
  622. return
  623. }