Browse Source

fixed bug in random array generation in prep for auditing

Simon 2 years ago
parent
commit
6a779cb74c
7 changed files with 20 additions and 324 deletions
  1. 1 0
      .gitignore
  2. 19 19
      client/client.go
  3. 0 57
      testing/array.go
  4. 0 46
      testing/box.go
  5. 0 45
      testing/json.go
  6. 0 83
      testing/key.go
  7. 0 74
      testing/randomArray.go

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+testing/

+ 19 - 19
client/client.go

@@ -20,7 +20,9 @@ import (
 	"encoding/json"
 	"fmt"
 	"math/big"
+	mr "math/rand"
 	"net"
+	"sort"
 	"sync"
 	"time"
 	"unsafe"
@@ -326,7 +328,6 @@ func createPIRQuery(subPhase int, clientNumber int) ([]byte, []byte) {
 
 	//creates fake topicsOfInterest if client is boooring
 	if len(tmptopicsOfInterest) < tmpNeededSubscriptions && subPhase != -1 {
-		//this should be done when client is idling
 		tmptopicsOfInterest = addFakeInterests(tmptopicsOfInterest)
 	}
 
@@ -502,39 +503,38 @@ func createSharedSecret() [numClients][2][32]byte {
 	return tmpSharedSecret
 }
 
-//generates a topicOfInterest Array with random values
+//generates a topicOfInterest array with random values
 //todo! fix
 func addFakeInterests(topicsOfInterest []int) []int {
-	length := neededSubscriptions
-	fakeTopicsOfInterest := make([]int, length)
+	fakeTopicsOfInterest := make([]int, neededSubscriptions)
+	maxInt := neededSubscriptions
 
 	//fills the array with unique random ascending values in range with len(topicList)
-	for index := 0; index < length; index++ {
-		min := (index * (len(topicList) / length)) + 1
-		max := ((index + 1) * (len(topicList) / length))
-		//fmt.Println("max", max, "list", len(topicList))
-		if max == len(topicList)-1 {
-			max++
-		}
-		//fmt.Println("i", index, min, max, len(topicList))
-		bigNumber, err := rand.Int(rand.Reader, big.NewInt(int64(max-min+1)))
-		if err != nil {
-			panic(err)
-		}
-		var number int = int(bigNumber.Int64()) + min
+	for i := 0; i < neededSubscriptions; i++ {
+		fakeTopicsOfInterest[i] = mr.Intn(maxInt) + 1
 
-		fakeTopicsOfInterest[index] = number
+		for j := 0; j < i; j++ {
+			if fakeTopicsOfInterest[i] == fakeTopicsOfInterest[j] {
+				i--
+				break
+			}
+		}
 	}
+	sort.Ints(fakeTopicsOfInterest)
+	fmt.Println(fakeTopicsOfInterest)
+
 	//adds unique and new random numbers to topicOfInterests until length is satisfied
 	for _, number := range fakeTopicsOfInterest {
 		if !inList(number, topicsOfInterest) {
 			topicsOfInterest = append(topicsOfInterest, number)
 		}
-		if len(topicsOfInterest) == length {
+		if len(topicsOfInterest) == neededSubscriptions {
 			break
 		}
 	}
 
+	fmt.Println(topicsOfInterest)
+
 	return topicsOfInterest
 }
 

+ 0 - 57
testing/array.go

@@ -1,57 +0,0 @@
-package main
-
-import "fmt"
-
-func main() {
-	var arr1 [1]byte
-	arr1[0] = 1
-	arr11 := arr1
-	arr11[0] = 0
-
-	fmt.Println(arr1)
-
-	arr2 := make([]byte, 1)
-	arr2[0] = 1
-
-	arr22 := arr2
-	arr22[0] = 0
-
-	fmt.Println(arr2)
-
-	arr3 := make([]byte, 1)
-	arr3[0] = 1
-
-	var arr33 []byte
-	arr33 = arr3
-	arr33[0] = 0
-
-	fmt.Println(arr3)
-
-	arr4 := make([]byte, 1)
-	arr4[0] = 1
-
-	arr44 := make([]byte, 1)
-	arr44 = arr4
-	arr44[0] = 0
-
-	fmt.Println(arr4)
-
-	//these two below work
-	arr5 := make([]byte, 1)
-	arr5[0] = 1
-
-	arr55 := make([]byte, 1)
-	copy(arr55, arr5)
-	arr55[0] = 0
-
-	fmt.Println(arr5)
-
-	var arr6 []byte
-	arr6 = append(arr6, 1)
-
-	arr66 := make([]byte, 1)
-	copy(arr66, arr6)
-	arr66[0] = 0
-
-	fmt.Println(arr5)
-}

+ 0 - 46
testing/box.go

@@ -1,46 +0,0 @@
-package main
-
-import (
-	"crypto/rand"
-	"fmt"
-
-	"golang.org/x/crypto/nacl/box"
-)
-
-func maina() {
-	senderPublicKey, senderPrivateKey, err := box.GenerateKey(rand.Reader)
-	if err != nil {
-		panic(err)
-	}
-
-	recipientPublicKey, recipientPrivateKey, err := box.GenerateKey(rand.Reader)
-	if err != nil {
-		panic(err)
-	}
-
-	// You must use a different nonce for each message you encrypt with the
-	// same key. Since the nonce here is 192 bits long, a random value
-	// provides a sufficiently small probability of repeats.
-	var nonce [24]byte
-	_, err = rand.Read(nonce[:])
-	if err != nil {
-		panic(err)
-	}
-
-	msg := []byte("Fucking finally")
-	// This encrypts msg and appends the result to the nonce.
-	encrypted := box.Seal(nonce[:], msg, &nonce, recipientPublicKey, senderPrivateKey)
-
-	// The recipient can decrypt the message using their private key and the
-	// sender's public key. When you decrypt, you must use the same nonce you
-	// used to encrypt the message. One way to achieve this is to store the
-	// nonce alongside the encrypted message. Above, we stored the nonce in the
-	// first 24 bytes of the encrypted text.
-	var decryptNonce [24]byte
-	copy(decryptNonce[:], encrypted[:24])
-	decrypted, ok := box.Open(nil, encrypted[24:], &decryptNonce, senderPublicKey, recipientPrivateKey)
-	if !ok {
-		panic("decryption error")
-	}
-	fmt.Println(string(decrypted))
-}

+ 0 - 45
testing/json.go

@@ -1,45 +0,0 @@
-package main
-
-import (
-	"bytes"
-	"encoding/json"
-	"fmt"
-)
-
-type tweet struct {
-	topicPointer string
-	textPointer  int
-	Text         string
-	Topics       []string
-}
-
-var topicList []string
-
-func test() {
-	topicList = append(topicList, "Sport", "Ball", "Kick")
-	fmt.Println(topicList[0])
-	topicByteArray := new(bytes.Buffer)
-	json.NewEncoder(topicByteArray).Encode(topicList)
-	fmt.Println(topicByteArray.String())
-
-	var recBookmark []string
-	arrayReader := bytes.NewReader(topicByteArray.Bytes())
-	json.NewDecoder(arrayReader).Decode(&recBookmark)
-
-	fmt.Println(recBookmark)
-
-	var tweets []tweet
-	tweet1 := tweet{"", 0, "Let's go", topicList}
-	tweet2 := tweet{"", 0, "HeyHo", topicList}
-	tweets = append(tweets, tweet1, tweet2)
-
-	tweetByteArray := new(bytes.Buffer)
-	json.NewEncoder(tweetByteArray).Encode(tweets)
-	fmt.Println(tweetByteArray.String())
-
-	var recTweets []tweet
-	tweetReader := bytes.NewReader(tweetByteArray.Bytes())
-	json.NewDecoder(tweetReader).Decode(&recTweets)
-	fmt.Println(recTweets[0].Text)
-
-}

+ 0 - 83
testing/key.go

@@ -1,83 +0,0 @@
-package main
-
-import (
-	"bytes"
-	"crypto/rand"
-	"crypto/rsa"
-	"crypto/tls"
-	"crypto/x509"
-	"crypto/x509/pkix"
-	"encoding/gob"
-	"encoding/pem"
-	"fmt"
-	"log"
-	"math/big"
-	"time"
-)
-
-func mainb() {
-	var leaderPrivateKey *rsa.PrivateKey
-	leaderPrivateKey, err := rsa.GenerateKey(rand.Reader, 2048)
-	if err != nil {
-		log.Fatal("Private key cannot be created.", err.Error())
-	}
-
-	// Generate a pem block with the private key
-	keyPem := pem.EncodeToMemory(&pem.Block{
-		Type:  "RSA PRIVATE KEY",
-		Bytes: x509.MarshalPKCS1PrivateKey(leaderPrivateKey),
-	})
-
-	tml := x509.Certificate{
-		// you can add any attr that you need
-		NotBefore: time.Now(),
-		NotAfter:  time.Now().AddDate(5, 0, 0),
-		// you have to generate a different serial number each execution
-		SerialNumber: big.NewInt(123123),
-		Subject: pkix.Name{
-			CommonName:   "New Name",
-			Organization: []string{"New Org."},
-		},
-		BasicConstraintsValid: true,
-	}
-	cert, err := x509.CreateCertificate(rand.Reader, &tml, &tml, &leaderPrivateKey.PublicKey, leaderPrivateKey)
-	if err != nil {
-		log.Fatal("Certificate cannot be created.", err.Error())
-	}
-
-	// Generate a pem block with the certificate
-	certPem := pem.EncodeToMemory(&pem.Block{
-		Type:  "CERTIFICATE",
-		Bytes: cert,
-	})
-
-	tlsCert, err := tls.X509KeyPair(certPem, keyPem)
-	if err != nil {
-		log.Fatal("Cannot be loaded the certificate.", err.Error())
-	}
-
-	//listens for clients
-	config := &tls.Config{Certificates: []tls.Certificate{tlsCert}}
-	lnClients, err := tls.Listen("tcp", ":4441", config)
-	if err != nil {
-		panic(err)
-	}
-	defer lnClients.Close()
-
-	var pub rsa.PublicKey = leaderPrivateKey.PublicKey
-
-	var network bytes.Buffer
-
-	enc := gob.NewEncoder(&network)
-	dec := gob.NewDecoder(&network)
-
-	enc.Encode(pub)
-
-	var pubRec rsa.PublicKey
-
-	dec.Decode(&pubRec)
-
-	fmt.Println(pub)
-	fmt.Println(pubRec)
-
-}

+ 0 - 74
testing/randomArray.go

@@ -1,74 +0,0 @@
-package main
-
-import (
-	"crypto/rand"
-	"fmt"
-	"math/big"
-)
-
-func notmain() {
-
-	//understanding pointers...
-	var i int = 1
-	var iArray [1]int
-	p := &i
-	iArray[0] = *p
-	i = 2
-	fmt.Println(iArray[0])
-	fmt.Println(i)
-
-	//understanding sclices...
-	var arr [5]int
-	arr[0] = 0
-	arr[1] = 1
-	arr[2] = 2
-	arr[3] = 3
-	arr[4] = 4
-	fmt.Println(arr[:1])
-	fmt.Println(arr[1:3]) // = [lower:upper)
-
-	//later this will be taken from application, this is only for testing
-	var topicsOfInterest []int
-	var lenTopicList = 100
-	//creates fake topicOfInterest if client has no interests
-	//creates random length with max being len(topicList)
-	if len(topicsOfInterest) == 0 {
-		maxLength := lenTopicList / 2
-		if maxLength == 0 {
-			value, err := rand.Int(rand.Reader, big.NewInt(int64(lenTopicList+1)))
-			if err != nil {
-				panic(err)
-			}
-			maxLength = int(value.Int64())
-		}
-		fmt.Println("MaxLength: ", maxLength)
-		lengthBig, err := rand.Int(rand.Reader, big.NewInt(int64(maxLength+1)))
-		if err != nil {
-			panic(err)
-		}
-		length := int(lengthBig.Int64())
-		fmt.Println("Actual length: ", length)
-		if length == 0 {
-			length++
-		}
-		fakeTopicsOfInterest := make([]int, length)
-
-		//fills the array with unique random ascending values in range with len(topicList)
-		for index := 0; index < length; index++ {
-			min := (index * (lenTopicList / length)) + 1
-			max := ((index + 1) * (lenTopicList / length))
-			if max == lenTopicList-1 {
-				max++
-			}
-			bigNumber, err := rand.Int(rand.Reader, big.NewInt(int64(max-min+1)))
-			if err != nil {
-				panic(err)
-			}
-			var number int = int(bigNumber.Int64()) + min
-
-			fakeTopicsOfInterest[index] = number
-
-			fmt.Println("Min: ", min, " Max: ", max, " Value chosen: ", fakeTopicsOfInterest[index])
-		}
-	}
-}