Skip to content

Commit e7794e7

Browse files
committed
Fix warnings and add to_compressed
Signed-off-by: Michael Lodder <redmike7@gmail.com>
1 parent ef03c86 commit e7794e7

File tree

7 files changed

+55
-20
lines changed

7 files changed

+55
-20
lines changed

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ byteorder = "1.3.1"
2626
serde = "1.0"
2727
serde_json = "1.0"
2828
serde_derive = "1.0"
29-
zeroize = "0.9.3"
30-
#tiny-keccak = "1.5"
29+
zeroize = "0.10"
3130
sha3 = "0.8.2"
3231

3332
[dependencies.amcl]

src/constants.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use super::ECCurve::rom;
55

66
pub const MODBYTES: usize = curve_MODBYTES;
77
pub const NLEN: usize = curve_NLEN;
8-
pub const BigNumBits: usize = BASEBITS;
8+
pub const BIG_NUM_BITS: usize = BASEBITS;
99

1010
// Byte size of element in group G1, 1 extra byte for compression flag
1111
pub const GROUP_G1_SIZE: usize = 2 * MODBYTES + 1;
@@ -38,3 +38,5 @@ lazy_static! {
3838

3939
#[cfg(any(feature = "bls381", feature = "bn254"))]
4040
pub use crate::types_g2::{GENERATOR_G2, GROUP_G2_SIZE, GROUP_GT_SIZE};
41+
#[cfg(any(feature = "bls381", feature = "bn254"))]
42+
pub use crate::types_g1::GROUP_G1_COMPRESSED_SIZE;

src/field_elem.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use rand::prelude::*;
2-
use crate::constants::{BARRETT_REDC_K, BARRETT_REDC_U, BARRETT_REDC_V, CURVE_ORDER, MODBYTES, NLEN};
2+
use crate::constants::{BARRETT_REDC_K, BARRETT_REDC_U, BARRETT_REDC_V, CURVE_ORDER, MODBYTES, NLEN, BIG_NUM_BITS};
33
use crate::errors::{SerzDeserzError, ValueError};
44
use crate::types::{BigNum, DoubleBigNum, Limb};
55
use crate::utils::{barrett_reduction, hash_mod_order, random_mod_order};
6-
use amcl::arch::CHUNK;
76
use std::cmp::Ordering;
87
use std::fmt;
98
use std::hash::{Hash, Hasher};
@@ -196,7 +195,7 @@ impl FieldElement {
196195
/// Exponentiation modulo curve order, i.e. self^exp % CurveOrder
197196
pub fn pow(&self, exp: &Self) -> Self {
198197
let mut base = self.value.clone();
199-
let res = base.powmod(&exp.value, &CurveOrder);
198+
let res = base.powmod(&exp.value, &CURVE_ORDER);
200199
res.into()
201200
}
202201

@@ -286,7 +285,7 @@ impl FieldElement {
286285
for mut bit_vec in bit_vecs.drain(..) {
287286
let len = bit_vec.len();
288287
bits.append(&mut bit_vec);
289-
bits.append(&mut vec![0; BigNumBits - len]);
288+
bits.append(&mut vec![0; BIG_NUM_BITS - len]);
290289
}
291290
bits
292291
}
@@ -1034,7 +1033,7 @@ mod test {
10341033
assert_eq!(FieldElement::from(3u64).nth_bit(2), 0);
10351034
assert_eq!(FieldElement::from(3u64).nth_bit(3), 0);
10361035

1037-
let mut rng = rand::thread_rng();
1036+
let mut rng = thread_rng();
10381037

10391038
for _ in 0..10 {
10401039
let r = FieldElement::random();
@@ -1058,7 +1057,6 @@ mod test {
10581057
a.or(&b);
10591058
assert_eq!(a, FieldElement::from(6));
10601059

1061-
let mut rng = rand::thread_rng();
10621060
for _ in 0..100 {
10631061
let r1 = FieldElement::random();
10641062
let r2 = FieldElement::random();
@@ -1205,21 +1203,21 @@ mod test {
12051203
#[test]
12061204
fn test_to_bits() {
12071205
let mut bits = vec![0, 1, 0, 1];
1208-
bits.append(&mut vec![0; BigNumBits-4]);
1206+
bits.append(&mut vec![0; BIG_NUM_BITS-4]);
12091207
assert_eq!(FieldElement::from(10u32).to_bits(), bits);
12101208

12111209
let mut bits = vec![0, 0, 1, 0, 0, 1, 1];
1212-
bits.append(&mut vec![0; BigNumBits-7]);
1210+
bits.append(&mut vec![0; BIG_NUM_BITS-7]);
12131211
assert_eq!(FieldElement::from(100u32).to_bits(), bits);
12141212

12151213
let mut c = vec![0i64; NLEN];
12161214
c[0] = 2;
12171215
c[1] = 100;
12181216
let m: FieldElement = BigNum::new_ints(&c).into();
12191217
let mut bits = vec![0, 1];
1220-
bits.append(&mut vec![0; BigNumBits-2]);
1218+
bits.append(&mut vec![0; BIG_NUM_BITS-2]);
12211219
bits.append(&mut vec![0, 0, 1, 0, 0, 1, 1]);
1222-
bits.append(&mut vec![0; BigNumBits-7]);
1220+
bits.append(&mut vec![0; BIG_NUM_BITS-7]);
12231221
assert_eq!(
12241222
m.to_bits(),
12251223
bits

src/group_elem.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ macro_rules! impl_group_elem_traits {
136136

137137
impl fmt::Display for $group_element {
138138
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
139-
let mut c = self.value.clone();
140-
write!(f, "{}", c.tostring())
139+
let c = self.value.clone().tostring();
140+
write!(f, "{}", c)
141141
}
142142
}
143143

@@ -849,14 +849,16 @@ macro_rules! impl_group_elem_vec_conversions {
849849
#[cfg(test)]
850850
mod test {
851851
use super::*;
852-
use crate::constants::GROUP_G1_SIZE;
852+
use crate::constants::{GROUP_G1_SIZE, GROUP_GT_SIZE};
853853
#[cfg(any(feature = "bls381", feature = "bn254"))]
854854
use crate::constants::GROUP_G2_SIZE;
855855
use crate::group_elem_g1::{G1LookupTable, G1Vector, G1};
856856
#[cfg(any(feature = "bls381", feature = "bn254"))]
857857
use crate::group_elem_g2::{G2LookupTable, G2Vector, G2};
858858
#[cfg(any(feature = "bls381", feature = "bn254"))]
859859
use crate::field_elem::FieldElementVector;
860+
#[cfg(any(feature = "bls381", feature = "bn254"))]
861+
use crate::extension_field_gt::GT;
860862
use std::collections::{HashMap, HashSet};
861863
use std::time::Instant;
862864

@@ -888,9 +890,9 @@ mod test {
888890

889891
to_and_fro_bytes!(G1, GROUP_G1_SIZE);
890892
#[cfg(any(feature = "bls381", feature = "bn254"))]
891-
to_and_fro_bytes!(G2, GroupG2_SIZE);
893+
to_and_fro_bytes!(G2, GROUP_G2_SIZE);
892894
#[cfg(any(feature = "bls381", feature = "bn254"))]
893-
to_and_fro_bytes!(GT, GroupGT_SIZE);
895+
to_and_fro_bytes!(GT, GROUP_GT_SIZE);
894896
}
895897

896898
#[test]

src/group_elem_g1.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::constants::{CURVE_ORDER, GROUP_G1_SIZE};
1+
use crate::constants::{CURVE_ORDER, GROUP_G1_SIZE, GROUP_G1_COMPRESSED_SIZE};
22
use crate::errors::{SerzDeserzError, ValueError};
33
use crate::field_elem::{FieldElement, FieldElementVector};
44
use crate::group_elem::{GroupElement, GroupElementVector};
@@ -59,7 +59,15 @@ impl GroupElement for G1 {
5959
}
6060

6161
fn from_bytes(bytes: &[u8]) -> Result<Self, SerzDeserzError> {
62-
if bytes.len() != GROUP_G1_SIZE {
62+
if bytes[0] == 0x02 || bytes[0] == 0x03 {
63+
if bytes.len() != GROUP_G1_COMPRESSED_SIZE {
64+
return Err(SerzDeserzError::G1BytesIncorrectSize(
65+
bytes.len(),
66+
GROUP_G1_COMPRESSED_SIZE,
67+
));
68+
}
69+
}
70+
else if bytes.len() != GROUP_G1_SIZE {
6371
return Err(SerzDeserzError::G1BytesIncorrectSize(
6472
bytes.len(),
6573
GROUP_G1_SIZE,
@@ -142,6 +150,14 @@ impl G1 {
142150
.mul2(&a.to_bignum(), &h.to_ecp(), &b.to_bignum())
143151
.into()
144152
}
153+
154+
pub fn to_compressed(&self) -> Vec<u8> {
155+
let mut temp = GroupG1::new();
156+
temp.copy(&self.value);
157+
let mut bytes = [0u8; GROUP_G1_COMPRESSED_SIZE];
158+
temp.tobytes(&mut bytes, true);
159+
bytes.to_vec()
160+
}
145161
}
146162

147163
impl_group_elem_traits!(G1, GroupG1);
@@ -354,4 +370,16 @@ mod test {
354370
start.elapsed()
355371
);
356372
}
373+
374+
#[test]
375+
fn to_compressed() {
376+
let s = FieldElement::random();
377+
let g1 = &G1::generator() * &s;
378+
let bytes = g1.to_compressed();
379+
380+
assert_eq!(GROUP_G1_COMPRESSED_SIZE, bytes.len());
381+
let res = G1::from_bytes(bytes.as_slice());
382+
assert!(res.is_ok());
383+
assert_eq!(res.unwrap(), g1);
384+
}
357385
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ pub mod group_elem;
4343
pub mod group_elem_g1;
4444
pub mod commitment;
4545

46+
#[cfg(any(feature = "bls381", feature = "bn254"))]
47+
pub mod types_g1;
4648
#[cfg(any(feature = "bls381", feature = "bn254"))]
4749
pub mod types_g2;
4850

src/types_g1.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
use crate::constants::MODBYTES;
2+
3+
// Byte size of x field element in group G1, 1 extra byte for compression flag
4+
pub const GROUP_G1_COMPRESSED_SIZE: usize = MODBYTES + 1;

0 commit comments

Comments
 (0)