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

Use #[udigest(as = ...)] attribute #106

Merged
merged 10 commits into from
Aug 27, 2024
Merged
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
17 changes: 1 addition & 16 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ on:
env:
CARGO_TERM_COLOR: always
CARGO_NET_GIT_FETCH_WITH_CLI: true
RUSTFLAGS: -D warnings

jobs:
build:
Expand Down Expand Up @@ -125,22 +126,6 @@ jobs:
with:
header: bench
path: pr-comment

check-publish:
runs-on: ubuntu-latest
strategy:
matrix:
package:
- key-share
- cggmp21-keygen
- cggmp21
steps:
- uses: actions/checkout@v3
- uses: Swatinem/rust-cache@v2
with:
cache-on-failure: "true"
- name: Dry-run publish
run: cargo publish --dry-run -p ${{ matrix.package }}
check-changelog:
runs-on: ubuntu-latest
steps:
Expand Down
22 changes: 11 additions & 11 deletions Cargo.lock

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

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ cggmp21 = { version = "0.4", path = "cggmp21" }
cggmp21-keygen = { version = "0.3", path = "cggmp21-keygen" }
key-share = { version = "0.4", path = "key-share", default-features = false }

generic-ec = { version = "0.4", default-features = false }
generic-ec-zkp = { version = "0.4", default-features = false }
generic-ec = { version = "0.4.1", default-features = false }
generic-ec-zkp = { version = "0.4.1", default-features = false }
round-based = { version = "0.3", default-features = false }

paillier-zk = "0.4"
udigest = { version = "0.2", default-features = false }
paillier-zk = "0.4.1"
udigest = { version = "0.2.1", default-features = false }

