Giter Club home page Giter Club logo

randao's Introduction

Join the chat at https://gitter.im/randao/randao

Random number in programming is very important!

RNG in a deterministic system is very difficult

Solutions

A DAO (decentralised autonomous organisation) that anyone can participate in, and the random number is generated by all participants together! First of all, we need to create a RANDAO contract in the blockchain, which defines the participation rules. Then the basic process of generating a random number can be divided into three phases:

The first phase: collecting valid sha3(s)

Anyone who want to participate in the random number generation needs to send a transaction to the contract C with m ETH as pledge in a specified time period (e.g, 6 block period, approximately 72s), accompanied by the result of sha3(s), s is the secret number respective picked by participant.

The second phase: collecting valid s

After the first phase, anyone who submitted sha3(s) successfully needs to send a transaction with the secret number s in the first stage to contract C within a specified time period. Contract C will check if s is valid by running sha3 against s and comparing the result with previous committed data. Valid s will be saved to the collection of seeds to finally generate the random number.

The third phase: calculating a random number, refund pledged ETH and bonus
  1. After all secret numbers have been successfully collected, contract C will calculate the random number from the function f(s1,s2,...,sn), the result will be written to the storage of C, and the result will be sent to all other contracts that requested the random number before.
  2. Contract C will send back the pledge to the participants in the first phase, and the profit is divided into equal parts and sent to all participants as an additional bonus. The profit comes from the fees that is paid by other contracts that consume the random number.

Additional rules

In order to ensure the RNG can't be manipulated, as well as for safety and efficiency, the contract C has the following additional rules:

  1. The first phase, if two or more of the same sha3(s) are submitted in sequence, only the first one is accepted.

  2. The first phase, there is a requirement for minimum number of participants, if it fails to collect enough sha3(s) within the time period, then RNG at this block height will fail.

  3. If a participant submits the sha3(s) and it is accepted by contract C, he must reveal the s in the second phase.

    3.1 If the participant fails to reveal s in the second phase, then the m ETH sent in the first phase will be confiscated without providing a return.

    3.2 If one or more s isn't revealed in the second phase, RNG at this block height will fail. Confiscated ETHs will be divided equally and send to other participants who revealed s at the second phase. The fees paid by other contracts will be refunded.

Incentive

The RNG cycle is very short, and could be for example 20 cycles in one hour, if one cycle's profit is 0.001% , the monthly rate of return is up to 0.00001 * 20 * 24 * 30 = 0.144. Targeting to 14.4% monthly rate of return, and RNG has n participants on average, the running costs of contract is n * 3 * 500 * gasPrice + Ccost. (Ccost is gas consumed by contract internally, including computing and storage, etc. ) Assuming each random numbers has r time requests on average, the call price is p ETH, the income is r * p. So each participant will get (rp - 1500n * gasPrice - Ccost) / n from one time participation. The current gasPrice is 10 szabo, and estimate of contract consumption is 1500n gas, so estimate of net income is (rp / n - 0.03) ETH. Assuming each RNG has 10 participations, and the pledge is 1000ETH, the minimum required income is 0.4 ETH, which over 0.001% profit in this case. So if the RNG is requested only once, the service price is 0.4 ETH, and if it is requested 10 times, the price is just 0.04 ETH for each request.

The RANDAO acts as an infrastructure in the Ethereum system. It is called by other contracts. Contracts for different purposes require different random numbers: some need high security, such as lottery; some need steady responses and the request should be responded immediately, these contracts are normally low-value; some need a callback, they want to receive a notification with random numbers when numbers are ready.

Obviously it's impossible to meet different requirements in various scenarios with only one RNG contract, so a lot of contracts will be created with different initial parameters, but the basic rules are the same.

For example, if we need high security, we can substantially increase the pledge of the first phase. Thus, the cost of leading to failure of RNG process by not revealing s is greatly increased. And for the contracts without much interest involved, the minimum number of participants and the pledge can be lower.

Let's look at an example of a dApp betting on odd or even numbers, we'll show how to adjust the contract's parameters to meet the desired security level, by making the cost of cheating higher than expected earnings. Assuming the bet is 1000 ETH, the betting contract calls a RNG contract C1, if C1 failed to generate a random number at requested block height, then betting contract waits for the next random number of C1, until there is one generated.

Let's build the RNG contract C1, and set the pledged ETH of C1 to 2000. The gambler G plays the betting dApp but also participates in the contract. When he finds himself in a disadvantageous position before he reveals his secret number, he can choose not to reveal s, so that the RNG failed and he got another chance. But he will lose the 2000 pledged ETH, so although he can get 1000 ETH expected return, it is still a bad deal. However, G can reduce his losses on C1 by some means, such as participating in C1 using two accounts, sending two sha3(s). if in a disadvantageous position, G will keep only one account's secret, and if only one participant expect G participate to in C1, G will only lose 1000 ETH in C1, but G will get 1000 ETH as expected return, which is a worthy try.

