-
Notifications
You must be signed in to change notification settings - Fork 40
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
Update range proof to match the paper #26
Open
survived
wants to merge
4
commits into
master
Choose a base branch
from
fix-25
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Changelog | ||
|
||
## v0.4.1 | ||
* Fixed a bug that could cause a valid range proof to be rejected by verifier with small probability | ||
(the larger a range, the smaller probability) [#26] | ||
|
||
[#26]: https://github.com/ZenGo-X/zk-paillier/pull/26 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -120,7 +120,7 @@ impl RangeProof { | |
// commit to challenge | ||
let m = compute_digest(&e.0); | ||
let r = BigInt::sample_below(&ek.n); | ||
let com = get_paillier_commitment(&ek, &m, &r); | ||
let com = get_paillier_commitment(ek, &m, &r); | ||
|
||
(Commitment(com), ChallengeRandomness(r), e) | ||
} | ||
|
@@ -199,7 +199,7 @@ impl RangeProof { | |
e: &ChallengeBits, | ||
) -> Result<(), IncorrectProof> { | ||
let m = compute_digest(&e.0); | ||
let com_tag = get_paillier_commitment(&ek, &m, &r.0); | ||
let com_tag = get_paillier_commitment(ek, &m, &r.0); | ||
if com.0 == com_tag { | ||
Ok(()) | ||
} else { | ||
|
@@ -230,19 +230,19 @@ impl RangeProof { | |
w2: data.w2[i].clone(), | ||
r2: data.r2[i].clone(), | ||
} | ||
} else if secret_x + &data.w1[i] > range_scaled_third | ||
&& secret_x + &data.w1[i] < range_scaled_two_thirds | ||
} else if secret_x + &data.w1[i] >= range_scaled_third | ||
&& secret_x + &data.w1[i] <= range_scaled_two_thirds | ||
{ | ||
Response::Mask { | ||
j: 1, | ||
masked_x: secret_x + &data.w1[i], | ||
masked_r: secret_r * &data.r1[i] % &ek.n, | ||
masked_r: (secret_r * &data.r1[i]) % &ek.n, | ||
} | ||
} else { | ||
Response::Mask { | ||
j: 2, | ||
masked_x: secret_x + &data.w2[i], | ||
masked_r: secret_r * &data.r2[i] % &ek.n, | ||
masked_r: (secret_r * &data.r2[i]) % &ek.n, | ||
} | ||
} | ||
}) | ||
|
@@ -297,12 +297,14 @@ impl RangeProof { | |
res = false; | ||
} | ||
|
||
let flag = (*w2 < range_scaled_third | ||
&& *w1 > range_scaled_third | ||
&& *w1 < range_scaled_two_thirds) | ||
|| (*w1 < range_scaled_third | ||
&& *w2 > range_scaled_third | ||
&& *w2 < range_scaled_two_thirds); | ||
let flag = (*w2 >= BigInt::zero() | ||
&& *w2 <= range_scaled_third | ||
&& *w1 >= range_scaled_third | ||
&& *w1 <= range_scaled_two_thirds) | ||
|| (*w1 >= BigInt::zero() | ||
&& *w1 <= range_scaled_third | ||
&& *w2 >= range_scaled_third | ||
&& *w2 <= range_scaled_two_thirds); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See Lindell 17 paper, appendix A, "V's output", step 1: |
||
|
||
if !flag { | ||
res = false; | ||
|
@@ -475,6 +477,57 @@ mod tests { | |
assert!(result.is_ok()); | ||
} | ||
|
||
#[test] | ||
fn test_range_proof_correct_proof_for_small_range_many_times() { | ||
for _ in 0..100 { | ||
// common: | ||
let range = BigInt::from(6000); | ||
// prover: | ||
let (ek, _dk) = test_keypair().keys(); | ||
let (verifier_ek, verifier_dk) = test_keypair().keys(); | ||
// verifier: | ||
let (com, r, e) = RangeProof::verifier_commit(&verifier_ek); | ||
let (challenge, verification_aid) = CorrectKey::challenge(&verifier_ek); | ||
let proof_results = CorrectKey::prove(&verifier_dk, &challenge); | ||
let _result = CorrectKey::verify(&proof_results.unwrap(), &verification_aid); | ||
assert!(RangeProof::verify_commit(&verifier_ek, &com, &r, &e).is_ok()); | ||
// prover: | ||
let (encrypted_pairs, data_and_randmoness_pairs) = | ||
RangeProof::generate_encrypted_pairs(&ek, &range, STATISTICAL_ERROR_FACTOR); | ||
// prover: | ||
let secret_r = BigInt::sample_below(&ek.n); | ||
let secret_x = BigInt::sample_below(&range.div_floor(&BigInt::from(3))); | ||
// common: | ||
let cipher_x = Paillier::encrypt_with_chosen_randomness( | ||
&ek, | ||
RawPlaintext::from(&secret_x), | ||
&Randomness(secret_r.clone()), | ||
); | ||
// verifer decommits (tested in test_commit_decommit) | ||
// prover: | ||
let z_vector = RangeProof::generate_proof( | ||
&ek, | ||
&secret_x, | ||
&secret_r, | ||
&e, | ||
&range, | ||
&data_and_randmoness_pairs, | ||
STATISTICAL_ERROR_FACTOR, | ||
); | ||
// verifier: | ||
let result = RangeProof::verifier_output( | ||
&ek, | ||
&e, | ||
&encrypted_pairs, | ||
&z_vector, | ||
&range, | ||
&cipher_x.0, | ||
STATISTICAL_ERROR_FACTOR, | ||
); | ||
assert!(result.is_ok()); | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_range_proof_incorrect_proof() { | ||
// common: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See Lindell 17 paper, appendix A, step 4b: