Skip to content

Commit

Permalink
syntax change: int in hex format & bytes b"" & int()/bytes() -> unpac…
Browse files Browse the repository at this point in the history
…k()/pack()
  • Loading branch information
xhliu committed Feb 8, 2020
1 parent c31d54b commit 4342af0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 31 deletions.
20 changes: 10 additions & 10 deletions docs/multipartyhashpuzzles.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ extended to multiple parties so that multiple preimages have to be provided such
Sha256 hash2;
Sha256 hash3;
// hash1 = 0x136523B9FEA2B7321817B28E254A81A683D319D715CEE2360D051360A272DD4C
// hash2 = 0xE222E30CF5C982E5F6251D755B0B16F608ACE631EB3BA9BDAF624FF1651ABF98
// hash3 = 0x2A79F5D9F8B3770A59F91E0E9B4C379F7C7A32353AA6450065E43A8616EF5722
// hash1 = b"136523B9FEA2B7321817B28E254A81A683D319D715CEE2360D051360A272DD4C"
// hash2 = b"E222E30CF5C982E5F6251D755B0B16F608ACE631EB3BA9BDAF624FF1651ABF98"
// hash3 = b"2A79F5D9F8B3770A59F91E0E9B4C379F7C7A32353AA6450065E43A8616EF5722"
constructor(Sha256 hash1, Sha256 hash2, Sha256 hash3) {
this.hash1 = Sha256(hash1);
this.hash2 = Sha256(hash2);
this.hash3 = Sha256(hash3);
}
// preimage1: e.g., "bsv" -> 0x627376
// preimage2: e.g., "sCrypt" -> 0x734372797074
// preimage3: e.g., "IDE" -> 0x494445
// preimage1: e.g., "bsv" -> b"627376"
// preimage2: e.g., "sCrypt" -> b"734372797074"
// preimage3: e.g., "IDE" -> b"494445"
public function unlock(bytes preimage1, bytes preimage2, bytes preimage3) {
require(sha256(preimage1) == this.hash1);
require(sha256(preimage2) == this.hash2);
Expand All @@ -41,14 +41,14 @@ Instead, we can combine all ``y``'s into a single y such that ``y = H(H(y1 || y2
// only 1 hash needs to go into the locking script, saving space
Sha256 combinedHash;
// combinedHash = 0xC9392767AB23CEFF09D207B9223C0C26F01A7F81F8C187A821A4266F8020064D
// combinedHash = b"C9392767AB23CEFF09D207B9223C0C26F01A7F81F8C187A821A4266F8020064D"
constructor(Sha256 combinedHash) {
this.combinedHash = combinedHash;
}
// preimage1: e.g., "bsv" -> 0x627376
// preimage2: e.g., "sCrypt" -> 0x734372797074
// preimage3: e.g., "IDE" -> 0x494445
// preimage1: e.g., "bsv" -> b"627376"
// preimage2: e.g., "sCrypt" -> b"734372797074"
// preimage3: e.g., "IDE" -> b"494445"
public function unlock(bytes preimage1, bytes preimage2, bytes preimage3) {
Sha256 hash1 = sha256(preimage1);
Sha256 hash2 = sha256(preimage2);
Expand Down
4 changes: 2 additions & 2 deletions docs/rabin.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Rabin Signature
contract RabinSignature {
Ripemd160 nHash;
// nHash = 0x88d9931ea73d60eaf7e5671efc0552b912911f2a
// nHash = b"88d9931ea73d60eaf7e5671efc0552b912911f2a"
constructor(Ripemd160 nHash) {
this.nHash = nHash;
}
Expand All @@ -19,7 +19,7 @@ Rabin Signature
int i = 0;
// start with empty
bytes ret = 0x;
bytes ret = b"";
// divide into 3072 / 256 = 12 slices
loop (12) {
// 256 bits is 32 bytes
Expand Down
4 changes: 2 additions & 2 deletions docs/rpuzzle.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ One crucial step in R-Puzzle is to extract ``r`` from `DER`_ encoded signature.
contract RPuzzle {
Sig s;
// s = 0x3045022100948c67a95f856ae875a48a2d104df9d232189897a811178a715617d4b090a7e90220616f6ced5ab219fe1bfcf9802994b3ce72afbb2db0c4b653a74c9f03fb99323f01
// s = b"3045022100948c67a95f856ae875a48a2d104df9d232189897a811178a715617d4b090a7e90220616f6ced5ab219fe1bfcf9802994b3ce72afbb2db0c4b653a74c9f03fb99323f01"
constructor(Sig s) {
this.s = s;
}
Expand All @@ -24,7 +24,7 @@ One crucial step in R-Puzzle is to extract ``r`` from `DER`_ encoded signature.
return r;
}
// r = 00948c67a95f856ae875a48a2d104df9d232189897a811178a715617d4b090a7e9
// r = b"00948c67a95f856ae875a48a2d104df9d232189897a811178a715617d4b090a7e9"
public function unlock(bytes r) {
require(r == this.getSigR(this.s));
}
Expand Down
35 changes: 18 additions & 17 deletions docs/syntax.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,31 +40,32 @@ Basic Types
-----------

* **bool** - a boolean value ``true`` or ``false``.
* **int** - a signed integer of arbitrary length, whose literals are in decimal format.
* **int** - a signed integer of arbitrary length, whose literals are in decimal or hexadecimal format.

.. code-block:: solidity
int a1 = 42;
int a2 = -4242424242424242;
int a3 = 55066263022277343669578718895168534326250603453777594175500187360389116729240;
int a4 = 0xFF8C;
* **bytes** - a variable length array of bytes, whose literals are in hex format.
* **bytes** - a variable length array of bytes, whose literals are in quoted hexadecimal format prefixed by ``b``.

.. code-block:: solidity
bytes b1 = 0xffee1234;
bytes b2 = 0x414136d08c5ed2bf3ba048afe6dcaebafeffffffffffffffffffffffffffffff00;
bytes b1 = b"ffee1234";
bytes b2 = b"414136d08c5ed2bf3ba048afe6dcaebafeffffffffffffffffffffffffffffff00";
``bytes`` can be explicitly cast to ``int`` using little-endian `sign-magnitude representation <https://www.tutorialspoint.com/sign-magnitude-notation>`_,
where the most significant bit indicates the sign (``0`` for positive, ``1`` for negative). ``int`` can also be explicitly cast to ``bytes``.
``bytes`` can be converted to ``int`` using function ``unpack``. Little-endian `sign-magnitude representation <https://www.tutorialspoint.com/sign-magnitude-notation>`_ is used,
where the most significant bit indicates the sign (``0`` for positive, ``1`` for negative). ``int`` can be converted to ``bytes`` with ``pack``.

.. code-block:: solidity
int a1 = int(0x36); // 54 decimal
int a2 = int(0xb6); // -54
int a3 = int(0xe803); // 1000
int a4 = int(0xe883); // -1000
bytes b = bytes(a4); // 0xe883
int a1 = unpack(b"36"); // 54 decimal
int a2 = unpack(b"b6"); // -54
int a3 = unpack(b"e803"); // 1000
int a4 = unpack(b"e883"); // -1000
bytes b = pack(a4); // b"e883"
Subtypes of ``bytes``
Expand All @@ -77,37 +78,37 @@ To cast a supertype ``bytes`` to them, a function of the type name must be expli

.. code-block:: solidity
PubKey pubKey = PubKey(0x0200112233445566778899aabbccddeeffffeeddccbbaa99887766554433221100);
PubKey pubKey = PubKey(b"0200112233445566778899aabbccddeeffffeeddccbbaa99887766554433221100");
* **PrivKey** - a private key type.

.. code-block:: solidity
PrivKey privKey = PrivKey(0x00112233445566778899aabbccddeeffffeeddccbbaa99887766554433221100);
PrivKey privKey = PrivKey(b"00112233445566778899aabbccddeeffffeeddccbbaa99887766554433221100");
* **Sig** - a signature type in DER format, including hash type.

.. code-block:: solidity
Sig sig = Sig(0x3045022100b71be3f1dc001e0a1ad65ed84e7a5a0bfe48325f2146ca1d677cf15e96e8b80302206d74605e8234eae3d4980fcd7b2fdc1c5b9374f0ce71dea38707fccdbd28cf7e41);
Sig sig = Sig(b"3045022100b71be3f1dc001e0a1ad65ed84e7a5a0bfe48325f2146ca1d677cf15e96e8b80302206d74605e8234eae3d4980fcd7b2fdc1c5b9374f0ce71dea38707fccdbd28cf7e41");
* **Ripemd160** - a RIPEMD-160 hash type.

.. code-block:: solidity
Ripemd160 r = hash160(0x0011223344556677889999887766554433221100);
Ripemd160 r = hash160(b"0011223344556677889999887766554433221100");
* **Sha1** - a SHA-1 hash type.

.. code-block:: solidity
Sha1 s = sha1(0x0011223344556677889999887766554433221100);
Sha1 s = sha1(b"0011223344556677889999887766554433221100");
* **Sha256** - a SHA-256 hash type.

.. code-block:: solidity
Sha256 s = hash256(0x00112233445566778899aabbccddeeffffeeddccbbaa99887766554433221100);
Sha256 s = hash256(b"00112233445566778899aabbccddeeffffeeddccbbaa99887766554433221100");
Operators
Expand Down

0 comments on commit 4342af0

Please sign in to comment.