Skip to content

Commit

Permalink
refactor(bech32 decoding): election pub key
Browse files Browse the repository at this point in the history
  • Loading branch information
cong-or committed Oct 12, 2023
1 parent b6043c6 commit 34c0b9c
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/sign/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ serde = "1.0"
serde_json = "1.0"
serde_yaml = "0.8.17"
rand = "0.8.3"
bech32 = "0.8"


rand_core = { version = "0.5.1", default-features = false }
Expand Down
4 changes: 2 additions & 2 deletions src/sign/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ cargo build --release -p sign

```bash

ELECTION_PUB_KEY=bed88887abe0a84f64691fe0bdfa3daf1a6cd697a13f07ae07588910ce39c927
ELECTION_PUB_KEY=ristretto255_votepk1ppxnuxrqa4728evnp2ues000uvwvwtxmtf77ejc29lknjuqqu44s4cfmja
ALICE_SK=56e367979579e2ce27fbd305892b0706b7dede999a534a864a7430a5c6aefd3c
ALICE_PK=ea084d2d80ed0ab681333d934efc56df3868d13d46a2de3b7f27f40b62e5344d
PROPOSAL=5
VOTE_PLAN_ID=36ad42885189a0ac3438cdb57bc8ac7f6542e05a59d1f2e4d1d38194c9d4ac7b

./target/release/signer --election-pub-key $ELECTION_PUB_KEY --private-key $ALICE_SK --public-key $ALICE_PK --proposal $PROPOSAL --vote-plan-id $VOTE_PLAN_ID
./target/release/sign --election-pub-key $ELECTION_PUB_KEY --private-key $ALICE_SK --public-key $ALICE_PK --proposal $PROPOSAL --vote-plan-id $VOTE_PLAN_ID

```
10 changes: 5 additions & 5 deletions src/sign/src/fragment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub fn generate_vote_fragment(
vote_cast.put_be_u32(data.len() as u32 + PADDING_AND_TAG_SIZE)?;
vote_cast.put_u8(PADDING)?;
vote_cast.put_u8(VOTE_CAST_TAG)?;
vote_cast.put_bytes(&data.as_slice())?;
vote_cast.put_bytes(data.as_slice())?;

Ok(vote_cast.into_inner())
}
Expand Down Expand Up @@ -130,23 +130,23 @@ pub fn compose_encrypted_vote_part(
// prepend with SIZE-ELEMENT-8BIT
let mut encrypted_vote = Codec::new(Vec::new());
encrypted_vote.put_u8(size_element as u8)?;
encrypted_vote.put_bytes(&encrypted_bytes.as_slice())?;
encrypted_vote.put_bytes(encrypted_bytes.as_slice())?;

let mut proof_bytes = Codec::new(Vec::new());

for announcement in proof.announcments_group_elements() {
proof_bytes.put_bytes(&announcement.to_bytes())?;
}

for cipher in proof.ds().into_iter() {
for cipher in proof.ds() {
proof_bytes.put_bytes(&cipher.to_bytes())?;
}

for response in proof.zwvs().into_iter() {
for response in proof.zwvs() {
proof_bytes.put_bytes(&response.to_bytes())?;
}

proof_bytes.put_bytes(&proof.r().as_bytes())?;
proof_bytes.put_bytes(proof.r().as_bytes())?;

// prepend with SIZE-ELEMENT-8BIT
let mut proof_vote = Codec::new(Vec::new());
Expand Down
12 changes: 10 additions & 2 deletions src/sign/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
//! Fragment generator
//!

use bech32::Error as Bech32Error;
use bech32::FromBase32;
use chain_vote::ElectionPublicKey;
use clap::Parser;
use color_eyre::Result;
Expand Down Expand Up @@ -46,14 +48,20 @@ fn main() -> Result<(), Box<dyn Error>> {

let pk = hex::decode(args.public_key)?;
let mut sk = hex::decode(args.private_key)?;
let election_pk = hex::decode(args.election_pub_key)?;

// Election pub key published as a Bech32_encoded address
// which consists of 3 parts: A Human-Readable Part (HRP) + Separator + Data:
let (_hrp, data, _variant) =
bech32::decode(&args.election_pub_key).map_err(Bech32Error::from)?;

let election_pk = Vec::<u8>::from_base32(&data).map_err(Bech32Error::from)?;

// join sk+pk together, api requirement
sk.extend(pk.clone());
let keypair: Keypair = Keypair::from_bytes(&sk)?;

// vote
let vote = chain_vote::Vote::new(2, 1 as usize).unwrap();
let vote = chain_vote::Vote::new(2, 1_usize)?;
let crs = chain_vote::Crs::from_hash(args.vote_plan_id.clone().as_bytes());

// parse ek key
Expand Down

0 comments on commit 34c0b9c

Please sign in to comment.