Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a randomBytes builtin to generate cryptographically secure bytes #2902

Closed
Tracked by #1283
paulcadman opened this issue Jul 17, 2024 · 6 comments · Fixed by #3129
Closed
Tracked by #1283

Add a randomBytes builtin to generate cryptographically secure bytes #2902

paulcadman opened this issue Jul 17, 2024 · 6 comments · Fixed by #3129
Labels
anoma backend:nockma core Related to JuvixCore enhancement New feature or request pending-review
Milestone

Comments

@paulcadman
Copy link
Collaborator

paulcadman commented Jul 17, 2024

Depends on

This issue proposes adding a builtin randomBytes function.

--- Generate a given number of cryptographically secure random bytes
builtin
axiom randomBytes : Nat -> Bytes

This is required to populate the rseed and nonce fields of an Anoma Resource. The types of these fields are not yet determined.

Backend Representation

Core

We can use a Haskell library e.g crypto-rng to implement the builtin.

Nockma

We can use the same implementation as Core in the Juvix Nockma evaluator.

We will need a randomBytes implementation in the Anoma Hoon stdlib in order to run programs containing this builtin in the Anoma Nock VM.

Native, Cairo, Risc0

The implementation of randomBytes for the native, Cairo and Risc0 backends is deferred.

@paulcadman paulcadman added enhancement New feature or request pending-review core Related to JuvixCore backend:nockma anoma labels Jul 17, 2024
@paulcadman paulcadman added this to the 0.6.5 milestone Jul 17, 2024
@lukaszcz
Copy link
Collaborator

lukaszcz commented Jul 17, 2024

On this note, we should add a pragma that would indicate that a certain identifier is "volatile" to prevent the optimisations from moving it around, duplicating and/or merging calls to it (this can happen currently).

@paulcadman
Copy link
Collaborator Author

On this note, we should add a pragma that would indicate that a certain identifier is "volatile" to prevent the optimisations from moving it around, duplicating and/or merging calls to it (this can happen currently).

Could you describe this in more detail? Or raise an issue as it seems important.

@lukaszcz
Copy link
Collaborator

Yes, I'll raise an issue

@paulcadman paulcadman modified the milestones: 0.6.5, 0.6.6 Jul 19, 2024
@paulcadman paulcadman modified the milestones: 0.6.5, 0.6.6 Aug 15, 2024
@paulcadman
Copy link
Collaborator Author

Link to discussed issue:

@heueristik
Copy link

Does randomBytes : Nat -> Bytes fit with the arm-transparent definitions

type Nonce := mkNonce {unNonce : Nat};
type RandSeed := mkRandSeed {unRandSeed : Nat};

?

@paulcadman
Copy link
Collaborator Author

paulcadman commented Oct 9, 2024

Does randomBytes : Nat -> Bytes fit with the arm-transparent definitions

type Nonce := mkNonce {unNonce : Nat};
type RandSeed := mkRandSeed {unRandSeed : Nat};

?

Current state of affairs:

Consequences:

  • The seed argument for randomBytes will have to be passed in to the main function that creates the transaction.
  • Because neither the Anoma API randomBytes nor the transaction data structure uses ByteArray it doesn't make sense to use ByteArray in the library. We're better off just using a newtype type Bytes := mkBytes Nat.

paulcadman added a commit that referenced this issue Oct 29, 2024
This PR adds frontend support for the Anoma Random API:

The frontend builtin APIs are:

```
builtin anoma-random-generator
axiom RandomGenerator : Type;

builtin anoma-random-generator-init
axiom randomGeneratorInit : Nat -> RandomGenerator;

builtin anoma-random-generator-split
axiom randomGeneratorSplit : RandomGenerator
  -> Pair RandomGenerator RandomGenerator;

builtin anoma-random-next-bytes
axiom randomNextBytes : Nat
  -> RandomGenerator
  -> Pair ByteArray RandomGenerator;
```

### Nockma Evaluator

The Nockma evaluator intercepts the corresponding Anoma random stdlib
calls using the
[System.Random](https://hackage.haskell.org/package/random-1.2.1.2/docs/System-Random.html)
API. The implementation uses the
[splitmix](https://hackage.haskell.org/package/splitmix-0.1.0.5/docs/System-Random-SplitMix.html)
generator directly because it has an API to destructure the generator
into a pair of integers. We can use this to serialise the generator.



* Closes #2902
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
anoma backend:nockma core Related to JuvixCore enhancement New feature or request pending-review
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants