Showing posts with label blockchain. Show all posts
Showing posts with label blockchain. Show all posts

Thursday, 4 April 2019

Security Token Offering

Concept

Security tokens are cryptographic blockchain based tokens that represent financial assets such as bonds, notes, debentures, shares, options, and private equities; as well as tokenised real assets.

It allows fractional ownership of assets. It meets regulation scrutiny.

Companies use STO to raise money from investors. STO investors are promised gains in the form of dividends, rewards (interest rates), or increase in the value of the company.


STO standards

-  ST-20, from Polymath, basically is ERC20 with whitelist investor
-  ERC1400, draft standard, tranches of security (different filings of same
underlying security, Reg D for U.S. investors and Reg S for foreigners), ERC
20 compatible, incorporates ERC1410
-  ERC1410, partially fungible token (organise tokens into set of partitions)
-  ERC1404, draft standard, with transfer restrictions, ERC20 compatible
-  R-Token, from Harbor, ERC20 with additional compliance checking

ERC1400
-  Transfer of tokens can be reversed
-  Token balance includes metadata - shareholder rights , other restrictions
-  Token separated into tranches
-  Standard UI to query transfer
-  Standard event for redemption and issuance
-  ST-20 results in ERC1400

ERC1404
-  Maintain a whitelist of investor addresses
-  Enforce complex restrictions
-  Support branded standards, such as ST-20 and R-token

STO issuance platforms
- Polymath, with DAPP for STO token issuance, using Poly tokens
- Harbor, using R-tokens
- Securitize, using DS protocols, issues security tokens on XRP and Ethereum
- Swarm, using src20 protocol
- Securrency, using CAT-20 token, with KYC and AML engines, compatible
with any blockchain
- tZERO, using tZERO token

Polymath STO steps
- Register ticker symbol
- Deploy smart token contract
- Add investor to whitelist
- Mint tokens for shareholder
- Setup STO parameters - start date, end date, supply cap
- Starts STO - deploy STO contract
It has modular approach: STO module, Transfer Manager module, etc.
It uses smart contract method, such as verifyTransfer method in Transfer Manager module, to validate transfer.



Friday, 19 October 2018

Elliptic Curve Cryptography

An elliptic curve is a set of points that satisfies a math equation:
   y2 = x3 + ax + b

The graph looks like

‌The graph has interesting properties
any point on curve can be reflected on x-axis , and remains on the curve
any non-vertical line can intersect at most 3 points on the curve
Easy to go forward, hard to reverse -> property of trap-door function


An elliptic curve crypto-system can be defined by picking a prime number as a maximum, a curve equation and a public point on the curve.


A private key is a number N, and a public key is the public point dotted with itself N times. ( multiplied the public point N times) 

Computing the private key from the public key - elliptic curve discrete logarithm function , eg. y = g ^ x mod q 

Discrete logarithm function, hard to solve x (nobody knows x from y) (think of y as public key , x as private key)

It is a good trap-door function

It can obtain same level of security with smaller key size (compare to RSA)

For Bitcoin, secp256k1 is the parameters of the elliptic curve used in Bitcoin public key cryptography. The graph of secp256k1 elliptic curve:

secp256k1 details:

y2 = x3+ax+b over P, is defined by T = (P,a,b,G,n,h) , where:

a = 0, b = 7 , so y2 = x3+7
P = large prime number : 2256 - 232 - 29 - 28 - 27 - 26 - 24 - 1
G = 02 79BE667E F9DCBBAC 55A06295 CE870B07 029BFCDB 2DCE28D9 59F2815B 16F81798
h = 01
n = FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE BAAEDCE6 AF48A03B BFD25E8C D0364141

Saturday, 29 September 2018

SHA Hash Algorithm

Important property:
  • One way from input to hash value, cannot reverse
  • Different input cannot generate the same hash value

