Skip to content

Commit 24813b1

Browse files
authored
Standard library chapter (#1)
* Added initial content. * Updates. * Fixed Math chapter. * Added cryptogrphy functions descriptions.
1 parent 4e40e8e commit 24813b1

File tree

14 files changed

+420
-1
lines changed

14 files changed

+420
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ You can just clone this repository and run the following commands:
2222

2323
```shell
2424
$ npm install
25-
$ task serve
25+
$ npm run start
2626
```
2727

2828
You will now be able to visit the locally served website at http://localhost:3000/
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"label": "Standard library",
3+
"position": 4,
4+
"link": {
5+
"type": "doc",
6+
"id": "standard-library"
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"label": "Cryptography",
3+
"position": 2,
4+
"link": {
5+
"type": "doc",
6+
"id": "cryptography"
7+
}
8+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
sidebar_position: 4
3+
---
4+
5+
# BLS12-381
6+
7+
BLS12-381 is a bit of a special curve. It is a pairing-friendly curve, allowing for fun things such
8+
as aggregated signatures. At the moment, CosmWasm only supports signature verifications. In the
9+
future we might add support for zero-knowledge proofs on this curve.
10+
11+
Common examples where this curve is used are Ethereum block-headers and [drand] randomness beacons.
12+
13+
## Example
14+
15+
CosmWasm offers a byte-oriented API for signature verification. This API also doesn't care whether
16+
the public key is part of the G1 or G2 group (same for the other components). They just have to
17+
somehow fit together.
18+
19+
## Verify on G1
20+
21+
Signature verification with public key in G1:
22+
23+
```rust title="contract.rs"
24+
#[cfg_attr(not(feature = "library"), entry_point)]
25+
pub fn query(
26+
deps: Deps,
27+
_env: Env,
28+
msg: Bls12VerifyMsg,
29+
) -> StdResult<QueryResponse> {
30+
// Verify the signature. On chain!
31+
let msg_hash = deps.api.bls12_381_hash_to_g2(HashFunction::Sha256, &msg.msg, &msg.dst)?;
32+
let is_valid = deps.api.bls12_381_pairing_equality(&BLS12_381_G1_GENERATOR, &msg.signature, &msg.pubkey, &msg_hash)?;
33+
let response = to_json_binary(&VerifyResponse {
34+
is_valid
35+
})?;
36+
37+
Ok(response)
38+
}
39+
```
40+
41+
## Verify on G2
42+
43+
Signature verification with public key in G2 (See
44+
https://hackmd.io/@benjaminion/bls12-381#Verification in combination with
45+
https://hackmd.io/@benjaminion/bls12-381#Swapping-G1-and-G2):
46+
47+
```rust title="contract.rs"
48+
#[cfg_attr(not(feature = "library"), entry_point)]
49+
pub fn query(
50+
deps: Deps,
51+
_env: Env,
52+
msg: Bls12VerifyMsg,
53+
) -> StdResult<QueryResponse> {
54+
// Verify the signature. On chain!
55+
let msg_hash = deps.api.bls12_381_hash_to_g1(HashFunction::Sha256, &msg.msg, &msg.dst)?;
56+
let is_valid = deps.api.bls12_381_pairing_equality(&msg.signature, &BLS12_381_G2_GENERATOR, &msg_hash, &msg.pubkey)?;
57+
let response = to_json_binary(&VerifyResponse {
58+
is_valid
59+
})?;
60+
61+
Ok(response)
62+
}
63+
```
64+
65+
[drand]: https://drand.love
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Cryptography
2+
3+
CosmWasm offers cryptographic primitives that are implemented as VM intrinsics. This means that they
4+
are more efficient than implementations in contract code could be, saving you gas and code size. In
5+
some cases they are also _impossible_ to implement in contract code, since some of them require a
6+
random intermediate value and randomness is generally not exposed to contracts to ensure
7+
deterministic execution.
8+
9+
In the sidebar, you can find all the supported algorithms/curves along with documentation about
10+
them.
11+
12+
## Gas costs
13+
14+
:::tip
15+
16+
Note that these values are in CosmWasm Gas (which is not equivalent to Cosmos SDK Gas; rather,
17+
CosmWasm Gas is eventually converted to Cosmos SDK Gas). For more info on gas [check this page].
18+
19+
:::
20+
21+
### secp256k1
22+
23+
- Verify: 96_000_000 gas
24+
- Recover public key: 194_000_000 gas
25+
26+
### secp256r1
27+
28+
- Verify: 279_000_000 gas
29+
- Recover public key: 592_000_000 gas
30+
31+
### ed25519
32+
33+
- Verify: 35_000_000 gas
34+
- Batch verify (multiple signatures; multiple keys):
35+
- Base: 24_000_000 gas
36+
- Per signature/key pair: 21_000_000 gas
37+
- Batch verify (multiple signatures; one key):
38+
- Base: 36_000_000 gas
39+
- Per signature: 10_000_000 gas
40+
41+
### BLS12-381
42+
43+
- Aggregate points to G1:
44+
- Base: 68_000_000 gas
45+
- Per point: 12_000_000 gas
46+
- Aggregate points to G2:
47+
- Base: 103_500_000 gas
48+
- Per point: 24_500_000 gas
49+
- Hash to G1: 563_000_000 gas
50+
- Hash to G2: 871_000_000 gas
51+
- Pairing equality:
52+
- Base: 2_112_000_000 gas
53+
- Per item: 163_000_000 gas
54+
55+
You can also check these numbers in the [source code].
56+
57+
## Note on hash functions
58+
59+
You'll notice that CosmWasm does not implement any hash functions as native functions, and we intend
60+
to keep it that way. The reasoning for that is:
61+
62+
- Hash functions are pretty much always cheap in terms of execution cost _and_ code size
63+
- There are way too many hash functions out there, adding one would open the floodgates where we
64+
would have to add more
65+
- SHA-3 family, Keccak256, cSHAKE256, BLAKE2, BLAKE3, SHA-2 family, KangarooTwelve, etc. (and this
66+
is just a small sample set to drive the point home)
67+
- Benchmarking and pricing functions (and keeping those up-to-date) correctly is a lot of work
68+
- We want to keep the API surface as small as possible for ease of maintenance
69+
70+
Keep in mind that, thanks to Wasm being our execution environment, contract execution is quite fast.
71+
In most cases the perceived need to move hashing into a host function is premature optimization and
72+
not actually needed.
73+
74+
[check this page]: ../../architecture/gas
75+
[source code]: https://github.com/CosmWasm/cosmwasm/blob/main/packages/vm/src/environment.rs#L62-L101
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
sidebar_position: 1
3+
---
4+
5+
# Ed25519
6+
7+
CosmWasm offers an API to verify and batch verify Ed25519 signatures. This is powerful, especially
8+
since batch verifications require a random component which is impossible to implement in contract
9+
code.
10+
11+
Ed25519 is known to [have issues with inconsistent validation criteria], the API we offer follows
12+
[ZIP215]. This means you will have no issues with signatures being valid in one place and invalid in
13+
another.
14+
15+
## Example
16+
17+
```rust title="contract.rs"
18+
#[cfg_attr(not(feature = "library"), entry_point)]
19+
pub fn query(
20+
deps: Deps,
21+
_env: Env,
22+
msg: Ed25519VerifyMsg,
23+
) -> StdResult<QueryResponse> {
24+
let public_key = msg.public_key;
25+
let signature = msg.signature;
26+
let message = msg.message;
27+
28+
// Verify the signature. On chain!
29+
let is_valid = deps.api.ed25519_verify(&message, &signature, &public_key)?;
30+
let response = to_json_binary(&VerifyResponse {
31+
is_valid
32+
})?;
33+
34+
Ok(response)
35+
}
36+
```
37+
38+
[have issues with inconsistent validation criteria]: https://hdevalence.ca/blog/2020-10-04-its-25519am
39+
[ZIP215]: https://zips.z.cash/zip-0215
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
title: K-256
3+
sidebar_position: 3
4+
---
5+
6+
# K256 (secp256k1)
7+
8+
K256 is a Koblitz curve that is widely used in the blockchain space (e.g. Bitcoin and Ethereum).
9+
CosmWasm offers the following APIs:
10+
11+
- Signature verification
12+
- Public key recovery
13+
14+
## Example
15+
16+
### Signature verification
17+
18+
```rust title="contract.rs"
19+
#[cfg_attr(not(feature = "library"), entry_point)]
20+
pub fn query(
21+
deps: Deps,
22+
_env: Env,
23+
msg: EcdsaVerifyMsg,
24+
) -> StdResult<QueryResponse> {
25+
let public_key = msg.public_key;
26+
let signature = msg.signature;
27+
let message_hash = msg.message_hash;
28+
29+
// Verify the signature. On chain!
30+
let is_valid = deps.api.secp256k1_verify(&message_hash, &signature, &public_key)?;
31+
let response = to_json_binary(&VerifyResponse {
32+
is_valid
33+
})?;
34+
35+
Ok(response)
36+
}
37+
```
38+
39+
### Public key recovery
40+
41+
```rust title="contract.rs"
42+
#[cfg_attr(not(feature = "library"), entry_point)]
43+
pub fn query(
44+
deps: Deps,
45+
_env: Env,
46+
msg: EcdsaRecoverMsg,
47+
) -> StdResult<QueryResponse> {
48+
let signature = msg.signature;
49+
let message_hash = msg.message_hash;
50+
let recovery_id = msg.recovery_id;
51+
52+
// Recover the public key. On chain!
53+
let public_key = deps.api.secp256k1_recover_pubkey(&message_hash, &signature, recovery_id)?;
54+
let response = to_json_binary(&RecoveryResponse {
55+
public_key: public_key.into(),
56+
})?;
57+
58+
Ok(response)
59+
}
60+
```
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
---
2+
title: P-256
3+
sidebar_position: 2
4+
---
5+
6+
# P256 (secp256r1)
7+
8+
P256 is one of NIST's prime-order elliptic curves. You may need this curve to implement protocols
9+
such as WebAuthn. This is the main reason this curve was added to CosmWasm.
10+
11+
## Example
12+
13+
### Signature verification
14+
15+
```rust title="contract.rs"
16+
#[cfg_attr(not(feature = "library"), entry_point)]
17+
pub fn query(
18+
deps: Deps,
19+
_env: Env,
20+
msg: EcdsaVerifyMsg,
21+
) -> StdResult<QueryResponse> {
22+
let public_key = msg.public_key;
23+
let signature = msg.signature;
24+
let message_hash = msg.message_hash;
25+
26+
// Verify the signature. On chain!
27+
let is_valid = deps.api.secp256r1_verify(&message_hash, &signature, &public_key)?;
28+
let response = to_json_binary(&VerifyResponse {
29+
is_valid
30+
})?;
31+
32+
Ok(response)
33+
}
34+
```
35+
36+
### Public key recovery
37+
38+
```rust title="contract.rs"
39+
#[cfg_attr(not(feature = "library"), entry_point)]
40+
pub fn query(
41+
deps: Deps,
42+
_env: Env,
43+
msg: EcdsaRecoverMsg,
44+
) -> StdResult<QueryResponse> {
45+
let signature = msg.signature;
46+
let message_hash = msg.message_hash;
47+
let recovery_id = msg.recovery_id;
48+
49+
// Recover the public key. On chain!
50+
let public_key = deps.api.secp256r1_recover_pubkey(&message_hash, &signature, recovery_id)?;
51+
let response = to_json_binary(&RecoveryResponse {
52+
public_key: public_key.into(),
53+
})?;
54+
55+
Ok(response)
56+
}
57+
```
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"label": "Math",
3+
"position": 1,
4+
"link": {
5+
"type": "doc",
6+
"id": "math"
7+
}
8+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
sidebar_position: 1
3+
---
4+
5+
# Decimals
6+
7+
CosmWasm offers decimal types for handling fractional numbers.
8+
9+
In contrast to floating point numbers, these datatypes do not suffer from the same precision issues.
10+
This means that these decimal types are safe for use in financial calculations.
11+
12+
Instead of storing numbers in the floating point format, which gives it a _floating_ amount of
13+
decimal places, these decimal types have a fixed precision of 18 decimal places.
14+
15+
:::tip
16+
17+
Note that, due to how the decimal types are structured, the value ranges can seem a little weird.
18+
19+
For example, 128-bit unsigned decimal value $1.0$ is represented by `Decimal(1_000_000_000_000_000_000)`.
20+
21+
The maximal value of the 128-bit unsigned decimal is:
22+
$$
23+
{2^{128} - 1 \over 10^{18}} = 340282366920938463463.374607431768211455
24+
$$
25+
26+
In practice, you don't really have to think about it, but it's something you should be aware of.
27+
28+
:::
29+
30+
Decimal types come in the following variants:
31+
32+
| Length | Unsigned | Signed |
33+
|---------|------------|------------------|
34+
| 128-bit | Decimal | SignedDecimal |
35+
| 256-bit | Decimal256 | SignedDecimal256 |
36+
37+
Choose one of the types according to your needs and off you go!

0 commit comments

Comments
 (0)