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

Fix issue with serde_json RangeProof failure #26

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

Entries are listed in reverse chronological order.

## 5.0.1

* Support for `serde_json`. Any library trying to deserialize RangeProof from json would run into an error, which is now fixed.

## 5.0.0

* Change `curve25519-dalek-ng` dependency to `curve25519-dalek`. A major version bump is required because one cannot import `curve25519-dalek` and `bulletproofs` without conflicts.
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ name = "bulletproofs"
# - update version field
# - update html_root_url
# - update CHANGELOG
version = "5.0.0"
version = "5.0.1"
authors = ["Cathie Yun <[email protected]>",
"Henry de Valence <[email protected]>",
"Oleg Andreev <[email protected]>"]
Expand Down Expand Up @@ -35,6 +35,7 @@ clear_on_drop = { version = "0.2", default-features = false }
hex = "0.3"
criterion = "0.3"
bincode = "1"
serde_json = "1"
rand_chacha = "0.3"
curve25519-dalek = { version = "4.1.1", features = ["digest", "group", "legacy_compatibility", "rand_core", "serde"] }

Expand Down
78 changes: 64 additions & 14 deletions src/range_proof/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::transcript::TranscriptProtocol;
use crate::util;

use rand_core::{CryptoRng, RngCore};
use serde::de::Visitor;
use serde::de::{Error, SeqAccess, Visitor};
use serde::{self, Deserialize, Deserializer, Serialize, Serializer};

// Modules for MPC protocol
Expand Down Expand Up @@ -559,7 +559,25 @@ impl<'de> Deserialize<'de> for RangeProof {
type Value = RangeProof;

fn expecting(&self, formatter: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
formatter.write_str("a valid RangeProof")
formatter.write_str("a byte sequence (raw or UTF-8) representation of RangeProof")
}

fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: SeqAccess<'de>,
{
let mut vec = Vec::<u8>::new();
while let Some(elem) = seq.next_element()? {
vec.push(elem);
}
// Using Error::custom requires T: Display, which our error
// type only implements when it implements std::error::Error.
#[cfg(feature = "std")]
return RangeProof::from_bytes(&*vec).map_err(serde::de::Error::custom);
// In no-std contexts, drop the error message.
#[cfg(not(feature = "std"))]
return RangeProof::from_bytes(&*vec)
.map_err(|_| serde::de::Error::custom("deserialization error"));
}

fn visit_bytes<E>(self, v: &[u8]) -> Result<RangeProof, E>
Expand Down Expand Up @@ -631,7 +649,15 @@ mod tests {
/// 2. Serialize to wire format;
/// 3. Deserialize from wire format;
/// 4. Verify the proof.
fn singleparty_create_and_verify_helper(n: usize, m: usize) {
fn singleparty_create_and_verify_helper<T, S, D>(
n: usize,
m: usize,
serialize: S,
deserialize: D,
) where
S: Fn(&RangeProof) -> T,
D: Fn(&T) -> RangeProof,
{
// Split the test into two scopes, so that it's explicit what
// data is shared between the prover and the verifier.

Expand All @@ -645,7 +671,7 @@ mod tests {
let bp_gens = BulletproofGens::new(max_bitsize, max_parties);

// Prover's scope
let (proof_bytes, value_commitments) = {
let (serialized_proof, value_commitments) = {
use self::rand::Rng;
let mut rng = rand::thread_rng();

Expand All @@ -667,13 +693,13 @@ mod tests {
.unwrap();

// 2. Return serialized proof and value commitments
(bincode::serialize(&proof).unwrap(), value_commitments)
(serialize(&proof), value_commitments)
};

// Verifier's scope
{
// 3. Deserialize
let proof: RangeProof = bincode::deserialize(&proof_bytes).unwrap();
let proof: RangeProof = deserialize(&serialized_proof);

// 4. Verify with the same customization label as above
let mut transcript = Transcript::new(b"AggregatedRangeProofTest");
Expand All @@ -684,44 +710,68 @@ mod tests {
}
}

fn serialize_json(proof: &RangeProof) -> String {
serde_json::to_string_pretty(proof).unwrap()
}

fn deserialize_json(serialized_json: &String) -> RangeProof {
serde_json::from_str(serialized_json).unwrap()
}

fn serialize_bin(proof: &RangeProof) -> Vec<u8> {
bincode::serialize(&proof).unwrap()
}

fn deserialize_bin(serialized_bytes: &Vec<u8>) -> RangeProof {
bincode::deserialize(&serialized_bytes).unwrap()
}

#[test]
fn create_and_verify_n_32_m_1() {
singleparty_create_and_verify_helper(32, 1);
singleparty_create_and_verify_helper(32, 1, serialize_json, deserialize_json);
singleparty_create_and_verify_helper(32, 1, serialize_bin, deserialize_bin);
}

#[test]
fn create_and_verify_n_32_m_2() {
singleparty_create_and_verify_helper(32, 2);
singleparty_create_and_verify_helper(32, 2, serialize_json, deserialize_json);
singleparty_create_and_verify_helper(32, 2, serialize_bin, deserialize_bin);
}

#[test]
fn create_and_verify_n_32_m_4() {
singleparty_create_and_verify_helper(32, 4);
singleparty_create_and_verify_helper(32, 4, serialize_json, deserialize_json);
singleparty_create_and_verify_helper(32, 4, serialize_bin, deserialize_bin);
}

#[test]
fn create_and_verify_n_32_m_8() {
singleparty_create_and_verify_helper(32, 8);
singleparty_create_and_verify_helper(32, 8, serialize_json, deserialize_json);
singleparty_create_and_verify_helper(32, 8, serialize_bin, deserialize_bin);
}

#[test]
fn create_and_verify_n_64_m_1() {
singleparty_create_and_verify_helper(64, 1);
singleparty_create_and_verify_helper(64, 1, serialize_json, deserialize_json);
singleparty_create_and_verify_helper(64, 1, serialize_bin, deserialize_bin);
}

#[test]
fn create_and_verify_n_64_m_2() {
singleparty_create_and_verify_helper(64, 2);
singleparty_create_and_verify_helper(64, 2, serialize_json, deserialize_json);
singleparty_create_and_verify_helper(64, 2, serialize_bin, deserialize_bin);
}

#[test]
fn create_and_verify_n_64_m_4() {
singleparty_create_and_verify_helper(64, 4);
singleparty_create_and_verify_helper(64, 4, serialize_json, deserialize_json);
singleparty_create_and_verify_helper(64, 4, serialize_bin, deserialize_bin);
}

#[test]
fn create_and_verify_n_64_m_8() {
singleparty_create_and_verify_helper(64, 8);
singleparty_create_and_verify_helper(64, 8, serialize_json, deserialize_json);
singleparty_create_and_verify_helper(64, 8, serialize_bin, deserialize_bin);
}

#[test]
Expand Down
Loading