2

most smart contracts that rely on "randomness", will require said entropy to be in a number format. A practical way to extract entropy from a arbitrary payload is by using a hashing function. This means that it's intuitive have the entropy in bytes format inside a Tezos smart contract. There are no "direct" ways to convert arbitrary bytes to nat/int in Michelson, so someone proposed (thank you Rodrigo) to go with something like this:

def entropy_to_int(self, param):
    entropy = sp.bytes("0x00") 
    sp.result(sp.to_int(sp.unpack(sp.bytes("0x050a00000020")+sp.blake2b(entropy), sp.TBls12_381_fr).open_some()))

It looks like it's working as intended. However as this is very security relevant is there a security concern when going with this solution (i.e. skewed/biased distribution of the resulting numbers)?

2
  • The trick is that it uses the representation of the Bls12_381_fr scalar field, so you bypass any issue with the encoding of nat. You should get a uniform distribution out of this, but I have only taken a cursory look.
    – Arthur B
    Feb 1 at 14:10
  • Bls12_381_fr does not support all 32 byte integers. (e.g: 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff will fail). You could just drop the latest byte and append 0x00. It is extremely unlikely to ever happen, but. Feb 1 at 17:39

1 Answer 1

2

The next protocol Mumbai brings new features to Michelson. It will be possible to perform logical operations on bytes, including converting between bytes and integers.

Documentation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.