Skip to content

Commit

Permalink
add a fast PRNG for simple extension of seed, and move CreatePRNG to
Browse files Browse the repository at this point in the history
public function
  • Loading branch information
xtaci committed Dec 22, 2024
1 parent fe38d2c commit aa7b746
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
21 changes: 18 additions & 3 deletions qpp.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ func NewQPP(seed []byte, numPads uint16) *QuantumPermutationPad {
reverse(pad, rpad)
}

qpp.encRand = qpp.CreatePRNG(seed) // Create default PRNG for encryption
qpp.decRand = qpp.CreatePRNG(seed) // Create default PRNG for decryption
qpp.encRand = CreatePRNG(seed) // Create default PRNG for encryption
qpp.decRand = CreatePRNG(seed) // Create default PRNG for decryption

return qpp
}
Expand All @@ -118,7 +118,7 @@ func (qpp *QuantumPermutationPad) Decrypt(data []byte) {

// CreatePRNG creates a deterministic pseudo-random number generator based on the provided seed
// It uses HMAC and PBKDF2 to derive a random seed for the PRNG
func (qpp *QuantumPermutationPad) CreatePRNG(seed []byte) *Rand {
func CreatePRNG(seed []byte) *Rand {
mac := hmac.New(sha256.New, seed)
mac.Write([]byte(PM_SELECTOR_IDENTIFIER))
sum := mac.Sum(nil)
Expand All @@ -135,6 +135,21 @@ func (qpp *QuantumPermutationPad) CreatePRNG(seed []byte) *Rand {
return rd
}

// FastPRNG creates a deterministic pseudo-random number generator based on the provided seed
func FastPRNG(seed []byte) *Rand {
sha := sha256.New()
sum := sha.Sum(seed)

// Create and return PRNG
rd := &Rand{}
rd.xoshiro[0] = binary.LittleEndian.Uint64(sum[0:8])
rd.xoshiro[1] = binary.LittleEndian.Uint64(sum[8:16])
rd.xoshiro[2] = binary.LittleEndian.Uint64(sum[16:24])
rd.xoshiro[3] = binary.LittleEndian.Uint64(sum[24:32])
rd.seed64 = xoshiro256ss(&rd.xoshiro)
return rd
}

// EncryptWithPRNG encrypts the data using the Quantum Permutation Pad with a custom PRNG
// This function shares the same permutation matrices
func (qpp *QuantumPermutationPad) EncryptWithPRNG(data []byte, rand *Rand) {
Expand Down
19 changes: 17 additions & 2 deletions qpp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,11 +320,11 @@ func TestEncryptionMixedPRNG(t *testing.T) {
msg := make([]byte, len(original))
copy(msg, original)

rand_enc := qpp.CreatePRNG(seed)
rand_enc := CreatePRNG(seed)
qpp.EncryptWithPRNG(msg, rand_enc)
assert.NotEqual(t, original, msg, "not encrypted")

rand_dec := qpp.CreatePRNG(seed)
rand_dec := CreatePRNG(seed)
qpp.DecryptWithPRNG(msg, rand_dec)
assert.Equal(t, original, msg, "not equal")
}
Expand Down Expand Up @@ -382,3 +382,18 @@ func BenchmarkRandV2(b *testing.B) {
_ = encRand.Uint32()
}
}

func BenchmarkCreatePRNG(b *testing.B) {
seed := make([]byte, 32)
io.ReadFull(rand.Reader, seed)
for i := 0; i < b.N; i++ {
_ = CreatePRNG(seed)
}
}
func BenchmarkFastPRNG(b *testing.B) {
seed := make([]byte, 32)
io.ReadFull(rand.Reader, seed)
for i := 0; i < b.N; i++ {
_ = FastPRNG(seed)
}
}

0 comments on commit aa7b746

Please sign in to comment.