Skip to content

Commit

Permalink
Update contracts to use "this"
Browse files Browse the repository at this point in the history
  • Loading branch information
xhliu committed Dec 16, 2019
1 parent e5b31c2 commit bfe3db1
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 33 deletions.
9 changes: 5 additions & 4 deletions docs/ackermann.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ nCrypt has devised a way to calculate the value of the Ackermann function using
int a;
int b;
constructor() {
a = 2;
b = 1;
// a = 2, b = 1
constructor(int a, int b) {
this.a = a;
this.b = b;
}
function ackermann(int m, int n) returns (int) {
Expand Down Expand Up @@ -61,7 +62,7 @@ nCrypt has devised a way to calculate the value of the Ackermann function using
// y = 5
public function unlock(int y) {
require(y == ackermann(a, b));
require(y == this.ackermann(this.a, this.b));
}
}
Expand Down
16 changes: 8 additions & 8 deletions docs/intro.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ Each contract provides a template for a certain type of contracts (e.g., P2PHK o
contract Test {
int x;
constructor(int _x) {
x = _x;
constructor(int x) {
this.x = x;
}
public function equal(int y) {
require(y == x);
require(y == this.x);
}
}
Expand Down Expand Up @@ -47,19 +47,19 @@ For example, if public function ``larger`` is called, ``scriptSig`` of ``y 3`` c
contract Test {
int x;
constructor(int _x) {
x = _x;
constructor(int x) {
this.x = x;
}
public function equal(int y) {
require(y == x);
require(y == this.x);
}
public function smaller(int y) {
require(y < x);
require(y < this.x);
}
public function larger(int y) {
require(y > x);
require(y > this.x);
}
}
24 changes: 14 additions & 10 deletions docs/multipartyhashpuzzles.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,22 @@ extended to multiple parties so that multiple preimages have to be provided such
Sha256 hash2;
Sha256 hash3;
constructor() {
hash1 = Sha256(0x136523B9FEA2B7321817B28E254A81A683D319D715CEE2360D051360A272DD4C);
hash2 = Sha256(0xE222E30CF5C982E5F6251D755B0B16F608ACE631EB3BA9BDAF624FF1651ABF98);
hash3 = Sha256(0x2A79F5D9F8B3770A59F91E0E9B4C379F7C7A32353AA6450065E43A8616EF5722);
// hash1 = 0x136523B9FEA2B7321817B28E254A81A683D319D715CEE2360D051360A272DD4C
// hash2 = 0xE222E30CF5C982E5F6251D755B0B16F608ACE631EB3BA9BDAF624FF1651ABF98
// hash3 = 0x2A79F5D9F8B3770A59F91E0E9B4C379F7C7A32353AA6450065E43A8616EF5722
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
public function unlock(bytes preimage1, bytes preimage2, bytes preimage3) {
require(sha256(preimage1) == hash1);
require(sha256(preimage2) == hash2);
require(sha256(preimage3) == hash3);
require(sha256(preimage1) == this.hash1);
require(sha256(preimage2) == this.hash2);
require(sha256(preimage3) == this.hash3);
}
}
Expand All @@ -38,8 +41,9 @@ 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;
constructor() {
combinedHash = Sha256(0xC9392767AB23CEFF09D207B9223C0C26F01A7F81F8C187A821A4266F8020064D);
// combinedHash = 0xC9392767AB23CEFF09D207B9223C0C26F01A7F81F8C187A821A4266F8020064D
constructor(Sha256 combinedHash) {
this.combinedHash = combinedHash;
}
// preimage1: e.g., "bsv" -> 0x627376
Expand All @@ -52,7 +56,7 @@ Instead, we can combine all ``y``'s into a single y such that ``y = H(H(y1 || y2
Sha256 hash12 = sha256(hash1 ++ hash2);
Sha256 hash123 = sha256(hash12 ++ hash3);
require(hash123 == combinedHash);
require(hash123 == this.combinedHash);
}
}
Expand Down
7 changes: 3 additions & 4 deletions docs/p2pkh.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ Pay to Public Key Hash
contract P2PKH {
Ripemd160 pubKeyHash;
constructor() {
// type casting bytes literal to Ripemd160 explicitly
pubKeyHash = Ripemd160(0x16475707a0c893698c82eb8d177a8164c0aa1e72);
constructor(Ripemd160 pubKeyHash) {
this.pubKeyHash = pubKeyHash;
}
public function unlock(Sig sig, PubKey pubKey) {
require(hash160(pubKey) == pubKeyHash);
require(hash160(pubKey) == this.pubKeyHash);
require(checkSig(sig, pubKey));
}
}
Expand Down
9 changes: 5 additions & 4 deletions docs/rabin.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ Rabin Signature
contract RabinSignature {
Ripemd160 nHash;
constructor() {
nHash = Ripemd160(0x88d9931ea73d60eaf7e5671efc0552b912911f2a);
// nHash = 0x88d9931ea73d60eaf7e5671efc0552b912911f2a
constructor(Ripemd160 nHash) {
this.nHash = nHash;
}
function hash3072(bytes x) returns (bytes) {
Expand All @@ -32,9 +33,9 @@ Rabin Signature
}
public function verifySig(int S, bytes U, bytes m, int lambda, bytes n) {
require(hash160(n) == nHash);
require(hash160(n) == this.nHash);
int h = bin2num(hash3072(m ++ U));
int h = bin2num(this.hash3072(m ++ U));
require(S * S == h + lambda * bin2num(n));
}
}
7 changes: 4 additions & 3 deletions docs/rpuzzle.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ One crucial step in R-Puzzle is to extract ``r`` from `DER`_ encoded signature.
contract RPuzzle {
Sig s;
constructor() {
s = Sig(0x3045022100948c67a95f856ae875a48a2d104df9d232189897a811178a715617d4b090a7e90220616f6ced5ab219fe1bfcf9802994b3ce72afbb2db0c4b653a74c9f03fb99323f01);
// s = 0x3045022100948c67a95f856ae875a48a2d104df9d232189897a811178a715617d4b090a7e90220616f6ced5ab219fe1bfcf9802994b3ce72afbb2db0c4b653a74c9f03fb99323f01
constructor(Sig s) {
this.s = s;
}
function getSigR(Sig sig) returns (bytes) {
Expand All @@ -25,7 +26,7 @@ One crucial step in R-Puzzle is to extract ``r`` from `DER`_ encoded signature.
// r = 00948c67a95f856ae875a48a2d104df9d232189897a811178a715617d4b090a7e9
public function unlock(bytes r) {
require(r == getSigR(s));
require(r == this.getSigR(this.s));
}
}
Expand Down

0 comments on commit bfe3db1

Please sign in to comment.