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

randomness doc inital #366

Merged
merged 10 commits into from
May 28, 2024
Merged

randomness doc inital #366

merged 10 commits into from
May 28, 2024

Conversation

zjma
Copy link
Contributor

@zjma zjma commented May 27, 2024

Description

Checklist

  • Do all Lints pass?
    • Have you ran pnpm spellcheck?
    • Have you ran pnpm fmt?
    • Have you ran pnpm lint?

@zjma zjma requested review from gregnazario and 0xZihan as code owners May 27, 2024 01:53
Copy link

vercel bot commented May 27, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
developer-docs-nextra ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 28, 2024 11:42pm

Copy link

netlify bot commented May 27, 2024

Deploy Preview for aptos-developer-docs ready!

Name Link
🔨 Latest commit 4736d07
🔍 Latest deploy log https://app.netlify.com/sites/aptos-developer-docs/deploys/66566b642438ea0009af7baa
😎 Deploy Preview https://deploy-preview-366--aptos-developer-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

```

The above example uses function `u64_range()` but many other basic types are also supported.
Here's a quick overview of all the API, where `T` can be one of `u8, u16, u32, u64, u128, u256`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's link to the Move module too?

Copy link
Contributor

@alinush alinush left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pre-approving, and will submit comments to be addressed later.


### How randomness may have been obtained insecurely/overcomplicated

Building a lottery system and pick a random winner from n participants is trivial, at least in the centralized world with a trusted server: the backend simply calls a `random.randint(0, n-1)` (this is a python example).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$n$ not n

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better n? no other latex here yet...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, if you prefer that!

2. Once the randomness seed is revealed, the clients fetch the it and derive the winner locally.
3. One of the participants submits the seed and the winner on chain.

```rust
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would link to the drand example in aptos-move/move-examples here, and copy & paste some of the code from it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Nextra, you can do code snippets directly imported, without copy pasting. Let's try moving there

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

separate PR?


### Achieve both simplicity and security with Aptos randomness API

Using Aptos randomness API, the implementation will look like this:
Copy link
Contributor

@alinush alinush May 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above: link to (and copy-paste) the code in aptos-move/examples

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

separate PR?


### Build aptos-cli from latest source

Some changes have been made to aptos-cli to support compiling a randomness consuming contract.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aptos-cli (plus link?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aptos CLI!

Here are some ideas of how to prevent undergasing attack generally.

- Make your entry function gas independent from the randomness outcome.
Simplest example is to not “act” on the randomness outcome, i.e. read it and store it for later.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can link to the example in AIP-41 (my AIP-41 changes did not merge yet though)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

separate PR?

Copy link
Collaborator

@gregnazario gregnazario left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move this to the Nextra pages. @hariria can help you if you have questions and the documentation for docs is here https://preview.aptos.dev/en/developer-platforms/contribute

Comment on lines 1 to 3
---
title: "Aptos Roll: on-chain public instant randomness API"
---
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update nextra as well for randomness.

These are the old docs, which will be replaced.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved.
(or should be copied?)

2. Once the randomness seed is revealed, the clients fetch the it and derive the winner locally.
3. One of the participants submits the seed and the winner on chain.

```rust
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Nextra, you can do code snippets directly imported, without copy pasting. Let's try moving there


It's now a good time to think about what user actions need randomness API, write them down, and make sure they are private and have the right attribute, as shown in the example below.

```rust
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use move instead of rust

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

fun permutation(n: u64): vector<u64>;
```

### Security warning: randomness API calls in non-entry functions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we keep a single Security as ### and the specific paragraphs as #### ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Copy link

@danielxiangzl danielxiangzl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "on-chain randomness" link in the main page from the preview does not link to this doc, but drand instead.

@zjma
Copy link
Contributor Author

zjma commented May 28, 2024

The "on-chain randomness" link in the main page from the preview does not link to this doc, but drand instead.

can you share a url/screenshot?

@danielxiangzl
Copy link

The "on-chain randomness" link in the main page from the preview does not link to this doc, but drand instead.

can you share a url/screenshot?

https://developer-docs-nextra-hklfyxvke-aptoslabs.vercel.app/en
or click "Preview" above
Screenshot 2024-05-28 at 1 26 44 PM

@danielxiangzl
Copy link

Also take a look at the preview of the doc. The title is incorrect right now.
Screenshot 2024-05-28 at 1 27 50 PM

@zjma
Copy link
Contributor Author

zjma commented May 28, 2024

Also take a look at the preview of the doc. The title is incorrect right now. Screenshot 2024-05-28 at 1 27 50 PM

fixed

Co-authored-by: Gerardo Di Giacomo <gedigi@live.com>

Not recommended, but if you intend to expose such a randomness-dependent function to the public, you can bypass the compiler check by annotating your function with `#[lint_allow_unsafe_randomness]`.

```rust

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/rust/move?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@zjma zjma merged commit 5998a43 into main May 28, 2024
8 checks passed
@zjma zjma deleted the zjma/randomness-doc branch May 28, 2024 23:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants