Skip to content

Commit ccff5ec

Browse files
committed
Fix warnings and add to_compressed
Signed-off-by: Michael Lodder <[email protected]>
1 parent ef03c86 commit ccff5ec

File tree

8 files changed

+129
-25
lines changed

8 files changed

+129
-25
lines changed

Cargo.toml

Lines changed: 2 additions & 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]
@@ -38,4 +37,5 @@ features = ["bls381", "bn254", "secp256k1", "ed25519"]
3837
[dev-dependencies]
3938
rand_chacha = "0.2.0"
4039
rand_core = "0.5.0"
40+
cfg-if = "0.1.10"
4141

src/constants.rs

Lines changed: 2 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,4 @@ 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+
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: 8 additions & 6 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

@@ -851,12 +851,14 @@ mod test {
851851
use super::*;
852852
use crate::constants::GROUP_G1_SIZE;
853853
#[cfg(any(feature = "bls381", feature = "bn254"))]
854-
use crate::constants::GROUP_G2_SIZE;
854+
use crate::constants::{GROUP_G2_SIZE, GROUP_GT_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};
858-
#[cfg(any(feature = "bls381", feature = "bn254"))]
858+
// #[cfg(any(feature = "bls381", feature = "bn254", feature = "secp256k1", feature))]
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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ pub mod group_elem;
4343
pub mod group_elem_g1;
4444
pub mod commitment;
4545

46+
pub mod types_g1;
4647
#[cfg(any(feature = "bls381", feature = "bn254"))]
4748
pub mod types_g2;
4849

@@ -61,4 +62,8 @@ extern crate serde_derive;
6162

6263
extern crate serde_json;
6364

65+
#[cfg(test)]
66+
#[macro_use]
67+
extern crate cfg_if;
68+
6469
// TODO: Move the timing tests to benchmark

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;

src/utils.rs

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -405,10 +405,42 @@ mod test {
405405
let mut rng = ChaChaRng::from_seed(*seed);
406406

407407
let r = random_mod_order(Some(&mut rng));
408-
assert_eq!(r, BigNum::from_hex("000000000000000000000000000000005C9F002063FDC7EDD33E8787C8322E794198C2F397DEF85F382FE9075A2A0E5F".to_string()));
408+
cfg_if! {
409+
if #[cfg(feature = "bls381")] {
410+
assert_eq!(r, BigNum::from_hex("000000000000000000000000000000005C9F002063FDC7EDD33E8787C8322E794198C2F397DEF85F382FE9075A2A0E5F".to_string()));
411+
}
412+
else if #[cfg(feature = "secp256k1")] {
413+
assert_eq!(r, BigNum::from_hex("00000000000000000000000000000000E01148AF147EEC7F873610D3AB8F7C89080E488515F5716C2BB044144F6A6059".to_string()));
414+
}
415+
else if #[cfg(feature = "bn254")] {
416+
assert_eq!(r, BigNum::from_hex("0000000000000000000000000000000014D85E8C2C7FD613FBD0F2D2CE4C6C7747DCD7DFA4FA3EA118563699282FD6AC".to_string()));
417+
}
418+
else if #[cfg(feature = "ed25519")] {
419+
assert_eq!(r, BigNum::from_hex("00000000000000000000000000000000002F8D87DCBAFEE1C58359C1C893D33755B09ADE3C3AEE92E38924A358DED363".to_string()));
420+
}
421+
else {
422+
assert_eq!(r, BigNum::from_hex("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000".to_string()));
423+
}
424+
}
409425
assert!(r < *CURVE_ORDER);
410426
let r = random_mod_order(Some(&mut rng));
411-
assert_eq!(r, BigNum::from_hex("0000000000000000000000000000000002EB5988AC48026ABEAF0206276C9D1158B5A2BE12EAFF2097A9AD8D8CFFD64D".to_string()));
427+
cfg_if! {
428+
if #[cfg(feature = "bls381")] {
429+
assert_eq!(r, BigNum::from_hex("0000000000000000000000000000000002EB5988AC48026ABEAF0206276C9D1158B5A2BE12EAFF2097A9AD8D8CFFD64D".to_string()));
430+
}
431+
else if #[cfg(feature = "secp256k1")] {
432+
assert_eq!(r, BigNum::from_hex("0000000000000000000000000000000000704BF56ED631AA09DF7AF7037F1D627B273EB9A31CAE5ED3316E2515C95708B".to_string()));
433+
}
434+
else if #[cfg(feature = "bn254")] {
435+
assert_eq!(r, BigNum::from_hex("00000000000000000000000000000000001F8953C73F7433A4EEDFD6CF8924D83C0A5A3E620CC20A03CB88AF662C466EFD".to_string()));
436+
}
437+
else if #[cfg(feature = "ed25519")] {
438+
assert_eq!(r, BigNum::from_hex("0000000000000000000000000000000000065B4AF9E6A305FE1E45530B53547E9F2C3934A3BB804A850BDE2D598565B2F2".to_string()));
439+
}
440+
else {
441+
assert_eq!(r, BigNum::from_hex("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000".to_string()));
442+
}
443+
}
412444

413445
for _ in 0..30 {
414446
let r1 = random_mod_order::<ThreadRng>(None);
@@ -426,13 +458,47 @@ mod test {
426458
rng.fill_bytes(&mut r);
427459

428460
let h = hash_mod_order(&r);
429-
assert_eq!(h, BigNum::from_hex("000000000000000000000000000000006E9AAA41F32B7DDC4C0CB1150B5414EA52C5234C329BF8B10E10C67F418A83FA".to_string()));
461+
cfg_if! {
462+
if #[cfg(feature = "bls381")] {
463+
assert_eq!(h, BigNum::from_hex("000000000000000000000000000000006E9AAA41F32B7DDC4C0CB1150B5414EA52C5234C329BF8B10E10C67F418A83FA".to_string()));
464+
}
465+
else if #[cfg(feature = "secp256k1")] {
466+
assert_eq!(h, BigNum::from_hex("000000000000000000000000000000004253801001280FE08CF7E565011C7897EA030349E05F10F62AB5A5A24FE66E2D".to_string()));
467+
}
468+
else if #[cfg(feature = "bn254")] {
469+
assert_eq!(h, BigNum::from_hex("00000000000000000000000000000000045355233F08831F94CDD540C89346D50BB8F13642D165EA18F3C867D5305921".to_string()));
470+
}
471+
else if #[cfg(feature = "ed25519")] {
472+
assert_eq!(h, BigNum::from_hex("0000000000000000000000000000000000E5BBEF7112D75FCF549BBC7CC8C0C20C309E9567BDE24DCA59698323789972F".to_string()))
473+
}
474+
else {
475+
assert_eq!(h, BigNum::from_hex("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000".to_string()));
476+
}
477+
}
478+
430479
assert!(h < *CURVE_ORDER);
431480
let h1 = hash_mod_order(&r);
432481
assert_eq!(h, h1);
433482
rng.fill_bytes(&mut r);
434483
let h = hash_mod_order(&r);
435-
assert_eq!(h, BIG::from_hex("0000000000000000000000000000000005A15BCF8E435724C15C8581587B2A408834236982E0405F047CADB2060B489B".to_string()));
484+
cfg_if! {
485+
if #[cfg(feature = "bls381")] {
486+
assert_eq!(h, BigNum::from_hex("0000000000000000000000000000000005A15BCF8E435724C15C8581587B2A408834236982E0405F047CADB2060B489B".to_string()));
487+
}
488+
else if #[cfg(feature = "secp256k1")] {
489+
assert_eq!(h, BigNum::from_hex("0000000000000000000000000000000000C1BF2AE2AA84465EEB44264108C152DDFCEE66FD695A68FC815996F134CF133".to_string()));
490+
}
491+
else if #[cfg(feature = "bn254")] {
492+
assert_eq!(h, BigNum::from_hex("00000000000000000000000000000000000E63105F56FB38E9568390D3EA13A51B69D1BCEA7190F1C7DAF365269D39176F".to_string()))
493+
}
494+
else if #[cfg(feature = "ed25519")] {
495+
assert_eq!(h, BigNum::from_hex("00000000000000000000000000000000000B364AB28CFAEC69DB8562EECE7A88F9204571FFE2AE934E610615483C750E02".to_string()))
496+
}
497+
else {
498+
assert_eq!(h, BigNum::from_hex("000000000000000000000000000000004253801001280FE08CF7E565011C7897EA030349E05F10F62AB5A5A24FE66E2D".to_string()));
499+
}
500+
}
501+
436502
assert!(h < *CURVE_ORDER);
437503
}
438504
}

0 commit comments

Comments
 (0)