Random generator

We are trying to develop a smart contract, where we need to generate a random key pair for ECC. However, it seems like all random generators use floats. This is a problem as @Kasper pinpoints that floats are not allowed.
Is there a recommended way to do this?

Do you need to generate the random key pair within the smart contract itself? This is usually not the desired behaviour for two reasons:

  1. Any computation that happens in the smart contract is on chain and uses fully public data, so any “secret” keys would not be secret at all.
  2. Smart contract computation is deterministic, and there is no good source of randomness on the chain.

If it is possible, I would suggest generating your keypairs outside of the smart contract, and, for instance, passing in the public key as an initialization parameter. Then hopefully you can exclude any key-pair generation code that makes use of floats from your on-chain smart contract code.

If this is not helpful, perhaps you could elaborate on what you are trying to do, and we may be able give better advice.

Hi again,

Thank you for your reply. I might have missed some details on my post.

I am trying to generate a key pair off chain for elliptic curves, where I need to generate a secret, which is a large number. Thus this number is generated at random. However, it seems like most random number generators seems to be using floats. That causes the cargo concordium build to fail, because of floats.

Are there any crates that you would recommend to do this? It seems most crates uses the standard library and/or floats

I’m not sure I follow, why are you using cargo-concordium for off-chain purposes? (I.e. generating your key pair)

Use only cargo-concordium for on-chain functionality (e.g. smart contracts). The key generation should probably not be part of this as per the remarks Thomas mentioned above.

Regarding the library - which algorithm(s)/curves do you want to support? We e.g. use the dalek crate for our ed25519 purposes.


1 Like