#prng bytes

23 messages · Page 1 of 1 (latest)

humble whale
#

Is there a technical reason we don't expose a prng fn for getting a random byte slice, or filling a byte slice with random bytes?

At the momen the only options for random are shuffling a vec and getting a u64.

#

@hollow shale? I think you wrote the prng.

white palm
#

Doesnt bytes_new do this?

#

I havent tried but i recall looking at the prng code in the host side quite a bit the other day about another question and recall seeing a method of bytes_new

#

Maybe its not exposed in the sdk?

#

This method takes a size and a budget as inputs, and it returns a Result<ScBytes, HostError>. It initializes a vector with the given size, fills it with random bytes using the chacha20_fill_bytes function, and then returns the bytes.

hollow shale
#

yeah prng_bytes_new is supposed to do that. is it not exposed in the SDK/

#

?

white palm
#

It might be if prng_bytes_new might be the function on the sdk i think its just bytes_new in the host

#

I can check shortly

#

Ya actually i dont see that exposed in the sdk

#

It would be pretty easy to add tho

white palm
#

You could still do it with the current sdk btw but not as efficiently so we should add that method.

you would need to write a wrapper function that calls u64_in_range multiple times to generate random chunks of data and then manually assemble these into a Bytes object.
(In the current methods exposed in the sdk)

humble whale
white palm
#
  pub fn bytes_new(&self, size: u32, budget: &Budget) -> Bytes {
    let env = self.env();
    internal::Env::prng_bytes_new(env, size, budget)
}.unwrap_infallible()
    }```
I guess something like this?
#

But maybe call it prng_bytes_new like u said

#

Dumb question but when we come across something that isnt implemented in the sdk is it feasible to directly import the host function to use it in a contract

ashen moss
#

I checked the docs for prng to see if it could produce bytes (for the question about salts for multiple deployments) but could not find it

frosty barn
#

is it feasible to directly import the host function to use it in a contract
it's currently a bit tricky because SDK hides the 'raw' host function interface. you could make it public, but I'd hope we just add the missing wrappers (going forward we shouldn't have such gaps)

humble whale
#

There's nothing stopping someone from importing the soroban-env-guest and using it directly if they wanted.

white palm
late kite
#

I am in favor of exposing these functions, I just used the Prng and I had to iterate to be able to fill a slice of random numbers 🤕

humble whale
#

They're implemented and coming to next release.