Skip to content

Commit 9dffa7c

Browse files
davidrusudirvine
authored andcommitted
fix(repr): don't alias G{1,2}Affine with G1 G2
Some dependent crates use the G1Affine types from this crate, aliasing made it impossible to import by the original names
1 parent f52d1f0 commit 9dffa7c

File tree

4 files changed

+52
-53
lines changed

4 files changed

+52
-53
lines changed

src/convert.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use blst::blst_scalar;
44

55
use crate::{
66
error::{Error, Result},
7-
Fr, DST, G1, G2, PK_SIZE, SIG_SIZE, SK_SIZE,
7+
Fr, G1Affine, G2Affine, DST, PK_SIZE, SIG_SIZE, SK_SIZE,
88
};
99

1010
pub(crate) fn derivation_index_into_fr(index: &[u8]) -> Fr {
@@ -43,18 +43,18 @@ pub(crate) fn fr_from_bytes(bytes: [u8; SK_SIZE]) -> Result<Fr> {
4343
Ok(fr.unwrap())
4444
}
4545

46-
pub(crate) fn g1_from_bytes(bytes: [u8; PK_SIZE]) -> Result<G1> {
46+
pub(crate) fn g1_from_bytes(bytes: [u8; PK_SIZE]) -> Result<G1Affine> {
4747
// TODO IC remove unwrap here? I'm not sure if it's possible with CtOption
48-
let g1 = G1::from_compressed(&bytes);
48+
let g1 = G1Affine::from_compressed(&bytes);
4949
if g1.is_none().into() {
5050
return Err(Error::InvalidBytes);
5151
};
5252
Ok(g1.unwrap())
5353
}
5454

55-
pub(crate) fn g2_from_bytes(bytes: [u8; SIG_SIZE]) -> Result<G2> {
55+
pub(crate) fn g2_from_bytes(bytes: [u8; SIG_SIZE]) -> Result<G2Affine> {
5656
// TODO IC remove unwrap here? I'm not sure if it's possible with CtOption
57-
let g2 = G2::from_compressed(&bytes);
57+
let g2 = G2Affine::from_compressed(&bytes);
5858
if g2.is_none().into() {
5959
return Err(Error::InvalidBytes);
6060
};

src/lib.rs

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ use crate::poly::{Commitment, Poly};
4444
use crate::secret::clear_fr;
4545
use crate::util::sha3_256;
4646

47-
pub use blstrs::{
48-
Bls12 as PEngine, G1Affine as G1, G1Projective, G2Affine as G2, G2Projective, Scalar as Fr,
49-
};
47+
pub use blstrs::{Bls12 as PEngine, G1Affine, G1Projective, G2Affine, G2Projective, Scalar as Fr};
5048

5149
/// The size of a secret key's representation in bytes.
5250
pub const SK_SIZE: usize = 32;
@@ -62,7 +60,7 @@ pub const DST: &[u8; 43] = b"BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_NUL_";
6260

6361
/// A public key.
6462
#[derive(Deserialize, Serialize, Copy, Clone, PartialEq, Eq)]
65-
pub struct PublicKey(#[serde(with = "serde_impl::affine")] G1);
63+
pub struct PublicKey(#[serde(with = "serde_impl::affine")] G1Affine);
6664

6765
impl Hash for PublicKey {
6866
fn hash<H: Hasher>(&self, state: &mut H) {
@@ -90,15 +88,15 @@ impl Ord for PublicKey {
9088
}
9189

9290
/// Utility to convert blsttc to blst types
93-
impl From<PublicKey> for G1 {
91+
impl From<PublicKey> for G1Affine {
9492
fn from(item: PublicKey) -> Self {
9593
item.0
9694
}
9795
}
9896

9997
/// Utility to convert blsttc to blst types
100-
impl From<G1> for PublicKey {
101-
fn from(item: G1) -> Self {
98+
impl From<G1Affine> for PublicKey {
99+
fn from(item: G1Affine) -> Self {
102100
PublicKey(item)
103101
}
104102
}
@@ -118,8 +116,8 @@ impl From<G1Projective> for PublicKey {
118116
}
119117

120118
/// Utility to compare between blsttc and blst types
121-
impl std::cmp::PartialEq<G1> for PublicKey {
122-
fn eq(&self, other: &G1) -> bool {
119+
impl std::cmp::PartialEq<G1Affine> for PublicKey {
120+
fn eq(&self, other: &G1Affine) -> bool {
123121
&self.0 == other
124122
}
125123
}
@@ -138,9 +136,10 @@ impl PublicKey {
138136
}
139137

140138
/// Returns `true` if the signature matches the element of `G2`.
141-
pub fn verify_g2<H: Into<G2>>(&self, sig: &Signature, hash: H) -> bool {
139+
pub fn verify_g2<H: Into<G2Affine>>(&self, sig: &Signature, hash: H) -> bool {
142140
!self.is_zero()
143-
&& PEngine::pairing(&self.0, &hash.into()) == PEngine::pairing(&G1::generator(), &sig.0)
141+
&& PEngine::pairing(&self.0, &hash.into())
142+
== PEngine::pairing(&G1Affine::generator(), &sig.0)
144143
}
145144

146145
/// Returns `true` if the signature matches the message.
@@ -159,7 +158,7 @@ impl PublicKey {
159158
/// Encrypts the message.
160159
pub fn encrypt_with_rng<R: RngCore, M: AsRef<[u8]>>(&self, rng: &mut R, msg: M) -> Ciphertext {
161160
let r: Fr = Fr::random(rng);
162-
let u = G1::generator().mul(r).to_affine();
161+
let u = G1Affine::generator().mul(r).to_affine();
163162
let v: Vec<u8> = {
164163
let g = self.0.mul(r);
165164
xor_with_hash(g.to_affine(), msg.as_ref())
@@ -213,7 +212,7 @@ impl fmt::Debug for PublicKeyShare {
213212

214213
impl PublicKeyShare {
215214
/// Returns `true` if the signature matches the element of `G2`.
216-
pub fn verify_g2<H: Into<G2>>(&self, sig: &SignatureShare, hash: H) -> bool {
215+
pub fn verify_g2<H: Into<G2Affine>>(&self, sig: &SignatureShare, hash: H) -> bool {
217216
self.0.verify_g2(&sig.0, hash)
218217
}
219218

@@ -248,7 +247,7 @@ impl PublicKeyShare {
248247
/// A signature.
249248
// Note: Random signatures can be generated for testing.
250249
#[derive(Deserialize, Serialize, Clone, PartialEq, Eq)]
251-
pub struct Signature(#[serde(with = "serde_impl::affine")] G2);
250+
pub struct Signature(#[serde(with = "serde_impl::affine")] G2Affine);
252251

253252
impl PartialOrd for Signature {
254253
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
@@ -421,11 +420,11 @@ impl SecretKey {
421420

422421
/// Returns the matching public key.
423422
pub fn public_key(&self) -> PublicKey {
424-
PublicKey((G1::generator() * self.0).to_affine())
423+
PublicKey((G1Affine::generator() * self.0).to_affine())
425424
}
426425

427426
/// Signs the given element of `G2`.
428-
pub fn sign_g2<H: Into<G2>>(&self, hash: H) -> Signature {
427+
pub fn sign_g2<H: Into<G2Affine>>(&self, hash: H) -> Signature {
429428
Signature(hash.into().mul(self.0).to_affine())
430429
}
431430

@@ -523,7 +522,7 @@ impl SecretKeyShare {
523522
}
524523

525524
/// Signs the given element of `G2`.
526-
pub fn sign_g2<H: Into<G2>>(&self, hash: H) -> SignatureShare {
525+
pub fn sign_g2<H: Into<G2Affine>>(&self, hash: H) -> SignatureShare {
527526
SignatureShare(self.0.sign_g2(hash))
528527
}
529528

@@ -571,9 +570,9 @@ impl SecretKeyShare {
571570
/// An encrypted message.
572571
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq)]
573572
pub struct Ciphertext(
574-
#[serde(with = "serde_impl::affine")] G1,
573+
#[serde(with = "serde_impl::affine")] G1Affine,
575574
Vec<u8>,
576-
#[serde(with = "serde_impl::affine")] G2,
575+
#[serde(with = "serde_impl::affine")] G2Affine,
577576
);
578577

579578
impl Hash for Ciphertext {
@@ -605,7 +604,7 @@ impl Ciphertext {
605604
pub fn verify(&self) -> bool {
606605
let Ciphertext(ref u, ref v, ref w) = *self;
607606
let hash = hash_g1_g2(*u, v);
608-
PEngine::pairing(&G1::generator(), w) == PEngine::pairing(u, &hash)
607+
PEngine::pairing(&G1Affine::generator(), w) == PEngine::pairing(u, &hash)
609608
}
610609

611610
/// Returns byte representation of Ciphertext
@@ -640,7 +639,7 @@ impl Ciphertext {
640639

641640
/// A decryption share. A threshold of decryption shares can be used to decrypt a message.
642641
#[derive(Clone, Deserialize, Serialize, PartialEq, Eq)]
643-
pub struct DecryptionShare(#[serde(with = "serde_impl::affine")] G1);
642+
pub struct DecryptionShare(#[serde(with = "serde_impl::affine")] G1Affine);
644643

645644
impl Distribution<DecryptionShare> for Standard {
646645
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> DecryptionShare {
@@ -785,7 +784,7 @@ impl PublicKeySet {
785784
/// Derives a child public key set for a given index.
786785
pub fn derive_child(&self, index: &[u8]) -> Self {
787786
let index_fr = derivation_index_into_fr(index);
788-
let child_coeffs: Vec<G1> = self
787+
let child_coeffs: Vec<G1Affine> = self
789788
.commit
790789
.coeff
791790
.iter()
@@ -907,12 +906,12 @@ impl SecretKeySet {
907906
}
908907

909908
/// Returns a hash of the given message in `G2`.
910-
pub fn hash_g2<M: AsRef<[u8]>>(msg: M) -> G2 {
909+
pub fn hash_g2<M: AsRef<[u8]>>(msg: M) -> G2Affine {
911910
G2Projective::hash_to_curve(msg.as_ref(), DST, &[]).to_affine()
912911
}
913912

914913
/// Returns a hash of the group element and message, in the second group.
915-
fn hash_g1_g2<M: AsRef<[u8]>>(g1: G1, msg: M) -> G2 {
914+
fn hash_g1_g2<M: AsRef<[u8]>>(g1: G1Affine, msg: M) -> G2Affine {
916915
// If the message is large, hash it, otherwise copy it.
917916
// TODO: Benchmark and optimize the threshold.
918917
let mut msg = if msg.as_ref().len() > 64 {
@@ -925,7 +924,7 @@ fn hash_g1_g2<M: AsRef<[u8]>>(g1: G1, msg: M) -> G2 {
925924
}
926925

927926
/// Returns the bitwise xor of `bytes` with a sequence of pseudorandom bytes determined by `g1`.
928-
fn xor_with_hash(g1: G1, bytes: &[u8]) -> Vec<u8> {
927+
fn xor_with_hash(g1: G1Affine, bytes: &[u8]) -> Vec<u8> {
929928
let digest = sha3_256(&g1.to_compressed());
930929
let rng = ChaChaRng::from_seed(digest);
931930
let xor = |(a, b): (u8, &u8)| a ^ b;
@@ -1905,19 +1904,19 @@ mod tests {
19051904
assert!(!pk.is_zero());
19061905

19071906
// Rogue 0 pubkey
1908-
let rogue_public_key = PublicKey(G1::identity());
1907+
let rogue_public_key = PublicKey(G1Affine::identity());
19091908
assert!(rogue_public_key.is_zero());
19101909

19111910
// Rogue 0 sig
1912-
let rogue_sig = Signature(G2::identity());
1911+
let rogue_sig = Signature(G2Affine::identity());
19131912

19141913
// just any hash will do
19151914
let hash = hash_g2(b"anything");
19161915

19171916
// check that the attack works without the 0 check
19181917
assert_eq!(
19191918
PEngine::pairing(&rogue_public_key.0, &hash),
1920-
PEngine::pairing(&G1::generator(), &rogue_sig.0)
1919+
PEngine::pairing(&G1Affine::generator(), &rogue_sig.0)
19211920
);
19221921

19231922
// check that verify_g2 is protected against this attack

src/poly.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use crate::convert::{fr_from_bytes, g1_from_bytes};
3434
use crate::error::{Error, Result};
3535
use crate::into_fr::IntoFr;
3636
use crate::secret::clear_fr;
37-
use crate::{Field, Fr, G1Projective, G1, PK_SIZE, SK_SIZE};
37+
use crate::{Field, Fr, G1Affine, G1Projective, PK_SIZE, SK_SIZE};
3838

3939
/// A univariate polynomial in the prime field.
4040
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone)]
@@ -371,7 +371,7 @@ impl Poly {
371371

372372
/// Returns the corresponding commitment.
373373
pub fn commitment(&self) -> Commitment {
374-
let to_g1 = |c: &Fr| G1::generator().mul(c).to_affine();
374+
let to_g1 = |c: &Fr| G1Affine::generator().mul(c).to_affine();
375375
Commitment {
376376
coeff: self.coeff.iter().map(to_g1).collect(),
377377
}
@@ -470,12 +470,12 @@ impl Poly {
470470
pub struct Commitment {
471471
/// The coefficients of the polynomial.
472472
#[serde(with = "super::serde_impl::affine_vec")]
473-
pub(super) coeff: Vec<G1>,
473+
pub(super) coeff: Vec<G1Affine>,
474474
}
475475

476476
/// Creates a new `Commitment` instance
477-
impl From<Vec<G1>> for Commitment {
478-
fn from(coeff: Vec<G1>) -> Self {
477+
impl From<Vec<G1Affine>> for Commitment {
478+
fn from(coeff: Vec<G1Affine>) -> Self {
479479
Commitment { coeff }
480480
}
481481
}
@@ -510,7 +510,7 @@ impl Hash for Commitment {
510510
impl<B: Borrow<Commitment>> ops::AddAssign<B> for Commitment {
511511
fn add_assign(&mut self, rhs: B) {
512512
let len = cmp::max(self.coeff.len(), rhs.borrow().coeff.len());
513-
self.coeff.resize(len, G1::identity());
513+
self.coeff.resize(len, G1Affine::identity());
514514
for (self_c, rhs_c) in self.coeff.iter_mut().zip(&rhs.borrow().coeff) {
515515
*self_c = (*self_c + G1Projective::from(rhs_c)).to_affine();
516516
}
@@ -542,9 +542,9 @@ impl Commitment {
542542
}
543543

544544
/// Returns the `i`-th public key share.
545-
pub fn evaluate<T: IntoFr>(&self, i: T) -> G1 {
545+
pub fn evaluate<T: IntoFr>(&self, i: T) -> G1Affine {
546546
let mut result = match self.coeff.last() {
547-
None => return G1::identity(),
547+
None => return G1Affine::identity(),
548548
Some(c) => G1Projective::from(c),
549549
};
550550
let x = i.into_fr();
@@ -573,7 +573,7 @@ impl Commitment {
573573

574574
/// Deserializes from big endian bytes
575575
pub fn from_bytes(bytes: Vec<u8>) -> Result<Self> {
576-
let mut c: Vec<G1> = vec![];
576+
let mut c: Vec<G1Affine> = vec![];
577577
let coeff_size = bytes.len() / PK_SIZE;
578578
for i in 0..coeff_size {
579579
let mut g1_bytes = [0; PK_SIZE];
@@ -708,7 +708,7 @@ impl BivarPoly {
708708

709709
/// Returns the corresponding commitment. That information can be shared publicly.
710710
pub fn commitment(&self) -> BivarCommitment {
711-
let to_pub = |c: &Fr| G1::generator().mul(c).to_affine();
711+
let to_pub = |c: &Fr| G1Affine::generator().mul(c).to_affine();
712712
BivarCommitment {
713713
degree: self.degree,
714714
coeff: self.coeff.iter().map(to_pub).collect(),
@@ -773,7 +773,7 @@ pub struct BivarCommitment {
773773
/// The polynomial's degree in each of the two variables.
774774
pub(crate) degree: usize,
775775
/// The commitments to the coefficients.
776-
pub(crate) coeff: Vec<G1>,
776+
pub(crate) coeff: Vec<G1Affine>,
777777
}
778778

779779
impl Hash for BivarCommitment {
@@ -810,7 +810,7 @@ impl BivarCommitment {
810810
}
811811

812812
/// Returns the commitment's value at the point `(x, y)`.
813-
pub fn evaluate<T: IntoFr>(&self, x: T, y: T) -> G1 {
813+
pub fn evaluate<T: IntoFr>(&self, x: T, y: T) -> G1Affine {
814814
let x_pow = self.powers(x);
815815
let y_pow = self.powers(y);
816816
// TODO: Can we save a few multiplication steps here due to the symmetry?
@@ -830,7 +830,7 @@ impl BivarCommitment {
830830
/// Returns the `x`-th row, as a commitment to a univariate polynomial.
831831
pub fn row<T: IntoFr>(&self, x: T) -> Commitment {
832832
let x_pow = self.powers(x);
833-
let coeff: Vec<G1> = (0..=self.degree)
833+
let coeff: Vec<G1Affine> = (0..=self.degree)
834834
.map(|i| {
835835
let mut result = G1Projective::identity();
836836
for (j, x_pow_j) in x_pow.iter().enumerate() {
@@ -870,7 +870,7 @@ impl BivarCommitment {
870870

871871
/// Deserializes from big endian bytes
872872
pub fn from_bytes(bytes: Vec<u8>) -> Result<Self> {
873-
let mut c: Vec<G1> = vec![];
873+
let mut c: Vec<G1Affine> = vec![];
874874
let coeff_size = bytes.len() / PK_SIZE;
875875
for i in 0..coeff_size {
876876
let mut g1_bytes = [0; PK_SIZE];
@@ -966,7 +966,7 @@ mod tests {
966966
assert_ne!(random_commitment, zero_commitment);
967967
let mut rng = rand::thread_rng();
968968
let (x, y): (Fr, Fr) = (Fr::random(&mut rng), Fr::random(&mut rng));
969-
assert_eq!(zero_commitment.evaluate(x, y), G1::identity());
969+
assert_eq!(zero_commitment.evaluate(x, y), G1Affine::identity());
970970
}
971971

972972
#[test]
@@ -998,7 +998,7 @@ mod tests {
998998
// Node `s` receives the `s`-th value and verifies it.
999999
for s in 1..=node_num {
10001000
let val = row_poly.evaluate(s);
1001-
let val_g1 = G1::generator().mul(val);
1001+
let val_g1 = G1Affine::generator().mul(val);
10021002
assert_eq!(bi_commit.evaluate(m, s), val_g1.to_affine());
10031003
// The node can't verify this directly, but it should have the correct value:
10041004
assert_eq!(bi_poly.evaluate(m, s), val);

src/serde_impl.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
1010

1111
use crate::poly::{coeff_pos, BivarCommitment};
1212
use crate::serde_impl::serialize_secret_internal::SerializeSecret;
13-
use crate::{SecretKey, G1};
13+
use crate::{G1Affine, SecretKey};
1414

1515
const ERR_DEG: &str = "commitment degree does not match coefficients";
1616

@@ -121,7 +121,7 @@ struct WireBivarCommitment<'a> {
121121
/// The polynomial's degree in each of the two variables.
122122
degree: usize,
123123
/// The commitments to the coefficients.
124-
coeff: Cow<'a, [G1]>,
124+
coeff: Cow<'a, [G1Affine]>,
125125
}
126126

127127
impl Serialize for BivarCommitment {
@@ -312,12 +312,12 @@ mod tests {
312312
use serde::{Deserialize, Serialize};
313313

314314
use crate::poly::BivarPoly;
315-
use crate::{Fr, G1Projective, G1};
315+
use crate::{Fr, G1Affine, G1Projective};
316316

317317
#[derive(Debug, Serialize, Deserialize)]
318318
pub struct Vecs {
319319
#[serde(with = "super::affine_vec")]
320-
curve_points: Vec<G1>,
320+
curve_points: Vec<G1Affine>,
321321
#[serde(with = "super::field_vec")]
322322
field_elements: Vec<Fr>,
323323
}

0 commit comments

Comments
 (0)