// alice implements all the necessary functionality for Alice's part of the // communication func alice(msg string, channel chan []*big.Int, conn net.Conn) { prime := shamir.GeneratePrime(PRIMEBITS) primeSlice := []*big.Int{prime} fmt.Printf("Alice generates a prime number:\n%x\n\n",prime) fmt.Printf("Alice sends the prime number to Bob\n") channel <- primeSlice send(channel, conn) fmt.Println("Alice wants to send the following message: " + msg) a, aInv := shamir.GenerateExponents(prime) fmt.Println("Alice computes a secret Exponent and the inverse of it") fmt.Printf("Alice's secret exponent:\n%x\n", a) fmt.Printf("Alice's secret inverse:\n%x\n\n", aInv) fmt.Println("Alice encrypts her message!") var messageInt []*big.Int = shamir.SliceMessage(msg, prime) x := shamir.CalculateParallel(messageInt, a, prime) fmt.Printf("Alice now sends the encrypted message to Bob:\n%x\n\n",shamir.GlueMessage(x)) channel <- x send(channel, conn) fmt.Println("Alice is waiting for Bob's answer...") receive(channel, conn) x = <-channel fmt.Println("Alice received the double-encrypted message and is now" +" decrypting her part!") y := shamir.CalculateParallel(x, aInv, prime) fmt.Printf("Alice now sends the partly decrypted message to Bob:\n%x\n\n",shamir.GlueMessage(y)) channel <- y send(channel, conn) }
// main implements all necessary functionality to setup the conversation // between Alice and Bob func main() { runtimeTest() var message string fmt.Print("Please enter the message to be exchanged in encrypted form: ") reader := bufio.NewReader(os.Stdin) for { line, err := reader.ReadString('\n') if err != nil { panic(err) } message = line if line != "" || err != nil { break } } prime := shamir.GeneratePrime(PRIMEBITS) fmt.Printf("Both Alice and Bob agree on a prime number:\n%x\n\n", prime) channel := make(chan []*big.Int) stop := make(chan int) go alice(message, prime, channel) go bob(prime, channel, stop) <-stop }
// main implements all necessary functionality to setup the conversation // between Alice and Bob func runtimeTest() { fmt.Println("CPU 's # ", runtime.NumCPU()) prime := shamir.GeneratePrime(PRIMEBITS) var message string //fmt.Print(prime, message) a, aInv := shamir.GenerateExponents(prime) var time0 time.Time var duration time.Duration //var words int for nCpu := 1; nCpu <= runtime.NumCPU(); nCpu++ { runtime.GOMAXPROCS(nCpu) fmt.Println("Using CPU # ", nCpu) //fmt.Println("\t\t Dauer Normal \t\tDauer Parallel") for i := 1; i < 5; i++ { word := math.Pow10(i) // words = 10 ^ i var buffer bytes.Buffer fmt.Print("Chars # ", word*50, "\t\t") var j float64 = 0 for j = 0; j < word; j++ { buffer.WriteString("abcdefghijklmnopqrstuvwxzy 1234567890?!, .-#+* ()[]") } message = buffer.String() var messageInt []*big.Int = shamir.SliceMessage(message, prime) // Normal time0 = time.Now() shamir.Calculate(messageInt, a, prime) duration = time.Since(time0) fmt.Print(float64(duration.Nanoseconds()) / 1000 / 1000) // Parrallel time0 = time.Now() shamir.CalculateParallel(messageInt, a, prime) duration = time.Since(time0) fmt.Print("\t\t", float64(duration.Nanoseconds())/1000/1000) fmt.Println("") } } var messageInt []*big.Int = shamir.SliceMessage("foo", prime) x := shamir.CalculateParallel(messageInt, a, prime) shamir.GlueMessage(x) shamir.Calculate(x, aInv, prime) }