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

[crypto] Move Bulletproofs and EC #4035

Merged
merged 1 commit into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
40 changes: 33 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/sui-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ sha3 = "0.10.2"
digest = "0.10.3"

[dev-dependencies]
insta = { version = "1.17.1", features = ["redactions"] }
insta = { version = "1.17.1", features = ["redactions", "yaml"] }
tempfile = "3.3.0"

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@ source: crates/sui-cost/tests/snapshot_tests.rs
expression: common_costs
---
Publish:
computationCost: 549
computationCost: 561
storageCost: 84
storageRebate: 16
MergeCoin:
computationCost: 489
computationCost: 501
storageCost: 32
storageRebate: 0
? SplitCoin: 0
: computationCost: 472
: computationCost: 484
storageCost: 32
storageRebate: 0
? SplitCoin: 1
: computationCost: 515
: computationCost: 527
storageCost: 48
storageRebate: 0
? SplitCoin: 2
: computationCost: 558
: computationCost: 570
storageCost: 64
storageRebate: 0
? SplitCoin: 3
: computationCost: 601
: computationCost: 613
storageCost: 80
storageRebate: 0
TransferWholeCoin:
Expand Down
1 change: 1 addition & 0 deletions crates/sui-framework/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ smallvec = "1.9.0"
num_enum = "0.5.7"
once_cell = "1.13.1"
sha3 = "0.10.1"
curve25519-dalek-ng = "4.1.1"

sui-types = { path = "../sui-types" }
sui-framework-build = { path = "../sui-framework-build" }
Expand Down
12 changes: 12 additions & 0 deletions crates/sui-framework/sources/crypto.move
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,16 @@ module sui::crypto {
/// 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;

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

native fun native_verify_full_range_proof(proof: vector<u8>, commitment: vector<u8>);

/// @param proof: The bulletproof
/// @param commitment: The commitment which we are trying to verify the range proof for
///
/// If the range proof is valid, execution succeeds, else panics.
public fun verify_full_range_proof(proof: vector<u8>, commitment: RistrettoPoint) {
native_verify_full_range_proof(proof, ec::bytes(&commitment))
}
}
120 changes: 120 additions & 0 deletions crates/sui-framework/sources/crypto/elliptic_curve.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

/// Library for Elliptic Curve operations on chain. We specifically support the Ristretto-255 sub-group.
module sui::elliptic_curve {
use std::vector;

///////////////////////////////////
/// Elliptic Curve structs
///////////////////////////////////

/// Represents a point on the Ristretto-255 subgroup.
struct RistrettoPoint has copy, drop, store {
// A 32-byte representation of the group element.
value: vector<u8>
}

/// Represents a scalar within the Curve25519 prime-order group.
struct Scalar has copy, drop, store {
// A 32-byte representation of the scalar
value: vector<u8>
}

///////////////////////////////////
/// Private
///////////////////////////////////

/// @param value: The value to commit to
/// @param blinding_factor: A random number used to ensure that the commitment is hiding.
native fun native_create_pedersen_commitment(value: vector<u8>, blinding_factor: vector<u8>): vector<u8>;

/// @param self: bytes representation of an EC point on the Ristretto-255 subgroup
/// @param other: bytes representation of an EC point on the Ristretto-255 subgroup
/// A native move wrapper around the addition of Ristretto points. Returns self + other.
native fun native_add_ristretto_point(point1: vector<u8>, point2: vector<u8>): vector<u8>;

/// @param self: bytes representation of an EC point on the Ristretto-255 subgroup
/// @param other: bytes representation of an EC point on the Ristretto-255 subgroup
/// A native move wrapper around the subtraction of Ristretto points. Returns self - other.
native fun native_subtract_ristretto_point(point1: vector<u8>, point2: vector<u8>): vector<u8>;

/// @param value: the value of the to-be-created scalar
/// TODO: Transfer this into a Move function some time in the future.
/// A native move wrapper for the creation of Scalars on Curve25519.
native fun native_scalar_from_u64(value: u64): vector<u8>;


/// @param value: the bytes representation of the scalar.
/// TODO: Transfer this into a Move function some time in the future.
/// A native move wrapper for the creation of Scalars on Curve25519.
native fun native_scalar_from_bytes(bytes: vector<u8>): vector<u8>;

///////////////////////////////////
/// Public
///////////////////////////////////

// Scalar
///////////////////////

/// Create a field element from u64
public fun new_scalar_from_u64(value: u64): Scalar {
Scalar {
value: native_scalar_from_u64(value)
}
}

/// Create a pedersen commitment from two field elements
public fun create_pedersen_commitment(value: Scalar, blinding_factor: Scalar): RistrettoPoint {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although it's not clear where we need that (because we reveal a blinding factor and in most privacy preserving apps we don't open commitments publicly), I'll approve for now as it might be useful in the future for fraud proofs etc.

Copy link
Contributor Author

@punwai punwai Aug 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll keep this for this PR, but if I can implement the example without this, I'll remove it with the example PR.

return RistrettoPoint {
value: native_create_pedersen_commitment(value.value, blinding_factor.value)
}
}

/// Creates a new field element from byte representation. Note that
/// `value` must be 32-bytes
public fun new_scalar_from_bytes(value: vector<u8>): Scalar {
Scalar {
value: native_scalar_from_bytes(value)
}
}

/// Get the byte representation of the field element
public fun scalar_bytes(self: &Scalar): vector<u8> {
self.value
}

// EC Point
///////////////////////

/// Get the underlying compressed byte representation of the group element
public fun bytes(self: &RistrettoPoint): vector<u8> {
self.value
}


/// Perform addition on two group elements
public fun add(self: &RistrettoPoint, other: &RistrettoPoint): RistrettoPoint {
RistrettoPoint {
value: native_add_ristretto_point(self.value, other.value)
}
}

/// Perform subtraction on two group elements
public fun subtract(self: &RistrettoPoint, other: &RistrettoPoint): RistrettoPoint {
RistrettoPoint {
value: native_subtract_ristretto_point(self.value, other.value)
}
}

/// Attempt to create a new group element from compressed bytes representation
public fun new_from_bytes(bytes: vector<u8>): RistrettoPoint {
assert!(vector::length(&bytes) == 32, 1);
RistrettoPoint {
value: bytes
}
}

// TODO: Add arithmetic for Scalar elements. We just need add, subtract, and multiply.
// TODO: Add scalar to point multiplication for group elements.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

}
Loading