-
Notifications
You must be signed in to change notification settings - Fork 22
fix: add real encoded tally #1122
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4dba882
test: add real encoded tally
cedoor d514714
test: update test to use real FHE computation output
cedoor 08b253a
refactor: update decodeTally to use entire halves
cedoor fe99f60
fix: add correct decoding function for rust
cedoor 9247184
chore: add new decoding to contract
ctrlc03 722d710
refactor: update types and decoding function
cedoor acbb022
refactor: add error for invalid tally length
cedoor f8dba93
refactor: add error for invalid tally length
cedoor d733ed7
refactor: update vote type to bigint
cedoor b30e9e6
test: remove only from decode tally test
cedoor fdb9430
refactor: add total votes to web result request
cedoor 6073bf6
ci: set correct cargo lock path for crisp e2e
cedoor 8dc9255
chore: choose shorter wait time for e3 activation
cedoor 217cc6e
chore: choose longer wait time after casting vote
cedoor 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 hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,14 @@ | ||
| [package] | ||
| name = "crisp-utils" | ||
| version.workspace = true | ||
| edition.workspace = true | ||
| license.workspace = true | ||
| description.workspace = true | ||
| repository.workspace = true | ||
|
|
||
| [dependencies] | ||
| e3-sdk = { workspace = true, default-features = false, features=["bfv"] } | ||
| eyre = { workspace = true } | ||
| hex = { workspace = true } | ||
| num-bigint = "=0.4.6" | ||
|
|
This file contains hidden or 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,72 @@ | ||
| // SPDX-License-Identifier: LGPL-3.0-only | ||
| // | ||
| // This file is provided WITHOUT ANY WARRANTY; | ||
| // without even the implied warranty of MERCHANTABILITY | ||
| // or FITNESS FOR A PARTICULAR PURPOSE. | ||
|
|
||
| use e3_sdk::bfv_helpers::decode_bytes_to_vec_u64; | ||
| use eyre::Result; | ||
| use num_bigint::BigUint; | ||
|
|
||
| /// Represents decoded vote counts from a tally | ||
| #[derive(Debug, Clone)] | ||
| pub struct VoteCounts { | ||
| pub yes: BigUint, | ||
| pub no: BigUint, | ||
| } | ||
|
|
||
| /// Decode an encoded tally into its decimal representation. | ||
| /// | ||
| /// The plaintext output from FHE computation contains the result as bytes. | ||
| /// Votes are encoded with yes votes in the first half and no votes in the second half, | ||
| /// right-aligned with leading zeros. | ||
| /// | ||
| /// # Arguments | ||
| /// | ||
| /// * `tally_bytes` - The encoded tally as bytes (little-endian format of u64s) | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// A `VoteCounts` struct containing the decoded yes and no vote counts | ||
| pub fn decode_tally(tally_bytes: &[u8]) -> Result<VoteCounts> { | ||
| // Decode bytes to numbers array (little-endian, 8 bytes per value) | ||
| let decoded = decode_bytes_to_vec_u64(tally_bytes)?; | ||
|
|
||
| // Votes are right-aligned with leading zeros, so we can use the entire halves | ||
| let half_d = decoded.len() / 2; | ||
| let yes_binary = &decoded[0..half_d]; | ||
| let no_binary = &decoded[half_d..decoded.len()]; | ||
|
|
||
| // Convert yes votes (entire first half) | ||
| let mut yes = BigUint::from(0u64); | ||
| for (i, &value) in yes_binary.iter().enumerate() { | ||
| let weight = BigUint::from(2u64).pow((yes_binary.len() - 1 - i) as u32); | ||
| yes += BigUint::from(value) * weight; | ||
| } | ||
|
|
||
| // Convert no votes (entire second half) | ||
| let mut no = BigUint::from(0u64); | ||
| for (i, &value) in no_binary.iter().enumerate() { | ||
| let weight = BigUint::from(2u64).pow((no_binary.len() - 1 - i) as u32); | ||
| no += BigUint::from(value) * weight; | ||
| } | ||
|
|
||
| Ok(VoteCounts { yes, no }) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn test_decode_tally_fhe_output() { | ||
| // Expected: yes = 10000000000, no = 30000000000 | ||
| let tally_hex = "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000001000000000000000000000000000000010000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000100000000000000010000000000000001000000000000000100000000000000010000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000300000000000000000000000000000003000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000030000000000000003000000000000000300000000000000030000000000000003000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; | ||
|
|
||
| let bytes = hex::decode(tally_hex.strip_prefix("0x").unwrap_or(tally_hex)).unwrap(); | ||
| let result = decode_tally(&bytes).unwrap(); | ||
|
|
||
| assert_eq!(result.yes, BigUint::from(10000000000u64)); | ||
| assert_eq!(result.no, BigUint::from(30000000000u64)); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.