Skip to content

Commit

Permalink
optimize QPP usage on selector randomization
Browse files Browse the repository at this point in the history
  • Loading branch information
xtaci committed Dec 17, 2024
1 parent 336e01c commit 80b146e
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions crypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,26 +243,35 @@ func (c *xteaBlockCrypt) Encrypt(dst, src []byte) { encrypt(c.block, dst, src, c
func (c *xteaBlockCrypt) Decrypt(dst, src []byte) { decrypt(c.block, dst, src, c.decbuf[:]) }

type qppCrypt struct {
key []byte
quantum *qpp.QuantumPermutationPad
}

// NewQPPCrypt https://link.springer.com/content/pdf/10.1140/epjqt/s40507-023-00164-3.pdf
func NewQPPCrypt(key []byte) (BlockCrypt, error) {
const numPad = 251
c := new(qppCrypt)
c.key = make([]byte, len(key))
copy(c.key, key)
c.quantum = qpp.NewQPP(key, numPad)
return c, nil
}

func (c *qppCrypt) Encrypt(dst, src []byte) {
copy(dst, src)
prng := c.quantum.CreatePRNG(dst[:8])
seed := make([]byte, 8+len(c.key))
copy(seed, dst[:8])
copy(seed[8:], c.key)
prng := c.quantum.CreatePRNG(seed)
c.quantum.EncryptWithPRNG(dst[8:], prng)
}

func (c *qppCrypt) Decrypt(dst, src []byte) {
copy(dst, src)
prng := c.quantum.CreatePRNG(dst[:8])
seed := make([]byte, 8+len(c.key))
copy(seed, dst[:8])
copy(seed[8:], c.key)
prng := c.quantum.CreatePRNG(seed)
c.quantum.DecryptWithPRNG(dst[8:], prng)
}

Expand Down

0 comments on commit 80b146e

Please sign in to comment.