or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx
Snapps
Snapps
Ethereum is a computer that can store user programs. Users execute a program by sending a transaction specifying one of its methods.
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →Mina is a computer that can store user programs. Users execute a program by sending a transaction containing the execution result of a method (accompanied with a proof of correct execution).
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →Snapps
As such, a snapp is eventually compiled down into two programs: one that you can use locally to execute one of its method (and create a proof), and one that others can use to verify that the execution was correct.
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →Note: one account = one snapp
Snapps
Each method of a snapp is actually compiled to a program which:
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →Snapps
under the hood:
The proof is thus of the following statement:
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →Note: of course effects on the world state can only be applied in constrained ways: an external account can only be credited, not debited. Only the current snapp's state can be updated (not other snapps' states).
Setup (NOT READY FOR WORKSHOP)
The Snapp CLI allows you to easily set up, test, and deploy your smart contract.
Setup (NOT READY FOR WORKSHOP)
Setup
or better:
or even better:
Hello World
To write a snapp, extend the
SmartContract
class of snarkyjs.Hello World
A snapp can contain some state (mutable storage).
Here,
some_var
is of typeField
. It is fundamentally the only type supported, and all other types are created from this type. It's almost exactly the same type as Ethereum'su256
type, except that it's a bit smaller.Hello World
TODO: explanation about init/constructor…
Hello World
a snapp can contain multiple methods, and each method can mutate the storage in different ways
note:
Hello World
The state of your snapp is considered "public", and any method of you snapps' methods can read and update them.
Any argument of a method is considered "private", as no one else but you will be able to learn them. (Unless, for example, a method stores an argument directly in the state.)
under the hood:
get()
-> reads the blockchain and encodes a precondition in the public input of the circuit (e.g.some_var
must be equal to 4 for the execution to be correct).square()
-> creates a new temporary variable in the circuit that is constrained to be the square ofsome_var
.assertEqual()
-> asserts that the temporary variable is equal to the private inputsquared
.set()
-> encodes an effect on the snapp's state (e.g. it is correct that executing this method setssome_var
to 8).Exercise 1
Create a similar smart contract, that requires that you pass the cubic root of
some_var
as private input. Another private input should set the new value forsome_var
.Exercise 1
State of the world
What else is public and accessible from a method? Blockchain data, given to you through
this.self
problem: state of the blockchain you see when you execute a method (and produce a transaction that includes a proof) is different from the state of the blockchain a block producer sees when they process your transaction.
solution: ??
State of the world
You can also modify blockchain data as part of the result of an execution, for example to send minas to another account as part of a method's execution
under the hood: this constrains that specific public output to be equal to Y
TODO: continue with updating accounts with payments + do exercise to extend the previous one by returning a reward
Types
Consult the documentation to see operations on
Field
, or let auto-complete do that for you.- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →Types
snarkyjs provides a number of useful types based on
Field
:UInt64
UInt32
PublicKey
Optional
Bool
array
Group
EndoScalar
Scalar
Types
You can also create custom types by extending snarkyjs'
CircuitValue
and using the@prop
decorator:Functionalities
Functionalities
Poseidon.hash(input: Field[]): Field
→ a hash function.Circuit.if(b: Bool, x: T, y: T): T
→ there's noif
for flow control, but you can use it as a ternary operator.Signature.verify(publicKey: PublicKey, msg: Field[]): Bool
→ verify signature.Exercise 2
Account
custom type that contains a counter (represented by a field element) (TODO: can we make it a u64?) and a public key.increment_counter
that allows you to increment the counter if you can provide a valid signature (using the account's public key) on the current counter.Exercise 2
note:
Dealing with large state
Note that the state of a snapp can only contain 8 field values.
To manage larger values, a snapp must use accumulator structures like Merkle trees. Snarkyjs standard library provides a number of functions to help you with that.
Dealing with large state
TODO: do we really need to name them "merkle trees" or "accumulators". Is there a more dev-friendly name?
Dealing with large state
pro-tip: you do not need to understand how merkle trees work to use them. Simply use them as blackboxes that provide a storage API.
Dealing with large state
For example:
only a field element (a commitment, why?) is stored in the state of a snapp.
Dealing with large state
The data associated to the commitment of a merkle tree needs to live off-chain. There's a data availability problem. You can store this on IPFS, locally, etc. but users of a snapp must have a way to retrieve that data in order to correctly use a snapp.
Exercise 3
create a smart contract that makes use of a Merkle tree
Exercise 3
Making a function payable
Sending money
TKTK
Exercise 5
Do we have time for that?
Events
TKTK
Recursion