From 6386a3490d48ce04873b0cee2a1091272e7fc791 Mon Sep 17 00:00:00 2001 From: "Weikai.Zhao" Date: Fri, 13 May 2022 14:49:42 +0800 Subject: [PATCH 1/2] modify bench function error --- src/sm2/signature.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sm2/signature.rs b/src/sm2/signature.rs index c756467..2fc89ce 100644 --- a/src/sm2/signature.rs +++ b/src/sm2/signature.rs @@ -490,7 +490,7 @@ mod signature_benches { fn sign_bench(bench: &mut test::Bencher) { let test_word = b"hello world"; let ctx = SigCtx::new(); - let (pk, sk) = ctx.new_keypair(); + let (pk, sk) = ctx.new_keypair().unwrap(); bench.iter(|| { let _ = ctx.sign(test_word, &sk, &pk); @@ -501,7 +501,7 @@ mod signature_benches { fn verify_bench(bench: &mut test::Bencher) { let test_word = b"hello world"; let ctx = SigCtx::new(); - let (pk, sk) = ctx.new_keypair(); + let (pk, sk) = ctx.new_keypair().unwrap(); let sig = ctx.sign(test_word, &sk, &pk).unwrap(); bench.iter(|| { From 510558f475db557ee4488633ef2a529ec831e7d3 Mon Sep 17 00:00:00 2001 From: "Weikai.Zhao" Date: Fri, 13 May 2022 15:00:34 +0800 Subject: [PATCH 2/2] remove outdated point_mul method --- src/sm2/ecc.rs | 44 ++++---------------------------------------- 1 file changed, 4 insertions(+), 40 deletions(-) diff --git a/src/sm2/ecc.rs b/src/sm2/ecc.rs index 88d03f8..3a2f024 100644 --- a/src/sm2/ecc.rs +++ b/src/sm2/ecc.rs @@ -57,7 +57,7 @@ lazy_static! { let ctx = EccCtx::new(); for i in 0..256 { let p1 = ctx - .mul_raw(&pre_vec_gen(i as u32), &ctx.generator().unwrap()) + .mul_raw_naf(&pre_vec_gen(i as u32), &ctx.generator().unwrap()) .unwrap(); table.push(p1); } @@ -68,7 +68,7 @@ lazy_static! { let ctx = EccCtx::new(); for i in 0..256 { let p1 = ctx - .mul_raw(&pre_vec_gen2(i as u32), &ctx.generator().unwrap()) + .mul_raw_naf(&pre_vec_gen2(i as u32), &ctx.generator().unwrap()) .unwrap(); table.push(p1); } @@ -389,6 +389,8 @@ impl EccCtx { self.mul_raw_naf(&k.value, p) } + //w-naf algorithm + //See https://crypto.stackexchange.com/questions/82013/simple-explanation-of-sliding-window-and-wnaf-methods-of-elliptic-curve-point-mu pub fn w_naf(&self, m: &[u32], w: usize, lst: &mut usize) -> [i8; 257] { let mut carry = 0; let mut bit = 0; @@ -462,27 +464,6 @@ impl EccCtx { Ok(q) } - pub fn mul_raw(&self, m: &[u32], p: &Point) -> Sm2Result { - let mut q = self.zero(); - - let mut i = 0; - while i < 256 { - let index = i as usize / 32; - let bit = 31 - i as usize % 32; - - // let sum = self.add(&q0, &q1); - q = self.double(&q)?; - - if (m[index] >> bit) & 0x01 != 0 { - q = self.add(&q, p)?; - - // q = self.double(&q0); - } - - i += 1; - } - Ok(q) - } #[inline(always)] fn ith_bit(n: u32, i: i32) -> u32 { (n >> i) & 0x01 @@ -837,23 +818,6 @@ mod internal_benches { }); } - #[bench] - fn bench_mul_raw(bench: &mut test::Bencher) { - let curve = EccCtx::new(); - let g = curve.generator().unwrap(); - let m = BigUint::from_str_radix( - "76415405cbb177ebb37a835a2b5a022f66c250abf482e4cb343dcb2091bc1f2e", - 16, - ) - .unwrap() - % curve.get_n(); - let k = FieldElem::from_biguint(&m).unwrap(); - - bench.iter(|| { - curve.mul_raw(&k.value, &g); - }); - } - #[bench] fn bench_mul_raw_naf(bench: &mut test::Bencher) { let curve = EccCtx::new();