Skip to content

Commit

Permalink
fixup! [frost] EncodedFrostKey BorrowDecode and correct markers
Browse files Browse the repository at this point in the history
  • Loading branch information
nickfarrow committed Feb 14, 2024
1 parent cb3097a commit bf70c26
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 31 deletions.
10 changes: 2 additions & 8 deletions schnorr_fun/src/frost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ impl<H: Digest<OutputSize = U32> + Clone, NG> Frost<H, NG> {
local_secret_polys: &BTreeMap<PartyIndex, S>,
) -> Result<KeyGen, NewKeyGenError>
where
S: AsRef<[Scalar<Secret>]>,
S: AsRef<[Scalar]>,
{
for (party_id, scalar_poly) in local_secret_polys {
let image = poly::scalar::to_point_poly(scalar_poly.as_ref());
Expand Down Expand Up @@ -1105,13 +1105,7 @@ impl crate::fun::bincode::Decode for EncodedFrostKey {
}

#[cfg(feature = "bincode")]
impl<'de> crate::fun::bincode::BorrowDecode<'de> for EncodedFrostKey {
fn borrow_decode<D: secp256kfun::bincode::de::BorrowDecoder<'de>>(
decoder: &mut D,
) -> Result<Self, secp256kfun::bincode::error::DecodeError> {
crate::fun::bincode::Decode::decode(decoder)
}
}
crate::fun::bincode::impl_borrow_decode!(EncodedFrostKey);

#[cfg(feature = "serde")]
impl<'de> crate::fun::serde::Deserialize<'de> for EncodedFrostKey {
Expand Down
7 changes: 4 additions & 3 deletions schnorr_fun/src/share_backup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,20 @@
use alloc::{fmt, string::String, vec::Vec};
use bech32::{u5, FromBase32, ToBase32, Variant::Bech32m};
use core::{num::NonZeroU32, str::FromStr};
use secp256kfun::marker::ZeroChoice;
use secp256kfun::{
digest::{generic_array::typenum::U32, Digest},
g,
hash::HashAdd,
marker::{Normal, Public, Zero},
marker::{Normal, Public},
poly, Point, Scalar, G,
};

/// Create an identifier that's used to determine compatibility of shamir secret shares.
/// The first 4 bech32 chars from a hash of the polynomial coefficients.
/// Collision expected once in (32)^4 = 2^20.
pub fn polynomial_identifier<H: Default + Digest<OutputSize = U32>>(
polynomial: &[Point<Normal, Public, Zero>],
polynomial: &[Point<Normal, Public, impl ZeroChoice>],
) -> [u5; 4] {
let hash = H::default();
hash.add(polynomial).finalize().to_base32()[0..4]
Expand Down Expand Up @@ -98,7 +99,7 @@ impl ShareBackup {
/// The threshold must be greater than 0 and less than 1024.
/// The secret share is checked to confirm that its image lies on the public point polynomial.
pub fn new<H: Default + Digest<OutputSize = U32>>(
polynomial: &Vec<Point<Normal, Public, Zero>>,
polynomial: &[Point<Normal, Public, impl ZeroChoice>],
secret_share: &Scalar,
share_index: &Scalar<Public>,
) -> Self {
Expand Down
25 changes: 5 additions & 20 deletions schnorr_fun/tests/share_backup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ use secp256kfun::{marker::Secret, poly, s, Scalar};
#[test]
fn short_backup() {
let secret_poly = vec![s!(6), s!(1), s!(9)];
let polynomial: Vec<_> = poly::scalar::to_point_poly(&secret_poly)
.into_iter()
.map(|c| c.mark_zero())
.collect(); // some polynomial coefficients
let polynomial: Vec<_> = poly::scalar::to_point_poly(&secret_poly);
let threshold = polynomial.len() as u16;
let share_index = s!(12).public();
let secret_share = poly::scalar::eval(&secret_poly, share_index)
Expand All @@ -34,10 +31,7 @@ fn short_backup() {
#[test]
fn long_backup() {
let secret_poly = vec![s!(1), s!(2), s!(3)];
let polynomial: Vec<_> = poly::scalar::to_point_poly(&secret_poly)
.into_iter()
.map(|c| c.mark_zero())
.collect(); // some polynomial coefficients
let polynomial: Vec<_> = poly::scalar::to_point_poly(&secret_poly);
let threshold = polynomial.len() as u16;
let share_index = Scalar::<Secret>::from_str(
"34f7ce653cfa8454b3463726a599ef2925736442d2d06455974d6feae9450d90",
Expand Down Expand Up @@ -67,10 +61,7 @@ fn long_backup() {
#[should_panic(expected = "too high of a threshold")]
fn threshold_too_high() {
let secret_poly: Vec<Scalar> = (0..1025).map(|_| s!(1)).collect();
let polynomial: Vec<_> = poly::scalar::to_point_poly(&secret_poly)
.into_iter()
.map(|c| c.mark_zero())
.collect(); // some polynomial coefficients
let polynomial: Vec<_> = poly::scalar::to_point_poly(&secret_poly);
let share_index = Scalar::<Secret>::from_str(
"91dbab9f62660e95258480d2f2cff6dcfdb513f28a85fa4fb55ee993a5b46809",
)
Expand All @@ -86,10 +77,7 @@ fn threshold_too_high() {
#[should_panic(expected = "threshold can not be zero")]
fn threshold_zero() {
let secret_poly: Vec<Scalar> = vec![];
let polynomial: Vec<_> = poly::scalar::to_point_poly(&secret_poly)
.into_iter()
.map(|c| c.mark_zero())
.collect(); // some polynomial coefficients
let polynomial: Vec<_> = poly::scalar::to_point_poly(&secret_poly);
let share_index = Scalar::<Secret>::from_str(
"000000000000000000000000000000000000000000000066726F7374736E6170",
)
Expand All @@ -107,10 +95,7 @@ fn threshold_zero() {
#[should_panic(expected = "Secret share is not valid with respect to the polynomial")]
fn share_not_on_poly() {
let secret_poly: Vec<Scalar> = vec![s!(1), s!(2), s!(3)];
let polynomial: Vec<_> = poly::scalar::to_point_poly(&secret_poly)
.into_iter()
.map(|c| c.mark_zero())
.collect(); // some polynomial coefficients
let polynomial: Vec<_> = poly::scalar::to_point_poly(&secret_poly);
let share_index = Scalar::<Secret>::from_str(
"00000000000000000000000000000000000000000000006672656520726F7373",
)
Expand Down

0 comments on commit bf70c26

Please sign in to comment.