This issue can be fixed by confiscating the pledged ETH, and not return them to participants as bonus. so a contract with 1000 pledged ETH will meet the requirement of the betting dApp.

Besides confiscation, another scheme can prevent such attacks by introducing an additional system: RANDAO membership. To become a member you must pay dues, anyone paid their dues is a member. Members have different levels according to the dues they paid. Membership does not belong to a contract, but instead functions like a passport to participate in some RANDAO contracts. If a breach of any contract happens, that person's membership will be ended and the dues will be confiscated. Now we can add an additional agreement to C1, C1 will only accept numbers committed by members whose level of investment is high enough (membership dues over 1000 ETH). This will ensure that nobody has a financial motive to try an attack.


QA:

Q: Why not let the miners participate in RNG? Why not use tx hash, nonce and other blockchain data? A: Miners have the ability to manipulate these blockchain data, and thus can indirectly affect RNG. If RNG contains blockchain data, it will give the miners capacity to construct random numbers in their favor.

Q: the miners can ignore certain transactions that contain random number they dislike, how to deal with that? A: That's why we need a time window period. A reasonable period should be greater than 6 blocks, we believe that nobody can produce 6 blocks in succession. So if the participant is honest, and he send numbers immediately as long as each time window open, he doesn't need to worry about being excluded.

Q: Why use all numbers of all participants, rather than a subset? A: The rule to pick a subset is deterministic, so participants will try to take specified position of the collection by various means, if they succeed, they will know in advance what the random number is generating from subsets. If the rule to pick a subset is randomised, then we still have the problem of true randomisation.

Q: Where does pledged dues go? A: It will be donated to a charity, or RANDAO to maintain funding.

Note: f(s1, s2, ..., sn) is a function with multiple inputs, for example r = s1 xor s2 xor s3 ... xor sn, or r = sha3(sn + sha3(sn-1 + ... (sha3(s2 + s1))))

randao's People

Contributors

atmb4u avatar btceth avatar mastereye-07 avatar thodges-gh avatar tomlion avatar totteplott avatar toyab avatar u2 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

randao's Issues

Ethereum network-level transaction spamming attack against honest participants

I only read the text description of the RANDAO protocol, but there appears to be a bug that enables attacking both the random number produced and honest participants.

In the protocol, pledges must be larger than any possible payout in order to be effective. In the case of a lottery such as this one, the sum of the pledge basically needs to exceed the amount of the lottery jackpot. Similarly, there may be other situations where large pledges are required.

In the RANDAO FAQ, this question appears (in relation to the corresponding part of the protocol):

Q: the miners can ignore certain transactions that contain random number they dislike, how to deal with that? A: That's why we need a time window period. A reasonable period should be greater than 6 blocks, we believe that nobody can produce 6 blocks in succession. So if the participant is honest, and he send numbers immediately as long as each time window open, he doesn't need to worry about being excluded.

However, there is another attack that can be executed. Instead of mining six blocks in succession, the attacker merely has to clog up six consecutive blocks: by constantly outbidding an honest participant in terms of gas price and filling the block, the attacker can prevent another participant's revealing transaction from being included. The cost of this attack is (slightly more than) the cost of gas required to fill the blocks. We can see that the average transaction fees per block on the main net at the time of this writing is about 0.6 ether. To fill the block, the malicious user Mal thus has to spend ~0.6 ether to block a transaction for a block. For six blocks, that's ~3.6 ether. For a random number protected with a pledge of 2000 ether, 3.6 ether is a very cheap attack. It's made even cheaper if Mal is also a miner and can mine some of those six blocks and can outright censor the honest participants' transactions on blocks OR include the spam transactions and collect the fees (which is the more surreptitious way of doing it). Mal can thus manipulate the outcome of the random number generation. Furthermore, if the censored participant(s)'s ether is divided amongst the other participants, Mal gets a cut if she is participating -- and can increase her cut by censoring more people. Indeed, Mal can make a profit simply on the RANDAO protocol without even having a stake in the outcome of the random number, even after including the transaction fees!

Lastly, Mal can be malicious in another way: suppose a particular RANDAO contract is only open approved participants. If Alice is one of those approved participants and Mal does not like Alice, Mal can force Alice to forfeit her pledge by spamming the network. This can be done purely out of spite or to make Alice relatively poorer than Mal. (Even if it makes Mal poorer, if Mal and Alice both want a particular Picasso painting more than anyone else and have more money than anyone else cares to bid, Mal might now be able to out-bid Alice on this painting by becoming relatively richer to Alice.)

