Skip to content

Commit

Permalink
crypto: split crypto move lib
Browse files Browse the repository at this point in the history
  • Loading branch information
joyqvq committed Oct 4, 2022
1 parent bda236c commit f96e43a
Show file tree
Hide file tree
Showing 12 changed files with 150 additions and 135 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ expression: common_costs_estimate
---
{
"MergeCoin": {
"computation_cost": 4939,
"storage_cost": 5805,
"computation_cost": 4983,
"storage_cost": 5870,
"storage_rebate": 0
},
"Publish": {
"computation_cost": 4929,
"storage_cost": 5742,
"computation_cost": 4972,
"storage_cost": 5806,
"storage_rebate": 0
},
"SharedCounterAssertValue": {
Expand All @@ -29,8 +29,8 @@ expression: common_costs_estimate
"storage_rebate": 0
},
"SplitCoin": {
"computation_cost": 4918,
"storage_cost": 5774,
"computation_cost": 4962,
"storage_cost": 5838,
"storage_rebate": 0
},
"TransferPortionSuiCoin": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
---
source: crates/sui-cost/tests/empirical_transaction_cost.rs
expression: common_costs
expression: common_costs_actual
---
{
"MergeCoin": {
"computation_cost": 485,
"computation_cost": 490,
"storage_cost": 30,
"storage_rebate": 0
},
"Publish": {
"computation_cost": 547,
"computation_cost": 551,
"storage_cost": 82,
"storage_rebate": 0
},
Expand All @@ -29,7 +29,7 @@ expression: common_costs
"storage_rebate": 15
},
"SplitCoin": {
"computation_cost": 596,
"computation_cost": 601,
"storage_cost": 75,
"storage_rebate": 0
},
Expand Down
94 changes: 0 additions & 94 deletions crates/sui-framework/sources/crypto.move

This file was deleted.

26 changes: 26 additions & 0 deletions crates/sui-framework/sources/crypto/bls12381.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

module sui::bls12381 {
friend sui::validator;

/// @param signature: A 48-bytes signature that is a point on the G1 subgroup
/// @param public_key: A 96-bytes public key that is a point on the G2 subgroup
/// @param msg: The message that we test the signature against.
///
/// If the signature is a valid BLS12381 signature of the message and public key, return true.
/// Otherwise, return false.
public native fun bls12381_verify_g1_sig(signature: &vector<u8>, public_key: &vector<u8>, msg: &vector<u8>): bool;

/// @param signature: 32-byte signature that is a point on the Ed25519 elliptic curve.
/// @param public_key: 32-byte signature that is a point on the Ed25519 elliptic curve.
/// @param msg: The message that we test the signature against.
/// @param domain: The domain that the signature is tested again. We essentially prepend this to the message.
///
/// If the signature is a valid Ed25519 signature of the message and public key, return true.
/// Otherwise, return false.
public(friend) fun bls12381_verify_with_domain(signature: &vector<u8>, public_key: &vector<u8>, msg: vector<u8>, domain: vector<u8>): bool {
std::vector::append(&mut domain, msg);
bls12381_verify_g1_sig(signature, public_key, &domain)
}
}
18 changes: 18 additions & 0 deletions crates/sui-framework/sources/crypto/bulletproofs.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

module sui::bulletproofs {
use sui::elliptic_curve::{Self as ec, RistrettoPoint};

/// Only bit_length = 64, 32, 16, 8 will work.
native fun native_verify_full_range_proof(proof: &vector<u8>, commitment: &vector<u8>, bit_length: u64);

/// @param proof: The bulletproof
/// @param commitment: The commitment which we are trying to verify the range proof for
/// @param bit_length: The bit length that we prove the committed value is whithin. Note that bit_length must be either 64, 32, 16, or 8.
///
/// If the range proof is valid, execution succeeds, else panics.
public fun verify_full_range_proof(proof: &vector<u8>, commitment: &RistrettoPoint, bit_length: u64) {
native_verify_full_range_proof(proof, &ec::bytes(commitment), bit_length)
}
}
37 changes: 37 additions & 0 deletions crates/sui-framework/sources/crypto/ecdsa.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

module sui::ecdsa {
/// @param signature: A 65-bytes signature in form (r, s, v) that is signed using
/// Secp256k1. Reference implementation on signature generation using RFC6979:
/// https://github.com/MystenLabs/narwhal/blob/5d6f6df8ccee94446ff88786c0dbbc98be7cfc09/crypto/src/secp256k1.rs
/// The accepted v values are {0, 1, 2, 3}.
///
/// @param hashed_msg: the hashed 32-bytes message. The message must be hashed instead
/// of plain text to be secure.
///
/// If the signature is valid, return the corresponding recovered Secpk256k1 public
/// key, otherwise throw error. This is similar to ecrecover in Ethereum, can only be
/// applied to Secp256k1 signatures.
public native fun ecrecover(signature: &vector<u8>, hashed_msg: &vector<u8>): vector<u8>;

/// @param pubkey: A 33-bytes compressed public key, a prefix either 0x02 or 0x03 and a 256-bit integer.
///
/// If the compressed public key is valid, return the 65-bytes uncompressed public key,
/// otherwise throw error.
public native fun decompress_pubkey(pubkey: &vector<u8>): vector<u8>;

/// @param data: arbitrary bytes data to hash
/// Hash the input bytes using keccak256 and returns 32 bytes.
public native fun keccak256(data: &vector<u8>): vector<u8>;

/// @param signature: A 65-bytes signature in form (r, s, v) that is signed using
/// Secp256k1. Reference implementation on signature generation using RFC6979:
/// https://github.com/MystenLabs/narwhal/blob/5d6f6df8ccee94446ff88786c0dbbc98be7cfc09/crypto/src/secp256k1.rs
///
/// @param public_key: The public key to verify the signature against
/// @param hashed_msg: The hashed 32-bytes message, same as what the signature is signed against.
///
/// If the signature is valid to the pubkey and hashed message, return true. Else false.
public native fun secp256k1_verify(signature: &vector<u8>, public_key: &vector<u8>, hashed_msg: &vector<u8>): bool;
}
26 changes: 26 additions & 0 deletions crates/sui-framework/sources/crypto/ed25519.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

module sui::ed25519 {
friend sui::validator;
/// @param signature: 32-byte signature that is a point on the Ed25519 elliptic curve.
/// @param public_key: 32-byte signature that is a point on the Ed25519 elliptic curve.
/// @param msg: The message that we test the signature against.
///
/// If the signature is a valid Ed25519 signature of the message and public key, return true.
/// Otherwise, return false.
public(friend) native fun ed25519_verify(signature: &vector<u8>, public_key: &vector<u8>, msg: &vector<u8>): bool;

/// @param signature: 32-byte signature that is a point on the Ed25519 elliptic curve.
/// @param public_key: 32-byte signature that is a point on the Ed25519 elliptic curve.
/// @param msg: The message that we test the signature against.
/// @param domain: The domain that the signature is tested again. We essentially prepend this to the message.
///
/// If the signature is a valid Ed25519 signature of the message and public key, return true.
/// Otherwise, return false.
public(friend) fun ed25519_verify_with_domain(signature: &vector<u8>, public_key: &vector<u8>, msg: vector<u8>, domain: vector<u8>): bool {
std::vector::append(&mut domain, msg);
ed25519_verify(signature, public_key, &domain)
}

}
4 changes: 2 additions & 2 deletions crates/sui-framework/sources/governance/validator.move
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module sui::validator {
use sui::stake::Stake;
use sui::epoch_time_lock::EpochTimeLock;
use std::option::Option;
use sui::crypto::Self;
use sui::bls12381::bls12381_verify_with_domain;
use sui::staking_pool::{Self, Delegation, StakedSui, StakingPool};

friend sui::genesis;
Expand Down Expand Up @@ -81,7 +81,7 @@ module sui::validator {
let address_bytes = bcs::to_bytes(&sui_address);
vector::append(&mut signed_bytes, address_bytes);
assert!(
crypto::bls12381_verify_with_domain(&proof_of_possession, &pubkey_bytes, signed_bytes, PROOF_OF_POSSESSION_DOMAIN) == true,
bls12381_verify_with_domain(&proof_of_possession, &pubkey_bytes, signed_bytes, PROOF_OF_POSSESSION_DOMAIN) == true,
0
);
}
Expand Down
14 changes: 7 additions & 7 deletions crates/sui-framework/src/natives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@ pub fn all_natives(
sui_framework_addr: AccountAddress,
) -> NativeFunctionTable {
let sui_natives: &[(&str, &str, NativeFunction)] = &[
("crypto", "ecrecover", make_native!(crypto::ecrecover)),
("ecdsa", "ecrecover", make_native!(crypto::ecrecover)),
(
"crypto",
"ecdsa",
"decompress_pubkey",
make_native!(crypto::decompress_pubkey),
),
("crypto", "keccak256", make_native!(crypto::keccak256)),
("ecdsa", "keccak256", make_native!(crypto::keccak256)),
(
"crypto",
"ecdsa",
"secp256k1_verify",
make_native!(crypto::secp256k1_verify),
),
(
"crypto",
"bls12381",
"bls12381_verify_g1_sig",
make_native!(crypto::bls12381_verify_g1_sig),
),
Expand All @@ -53,7 +53,7 @@ pub fn all_natives(
("object", "delete_impl", make_native!(object::delete_impl)),
("object", "borrow_uid", make_native!(object::borrow_uid)),
(
"crypto",
"bulletproofs",
"native_verify_full_range_proof",
make_native!(crypto::verify_range_proof),
),
Expand Down Expand Up @@ -83,7 +83,7 @@ pub fn all_natives(
make_native!(crypto::scalar_from_bytes),
),
(
"crypto",
"ed25519",
"ed25519_verify",
make_native!(crypto::ed25519_verify),
),
Expand Down
Loading

0 comments on commit f96e43a

Please sign in to comment.