Skip to content

Commit

Permalink
fix Encrypt
Browse files Browse the repository at this point in the history
  • Loading branch information
xtaci committed Jul 12, 2024
1 parent b43b714 commit 023fe7f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
8 changes: 4 additions & 4 deletions dppk.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,16 +171,16 @@ RETRY:
}

// Encrypt encrypts a message using the given public key.
func (priv *PrivateKey) Encrypt(pk *PublicKey, msg []byte) (Ps *big.Int, Qs *big.Int, err error) {
func Encrypt(pk *PublicKey, msg []byte) (Ps *big.Int, Qs *big.Int, err error) {
// Convert the message to a big integer
secret := new(big.Int).SetBytes(msg)
if secret.Cmp(priv.PublicKey.prime) >= 0 {
if secret.Cmp(pk.prime) >= 0 {
return nil, nil, errors.New(ERRMSG_DATA_EXCEEDED_FIELD)
}

// Extend the vectors U and Q with a constant term of 1
vecUExt := make([]*big.Int, len(priv.PublicKey.vecU)+1)
vecVExt := make([]*big.Int, len(priv.PublicKey.vecV)+1)
vecUExt := make([]*big.Int, len(pk.vecU)+1)
vecVExt := make([]*big.Int, len(pk.vecV)+1)
copy(vecUExt, pk.vecU)
copy(vecVExt, pk.vecV)
vecUExt[len(vecUExt)-1] = big.NewInt(1)
Expand Down
12 changes: 4 additions & 8 deletions dppk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ import (
func TestDPPK(t *testing.T) {
alice, err := GenerateKey(10)
assert.Nil(t, err)
bob, err := GenerateKey(10)
assert.Nil(t, err)

secret := []byte("hello quantum")
Ps, Qs, err := bob.Encrypt(&alice.PublicKey, secret)
Ps, Qs, err := Encrypt(&alice.PublicKey, secret)
assert.Nil(t, err)
t.Log("secret:", string(secret))

Expand All @@ -31,11 +29,9 @@ func TestDPPKSmallPrime(t *testing.T) {
prime := "977"
alice, err := GenerateKeyWithPrime(10, prime)
assert.Nil(t, err)
bob, err := GenerateKeyWithPrime(10, prime)
assert.Nil(t, err)

secret := []byte("X")
Ps, Qs, err := bob.Encrypt(&alice.PublicKey, secret)
Ps, Qs, err := Encrypt(&alice.PublicKey, secret)
assert.Nil(t, err)
t.Log("secret:", string(secret))

Expand Down Expand Up @@ -75,14 +71,14 @@ func BenchmarkDPPKEncrypt(b *testing.B) {
dppk, _ := GenerateKey(5)
secret := []byte("hello quantum")
for i := 0; i < b.N; i++ {
_, _, _ = dppk.Encrypt(&dppk.PublicKey, secret)
_, _, _ = Encrypt(&dppk.PublicKey, secret)
}
}

func BenchmarkDPPKDecrypt(b *testing.B) {
dppk, _ := GenerateKey(5)
secret := []byte("hello quantum")
Ps, Qs, _ := dppk.Encrypt(&dppk.PublicKey, secret)
Ps, Qs, _ := Encrypt(&dppk.PublicKey, secret)
for i := 0; i < b.N; i++ {
_, _, _ = dppk.Decrypt(Ps, Qs)
}
Expand Down

0 comments on commit 023fe7f

Please sign in to comment.