-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* initial commit * add unit test * Update PRNG * update permutation interface * remove redundant print * Retry only when timeout occurs
- Loading branch information
Showing
6 changed files
with
113 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package utils | ||
|
||
import ( | ||
"crypto/sha256" | ||
) | ||
|
||
func gcd(a, b int) int { | ||
if b == 0 { | ||
return a | ||
} | ||
return gcd(b, a%b) | ||
} | ||
|
||
// Pseudo-random number generator based on a polynomial permutation | ||
// `f(x) = (a*x + b) mod maxElements“ | ||
// where `a` and `maxElements` are coprime, guarantees unique indices | ||
// and `b` is a random integer - ofset | ||
type PolynomialPermutation struct { | ||
a int | ||
b int | ||
maxElements int | ||
} | ||
|
||
// NewPolynomialPermutation creates a new PolynomialPermutation | ||
func NewPolynomialPermutation(publicSeed []byte, maxElements int) *PolynomialPermutation { | ||
// Hash the public seed to derive coefficients | ||
hash := sha256.Sum256(publicSeed) | ||
seed := int64(hash[0]) + int64(hash[1])<<8 + int64(hash[2])<<16 + int64(hash[3])<<24 | ||
|
||
// Derive coefficients a and b | ||
a := int(seed%int64(maxElements)) + 1 | ||
b := int((seed / int64(maxElements)) % int64(maxElements)) | ||
|
||
// Ensure a is coprime with maxElements | ||
for gcd(a, maxElements) != 1 { | ||
a = (a + 1) % maxElements | ||
} | ||
|
||
return &PolynomialPermutation{ | ||
a: a, | ||
b: b, | ||
maxElements: maxElements, | ||
} | ||
} | ||
|
||
func (p *PolynomialPermutation) Permute(index int) int { | ||
return (p.a*index + p.b) % p.maxElements | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package utils | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
// TestPermutationAlgorihm tests the GenerateIndices function. | ||
func TestPermutationAlgorihm(t *testing.T) { | ||
now := time.Now().UnixNano() | ||
publicSeed := []byte{byte(now), byte(now >> 8), byte(now >> 16), byte(now >> 24)} | ||
numSamples := 128 | ||
totalIndices := 128 | ||
|
||
rand := NewPolynomialPermutation(publicSeed, totalIndices) | ||
|
||
// Check for uniqueness | ||
uniqueIndices := make(map[int]int) | ||
for idx := range numSamples { | ||
uniqueIndices[rand.Permute(idx)] = +1 | ||
|
||
if uniqueIndices[rand.Permute(idx)] > 1 { | ||
t.Fatalf("Index %d is not unique", idx) | ||
} | ||
} | ||
} | ||
|
||
func TestPermutationWithSeed(t *testing.T) { | ||
seed := []byte{51, 132, 112, 103, 0, 0, 0, 0} | ||
expected := []int{78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77} | ||
maxElements := 128 | ||
numSamples := 128 | ||
|
||
rand := NewPolynomialPermutation(seed, maxElements) | ||
|
||
for idx := range numSamples { | ||
if rand.Permute(idx) != expected[idx] { | ||
t.Fatalf("Index %d is not unique", idx) | ||
} | ||
} | ||
} |