digest = { version = "0.10", default-features = false }
sha2 = { version = "0.10", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion cggmp21-keygen/src/non_threshold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub struct MsgRound2<E: Curve, L: SecurityLevel> {
/// Party contribution to chain code
#[cfg(feature = "hd-wallets")]
#[serde_as(as = "Option<utils::HexOrBin>")]
#[udigest(with = utils::encoding::maybe_bytes)]
#[udigest(as = Option<udigest::Bytes>)]
pub chain_code: Option<slip_10::ChainCode>,
/// $u_i$
#[serde(with = "hex::serde")]
Expand Down
2 changes: 1 addition & 1 deletion cggmp21-keygen/src/threshold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub struct MsgRound2Broad<E: Curve, L: SecurityLevel> {
/// Party contribution to chain code
#[cfg(feature = "hd-wallets")]
#[serde_as(as = "Option<utils::HexOrBin>")]
#[udigest(with = utils::encoding::maybe_bytes)]
#[udigest(as = Option<udigest::Bytes>)]
pub chain_code: Option<slip_10::ChainCode>,
/// $u_i$
#[serde(with = "hex::serde")]
Expand Down
13 changes: 1 addition & 12 deletions cggmp21-keygen/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ where
/// In the future we might want to replace the data_message and proof_message
/// with a generic vec of messages.
#[derive(Debug)]
#[allow(dead_code)] // removes false-positive warnings
pub struct AbortBlame {
/// Party which can be blamed for breaking the protocol
pub faulty_party: PartyIndex,
Expand Down Expand Up @@ -89,15 +90,3 @@ where
pub fn iter_peers(i: u16, n: u16) -> impl Iterator<Item = u16> {
(0..n).filter(move |x| *x != i)
}

/// Unambiguous encoding for different types for which it was not defined
pub mod encoding {
#[cfg(feature = "hd-wallets")]
pub fn maybe_bytes<B: udigest::Buffer>(
m: &Option<impl AsRef<[u8]>>,
encoder: udigest::encoding::EncodeValue<B>,
) {
use udigest::Digestable;
m.as_ref().map(udigest::Bytes).unambiguously_encode(encoder)
}
}
6 changes: 3 additions & 3 deletions cggmp21/src/key_refresh/aux_only.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ pub struct MsgRound1<D: Digest> {
#[serde(bound = "")]
pub struct MsgRound2<L: SecurityLevel> {
/// $N_i$
#[udigest(with = utils::encoding::integer)]
#[udigest(as = utils::encoding::Integer)]
pub N: Integer,
/// $s_i$
#[udigest(with = utils::encoding::integer)]
#[udigest(as = utils::encoding::Integer)]
pub s: Integer,
/// $t_i$
#[udigest(with = utils::encoding::integer)]
#[udigest(as = utils::encoding::Integer)]
pub t: Integer,
/// $\hat \psi_i$
// this should be L::M instead, but no rustc support yet
Expand Down
6 changes: 3 additions & 3 deletions cggmp21/src/key_refresh/non_threshold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ pub struct MsgRound2<E: Curve, L: SecurityLevel> {
/// $\vec A_i$
pub sch_commits_a: Vec<schnorr_pok::Commit<E>>,
/// $N_i$
#[udigest(with = utils::encoding::integer)]
#[udigest(as = utils::encoding::Integer)]
pub N: Integer,
/// $s_i$
#[udigest(with = utils::encoding::integer)]
#[udigest(as = utils::encoding::Integer)]
pub s: Integer,
/// $t_i$
#[udigest(with = utils::encoding::integer)]
#[udigest(as = utils::encoding::Integer)]
pub t: Integer,
/// $\hat \psi_i$
// this should be L::M instead, but no rustc support yet
Expand Down
4 changes: 2 additions & 2 deletions cggmp21/src/key_share.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::ops;
use std::sync::Arc;

use generic_ec::{Curve, NonZero, Point, SecretScalar};
use generic_ec::{Curve, NonZero, Point};
use paillier_zk::paillier_encryption_in_range as π_enc;
use paillier_zk::rug::{Complete, Integer};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -346,7 +346,7 @@ impl<E: Curve, T: AsRef<IncompleteKeyShare<E>>> AnyKeyShare<E> for T {}
#[cfg(feature = "spof")]
pub fn reconstruct_secret_key<E: Curve>(
key_shares: &[impl AnyKeyShare<E>],
) -> Result<SecretScalar<E>, ReconstructError> {
) -> Result<generic_ec::SecretScalar<E>, ReconstructError> {
key_share::reconstruct_secret_key(key_shares)
}

Expand Down
4 changes: 2 additions & 2 deletions cggmp21/src/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,10 @@ pub mod msg {
#[udigest(tag = prefixed!("round1"))]
pub struct MsgRound1a {
/// $K_i$
#[udigest(with = utils::encoding::integer)]
#[udigest(as = utils::encoding::Integer)]
pub K: fast_paillier::Ciphertext,
/// $G_i$
#[udigest(with = utils::encoding::integer)]
#[udigest(as = utils::encoding::Integer)]
pub G: fast_paillier::Ciphertext,
}

Expand Down
24 changes: 8 additions & 16 deletions cggmp21/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ where
/// In the future we might want to replace the data_message and proof_message
/// with a generic vec of messages.
#[derive(Debug)]
#[allow(dead_code)] // removes false-positive warnings
pub struct AbortBlame {
/// Party which can be blamed for breaking the protocol
pub faulty_party: PartyIndex,
Expand Down Expand Up @@ -218,22 +219,13 @@ pub fn generate_blum_prime(rng: &mut impl rand_core::RngCore, bits_size: u32) ->
pub mod encoding {
use paillier_zk::rug;

pub fn integer<B: udigest::Buffer>(
x: &rug::Integer,
encoder: udigest::encoding::EncodeValue<B>,
) {
encoder
.encode_leaf()
.chain(x.to_digits(rug::integer::Order::Msf));
}

pub fn integers_list<B: udigest::Buffer>(
list: impl AsRef<[rug::Integer]>,
encoder: udigest::encoding::EncodeValue<B>,
) {
let mut encoder = encoder.encode_list();
for x in list.as_ref() {
integer(x, encoder.add_item())
pub struct Integer;
impl udigest::DigestAs<rug::Integer> for Integer {
fn digest_as<B: udigest::Buffer>(
x: &rug::Integer,
encoder: udigest::encoding::EncodeValue<B>,
) {
encoder.encode_leaf_value(x.to_digits(rug::integer::Order::Msf))
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions cggmp21/src/zk/ring_pedersen_parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ struct Challenge<const M: usize> {
/// Data to construct proof about
#[derive(Clone, Copy, udigest::Digestable)]
pub struct Data<'a> {
#[udigest(with = crate::utils::encoding::integer)]
#[udigest(as = &crate::utils::encoding::Integer)]
pub N: &'a Integer,
#[udigest(with = crate::utils::encoding::integer)]
#[udigest(as = &crate::utils::encoding::Integer)]
pub s: &'a Integer,
#[udigest(with = crate::utils::encoding::integer)]
#[udigest(as = &crate::utils::encoding::Integer)]
pub t: &'a Integer,
}

Expand All @@ -34,10 +34,10 @@ pub struct Data<'a> {
#[derive(Clone, Serialize, Deserialize, udigest::Digestable)]
pub struct Proof<const M: usize> {
#[serde_as(as = "[_; M]")]
#[udigest(with = crate::utils::encoding::integers_list)]
#[udigest(as = [crate::utils::encoding::Integer; M])]
pub commitment: [Integer; M],
#[serde_as(as = "[_; M]")]
#[udigest(with = crate::utils::encoding::integers_list)]
#[udigest(as = [crate::utils::encoding::Integer; M])]
pub zs: [Integer; M],
}

Expand All @@ -51,7 +51,7 @@ fn derive_challenge<const M: usize, D: Digest>(
struct Seed<'a, S: udigest::Digestable, const M: usize> {
shared_state: &'a S,
data: Data<'a>,
#[udigest(with = crate::utils::encoding::integers_list)]
#[udigest(as = &[crate::utils::encoding::Integer; M])]
commitment: &'a [Integer; M],
}

Expand Down
4 changes: 2 additions & 2 deletions key-share/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ pub struct DirtyKeyInfo<E: Curve> {
// `crate::serde_fix` as well!
//
/// Guard that ensures curve consistency for deseraization
#[cfg_attr(feature = "udigest", udigest(with = utils::encoding::curve_name))]
#[cfg_attr(feature = "udigest", udigest(as = utils::encoding::CurveName))]
pub curve: CurveName<E>,
/// Public key corresponding to shared secret key. Corresponds to _X_ in paper.
#[cfg_attr(feature = "serde", serde(with = "As::<generic_ec::serde::Compact>"))]
Expand All @@ -231,7 +231,7 @@ pub struct DirtyKeyInfo<E: Curve> {
serde(skip_serializing_if = "Option::is_none"),
serde(with = "As::<Option<utils::HexOrBin>>")
)]
#[cfg_attr(feature = "udigest", udigest(with = utils::encoding::maybe_bytes))]
#[cfg_attr(feature = "udigest", udigest(as = Option<udigest::Bytes>))]
pub chain_code: Option<slip_10::ChainCode>,
}

Expand Down
22 changes: 8 additions & 14 deletions key-share/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,14 @@ use hex as _;

#[cfg(feature = "udigest")]
pub mod encoding {
pub fn curve_name<B: udigest::Buffer, E: generic_ec::Curve>(
_value: &generic_ec::serde::CurveName<E>,
encoder: udigest::encoding::EncodeValue<B>,
) {
encoder.encode_leaf_value(E::CURVE_NAME)
}

#[cfg(feature = "hd-wallets")]
pub fn maybe_bytes<B: udigest::Buffer>(
m: &Option<impl AsRef<[u8]>>,
encoder: udigest::encoding::EncodeValue<B>,
) {
use udigest::Digestable;
m.as_ref().map(udigest::Bytes).unambiguously_encode(encoder)
pub struct CurveName;
impl<E: generic_ec::Curve> udigest::DigestAs<generic_ec::serde::CurveName<E>> for CurveName {
fn digest_as<B: udigest::Buffer>(
_value: &generic_ec::serde::CurveName<E>,
encoder: udigest::encoding::EncodeValue<B>,
) {
encoder.encode_leaf_value(E::CURVE_NAME)
}
}
}

Expand Down
Loading
Loading