Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add examples to new crypto APIs #103

Merged
merged 7 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions docs-test-gen/templates/core.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@ struct Ed25519VerifyMsg {
signature: Binary,
}

#[cw_serde]
struct Bls12VerifyMsg {
signature: Binary,
pubkey: Binary,
msg: Binary,
dst: Binary,
}

#[cw_serde]
struct RecoveryResponse {
public_key: HexBinary,
}

#[cw_serde]
struct VerifyResponse {
is_valid: bool,
}

#[cw_serde]
struct ExecuteMsg {}

Expand Down
40 changes: 39 additions & 1 deletion src/pages/core/standard-library/cryptography/bls12-381.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,44 @@ CosmWasm offers a byte-oriented API for signature verification. This API also
doesn't care whether the public key is part of the G1 or G2 group (same for the
other components). They just have to somehow fit together.

<Callout>TODO: Add example as soon as BLS12-381 is properly released</Callout>
## Verify on G1
aumetra marked this conversation as resolved.
Show resolved Hide resolved

```rust filename="contract.rs" template="core"
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(
deps: Deps,
_env: Env,
msg: Bls12VerifyMsg,
) -> StdResult<QueryResponse> {
// Verify the signature. On chain!
let msg_hash = deps.api.bls12_381_hash_to_g2(HashFunction::Sha256, &msg.msg, &msg.dst)?;
let is_valid = deps.api.bls12_381_pairing_equality(&BLS12_381_G1_GENERATOR, &msg.signature, &msg.pubkey, &msg_hash)?;
let response = to_json_binary(&VerifyResponse {
is_valid
})?;

Ok(response)
}
```

## Verify on G2
aumetra marked this conversation as resolved.
Show resolved Hide resolved

```rust filename="contract.rs" template="core"
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(
deps: Deps,
_env: Env,
msg: Bls12VerifyMsg,
) -> StdResult<QueryResponse> {
// Verify the signature. On chain!
let msg_hash = deps.api.bls12_381_hash_to_g1(HashFunction::Sha256, &msg.msg, &msg.dst)?;
let is_valid = deps.api.bls12_381_pairing_equality(&msg.signature, &BLS12_381_G2_GENERATOR, &msg.pubkey, &msg_hash)?;
aumetra marked this conversation as resolved.
Show resolved Hide resolved
let response = to_json_binary(&VerifyResponse {
is_valid
})?;

Ok(response)
}
```

[drand]: https://drand.love/
6 changes: 4 additions & 2 deletions src/pages/core/standard-library/cryptography/ed25519.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ pub fn query(

// Verify the signature. On chain!
let is_valid = deps.api.ed25519_verify(&message, &signature, &public_key)?;
let response = format!("{{ \"is_valid\": {is_valid} }}");
let response = to_json_binary(&VerifyResponse {
is_valid
})?;

Ok(QueryResponse::new(response.into_bytes()))
Ok(response)
}
```

Expand Down
13 changes: 8 additions & 5 deletions src/pages/core/standard-library/cryptography/k256.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ pub fn query(

// Verify the signature. On chain!
let is_valid = deps.api.secp256k1_verify(&message_hash, &signature, &public_key)?;
let response = format!("{{ \"is_valid\": {is_valid} }}");
let response = to_json_binary(&VerifyResponse {
is_valid
})?;

Ok(QueryResponse::new(response.into_bytes()))
Ok(response)
}
```

Expand All @@ -50,9 +52,10 @@ pub fn query(

// Recover the public key. On chain!
let public_key = deps.api.secp256k1_recover_pubkey(&message_hash, &signature, recovery_id)?;
let public_key = HexBinary::from(public_key).to_hex();
let response = format!("{{ \"public_key\": \"{public_key}\" }}");
let response = to_json_binary(&RecoveryResponse {
public_key: public_key.into(),
})?;

Ok(QueryResponse::new(response.into_bytes()))
Ok(response)
}
```
46 changes: 44 additions & 2 deletions src/pages/core/standard-library/cryptography/p256.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,48 @@ added to CosmWasm.

## Example

## Signature verification
### Signature verification

<Callout>TODO: Add example as soon as secp256r1 is properly released</Callout>
```rust filename="contract.rs" template="core"
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(
deps: Deps,
_env: Env,
msg: EcdsaVerifyMsg,
) -> StdResult<QueryResponse> {
let public_key = msg.public_key;
let signature = msg.signature;
let message_hash = msg.message_hash;

// Verify the signature. On chain!
let is_valid = deps.api.secp256r1_verify(&message_hash, &signature, &public_key)?;
let response = to_json_binary(&VerifyResponse {
is_valid
})?;

Ok(response)
}
```

### Public key recovery

```rust filename="contract.rs" template="core"
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(
deps: Deps,
_env: Env,
msg: EcdsaRecoverMsg,
) -> StdResult<QueryResponse> {
let signature = msg.signature;
let message_hash = msg.message_hash;
let recovery_id = msg.recovery_id;

// Recover the public key. On chain!
let public_key = deps.api.secp256r1_recover_pubkey(&message_hash, &signature, recovery_id)?;
let response = to_json_binary(&RecoveryResponse {
public_key: public_key.into(),
})?;

Ok(response)
}
```