forked from MystenLabs/sui
-
Notifications
You must be signed in to change notification settings - Fork 3
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
Example of drand library in Move. This also includes a needed fix in translate.rs. #51
Open
jcivlin
wants to merge
5
commits into
anza-xyz:solana
Choose a base branch
from
jcivlin:053024-fixing-translate-more-impl-Constant
base: solana
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
881530a
052824-fixing-bug-in-translate: ini
jcivlin e0efffa
052824-fixing-bug-in-translate: adding Some/None
jcivlin c7e4a37
052824-fixing-bug-in-translate: adding debug to rttydesc.rs
jcivlin e97b283
053024-fixing-translate-more-impl-Constant: fix and example
jcivlin d78e84c
053024-fixing-translate-more-impl-Constant: correct example
jcivlin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
external-crates/move/solana/move-mv-llvm-compiler/tests/drand-tests/Move.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "Games" | ||
version = "0.0.1" | ||
|
||
[dependencies] | ||
Sui = { local = "../../../../../../crates/sui-framework/packages/sui-framework" } | ||
|
||
[addresses] | ||
games = "0x0" |
87 changes: 87 additions & 0 deletions
87
external-crates/move/solana/move-mv-llvm-compiler/tests/drand-tests/drand_lib.move
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
/// Helper module for working with drand outputs. | ||
/// Currently works with chain 52db9ba70e0cc0f6eaf7803dd07447a1f5477735fd3f661792ba94600c84e971 (quicknet). | ||
/// | ||
/// See examples of how to use this in drand_based_lottery.move and drand_based_scratch_card.move. | ||
/// | ||
/// If you want to use this module with the default network which has a 30s period, you need to change the public key, | ||
/// genesis time and include the previous signature in verify_drand_signature. See https://drand.love/developer/ or the | ||
/// previous version of this file: https://github.com/MystenLabs/sui/blob/92df778310679626f00bc4226d7f7a281322cfdd/sui_programmability/examples/games/sources/drand_lib.move | ||
|
||
|
||
// Copied from sui_programmability/examples/games/sources/drand_lib.move | ||
// Run it from current directory as move-mv-llvm-compiler -c ./drand_lib.move -p ./Move.toml --test {-O|-S} -o <out_dir> | ||
|
||
module games::drand_lib { | ||
use std::hash::sha2_256; | ||
use std::vector; | ||
|
||
use sui::bls12381; | ||
|
||
/// Error codes | ||
const EInvalidRndLength: u64 = 0; | ||
const EInvalidProof: u64 = 1; | ||
|
||
/// The genesis time of chain 52db9ba70e0cc0f6eaf7803dd07447a1f5477735fd3f661792ba94600c84e971. | ||
const GENESIS: u64 = 1692803367; | ||
/// The public key of chain 52db9ba70e0cc0f6eaf7803dd07447a1f5477735fd3f661792ba94600c84e971. | ||
const DRAND_PK: vector<u8> = | ||
x"83cf0f2896adee7eb8b5f01fcad3912212c437e0073e911fb90022d3e760183c8c4b450b6a0a6c3ac6a5776a2d1064510d1fec758c921cc22b0e17e63aaf4bcb5ed66304de9cf809bd274ca73bab4af5a6e9c76a4bc09e76eae8991ef5ece45a"; | ||
|
||
/// The time in seconds between randomness beacon rounds. | ||
const PERIOD: u64 = 3; | ||
|
||
/// Check that a given epoch time has passed by verifying a drand signature from a later time. | ||
/// round must be at least (epoch_time - GENESIS)/PERIOD + 1). | ||
public fun verify_time_has_passed(epoch_time: u64, sig: vector<u8>, round: u64) { | ||
assert!(epoch_time <= GENESIS + PERIOD * (round - 1), EInvalidProof); | ||
verify_drand_signature(sig, round); | ||
} | ||
|
||
/// Check a drand output. | ||
public fun verify_drand_signature(sig: vector<u8>, round: u64) { | ||
// Convert round to a byte array in big-endian order. | ||
let round_bytes: vector<u8> = vector[0, 0, 0, 0, 0, 0, 0, 0]; | ||
let i = 7; | ||
|
||
// Note that this loop never copies the last byte of round_bytes, though it is not expected to ever be non-zero. | ||
while (i > 0) { | ||
let curr_byte = round % 0x100; | ||
let curr_element = vector::borrow_mut(&mut round_bytes, i); | ||
*curr_element = (curr_byte as u8); | ||
round = round >> 8; | ||
i = i - 1; | ||
}; | ||
|
||
// Compute sha256(prev_sig, round_bytes). | ||
let digest = sha2_256(round_bytes); | ||
// Verify the signature on the hash. | ||
let drand_pk = DRAND_PK; | ||
assert!(bls12381::bls12381_min_sig_verify(&sig, &drand_pk, &digest), EInvalidProof); | ||
} | ||
|
||
/// Derive a uniform vector from a drand signature. | ||
public fun derive_randomness(drand_sig: vector<u8>): vector<u8> { | ||
sha2_256(drand_sig) | ||
} | ||
|
||
// Converts the first 16 bytes of rnd to a u128 number and outputs its modulo with input n. | ||
// Since n is u64, the output is at most 2^{-64} biased assuming rnd is uniformly random. | ||
public fun safe_selection(n: u64, rnd: &vector<u8>): u64 { | ||
assert!(vector::length(rnd) >= 16, EInvalidRndLength); | ||
let m: u128 = 0; | ||
let i = 0; | ||
while (i < 16) { | ||
m = m << 8; | ||
let curr_byte = *vector::borrow(rnd, i); | ||
m = m + (curr_byte as u128); | ||
i = i + 1; | ||
}; | ||
let n_128 = (n as u128); | ||
let module_128 = m % n_128; | ||
let res = (module_128 as u64); | ||
res | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make it an
entry
.