Skip to content

Commit

Permalink
prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
dovgopoly committed Jan 29, 2025
1 parent ef39f8f commit cc4a57b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 28 deletions.
69 changes: 46 additions & 23 deletions aptos-move/framework/src/natives/cryptography/bulletproofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

#[cfg(feature = "testing")]
use crate::natives::cryptography::ristretto255::{
pop_scalar_from_bytes, pop_scalars_from_bytes,
};
use crate::natives::cryptography::ristretto255::{pop_scalar_from_bytes, pop_scalars_from_bytes};
use crate::natives::cryptography::ristretto255_point::{
get_point_handle, NativeRistrettoPointContext,
};
Expand All @@ -27,8 +25,7 @@ use move_vm_types::{
};
use once_cell::sync::Lazy;
use smallvec::{smallvec, SmallVec};
use std::collections::{VecDeque};
use aptos_gas_algebra::GasExpression;
use std::collections::VecDeque;

pub mod abort_codes {
/// Abort code when deserialization fails (leading 0x01 == INVALID_ARGUMENT)
Expand Down Expand Up @@ -213,7 +210,7 @@ fn native_test_only_prove_range(
&v_blinding,
num_bits,
)
.expect("Bulletproofs prover failed unexpectedly");
.expect("Bulletproofs prover failed unexpectedly");

Ok(smallvec![
Value::vector_u8(proof.to_bytes()),
Expand Down Expand Up @@ -255,7 +252,10 @@ fn native_test_only_batch_prove_range(
}

// Make sure only the first 64 bits are set for each Scalar.
if !vs.iter().all(|v| v.as_bytes()[8..].iter().all(|&byte| byte == 0u8)) {
if !vs
.iter()
.all(|v| v.as_bytes()[8..].iter().all(|&byte| byte == 0u8))
{
return Err(SafeNativeError::Abort {
abort_code: abort_codes::NFE_VALUE_OUTSIDE_RANGE,
});
Expand Down Expand Up @@ -292,13 +292,16 @@ fn native_test_only_batch_prove_range(
&v_blindings,
num_bits,
)
.expect("Bulletproofs prover failed unexpectedly");
.expect("Bulletproofs prover failed unexpectedly");

Ok(smallvec![
Value::vector_u8(proof.to_bytes()),
Value::vector_for_testing_only(commitments.iter()
.map(|commitment| Value::vector_u8(commitment.as_bytes().to_vec()))
.collect::<Vec<_>>())
Value::vector_for_testing_only(
commitments
.iter()
.map(|commitment| Value::vector_u8(commitment.as_bytes().to_vec()))
.collect::<Vec<_>>()
)
])
}

Expand All @@ -321,7 +324,7 @@ fn verify_range_proof(

let range_proof = match bulletproofs::RangeProof::from_bytes(proof_bytes) {
Ok(proof) => proof,
Err(_) => return Ok(smallvec![Value::bool(false)])
Err(_) => return Ok(smallvec![Value::bool(false)]),
};

// The (Bullet)proof size is $\log_2(num_bits)$ and its verification time is $O(num_bits)$
Expand Down Expand Up @@ -356,7 +359,7 @@ fn verify_batch_range_proof(

let range_proof = match bulletproofs::RangeProof::from_bytes(proof_bytes) {
Ok(proof) => proof,
Err(_) => return Ok(smallvec![Value::bool(false)])
Err(_) => return Ok(smallvec![Value::bool(false)]),
};

// The (Bullet)proof size is $\log_2(num_bits)$ and its verification time is $O(num_bits)$
Expand Down Expand Up @@ -417,11 +420,22 @@ fn charge_gas_for_deserialization(
let proof_bytes_len = NumBytes::new(proof_bytes.len() as u64);

match batch_size {
1 => context.charge(BULLETPROOFS_DESERIALIZE_BASE_1 + BULLETPROOFS_DESERIALIZE_PER_BYTE_1 * proof_bytes_len),
2 => context.charge(BULLETPROOFS_DESERIALIZE_BASE_2 + BULLETPROOFS_DESERIALIZE_PER_BYTE_2 * proof_bytes_len),
4 => context.charge(BULLETPROOFS_DESERIALIZE_BASE_4 + BULLETPROOFS_DESERIALIZE_PER_BYTE_4 * proof_bytes_len),
8 => context.charge(BULLETPROOFS_DESERIALIZE_BASE_8 + BULLETPROOFS_DESERIALIZE_PER_BYTE_8 * proof_bytes_len),
16 => context.charge(BULLETPROOFS_DESERIALIZE_BASE_16 + BULLETPROOFS_DESERIALIZE_PER_BYTE_16 * proof_bytes_len),
1 => context.charge(
BULLETPROOFS_DESERIALIZE_BASE_1 + BULLETPROOFS_DESERIALIZE_PER_BYTE_1 * proof_bytes_len,
),
2 => context.charge(
BULLETPROOFS_DESERIALIZE_BASE_2 + BULLETPROOFS_DESERIALIZE_PER_BYTE_2 * proof_bytes_len,
),
4 => context.charge(
BULLETPROOFS_DESERIALIZE_BASE_4 + BULLETPROOFS_DESERIALIZE_PER_BYTE_4 * proof_bytes_len,
),
8 => context.charge(
BULLETPROOFS_DESERIALIZE_BASE_8 + BULLETPROOFS_DESERIALIZE_PER_BYTE_8 * proof_bytes_len,
),
16 => context.charge(
BULLETPROOFS_DESERIALIZE_BASE_16
+ BULLETPROOFS_DESERIALIZE_PER_BYTE_16 * proof_bytes_len,
),
_ => unreachable!(),
}
}
Expand All @@ -435,11 +449,20 @@ fn charge_gas_for_verification(
let bit_length = NumBytes::new(bit_length as u64);

match batch_size {
1 => context.charge(BULLETPROOFS_VERIFY_BASE_1 + BULLETPROOFS_VERIFY_PER_BIT_1 * bit_length),
2 => context.charge(BULLETPROOFS_VERIFY_BASE_2 + BULLETPROOFS_VERIFY_PER_BIT_2 * bit_length),
4 => context.charge(BULLETPROOFS_VERIFY_BASE_4 + BULLETPROOFS_VERIFY_PER_BIT_4 * bit_length),
8 => context.charge(BULLETPROOFS_VERIFY_BASE_8 + BULLETPROOFS_VERIFY_PER_BIT_8 * bit_length),
16 => context.charge(BULLETPROOFS_VERIFY_BASE_16 + BULLETPROOFS_VERIFY_PER_BIT_16 * bit_length),
1 => {
context.charge(BULLETPROOFS_VERIFY_BASE_1 + BULLETPROOFS_VERIFY_PER_BIT_1 * bit_length)
},
2 => {
context.charge(BULLETPROOFS_VERIFY_BASE_2 + BULLETPROOFS_VERIFY_PER_BIT_2 * bit_length)
},
4 => {
context.charge(BULLETPROOFS_VERIFY_BASE_4 + BULLETPROOFS_VERIFY_PER_BIT_4 * bit_length)
},
8 => {
context.charge(BULLETPROOFS_VERIFY_BASE_8 + BULLETPROOFS_VERIFY_PER_BIT_8 * bit_length)
},
16 => context
.charge(BULLETPROOFS_VERIFY_BASE_16 + BULLETPROOFS_VERIFY_PER_BIT_16 * bit_length),
_ => unreachable!(),
}
}
7 changes: 5 additions & 2 deletions aptos-move/framework/src/natives/cryptography/ristretto255.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
use crate::natives::cryptography::{ristretto255_point, ristretto255_scalar};
use aptos_gas_algebra::GasExpression;
use aptos_gas_schedule::{gas_params::natives::aptos_framework::*, NativeGasParameters};
use aptos_native_interface::{safely_assert_eq, safely_pop_arg, safely_pop_vec_arg, RawSafeNative, SafeNativeBuilder, SafeNativeError, SafeNativeResult};
use aptos_native_interface::{
safely_assert_eq, safely_pop_arg, safely_pop_vec_arg, RawSafeNative, SafeNativeBuilder,
SafeNativeError, SafeNativeResult,
};
use aptos_types::vm_status::StatusCode;
use curve25519_dalek::scalar::Scalar;
use move_binary_format::errors::PartialVMError;
Expand Down Expand Up @@ -173,7 +176,7 @@ pub fn pop_scalar_from_bytes(arguments: &mut VecDeque<Value>) -> SafeNativeResul

/// Pops a Scalars off the argument stack when the argument was a `vector<vector<u8>>`.
pub fn pop_scalars_from_bytes(arguments: &mut VecDeque<Value>) -> SafeNativeResult<Vec<Scalar>> {
let bytes: Vec<Vec<u8>> = safely_pop_vec_arg!(arguments, Vec<u8>);
let bytes = safely_pop_vec_arg!(arguments, Vec<u8>);

bytes
.into_iter()
Expand Down
13 changes: 10 additions & 3 deletions crates/aptos-crypto/benches/bulletproofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
#[macro_use]
extern crate criterion;

use aptos_crypto::bulletproofs::MAX_RANGE_BITS;
use bulletproofs::{BulletproofGens, PedersenGens, RangeProof};
use criterion::{measurement::Measurement, BenchmarkGroup, BenchmarkId, Criterion, Throughput};
use curve25519_dalek_ng::scalar::Scalar;
use merlin::Transcript;
use rand::{thread_rng, Rng};
use aptos_crypto::bulletproofs::MAX_RANGE_BITS;

fn get_values(num_bits: usize, batch_size: usize) -> (Vec<u64>, Vec<Scalar>) {
let mut rng = thread_rng();
Expand Down Expand Up @@ -67,13 +67,20 @@ fn range_prove<M: Measurement>(g: &mut BenchmarkGroup<M>, num_bits: usize, batch
);
}

fn range_proof_deserialize<M: Measurement>(g: &mut BenchmarkGroup<M>, num_bits: usize, batch_size: usize) {
fn range_proof_deserialize<M: Measurement>(
g: &mut BenchmarkGroup<M>,
num_bits: usize,
batch_size: usize,
) {
let bp_gens = BulletproofGens::new(MAX_RANGE_BITS, 16);
let pc_gens = PedersenGens::default();

g.throughput(Throughput::Elements(1));
g.bench_function(
BenchmarkId::new(format!("range_proof_deserialize_batch_{}", batch_size), num_bits),
BenchmarkId::new(
format!("range_proof_deserialize_batch_{}", batch_size),
num_bits,
),
move |b| {
b.iter_with_setup(
|| {
Expand Down

0 comments on commit cc4a57b

Please sign in to comment.