This attack cannot be patched by putting a maximum gas price on the transaction to make the attack have the same priority for transaction queuing as the honest participant(s). Indeed, this makes the attack cheaper. If Mal manages to reveal block her secret during block 1 of the reveal period, then she can flood the remaining n blocks of the reveal window with non-RANDAO related transactions. If there is a cap, then the attacker knows that other participants' valid revelation transactions have a maximum price and can set her spam transactions to have a gas price of 1 gwei/gas more than this maximum.

The situation improves when Ethereum protocol scaling options are adopted.

Has a security audit been done on this contract? Who would like to discuss how to improve the security?

In light of the DAO hacks, I noticed things like a potentially exceptionally large callback loop (in function callback), which may fail, as there is a maximum limit to how much can be spent in a block (see Loops and the Block Gas Limit in this Ethereum blog post). I also noticed that you do a callback to a function I have signed up, which might lead to a recursive callback.

Those are just two potential risks in 3 minutes of reading. Is there a group that we can discuss together other potential holes in this contract?

test Randao?

Hello I would like to test randao algorithm.
But I dont undestand very well how do it. what is the easy way to do it?
Could you help me?
Maybe write something in the README will help also other people.

TODO

  1. All lifecycle tests and all smart contract details(including event or others) (4.13)
  2. User commit random from browser.(4.22)

Potential impact of spam attacks during reveal phase

Q: the miners can ignore certain transactions that contain random number they dislike, how to deal with that? A: That's why we need a time window period. A reasonable period should be greater than 6 blocks, we believe that nobody can produce 6 blocks in succession. So if the participant is honest, and he send numbers immediately as long as each time window open, he doesn't need to worry about being excluded.

What about spam/DDoS attacks to clog the reveal phase during that x-block window? If the payoff of a certain random number appearing is large enough it may justify stuffing those blocks with high fee transactions to control the number that appears (by pushing out "bad" numbers).

n grow the reward will reduce

each participant will get (rp - 1500n * gasPrice - Ccost) / n from one time participation. So when n grow the reward will reduce, when reward is less than 0, no one else will take part in. Maybe I can manufacture the random number by stop other people joining 'The first phase'

Contract address

Hi, could you post list of valid and working contract adresses on Ethereum?

Where is the _campaignID set

In the Randao.sol file, you have a _campaignID variable:

  function newCampaign(uint32 _bnum, uint96 _deposit, uint8 _commitDeadline, uint8 _commitBalkline) returns (uint32 _campaignID) {
    if(block.number >= _bnum){ throw; }
    if(_commitDeadline <= 0){ throw; }
    if(_commitBalkline <= 0){ throw; }
    if(_commitDeadline >= _commitBalkline){ throw; }

    Campaign c = campaigns[_campaignID];

How is the _campaignID set?

Truffle test doesn´t work

Hi. I did a test using truffle v4.0.7 and Ganache and the following error occurred:

1 passing (4s)
3 failing

  1. Contract: Randao randao campaign lifecycle:
    TypeError: counter.count is not a function
    at Object.ff (test\helper\timecop.js:15:24)
    at Randao.deployed.then.then.then.then (test\randao.js:37:22)
    at
    at process._tickDomainCallback (internal/process/next_tick.js:228:7)

  2. Contract: Randao randao campaign lifecycle:
    TypeError: counter.count is not a function
    at Object.ff (test\helper\timecop.js:15:24)
    at Randao.deployed.then.then.then.then (test\refundBounty.js:35:22)
    at
    at process._tickDomainCallback (internal/process/next_tick.js:228:7)

  3. Contract: Timecop fast forward fast forward 3 blocks:
    TypeError: counter.count is not a function
    at Object.ff (test\helper\timecop.js:15:24)
    at Context.it (test\timecop.js:8:13)

Why change from send() to call.value ?

Why change cf75c20 ?

Contracts can call the external functions like refundBounty and then can proceed to do whatever the contract wants to do. Why does randao need to give gas to the fallback function of external contracts?

Eliminate or Rephrase: "Miners can't be trusted!"

The section header Miners can't be trusted! should be eliminated or replaced:

  • This sentiment biases the reader to consider that only Miners cannot be trusted. In reality, no entity, whether it's an individual or group, that can indirectly or directly affect the system's state, should be trusted. IMO, indirect actors, those not directly incentivize by the Ethereum mechanisms, such as state actors, have proven to be far more detrimental than any incentivize group.
  • By focusing on malicious Miners, you risk considering only those behaviors/mechanisms/responsibilities reserved for this role as vectors for exploit, ignoring other possible mechanisms not associated to this role.
  • Why call out a particular group, especially one that's currently securing the community's interests? Why anger honest Miners?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.