Skip to content

Commit

Permalink
[fix] fix bulletproofs move test failure (MystenLabs#4158)
Browse files Browse the repository at this point in the history
  • Loading branch information
punwai authored Aug 19, 2022
1 parent 0c884b9 commit 7a32715
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions crates/sui-framework/sources/crypto.move
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ module sui::crypto {

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

native fun native_verify_full_range_proof(proof: vector<u8>, commitment: vector<u8>);
/// 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) {
native_verify_full_range_proof(proof, ec::bytes(&commitment))
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)
}
}
4 changes: 2 additions & 2 deletions crates/sui-framework/src/natives/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub const INVALID_RISTRETTO_GROUP_ELEMENT: u64 = 3;
pub const INVALID_RISTRETTO_SCALAR: u64 = 4;
pub const BULLETPROOFS_VERIFICATION_FAILED: u64 = 5;

pub const BP_DOMAIN: &[u8] = b"sui";
pub const BP_DOMAIN: &[u8] = b"mizu";

/// Native implemention of ecrecover in public Move API, see crypto.move for specifications.
pub fn ecrecover(
Expand Down Expand Up @@ -116,7 +116,7 @@ pub fn verify_range_proof(
mut args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
debug_assert!(ty_args.is_empty());
debug_assert!(args.len() == 2);
debug_assert!(args.len() == 3);

let bit_length = pop_arg!(args, u64);
let commitment_bytes = pop_arg!(args, Vec<u8>);
Expand Down
28 changes: 16 additions & 12 deletions crates/sui-framework/tests/crypto_tests.move
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ module sui::crypto_tests {
let committed_value_2 = 500u64;
let blinding_value_2 = 50u64;

let committed_sum = committed_value_1 - committed_value_2;
let blinding_sum = blinding_value_1 - blinding_value_2;
let committed_diff = committed_value_1 - committed_value_2;
let blinding_diff = blinding_value_1 - blinding_value_2;

let point_1 = ec::create_pedersen_commitment(
ec::new_scalar_from_u64(committed_value_1),
Expand All @@ -129,11 +129,11 @@ module sui::crypto_tests {
);

let point_diff_reference = ec::create_pedersen_commitment(
ec::new_scalar_from_u64(committed_sum),
ec::new_scalar_from_u64(blinding_sum)
ec::new_scalar_from_u64(committed_diff),
ec::new_scalar_from_u64(blinding_diff)
);

let point_diff = ec::add(&point_1, &point_2);
let point_diff = ec::subtract(&point_1, &point_2);

assert!(ec::bytes(&point_diff) == ec::bytes(&point_diff_reference), 0)
}
Expand All @@ -156,38 +156,42 @@ module sui::crypto_tests {

#[test]
fun test_bulletproof_standard_0_2pow64_proof() {
let bit_length: u64 = 64;

// These are generated elsewhere;
let bulletproof = vector[
222, 248, 245, 34, 209, 240, 49, 64, 37, 205, 174, 236, 167, 140, 136, 230, 139, 12, 184, 101, 238, 245, 71, 131, 3, 75, 63, 156, 132, 61, 81, 78, 216, 165, 254, 193, 92, 55, 38, 84, 116, 131, 201, 75, 255, 210, 224, 57, 212, 94, 24, 145, 57, 81, 19, 202, 3, 33, 211, 226, 82, 0, 207, 4, 208, 153, 197, 69, 170, 208, 107, 254, 191, 225, 171, 130, 101, 91, 251, 163, 15, 4, 84, 99, 140, 84, 13, 171, 26, 66, 79, 144, 116, 182, 167, 31, 192, 45, 135, 143, 107, 138, 171, 48, 173, 141, 45, 26, 115, 187, 60, 82, 130, 159, 207, 118, 43, 176, 28, 1, 231, 180, 203, 211, 167, 238, 193, 27, 66, 225, 58, 187, 56, 10, 162, 127, 30, 83, 147, 177, 116, 243, 11, 107, 241, 23, 236, 240, 144, 136, 137, 253, 52, 135, 170, 120, 221, 125, 125, 4, 5, 10, 45, 79, 15, 86, 218, 136, 48, 48, 240, 17, 124, 108, 44, 0, 58, 231, 254, 224, 99, 30, 198, 103, 50, 132, 7, 227, 171, 79, 193, 14, 99, 43, 24, 249, 119, 153, 49, 31, 167, 165, 153, 144, 20, 241, 151, 115, 242, 62, 163, 151, 218, 194, 175, 117, 161, 188, 105, 112, 13, 122, 57, 2, 226, 11, 244, 165, 216, 20, 53, 251, 110, 37, 17, 103, 229, 119, 100, 188, 62, 245, 36, 35, 179, 119, 146, 154, 17, 174, 135, 163, 69, 213, 200, 101, 178, 74, 23, 8, 31, 140, 208, 9, 118, 163, 55, 43, 247, 228, 13, 15, 243, 83, 185, 38, 27, 40, 9, 180, 98, 21, 232, 94, 254, 191, 150, 122, 186, 248, 107, 155, 120, 58, 242, 253, 130, 31, 211, 136, 201, 51, 213, 159, 97, 142, 140, 158, 138, 31, 65, 238, 39, 196, 207, 85, 65, 105, 210, 91, 46, 45, 36, 137, 225, 247, 5, 105, 164, 23, 152, 220, 222, 228, 200, 107, 42, 44, 113, 100, 209, 209, 169, 158, 234, 234, 221, 138, 51, 90, 126, 52, 84, 178, 69, 19, 55, 240, 193, 215, 175, 10, 86, 167, 149, 49, 129, 22, 84, 162, 147, 5, 170, 110, 231, 158, 201, 221, 149, 54, 251, 97, 113, 84, 220, 201, 208, 66, 96, 124, 127, 244, 35, 227, 124, 46, 22, 75, 21, 112, 103, 122, 99, 252, 42, 238, 64, 190, 27, 166, 101, 43, 29, 231, 196, 80, 120, 1, 80, 131, 94, 130, 27, 254, 238, 157, 28, 168, 76, 21, 84, 177, 133, 191, 198, 69, 2, 214, 223, 160, 47, 36, 164, 236, 247, 93, 61, 39, 24, 157, 46, 174, 189, 140, 170, 52, 92, 45, 22, 71, 177, 183, 91, 221, 92, 123, 40, 201, 16, 66, 157, 198, 54, 119, 251, 102, 143, 89, 188, 110, 12, 248, 7, 124, 222, 92, 66, 249, 74, 67, 183, 138, 235, 40, 203, 27, 152, 11, 64, 96, 129, 50, 249, 156, 189, 233, 182, 226, 120, 209, 135, 9, 24, 10, 109, 34, 84, 234, 148, 72, 2, 253, 66, 242, 81, 63, 174, 60, 103, 65, 151, 143, 46, 251, 188, 228, 38, 19, 143, 246, 185, 126, 158, 13, 224, 84, 174, 19, 72, 70, 80, 109, 113, 52, 206, 31, 83, 61, 186, 44, 95, 114, 157, 123, 15, 165, 58, 47, 7, 78, 86, 149, 111, 235, 142, 75, 250, 141, 40, 85, 137, 237, 61, 233, 229, 142, 196, 47, 60, 95, 191, 202, 81, 153, 39, 229, 11, 225, 209, 212, 115, 175, 78, 3, 1, 87, 165, 107, 144, 213, 166, 166, 234, 42, 72, 200, 27, 19, 154, 201, 124, 233, 165, 201, 41, 103, 235, 143, 175, 200, 55, 64, 33, 120, 143, 233, 163, 157, 145, 3, 162, 228, 232, 81, 110, 194, 46, 94, 214, 145, 137, 57, 2, 128, 225, 40, 23, 210, 71, 172, 32, 57, 127, 32, 110, 221, 252, 161, 146, 112, 140, 0
134, 202, 56, 10, 230, 57, 155, 136, 64, 241, 33, 239, 133, 26, 63, 138, 65, 126, 109, 92, 239, 105, 128, 173, 28, 250, 214, 18, 209, 189, 46, 40, 166, 22, 208, 81, 154, 96, 78, 3, 238, 160, 30, 110, 30, 129, 58, 0, 225, 200, 118, 71, 228, 190, 191, 60, 102, 39, 185, 55, 18, 188, 113, 101, 8, 66, 68, 32, 67, 166, 124, 160, 1, 130, 124, 19, 181, 195, 67, 181, 140, 7, 56, 135, 242, 195, 60, 93, 6, 189, 95, 185, 39, 176, 180, 3, 234, 220, 117, 89, 165, 181, 100, 130, 229, 49, 108, 73, 244, 88, 251, 252, 86, 217, 113, 246, 244, 54, 195, 209, 206, 142, 201, 223, 240, 218, 166, 68, 186, 69, 147, 75, 84, 92, 44, 10, 79, 92, 207, 222, 129, 182, 157, 37, 117, 227, 233, 39, 125, 109, 90, 62, 79, 240, 167, 158, 123, 239, 31, 5, 43, 34, 136, 24, 26, 166, 58, 127, 203, 154, 27, 55, 234, 105, 16, 58, 140, 116, 45, 72, 98, 98, 105, 33, 46, 49, 81, 243, 211, 14, 241, 0, 1, 194, 207, 205, 205, 86, 35, 36, 33, 150, 50, 65, 62, 51, 11, 133, 60, 19, 68, 189, 46, 40, 185, 109, 179, 113, 84, 164, 191, 87, 57, 8, 226, 207, 49, 179, 235, 40, 255, 251, 147, 5, 64, 57, 109, 2, 217, 0, 83, 244, 123, 121, 14, 25, 152, 31, 138, 102, 113, 126, 137, 14, 231, 81, 212, 103, 120, 11, 136, 169, 75, 53, 210, 156, 72, 78, 116, 56, 150, 90, 141, 216, 71, 185, 130, 205, 235, 248, 131, 62, 191, 242, 216, 244, 47, 54, 166, 44, 212, 114, 252, 102, 136, 95, 140, 77, 128, 136, 163, 110, 194, 85, 41, 10, 241, 156, 236, 50, 144, 11, 191, 5, 225, 46, 121, 252, 163, 60, 206, 206, 160, 172, 234, 205, 30, 230, 177, 173, 103, 173, 3, 201, 170, 244, 218, 108, 232, 112, 68, 188, 21, 95, 193, 6, 86, 41, 13, 55, 58, 24, 118, 28, 145, 198, 185, 241, 241, 184, 177, 39, 224, 149, 171, 194, 130, 10, 104, 236, 178, 8, 172, 191, 118, 143, 121, 56, 46, 158, 35, 17, 159, 24, 28, 59, 68, 3, 221, 52, 34, 241, 168, 246, 234, 122, 244, 29, 87, 71, 222, 142, 44, 160, 164, 30, 199, 10, 247, 99, 90, 149, 72, 77, 214, 45, 42, 255, 15, 255, 76, 45, 163, 113, 163, 181, 101, 248, 248, 101, 10, 247, 6, 164, 201, 191, 22, 230, 151, 96, 146, 188, 79, 33, 243, 244, 15, 56, 74, 205, 186, 42, 97, 113, 151, 82, 115, 132, 202, 19, 32, 146, 209, 112, 215, 174, 41, 110, 190, 1, 123, 152, 161, 106, 55, 214, 114, 46, 163, 18, 138, 49, 204, 124, 39, 42, 124, 146, 138, 231, 101, 232, 152, 45, 101, 58, 119, 58, 220, 214, 229, 169, 169, 23, 231, 18, 175, 78, 230, 32, 95, 110, 90, 142, 165, 249, 20, 179, 57, 117, 217, 212, 11, 152, 47, 62, 64, 158, 72, 76, 201, 59, 51, 64, 245, 94, 69, 187, 41, 130, 177, 196, 14, 104, 72, 27, 186, 6, 10, 196, 203, 204, 20, 14, 113, 142, 46, 115, 76, 117, 157, 191, 89, 219, 169, 50, 83, 34, 244, 75, 199, 4, 160, 77, 22, 117, 118, 161, 188, 34, 60, 212, 72, 187, 223, 245, 226, 252, 79, 214, 83, 161, 35, 52, 32, 169, 21, 115, 151, 213, 170, 132, 71, 175, 99, 43, 129, 97, 226, 27, 238, 168, 206, 117, 39, 237, 192, 90, 107, 178, 184, 105, 68, 54, 90, 78, 197, 1, 87, 191, 212, 156, 195, 135, 153, 165, 247, 111, 113, 0, 187, 80, 255, 162, 188, 107, 200, 250, 91, 160, 165, 22, 142, 222, 15, 126, 147, 234, 230, 129, 41, 145, 103, 190, 78, 101, 75, 8, 75, 50, 89, 5
];

let committed_value = 1000u64;
let blinding_factor = 10u64;
let blinding_factor = 100u64;

let point = ec::create_pedersen_commitment(
ec::new_scalar_from_u64(committed_value),
ec::new_scalar_from_u64(blinding_factor)
);

crypto::verify_full_range_proof(bulletproof, point);
crypto::verify_full_range_proof(bulletproof, point, bit_length);
}

#[test]
#[expected_failure(abort_code = 0)]
#[expected_failure(abort_code = 5)]
fun test_bulletproof_standard_0_2pow64_invalid_proof() {
let bit_length: u64 = 64;

// These are generated elsewhere;
let bulletproof = vector[
0, 0, 0, 0, 0, 240, 49, 64, 37, 205, 174, 236, 167, 140, 136, 230, 139, 12, 184, 101, 238, 245, 71, 131, 3, 75, 63, 156, 132, 61, 81, 78, 216, 165, 254, 193, 92, 55, 38, 84, 116, 131, 201, 75, 255, 210, 224, 57, 212, 94, 24, 145, 57, 81, 19, 202, 3, 33, 211, 226, 82, 0, 207, 4, 208, 153, 197, 69, 170, 208, 107, 254, 191, 225, 171, 130, 101, 91, 251, 163, 15, 4, 84, 99, 140, 84, 13, 171, 26, 66, 79, 144, 116, 182, 167, 31, 192, 45, 135, 143, 107, 138, 171, 48, 173, 141, 45, 26, 115, 187, 60, 82, 130, 159, 207, 118, 43, 176, 28, 1, 231, 180, 203, 211, 167, 238, 193, 27, 66, 225, 58, 187, 56, 10, 162, 127, 30, 83, 147, 177, 116, 243, 11, 107, 241, 23, 236, 240, 144, 136, 137, 253, 52, 135, 170, 120, 221, 125, 125, 4, 5, 10, 45, 79, 15, 86, 218, 136, 48, 48, 240, 17, 124, 108, 44, 0, 58, 231, 254, 224, 99, 30, 198, 103, 50, 132, 7, 227, 171, 79, 193, 14, 99, 43, 24, 249, 119, 153, 49, 31, 167, 165, 153, 144, 20, 241, 151, 115, 242, 62, 163, 151, 218, 194, 175, 117, 161, 188, 105, 112, 13, 122, 57, 2, 226, 11, 244, 165, 216, 20, 53, 251, 110, 37, 17, 103, 229, 119, 100, 188, 62, 245, 36, 35, 179, 119, 146, 154, 17, 174, 135, 163, 69, 213, 200, 101, 178, 74, 23, 8, 31, 140, 208, 9, 118, 163, 55, 43, 247, 228, 13, 15, 243, 83, 185, 38, 27, 40, 9, 180, 98, 21, 232, 94, 254, 191, 150, 122, 186, 248, 107, 155, 120, 58, 242, 253, 130, 31, 211, 136, 201, 51, 213, 159, 97, 142, 140, 158, 138, 31, 65, 238, 39, 196, 207, 85, 65, 105, 210, 91, 46, 45, 36, 137, 225, 247, 5, 105, 164, 23, 152, 220, 222, 228, 200, 107, 42, 44, 113, 100, 209, 209, 169, 158, 234, 234, 221, 138, 51, 90, 126, 52, 84, 178, 69, 19, 55, 240, 193, 215, 175, 10, 86, 167, 149, 49, 129, 22, 84, 162, 147, 5, 170, 110, 231, 158, 201, 221, 149, 54, 251, 97, 113, 84, 220, 201, 208, 66, 96, 124, 127, 244, 35, 227, 124, 46, 22, 75, 21, 112, 103, 122, 99, 252, 42, 238, 64, 190, 27, 166, 101, 43, 29, 231, 196, 80, 120, 1, 80, 131, 94, 130, 27, 254, 238, 157, 28, 168, 76, 21, 84, 177, 133, 191, 198, 69, 2, 214, 223, 160, 47, 36, 164, 236, 247, 93, 61, 39, 24, 157, 46, 174, 189, 140, 170, 52, 92, 45, 22, 71, 177, 183, 91, 221, 92, 123, 40, 201, 16, 66, 157, 198, 54, 119, 251, 102, 143, 89, 188, 110, 12, 248, 7, 124, 222, 92, 66, 249, 74, 67, 183, 138, 235, 40, 203, 27, 152, 11, 64, 96, 129, 50, 249, 156, 189, 233, 182, 226, 120, 209, 135, 9, 24, 10, 109, 34, 84, 234, 148, 72, 2, 253, 66, 242, 81, 63, 174, 60, 103, 65, 151, 143, 46, 251, 188, 228, 38, 19, 143, 246, 185, 126, 158, 13, 224, 84, 174, 19, 72, 70, 80, 109, 113, 52, 206, 31, 83, 61, 186, 44, 95, 114, 157, 123, 15, 165, 58, 47, 7, 78, 86, 149, 111, 235, 142, 75, 250, 141, 40, 85, 137, 237, 61, 233, 229, 142, 196, 47, 60, 95, 191, 202, 81, 153, 39, 229, 11, 225, 209, 212, 115, 175, 78, 3, 1, 87, 165, 107, 144, 213, 166, 166, 234, 42, 72, 200, 27, 19, 154, 201, 124, 233, 165, 201, 41, 103, 235, 143, 175, 200, 55, 64, 33, 120, 143, 233, 163, 157, 145, 3, 162, 228, 232, 81, 110, 194, 46, 94, 214, 145, 137, 57, 2, 128, 225, 40, 23, 210, 71, 172, 32, 57, 127, 32, 110, 221, 252, 161, 146, 112, 140, 0
0, 202, 56, 10, 230, 57, 155, 136, 64, 241, 33, 239, 133, 26, 63, 138, 65, 126, 109, 92, 239, 105, 128, 173, 28, 250, 214, 18, 209, 189, 46, 40, 166, 22, 208, 81, 154, 96, 78, 3, 238, 160, 30, 110, 30, 129, 58, 0, 225, 200, 118, 71, 228, 190, 191, 60, 102, 39, 185, 55, 18, 188, 113, 101, 8, 66, 68, 32, 67, 166, 124, 160, 1, 130, 124, 19, 181, 195, 67, 181, 140, 7, 56, 135, 242, 195, 60, 93, 6, 189, 95, 185, 39, 176, 180, 3, 234, 220, 117, 89, 165, 181, 100, 130, 229, 49, 108, 73, 244, 88, 251, 252, 86, 217, 113, 246, 244, 54, 195, 209, 206, 142, 201, 223, 240, 218, 166, 68, 186, 69, 147, 75, 84, 92, 44, 10, 79, 92, 207, 222, 129, 182, 157, 37, 117, 227, 233, 39, 125, 109, 90, 62, 79, 240, 167, 158, 123, 239, 31, 5, 43, 34, 136, 24, 26, 166, 58, 127, 203, 154, 27, 55, 234, 105, 16, 58, 140, 116, 45, 72, 98, 98, 105, 33, 46, 49, 81, 243, 211, 14, 241, 0, 1, 194, 207, 205, 205, 86, 35, 36, 33, 150, 50, 65, 62, 51, 11, 133, 60, 19, 68, 189, 46, 40, 185, 109, 179, 113, 84, 164, 191, 87, 57, 8, 226, 207, 49, 179, 235, 40, 255, 251, 147, 5, 64, 57, 109, 2, 217, 0, 83, 244, 123, 121, 14, 25, 152, 31, 138, 102, 113, 126, 137, 14, 231, 81, 212, 103, 120, 11, 136, 169, 75, 53, 210, 156, 72, 78, 116, 56, 150, 90, 141, 216, 71, 185, 130, 205, 235, 248, 131, 62, 191, 242, 216, 244, 47, 54, 166, 44, 212, 114, 252, 102, 136, 95, 140, 77, 128, 136, 163, 110, 194, 85, 41, 10, 241, 156, 236, 50, 144, 11, 191, 5, 225, 46, 121, 252, 163, 60, 206, 206, 160, 172, 234, 205, 30, 230, 177, 173, 103, 173, 3, 201, 170, 244, 218, 108, 232, 112, 68, 188, 21, 95, 193, 6, 86, 41, 13, 55, 58, 24, 118, 28, 145, 198, 185, 241, 241, 184, 177, 39, 224, 149, 171, 194, 130, 10, 104, 236, 178, 8, 172, 191, 118, 143, 121, 56, 46, 158, 35, 17, 159, 24, 28, 59, 68, 3, 221, 52, 34, 241, 168, 246, 234, 122, 244, 29, 87, 71, 222, 142, 44, 160, 164, 30, 199, 10, 247, 99, 90, 149, 72, 77, 214, 45, 42, 255, 15, 255, 76, 45, 163, 113, 163, 181, 101, 248, 248, 101, 10, 247, 6, 164, 201, 191, 22, 230, 151, 96, 146, 188, 79, 33, 243, 244, 15, 56, 74, 205, 186, 42, 97, 113, 151, 82, 115, 132, 202, 19, 32, 146, 209, 112, 215, 174, 41, 110, 190, 1, 123, 152, 161, 106, 55, 214, 114, 46, 163, 18, 138, 49, 204, 124, 39, 42, 124, 146, 138, 231, 101, 232, 152, 45, 101, 58, 119, 58, 220, 214, 229, 169, 169, 23, 231, 18, 175, 78, 230, 32, 95, 110, 90, 142, 165, 249, 20, 179, 57, 117, 217, 212, 11, 152, 47, 62, 64, 158, 72, 76, 201, 59, 51, 64, 245, 94, 69, 187, 41, 130, 177, 196, 14, 104, 72, 27, 186, 6, 10, 196, 203, 204, 20, 14, 113, 142, 46, 115, 76, 117, 157, 191, 89, 219, 169, 50, 83, 34, 244, 75, 199, 4, 160, 77, 22, 117, 118, 161, 188, 34, 60, 212, 72, 187, 223, 245, 226, 252, 79, 214, 83, 161, 35, 52, 32, 169, 21, 115, 151, 213, 170, 132, 71, 175, 99, 43, 129, 97, 226, 27, 238, 168, 206, 117, 39, 237, 192, 90, 107, 178, 184, 105, 68, 54, 90, 78, 197, 1, 87, 191, 212, 156, 195, 135, 153, 165, 247, 111, 113, 0, 187, 80, 255, 162, 188, 107, 200, 250, 91, 160, 165, 22, 142, 222, 15, 126, 147, 234, 230, 129, 41, 145, 103, 190, 78, 101, 75, 8, 75, 50, 89, 5
];

let committed_value = 1000u64;
let blinding_factor = 10u64;
let blinding_factor = 100u64;

let point = ec::create_pedersen_commitment(
ec::new_scalar_from_u64(committed_value),
ec::new_scalar_from_u64(blinding_factor)
);

crypto::verify_full_range_proof(bulletproof, point);
crypto::verify_full_range_proof(bulletproof, point, bit_length);
}
}

0 comments on commit 7a32715

Please sign in to comment.