Common methods
Some common methods you will use often are:
let x = new Field(4); // x = 4x = x.add(3); // x = 7x = x.sub(1); // x = 6x = x.mul(3); // x = 18x = x.div(2); // x = 9x = x.square(); // x = 81x = x.sqrt(); // x = 9
let b = x.equals(8); // b = Bool(false)b = x.greaterThan(8); // b = Bool(true)b = b.not().or(b).and(b); // b = Bool(true)b.toBoolean(); // trueTry it out
Create a mutable variable x for a Field that has a default value of 1:
let x = Field(1);Mutate the variable x by adding 2 to our Field:
x = x.add(2); // x is now 3 since 1 + 2 = 3Mutate the variable x by subtracting 1 from our Field:
x = x.sub(1); // x is now 2 since 3 - 1 = 2Mutate the variable x by multiplying our Field with 2:
x = x.mul(2); // x is now 4 since 2 * 2 = 4Mutate the variable x by dividing our Field with 2:
x = x.div(2); // x is now 2 since 4 / 2 = 2We can log the value of x to check that it has been changed with the following methods: add, sub, mul and div:
Provable.log('x is now:', x) // should be 2We can also use equals to check if our variable x is equal to a specific value, add the following:
let b = x.equals(3); // b is false since x is equal to 2Mutate the variable b using greaterThan:
b = x.greaterThan(2) // b is false since x is not greater than 2We can log the value of b to check that it has been set to false:
Provable.log('b is now:', b) // should be falseWe have three essential operators: and, or and not. and requires both conditions to be true, or permits at least one condition to be true, and not negates a statement, flipping it from true to false or vice versa. Add the following:
let c = b.not()let d = b.not().or(b)let e = b.not().and(b)We can now log the values of c, d and e:
Provable.log('c is now:', c) // should be trueProvable.log('d is now:', d) // should be trueProvable.log('e is now:', e) // should be falseNow build and run the script in the terminal:
npm run build && node build/src/index.js- Installing dependencies