i'm currently implementing a secret sharing scheme.(shamir) In order to generate some secret shares, I need to generate some random numbers within a range. FOr this purpose, I have this very simple code:
val sharesPRG = SecureRandom()
fun generateShares(k :Int): List<Pair<BigDecimal,BigDecimal>> {
val xs = IntArray(k){ i -> sharesPRG.nextInt(5)}
return xs
}
I have left out the part that actually creates the shares as coordinates, just to make it reproduceable, and picked an arbitrarily small bound of 5. My problem is that I of course need these shares to be unique, it doesnt make sense to have shares that are the same. So would it be possible for the Securerandom.nextint to not return a value that it has already returned? Of course I could do some logic where I was checking for duplicates, but I really thought there should be something more elegant
If your
kis not too large you can keep adding random values to a set until it reaches sizek:You can then use the set as the source material to your list of pairs (
kneeds to be even):