Actual SHA example:
  • Choose a word to hash, eg CRYPTO
  • Convert the word to ASCII
         CRYPTO becomes 67 82 89 80 84 79
  • Convert from ASCII to binary
         01000011-01010010-01011001-01010000-01010100-01001111 
        (it becomes a 48 bit message)
  • Join and add 1 at the end
         0100001101010010010110010101000001010100010011111
  • Add zeros to make message equal to 448 mod 512, a 48 bit message with the added one will need to have 399 zeros added to the end
  • Add original message length to the 64 bit field (which is the left over field after the 448 modular arithmetic), and let the message become 16 sections of 32 bits
  • 01000011010100100101100101010000
    01010100010011111000000000000000
    00000000000000000000000000000000
    00000000000000000000000000000000
    00000000000000000000000000000000
    00000000000000000000000000000000
    00000000000000000000000000000000
    00000000000000000000000000000000
    00000000000000000000000000000000
    00000000000000000000000000000000
    00000000000000000000000000000000
    00000000000000000000000000000000
    00000000000000000000000000000000
    00000000000000000000000000000000
    00000000000000000000000000000000
    00000000000000000000000000110000
  • Transform the 16 x 32 message into 80 words using step loop function, firstly, do ((14 XOR 9) XOR 3) XOR 1), we get
         01000011010100100101100101010000
  • Rotate left, we get
         10000110101001001011001010100000
  • Process is repeated until there are 80 words (one word = 32 bits)
  • The 1st, 3rd, 9th 14th words are chosen from the algorithm:
  • for i from 16 to 79
          w[i] = (w[i-3] xor w[i-8] xor w[i-14] xor w[i-16]) leftrotate 1

  • Run a set of operations on the 80 words in specific order using the five variables

  • H0 - 01100111010001010010001100000001
    H1 - 11101111110011011010101110001001
    H2 - 10011000101110101101110011111110
    H3 - 00010000001100100101010001110110
    H4 – 11000011110100101110000111110000
    • The Operations are combination of AND, OR and NOT operators
    • The five variables are from first 32 bits of the fractional part of the square roots of the first 5 prime numbers
    • The result : get five variables
    H0 – 01000100101010010111000100110011
    H1- 01010000111001010011100001011000
    H2-11110000010110000100011000111101
    H3-01001011111101111111000111100101
    H4-01000010110110011100101001001011

    • Convert the five variables to hex
    H0- 44a97133
    H1- 50e53858
    H2- f058463d
    H3 - 4bf7f1e5
    H4 - 42d9ca4b
    • Join the variables together, get the hash
    44a9713350e53858f058463d4bf7f1e542d9ca4b

    Saturday, 24 March 2018

    Smart contract with Ethereum ERC20 token

    In this tutorial , i will walk through the solidity code to create a smart contract that is ERC20 compliant. ERC20 is an Ethereum Request for Comment technical standard that defines set of interface. An Ethereum token must implement. say, if you want to have an ICO and get your tokens traded on exchange, the tokens must be ERC20 compliant.


    Firstly, the smart contract must implement the following interface. 
    contract ERC20Interface {
        function totalSupply() public constant returns (uint);
        function balanceOf(address tokenOwner) public constant returns (uint balance);
        function allowance(address tokenOwner, address spender) public constant returns (uint remaining);
        function transfer(address to, uint tokens) public returns (bool success);
        function approve(address spender, uint tokens) public returns (bool success);
        function transferFrom(address from, address to, uint tokens) public returns (bool success);
        event Transfer(address indexed from, address indexed to, uint tokens);
        event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
    }
    Secondly, the implementation of the interface is shown as below.
        // ------------------------------------------------------------------------
        // Total supply
        // ------------------------------------------------------------------------
        function totalSupply() public constant returns (uint) {
            return _totalSupply  - balances[address(0)];
        }

        // ------------------------------------------------------------------------
        // Get the token balance for account tokenOwner
        // ------------------------------------------------------------------------
        function balanceOf(address tokenOwner) public constant returns (uint balance) {
            return balances[tokenOwner];
        }

        // ------------------------------------------------------------------------
        // Transfer the balance from token owner's account to to account
        // - Owner's account must have sufficient balance to transfer
        // - 0 value transfers are allowed
        // ------------------------------------------------------------------------
        function transfer(address to, uint tokens) public returns (bool success) {
            balances[msg.sender] = safeSub(balances[msg.sender], tokens);
            balances[to] = safeAdd(balances[to], tokens);
            Transfer(msg.sender, to, tokens);
            return true;
        }
        // ------------------------------------------------------------------------
        // Transfer tokens from the from account to the to account
        //
        // The calling account must already have sufficient tokens approve(...)-d
        // for spending from the from account and
        // - From account must have sufficient balance to transfer
        // - Spender must have sufficient allowance to transfer
        // - 0 value transfers are allowed
        // ------------------------------------------------------------------------
        function transferFrom(address from, address to, uint tokens) public returns (bool success) {
            balances[from] = safeSub(balances[from], tokens);
            allowed[manager][from] = safeSub(allowed[manager][from], tokens);
            balances[to] = safeAdd(balances[to], tokens);
            Transfer(from, to, tokens);
            return true;
        }

        // ------------------------------------------------------------------------
        // Token owner can approve for spender to transferFrom(...) tokens
        // from the token owner's account
        //
        // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20-token-standard.md
        // recommends that there are no checks for the approval double-spend attack
        // as this should be implemented in user interfaces
        // ------------------------------------------------------------------------
        function approve(address spender, uint tokens) public returns (bool success) {
            allowed[manager][spender] = tokens;
            Approval(manager, spender, tokens);
            return true;
        }
        // ------------------------------------------------------------------------
        // Returns the amount of tokens approved by the owner that can be
        // transferred to the spender's account
        // ------------------------------------------------------------------------
        function allowance(address tokenOwner, address spender) public constant returns (uint remaining)     {
            return allowed[tokenOwner][spender];
        }
    Thirdly, to test the smart contract, we can go to remix.ethereum.org. In environment, choose Java VM, which will utilize the local network. The details is shown in the figure below.

    Fourthly, i will explain the testing sequence according to the figure above. 

    1. To add one token to the user with user account address 0x111, call token_create() function, such as token_create(111). The token_create() function is shown below. You can call token_create() continuously.
      function token_create(address userAddr) public {
        transfer(userAddr, 1);  
    }
    2. To spend the token, must call approve() function first, such as approve(111, 3). It means user account address 0x111 has got three token approved to be spent.

    3. To check the allowance to spend , call allowance(), such as allowance(0xca35b7....., 111). The first parameter is the tokenOwner, the second parameter is the spender. The function returns the number of tokens approved to spend.

    4. If allowance() return sufficient amount of tokens, to spend the token, call token_redeem(), such as token_redeem(111, 1). It means account address 0x111 wants to spend 1 token.
      function token_redeem(address userAddr, uint amount) public  {
        transferFrom(
    userAddr, manager, amount);
      }
    Well, the token_redeem() function will transfer tokens from userAddr to the manager address, who owns the smart contract.

    Then, if you want to accept crypto currencies in exchange for your token, modify the function below.
        function () public payable {
           ...
        }
    to such as:
    function () public payable {
      require(now >= startDate && now <= endDate);
      uint tokens;
      balances[msg.sender] = safeAdd(balances[msg.sender], tokens];
      Transfer(address(0), msg.sender, tokens);
    }

    The end.