HideEncode a Int such that it appears indistinguishable from a HideLen()-byte string chosen uniformly at random, assuming the Int contains a uniform integer modulo M. For a Int this always succeeds and returns non-nil.
(rand cipher.Stream)
| 419 | // assuming the Int contains a uniform integer modulo M. |
| 420 | // For a Int this always succeeds and returns non-nil. |
| 421 | func (i *Int) HideEncode(rand cipher.Stream) []byte { |
| 422 | |
| 423 | // Lengh of required encoding |
| 424 | hidelen := i.HideLen() |
| 425 | |
| 426 | // Bit-position of the most-significant bit of the modular integer |
| 427 | // in the most-significant byte of its encoding. |
| 428 | highbit := uint((i.M.BitLen() - 1) & 7) |
| 429 | |
| 430 | var enc big.Int |
| 431 | for { |
| 432 | // Pick a random multiplier of a suitable bit-length. |
| 433 | var b [1]byte |
| 434 | rand.XORKeyStream(b[:], b[:]) |
| 435 | mult := int64(b[0] >> highbit) |
| 436 | |
| 437 | // Multiply, and see if we end up with |
| 438 | // a Int of the proper byte-length. |
| 439 | // Reroll if we get a result larger than HideLen(), |
| 440 | // to ensure uniformity of the resulting encoding. |
| 441 | enc.SetInt64(mult).Mul(&i.V, &enc) |
| 442 | if enc.BitLen() <= hidelen*8 { |
| 443 | break |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | b := enc.Bytes() // may be shorter than l |
| 448 | if ofs := hidelen - len(b); ofs != 0 { |
| 449 | b = append(make([]byte, ofs), b...) |
| 450 | } |
| 451 | return b |
| 452 | } |
| 453 | |
| 454 | // HideDecode a uniform representation of this object from a slice, |
| 455 | // whose length must be exactly HideLen(). |