Skip to content

Instantly share code, notes, and snippets.

Created February 9, 2016 15:23
Show Gist options
  • Save anonymous/857e162358b65f60c421 to your computer and use it in GitHub Desktop.
Save anonymous/857e162358b65f60c421 to your computer and use it in GitHub Desktop.

Revisions

  1. @invalid-email-address Anonymous created this gist Feb 9, 2016.
    16 changes: 16 additions & 0 deletions LamportVerify.sol
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    library LamportVerify{


    function getBit(bytes32 data, uint256 index) constant returns(uint8) { // gets bit `i` from data
    return uint8(uint256(data) / (2**((255-index)))) & 0x01;
    }

    function verify_sig(bytes32 msgHash, bytes32[512] pubKey, bytes32[256] signature) returns(bool){
    for(uint i; i < 256; i++){
    bytes32 pub;
    if(getBit(msgHash,i) == 0) pub
    if(!(pubKey[getBit(msgHash,i)][i] == sha3(signature[i]))) return false;
    }
    return true;
    }
    }
    12 changes: 12 additions & 0 deletions Untitled
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    contract testarray{
    bytes32[2][2] data;

    function add (bytes32[2][2] dat) external {
    data = dat;
    }

    function get(uint8 i, uint8 j) external constant returns (bytes32){
    return data[j][i];
    }

    }
    67 changes: 67 additions & 0 deletions powerball.sol
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,67 @@
    contract BTCRelay{
    function getLastBlockHeight() public returns(int256);
    function getBlockHash(int256) public returns(int256);

    }

    contract Powerball{
    mapping(address => uint8[6][]) public tickets;
    mapping(address => uint) public balances;
    uint public roundStart;
    uint public constant roundLength = 3 days;
    bool roundOver = false;
    enum Phase {Bet,Draw,Claim}
    Phase currentPhase = Phase.Bet;
    uint8[] public balls;
    BTCRelay relay;
    uint public constant houseEdge = 1; //percent of each ticket
    address house;
    uint houseFunds;
    int256 firstBlock;

    function Powerball(address relayAddress){
    relay = BTCRelay(relayAddress);
    roundStart = block.timestamp;
    house = msg.sender;
    }

    function buyTicket(uint8[6] numbers){
    if(startDraw()) throw;
    if(!checkNumbers(numbers)) throw;
    if(msg.value < 2 ether) throw;
    for(uint16 i = 0; i < msg.value/2; i++){
    tickets[msg.sender].push(numbers);
    }
    houseFunds += msg.value/100;
    }

    function startDraw() returns(bool){
    if(block.timestamp > roundStart + roundLength){
    if(currentPhase == Phase.Bet){
    currentPhase = Phase.Draw;
    firstBlock = relay.getLastBlockHeight() + 2;
    }
    return false;
    }
    return true;
    }

    function checkNumbers(uint8[6] numbers) returns (bool){
    for(var i = 0; i <6; i++){
    if (numbers[i] > 69 || numbers[i] <1) return false;
    }
    if(numbers[5]>26) return false;
    return true;
    }

    function getBall(uint8 ball) returns(uint8){
    if(balls[ball] != 0) return balls[ball];
    if(ball > 5) throw;
    if (relay.getLastBlockHeight() < 8+ball+firstBlock) throw;
    int256 hash = relay.getBlockHash(firstBlock + ball);
    if (hash == 0) throw;
    else if(ball !=5) balls[ball] = uint8(hash) % 69 +1;
    else balls[ball] = uint8(hash)%26;
    }

    }
    78 changes: 78 additions & 0 deletions qETH.sol
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,78 @@
    library LamportVerify{


    function getBit(bytes32 data, uint256 index) constant returns(uint8) { // gets bit `i` from data
    return uint8(uint256(data) / (2**((255-index)))) & 0x01;
    }

    function verify_sig(bytes32 msgHash, bytes32[2][256] pubKey, bytes32[256] signature) returns(bool){
    for(uint i; i < 256; i++){
    if(!(pubKey[getBit(msgHash,i)][i] == sha3(signature[i]))) return false;
    }
    return true;
    }
    }


    contract qETH {
    LamportVerify LamportLib;

    struct account {
    bool _init; // Has pubKey been set?
    uint _nonce; // Account nonce for rederiving privKeys. newPrivKey = sha3(masterKey + nonce)
    bytes32[2][256] _pubKey; // Current public key
    uint _balance;
    }

    mapping(address => account) public accounts;


    function qETH(address lib){
    if (lib == 0) LamportLib = new LamportVerify(); // if no library exists, make new library
    else LamportLib = LamportVerify(lib);
    }

    function hashMsg(address from, address to, uint amount, bytes32[2][256] newKey) public constant returns(bytes32) { // Helper function to construct msg hash
    return sha3(from,to,amount);
    }

    function balanceOf(address addr) constant returns(uint){
    return accounts[addr]._balance;
    }

    function setPubKey (bytes32[2][256] pubKey) public {
    accounts[msg.sender]._balance += msg.value;
    if(accounts[msg.sender]._init == true) throw;
    accounts[msg.sender]._pubKey = pubKey;
    accounts[msg.sender]._init = true;
    }

    function sendVerify(address from, address to, uint amount, bytes32[2][256] newKey, bytes32[256] sig) { //sig is signature of msgHash
    bytes32 msgHash = hashMsg(from,to,amount, newKey);
    if (accounts[from]._init == false || !LamportLib.verify_sig(msgHash,accounts[from]._pubKey,sig)) throw; //Check initialization and Lamport signature
    if (accounts[from]._balance < amount || accounts[to]._balance + amount < accounts[to]._balance) throw; //Check overflow and underflow

    accounts[from]._balance -= amount;
    accounts[from]._nonce++;
    accounts[from]._pubKey = newKey;
    }

    function transferFrom(address from, address to, uint amount, bytes32[2][256] newKey, bytes32[256] sig) public {
    sendVerify(from, to, amount, newKey, sig);
    accounts[to]._balance += amount;
    }

    function transfer(address to, uint amount, bytes32[2][256] newKey, bytes32[256] sig){
    sendVerify(msg.sender, to, amount, newKey, sig);
    accounts[to]._balance += amount;
    }

    function withdraw(address from, address to, uint amount, bytes32[2][256] newKey, bytes32[256] sig) public {
    sendVerify(from, to, amount, newKey, sig);
    to.send(amount);
    }

    function(){
    accounts[msg.sender]._balance += msg.value;
    }
    }
    14 changes: 14 additions & 0 deletions steal.sol
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    contract Lotto{
    function Under_the_Hood() constant returns (
    uint256 UtH_res_, // Reslut
    uint256 UtH_a_, // 'Random' value
    uint256 UtH_m_); // Modulus
    }

    contract Steal {
    Lotto lotto = Lotto(0xc0cd30de8fbe76ebe4e14bdb2667436e05f7cfc9);
    function get() public constant returns (uint _a){
    var (res,a,m) = lotto.Under_the_Hood();
    return a;
    }
    }