MCPcopy Create free account
hub / github.com/DOSNetwork/core / HideEncode

Method HideEncode

group/mod/int.go:421–452  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

419// assuming the Int contains a uniform integer modulo M.
420// For a Int this always succeeds and returns non-nil.
421func (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().

Callers

nothing calls this directly

Calls 3

HideLenMethod · 0.95
SetInt64Method · 0.95
MulMethod · 0.45

Tested by

no test coverage detected