Skip to content

Commit ce5b24b

Browse files
committed
clean up
1 parent 177fa75 commit ce5b24b

File tree

4 files changed

+44
-32
lines changed

4 files changed

+44
-32
lines changed

fastcrypto/src/bulletproofs.rs

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
//! ```rust
88
//! # use fastcrypto::bulletproofs::*;
99
//! use rand::{thread_rng, RngCore};
10+
//! use fastcrypto::bulletproofs::Range::Bits16;
1011
//! # use fastcrypto::groups::ristretto255::RistrettoScalar;
1112
//! # use fastcrypto::groups::Scalar;
1213
//! let value = 300;
13-
//! let bits = 16;
14+
//! let range = Bits16;
1415
//! let output =
15-
//! RangeProof::prove(value, bits, b"MY_DOMAIN", &mut thread_rng()).unwrap();
16-
//! assert!(output.proof.verify(&output.commitment, &output.blinding, bits, b"MY_DOMAIN").is_ok());
16+
//! RangeProof::prove(value, &range, b"MY_DOMAIN", &mut thread_rng()).unwrap();
17+
//! assert!(output.proof.verify(&output.commitment, &output.blinding, &range, b"MY_DOMAIN").is_ok());
1718
//! ```
1819
1920
use crate::error::FastCryptoError::{GeneralOpaqueError, InvalidInput, InvalidProof};
@@ -55,15 +56,25 @@ pub struct AggregateRangeProofOutput {
5556
}
5657

5758
pub enum Range {
59+
/// The range [0,...,2^8).
5860
Bits8,
61+
62+
/// The range [0,...,2^16).
5963
Bits16,
64+
65+
/// The range [0,...,2^32).
6066
Bits32,
67+
68+
/// The range [0,...,2^64).
6169
Bits64,
6270
}
6371

6472
impl Range {
65-
pub fn is_in_range(&self, value: &u64) -> bool {
66-
value.ilog2() <= self.upper_bound_in_bits()
73+
pub fn is_in_range(&self, value: u64) -> bool {
74+
if value == 0 {
75+
return true;
76+
}
77+
value.ilog2() < self.upper_bound_in_bits()
6778
}
6879

6980
fn upper_bound_in_bits(&self) -> u32 {
@@ -159,7 +170,7 @@ impl RangeProof {
159170
domain: &'static [u8],
160171
rng: &mut impl AllowedRng,
161172
) -> FastCryptoResult<AggregateRangeProofOutput> {
162-
if values.iter().any(|v| !range.is_in_range(v))
173+
if values.iter().any(|&v| !range.is_in_range(v))
163174
|| blindings.len() != values.len()
164175
|| !values.len().is_power_of_two()
165176
{
@@ -227,3 +238,29 @@ impl RangeProof {
227238
.map_err(|_| InvalidProof)
228239
}
229240
}
241+
242+
#[test]
243+
fn test_is_in_range() {
244+
assert!(Range::Bits8.is_in_range(0));
245+
assert!(Range::Bits8.is_in_range(255));
246+
assert!(!Range::Bits8.is_in_range(256));
247+
assert!(Range::Bits16.is_in_range(0));
248+
assert!(Range::Bits16.is_in_range(65535));
249+
assert!(!Range::Bits16.is_in_range(65536));
250+
assert!(Range::Bits32.is_in_range(0));
251+
assert!(Range::Bits32.is_in_range(4294967295));
252+
assert!(!Range::Bits32.is_in_range(4294967296));
253+
assert!(Range::Bits64.is_in_range(0));
254+
assert!(Range::Bits64.is_in_range(u64::MAX));
255+
}
256+
257+
#[test]
258+
fn test_range_proof_valid() {
259+
use rand::thread_rng;
260+
let range = Range::Bits32;
261+
let output = RangeProof::prove(1u64, &range, b"NARWHAL", &mut thread_rng()).unwrap();
262+
assert!(output
263+
.proof
264+
.verify(&output.commitment, &output.blinding, &range, b"NARWHAL")
265+
.is_ok());
266+
}

fastcrypto/src/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ pub mod secp256r1_recoverable_tests;
3232
#[path = "tests/bls12381_tests.rs"]
3333
pub mod bls12381_tests;
3434

35-
#[cfg(test)]
36-
#[path = "tests/bulletproofs_tests.rs"]
37-
pub mod bulletproofs_tests;
38-
3935
#[cfg(all(test, feature = "aes"))]
4036
#[path = "tests/aes_tests.rs"]
4137
pub mod aes_tests;

fastcrypto/src/tests/bulletproofs_tests.rs

Lines changed: 0 additions & 20 deletions
This file was deleted.

fastcrypto/src/twisted_elgamal.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) 2022, Mysten Labs, Inc.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
use crate::bulletproofs::Range;
54
use crate::error::FastCryptoError::{InvalidInput, InvalidProof};
65
use crate::error::FastCryptoResult;
76
use crate::groups::ristretto255::{RistrettoPoint, RistrettoScalar, RISTRETTO_POINT_BYTE_LENGTH};
@@ -278,7 +277,7 @@ fn test_equality_proof() {
278277
#[test]
279278
fn encrypt_and_range_proof() {
280279
let value = 1234u32;
281-
let range = Range::Bits32;
280+
let range = crate::bulletproofs::Range::Bits32;
282281
let mut rng = rand::thread_rng();
283282
let (pk, sk) = generate_keypair(&mut rng);
284283
let (ciphertext, blinding) = Ciphertext::encrypt(&pk, value, &mut rng);

0 commit comments

Comments
 (0)