1
0
Fork 0

Initial Commit

Esse commit está contido em:
Sarah Jamie Lewis 2019-03-19 19:27:34 -07:00
commit 4ed330c083
4 arquivos alterados com 137 adições e 0 exclusões

9
LICENSE Normal file
Ver arquivo

@ -0,0 +1,9 @@
All code in this repository, unless otherwise indicated, is distributed under the following license:
Copyright 2019 Open Privacy Research Society
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

12
README.md Normal file
Ver arquivo

@ -0,0 +1,12 @@
# Trostle-Parrish: A somewhat homomorphic cryptosystem
TrostleParrish implements a somewhat-homomorphic cryptosystem outlined by [1] and [2]. Designed for efficient encryption in a PIR scheme.
This library is a work in progress, unvetted, unaudited. Please do not use this unless you understand the risks of doing so.
* [1] Trostle, Jonathan, and Andy Parrish. "Efficient computationally private information retrieval from anonymity or trapdoor groups." International Conference on Information Security. Springer, Berlin, Heidelberg, 2010.
* [2] Mayberry, Travis, Erik-Oliver Blass, and Agnes Hui Chan. "PIRMAP: Efficient private information retrieval for MapReduce." International Conference on Financial Cryptography and Data Security. Springer, Berlin, Heidelberg, 2013.

76
trostle-parrish.go Normal file
Ver arquivo

@ -0,0 +1,76 @@
package trostle_parrish
import (
"crypto/rand"
"math/big"
)
// TrostleParrish implements a somewhat-homomorphic cryptosystem outlined by [1] and [2]
// [1] Trostle, Jonathan, and Andy Parrish. "Efficient computationally private information retrieval from anonymity or trapdoor groups." International Conference on Information Security. Springer, Berlin, Heidelberg, 2010.
// [2] Mayberry, Travis, Erik-Oliver Blass, and Agnes Hui Chan. "PIRMAP: Efficient private information retrieval for MapReduce." International Conference on Financial Cryptography and Data Security. Springer, Berlin, Heidelberg, 2013.
type TrostleParrish struct {
m, b, T, N *big.Int
// We precompute these values.
bInv *big.Int
twon *big.Int
}
// More readable type definitions
type Ciphertext *big.Int
// Generate sets up a Trostle-Parish cryptosystem
// This system is "somewhat-homomorphic"
// 2^t = number of additions to support
// n = max plaintext bit size
func Generate(t int, n int) (pk TrostleParrish) {
for {
// choose secret m and b
pk.m, _ = rand.Prime(rand.Reader, (t*2)+n)
pk.b, _ = rand.Int(rand.Reader, pk.m)
pk.N = big.NewInt(int64(n))
pk.twon = new(big.Int).Exp(big.NewInt(2), pk.N, pk.m)
// T is our parameter that determines how many addition operations we can safely do
tbi := big.NewInt(int64(n/t))
pk.T = new(big.Int).Exp(big.NewInt(2), tbi, pk.m)
// Check that b has an inverse mod m
pk.bInv = new(big.Int).ModInverse(pk.b, pk.m)
for pk.bInv != nil {
return
}
}
}
// Encrypt takes in a Plaintext(x) and a keyset (pk)
func (pk TrostleParrish) Encrypt(x *big.Int) Ciphertext {
r, _ := rand.Int(rand.Reader, pk.T)
// r * 2^n
rtwon := new(big.Int).Mul(r, pk.twon)
// r * 2^n + x
rtwonx := new(big.Int).Add(rtwon, x)
// b * (r * 2^n + x)
bmod := new(big.Int).Mul(pk.b, rtwonx)
// b * (r * 2^n + x) mod m
return new(big.Int).Mod(bmod, pk.m)
}
// Add two ciphertexts together and return the result.
// Note that the number of meaningful additions is limited by the encryption parameters
func Add(a Ciphertext, b Ciphertext) Ciphertext {
return new(big.Int).Add(a, b)
}
// Decrypt takes in a Ciphertext(c) and Keyset(pk) and outputs the Plaintext
func (pk TrostleParrish) Decrypt(c Ciphertext) *big.Int {
// b^-1 * c
plaintext := new(big.Int).Mul(pk.bInv, c)
// b^-1 * c mod m
modm := new(big.Int).Mod(plaintext, pk.m)
// b^-1 * c mod m mod 2^n
return new(big.Int).Mod(modm, pk.twon)
}

40
trostle-parrish_test.go Normal file
Ver arquivo

@ -0,0 +1,40 @@
package trostle_parrish
import (
"math/big"
"testing"
)
func TestEncryptDecrypt(t *testing.T) {
// These are representative encryption params
pk := Generate(30, 255)
plaintext := big.NewInt(14)
ciphertext := pk.Encrypt(plaintext)
t.Logf("Encrypted: %v", ciphertext)
decrypted := pk.Decrypt(ciphertext)
t.Logf("Decrypted: %v", decrypted)
if decrypted.Cmp(plaintext) != 0 {
t.Error("Failed to Decrypt")
}
}
func TestMultipleAdditions(t *testing.T) {
// These params are ridiculous low, but they enable us to
// check that addition is working as expected.
pk := Generate(6, 32)
plaintext := big.NewInt(1)
total := pk.Encrypt(plaintext)
for i := 0; i < 128; i++ {
plaintext := big.NewInt(5)
ciphertext := pk.Encrypt(plaintext)
total = Add(total, ciphertext)
t.Logf("Encrypted: %v", total)
decrypted := pk.Decrypt(total)
t.Logf("Decrypted: %v", decrypted)
// We should see a wrap around at 16 based on our encryption params
if decrypted.Cmp(big.NewInt(int64(((i+1)*5)+1))) != 0 {
t.Errorf("Should have been able to do ~2^6 additions = 64, instead: %v", i)
}
}
}