Update sign&verify (poseidon) to last optimization

(On a Intel(R) Core(TM) i7-8705G CPU @ 3.10GHz, with 32 GB of RAM)

- before:
```
sign_poseidon           time:   [383.01 ms 384.46 ms 385.98 ms]
verify_poseidon         time:   [250.56 ms 251.46 ms 252.43 ms]
```

- current:
```
sign_poseidon           time:   [973.38 us 973.83 us 974.41 us]
verify_poseidon         time:   [835.34 us 839.94 us 845.29 us]
```

sign_poseidon: `394x` improvement
verify_poseidon: `300x` improvement
This commit is contained in:
2020-08-02 16:23:40 +02:00
parent 2d94206899
commit 04d20b9e05
3 changed files with 235 additions and 229 deletions

View File

@@ -11,6 +11,7 @@ readme = "README.md"
[dependencies] [dependencies]
ff = {package="ff_ce" , version="0.11", features = ["derive"]} ff = {package="ff_ce" , version="0.11", features = ["derive"]}
rand = "0.4" rand = "0.4"
rand6 = {package="rand", version="0.6.5"}
num = "0.2.0" num = "0.2.0"
num-bigint = {version = "0.2.2", features = ["rand"]} num-bigint = {version = "0.2.2", features = ["rand"]}
num-traits = "0.2.8" num-traits = "0.2.8"
@@ -19,7 +20,7 @@ generic-array = "0.13.2"
tiny-keccak = "1.5" tiny-keccak = "1.5"
rustc-hex = "1.0.0" rustc-hex = "1.0.0"
mimc-rs = "0.0.2" mimc-rs = "0.0.2"
poseidon-rs = "0.0.1" poseidon-rs = "0.0.3"
arrayref = "0.3.5" arrayref = "0.3.5"
lazy_static = "1.4.0" lazy_static = "1.4.0"

View File

@@ -7,20 +7,11 @@ use ff::*;
extern crate num; extern crate num;
extern crate num_bigint; extern crate num_bigint;
use num_bigint::BigInt; use num_bigint::{BigInt, ToBigInt};
use babyjubjub_rs::{utils, Point}; use babyjubjub_rs::{utils, Point};
fn criterion_benchmark(c: &mut Criterion) { fn criterion_benchmark(c: &mut Criterion) {
// let x: BigInt = BigInt::parse_bytes(
// b"17777552123799933955779906779655732241715742912184938656739573121738514868268",
// 10,
// )
// .unwrap();
// c.bench_function("modulus", |b| {
// b.iter(|| utils::modulus(&x, &babyjubjub_rs::Q))
// });
let p: Point = Point { let p: Point = Point {
x: babyjubjub_rs::Fr::from_str( x: babyjubjub_rs::Fr::from_str(
"17777552123799933955779906779655732241715742912184938656739573121738514868268", "17777552123799933955779906779655732241715742912184938656739573121738514868268",
@@ -30,11 +21,13 @@ fn criterion_benchmark(c: &mut Criterion) {
"2626589144620713026669568689430873010625803728049924121243784502389097019475", "2626589144620713026669568689430873010625803728049924121243784502389097019475",
) )
.unwrap(), .unwrap(),
z: babyjubjub_rs::Fr::one(),
}; };
let q = p.clone(); let q = p.clone();
c.bench_function("add", |b| b.iter(|| p.add(&q))); let p_projective = p.projective();
let q_projective = q.projective();
c.bench_function("add", |b| b.iter(|| p_projective.add(&q_projective)));
let r: BigInt = BigInt::parse_bytes(b"3", 10).unwrap(); let r: BigInt = BigInt::parse_bytes(b"3", 10).unwrap();
c.bench_function("mul_scalar_small", |b| b.iter(|| p.mul_scalar(&r))); c.bench_function("mul_scalar_small", |b| b.iter(|| p.mul_scalar(&r)));
let r: BigInt = BigInt::parse_bytes( let r: BigInt = BigInt::parse_bytes(
@@ -44,22 +37,22 @@ fn criterion_benchmark(c: &mut Criterion) {
.unwrap(); .unwrap();
c.bench_function("mul_scalar", |b| b.iter(|| p.mul_scalar(&r))); c.bench_function("mul_scalar", |b| b.iter(|| p.mul_scalar(&r)));
// c.bench_function("compress", |b| b.iter(|| p.compress())); c.bench_function("point compress", |b| b.iter(|| p.compress()));
// let p_comp = p.compress(); let p_comp = p.compress();
// c.bench_function("decompress", |b| { c.bench_function("point decompress", |b| {
// b.iter(|| babyjubjub_rs::decompress_point(p_comp)) b.iter(|| babyjubjub_rs::decompress_point(p_comp))
// }); });
// let sk = babyjubjub_rs::new_key(); let sk = babyjubjub_rs::new_key();
// let pk = sk.public().unwrap(); let pk = sk.public().unwrap();
// let msg = 5.to_bigint().unwrap(); let msg = 5.to_bigint().unwrap();
// c.bench_function("sign_poseidon", |b| { c.bench_function("sign_poseidon", |b| {
// b.iter(|| sk.sign_poseidon(msg.clone())) b.iter(|| sk.sign_poseidon(msg.clone()))
// }); });
// let sig = sk.sign_poseidon(msg.clone()).unwrap(); let sig = sk.sign_poseidon(msg.clone()).unwrap();
// c.bench_function("verify_poseidon", |b| { c.bench_function("verify_poseidon", |b| {
// b.iter(|| babyjubjub_rs::verify_poseidon(pk.clone(), sig.clone(), msg.clone())) b.iter(|| babyjubjub_rs::verify_poseidon(pk.clone(), sig.clone(), msg.clone()))
// }); });
} }
criterion_group!(benches, criterion_benchmark); criterion_group!(benches, criterion_benchmark);

View File

@@ -3,10 +3,8 @@ extern crate rand;
extern crate ff; extern crate ff;
use ff::*; use ff::*;
#[derive(PrimeField)] use poseidon_rs::Poseidon;
#[PrimeFieldModulus = "21888242871839275222246405745257275088548364400416034343698204186575808495617"] pub type Fr = poseidon_rs::Fr; // alias
#[PrimeFieldGenerator = "7"]
pub struct Fr(FrRepr);
#[macro_use] #[macro_use]
extern crate arrayref; extern crate arrayref;
@@ -16,11 +14,11 @@ extern crate num;
extern crate num_bigint; extern crate num_bigint;
extern crate num_traits; extern crate num_traits;
use rand::Rng; extern crate rand6;
use rand6::Rng;
use blake2::{Blake2b, Digest}; use blake2::{Blake2b, Digest};
use mimc_rs::Mimc7; use mimc_rs::Mimc7;
use poseidon_rs::Poseidon;
use std::cmp::min; use std::cmp::min;
use num_bigint::{BigInt, RandBigInt, RandomBits, Sign, ToBigInt}; use num_bigint::{BigInt, RandBigInt, RandomBits, Sign, ToBigInt};
@@ -55,35 +53,37 @@ lazy_static! {
"16950150798460657717958625567821834550301663161624707787222815936182638968203", "16950150798460657717958625567821834550301663161624707787222815936182638968203",
) )
.unwrap(), .unwrap(),
z: Fr::one(), // z: Fr::one(),
}; };
static ref ORDER: Fr = Fr::from_str( static ref ORDER: Fr = Fr::from_str(
"21888242871839275222246405745257275088614511777268538073601725287587578984328", "21888242871839275222246405745257275088614511777268538073601725287587578984328",
) )
.unwrap(); .unwrap();
// SUBORDER = ORDER >> 3
static ref SUBORDER: Fr = Fr::from_str(
"i2736030358979909402780800718157159386076813972158567259200215660948447373041",
)
.unwrap();
}
pub struct PointAffine { // SUBORDER = ORDER >> 3
pub x: Fr, // static ref SUBORDER: Fr = Fr::from_str(
pub y: Fr, // "i2736030358979909402780800718157159386076813972158567259200215660948447373041",
// )
// .unwrap();
static ref SUBORDER: BigInt = &BigInt::parse_bytes(
b"21888242871839275222246405745257275088614511777268538073601725287587578984328",
10,
)
.unwrap()
>> 3;
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Point { pub struct PointProjective {
pub x: Fr, pub x: Fr,
pub y: Fr, pub y: Fr,
pub z: Fr, pub z: Fr,
} }
impl Point { impl PointProjective {
pub fn affine(&self) -> PointAffine { pub fn affine(&self) -> Point {
if self.z.is_zero() { if self.z.is_zero() {
return PointAffine { return Point {
x: Fr::zero(), x: Fr::zero(),
y: Fr::zero(), y: Fr::zero(),
}; };
@@ -95,20 +95,12 @@ impl Point {
let mut y = self.y; let mut y = self.y;
y.mul_assign(&zinv); y.mul_assign(&zinv);
PointAffine { Point {
x: x.clone(), x: x.clone(),
y: y.clone(), y: y.clone(),
} }
} }
pub fn from_affine(p: PointAffine) -> Point { pub fn add(&self, q: &PointProjective) -> Result<PointProjective, String> {
Point {
x: p.x.clone(),
y: p.y.clone(),
z: Fr::one(),
}
}
pub fn add(&self, q: &Point) -> Result<Point, String> {
// add-2008-bbjlp https://hyperelliptic.org/EFD/g1p/auto-twisted-projective.html#doubling-dbl-2008-bbjlp // add-2008-bbjlp https://hyperelliptic.org/EFD/g1p/auto-twisted-projective.html#doubling-dbl-2008-bbjlp
let mut a = self.z; let mut a = self.z;
a.mul_assign(&q.z); a.mul_assign(&q.z);
@@ -146,20 +138,36 @@ impl Point {
let mut z3 = f; let mut z3 = f;
z3.mul_assign(&g); z3.mul_assign(&g);
Ok(Point { Ok(PointProjective {
x: x3.clone(), x: x3.clone(),
y: y3.clone(), y: y3.clone(),
z: z3.clone(), z: z3.clone(),
}) })
} }
}
#[derive(Clone, Debug)]
pub struct Point {
pub x: Fr,
pub y: Fr,
}
impl Point {
pub fn projective(&self) -> PointProjective {
PointProjective {
x: self.x.clone(),
y: self.y.clone(),
z: Fr::one(),
}
}
pub fn mul_scalar(&self, n: &BigInt) -> Result<Point, String> { pub fn mul_scalar(&self, n: &BigInt) -> Result<Point, String> {
let mut r: Point = Point { let mut r: PointProjective = PointProjective {
x: Fr::zero(), x: Fr::zero(),
y: Fr::one(), y: Fr::one(),
z: Fr::one(), z: Fr::one(),
}; };
let mut exp: Point = self.clone(); let mut exp: PointProjective = self.projective();
let (_, b) = n.to_bytes_le(); let (_, b) = n.to_bytes_le();
for i in 0..n.bits() { for i in 0..n.bits() {
if test_bit(&b, i) { if test_bit(&b, i) {
@@ -167,11 +175,11 @@ impl Point {
} }
exp = exp.add(&exp)?; exp = exp.add(&exp)?;
} }
Ok(r) Ok(r.affine())
} }
pub fn compress(&self) -> [u8; 32] { pub fn compress(&self) -> [u8; 32] {
let p = &self.affine(); let p = &self;
let mut r: [u8; 32] = [0; 32]; let mut r: [u8; 32] = [0; 32];
let x_big = BigInt::parse_bytes(to_hex(&p.x).as_bytes(), 16).unwrap(); let x_big = BigInt::parse_bytes(to_hex(&p.x).as_bytes(), 16).unwrap();
let y_big = BigInt::parse_bytes(to_hex(&p.y).as_bytes(), 16).unwrap(); let y_big = BigInt::parse_bytes(to_hex(&p.y).as_bytes(), 16).unwrap();
@@ -227,43 +235,43 @@ pub fn decompress_point(bb: [u8; 32]) -> Result<Point, String> {
x = utils::modulus(&x, &Q); x = utils::modulus(&x, &Q);
let x_fr: Fr = Fr::from_str(&x.to_string()).unwrap(); let x_fr: Fr = Fr::from_str(&x.to_string()).unwrap();
let y_fr: Fr = Fr::from_str(&y.to_string()).unwrap(); let y_fr: Fr = Fr::from_str(&y.to_string()).unwrap();
Ok(Point::from_affine(PointAffine { x: x_fr, y: y_fr })) Ok(Point { x: x_fr, y: y_fr })
} }
//
// #[derive(Debug, Clone)] #[derive(Debug, Clone)]
// pub struct Signature { pub struct Signature {
// r_b8: Point, r_b8: Point,
// s: BigInt, s: BigInt,
// } }
//
// impl Signature { impl Signature {
// pub fn compress(&self) -> [u8; 64] { pub fn compress(&self) -> [u8; 64] {
// let mut b: Vec<u8> = Vec::new(); let mut b: Vec<u8> = Vec::new();
// b.append(&mut self.r_b8.compress().to_vec()); b.append(&mut self.r_b8.compress().to_vec());
// let (_, s_bytes) = self.s.to_bytes_le(); let (_, s_bytes) = self.s.to_bytes_le();
// let mut s_32bytes: [u8; 32] = [0; 32]; let mut s_32bytes: [u8; 32] = [0; 32];
// let len = min(s_bytes.len(), s_32bytes.len()); let len = min(s_bytes.len(), s_32bytes.len());
// s_32bytes[..len].copy_from_slice(&s_bytes[..len]); s_32bytes[..len].copy_from_slice(&s_bytes[..len]);
// b.append(&mut s_32bytes.to_vec()); b.append(&mut s_32bytes.to_vec());
// let mut r: [u8; 64] = [0; 64]; let mut r: [u8; 64] = [0; 64];
// r[..].copy_from_slice(&b[..]); r[..].copy_from_slice(&b[..]);
// r r
// } }
// } }
//
// pub fn decompress_signature(b: &[u8; 64]) -> Result<Signature, String> { pub fn decompress_signature(b: &[u8; 64]) -> Result<Signature, String> {
// let r_b8_bytes: [u8; 32] = *array_ref!(b[..32], 0, 32); let r_b8_bytes: [u8; 32] = *array_ref!(b[..32], 0, 32);
// let s: BigInt = BigInt::from_bytes_le(Sign::Plus, &b[32..]); let s: BigInt = BigInt::from_bytes_le(Sign::Plus, &b[32..]);
// let r_b8 = decompress_point(r_b8_bytes); let r_b8 = decompress_point(r_b8_bytes);
// match r_b8 { match r_b8 {
// Result::Err(err) => return Err(err.to_string()), Result::Err(err) => return Err(err.to_string()),
// Result::Ok(res) => Ok(Signature { Result::Ok(res) => Ok(Signature {
// r_b8: res.clone(), r_b8: res.clone(),
// s: s, s: s,
// }), }),
// } }
// } }
//
pub struct PrivateKey { pub struct PrivateKey {
key: BigInt, key: BigInt,
} }
@@ -304,35 +312,39 @@ impl PrivateKey {
// s: s, // s: s,
// }) // })
// } // }
// pub fn sign_poseidon(&self, msg: BigInt) -> Result<Signature, String> {
// // https://tools.ietf.org/html/rfc8032#section-5.1.6 pub fn sign_poseidon(&self, msg: BigInt) -> Result<Signature, String> {
// let mut hasher = Blake2b::new(); // https://tools.ietf.org/html/rfc8032#section-5.1.6
// let (_, sk_bytes) = self.key.to_bytes_be(); let mut hasher = Blake2b::new();
// hasher.input(sk_bytes); let (_, sk_bytes) = self.key.to_bytes_be();
// let mut h = hasher.result(); // h: hash(sk) hasher.input(sk_bytes);
// // s: h[32:64] let mut h = hasher.result(); // h: hash(sk)
// let s = GenericArray::<u8, generic_array::typenum::U32>::from_mut_slice(&mut h[32..64]); // s: h[32:64]
// let (_, msg_bytes) = msg.to_bytes_be(); let (_, msg_bytes) = msg.to_bytes_be();
// let r_bytes = utils::concatenate_arrays(s, &msg_bytes); let msgFr: Fr = Fr::from_str(&msg.to_string()).unwrap();
// let mut r = BigInt::from_bytes_be(Sign::Plus, &r_bytes[..]);
// r = utils::modulus(&r, &SUBORDER); let s = GenericArray::<u8, generic_array::typenum::U32>::from_mut_slice(&mut h[32..64]);
// let r8: Point = B8.mul_scalar(&r)?; let r_bytes = utils::concatenate_arrays(s, &msg_bytes);
// let a = &self.public()?; let mut r = BigInt::from_bytes_be(Sign::Plus, &r_bytes[..]);
// r = utils::modulus(&r, &SUBORDER);
// let hm_input = vec![r8.x.clone(), r8.y.clone(), a.x.clone(), a.y.clone(), msg]; let r8: Point = B8.mul_scalar(&r)?;
// let poseidon = Poseidon::new(); let a = &self.public()?;
// let hm = poseidon.hash(hm_input)?;
// let hm_input = vec![r8.x.clone(), r8.y.clone(), a.x.clone(), a.y.clone(), msgFr];
// let mut s = &self.key << 3; let poseidon = Poseidon::new();
// s = hm * s; let hm = poseidon.hash(hm_input)?;
// s = r + s;
// s = s % &SUBORDER.clone(); let mut s = &self.key << 3;
// let hmB = BigInt::parse_bytes(to_hex(&hm).as_bytes(), 16).unwrap();
// Ok(Signature { s = hmB * s;
// r_b8: r8.clone(), s = r + s;
// s: s, s = s % &SUBORDER.clone();
// })
// } Ok(Signature {
r_b8: r8.clone(),
s: s,
})
}
// pub fn sign_schnorr(&self, m: Vec<u8>) -> Result<(Point, BigInt), String> { // pub fn sign_schnorr(&self, m: Vec<u8>) -> Result<(Point, BigInt), String> {
// // random r // // random r
@@ -378,24 +390,24 @@ impl PrivateKey {
// Ok(sg.equals(right)) // Ok(sg.equals(right))
// } // }
// pub fn new_key() -> PrivateKey { pub fn new_key() -> PrivateKey {
// // https://tools.ietf.org/html/rfc8032#section-5.1.5 // https://tools.ietf.org/html/rfc8032#section-5.1.5
// let mut rng = rand::thread_rng(); let mut rng = rand6::thread_rng();
// let sk_raw = rng.gen_biguint(1024).to_bigint().unwrap(); let sk_raw = rng.gen_biguint(1024).to_bigint().unwrap();
//
// let mut hasher = Blake2b::new(); let mut hasher = Blake2b::new();
// let (_, sk_raw_bytes) = sk_raw.to_bytes_be(); let (_, sk_raw_bytes) = sk_raw.to_bytes_be();
// hasher.input(sk_raw_bytes); hasher.input(sk_raw_bytes);
// let mut h = hasher.result(); let mut h = hasher.result();
//
// h[0] = h[0] & 0xF8; h[0] = h[0] & 0xF8;
// h[31] = h[31] & 0x7F; h[31] = h[31] & 0x7F;
// h[31] = h[31] | 0x40; h[31] = h[31] | 0x40;
//
// let sk = BigInt::from_bytes_le(Sign::Plus, &h[..]); let sk = BigInt::from_bytes_le(Sign::Plus, &h[..]);
//
// PrivateKey { key: sk } PrivateKey { key: sk }
// } }
// pub fn verify_mimc(pk: Point, sig: Signature, msg: BigInt) -> bool { // pub fn verify_mimc(pk: Point, sig: Signature, msg: BigInt) -> bool {
// let hm_input = vec![ // let hm_input = vec![
@@ -423,32 +435,37 @@ impl PrivateKey {
// }; // };
// l.equals(r) // l.equals(r)
// } // }
// pub fn verify_poseidon(pk: Point, sig: Signature, msg: BigInt) -> bool {
// let hm_input = vec![ pub fn verify_poseidon(pk: Point, sig: Signature, msg: BigInt) -> bool {
// sig.r_b8.x.clone(), let (_, msg_bytes) = msg.to_bytes_be();
// sig.r_b8.y.clone(), let msgFr: Fr = Fr::from_str(&msg.to_string()).unwrap();
// pk.x.clone(), let hm_input = vec![
// pk.y.clone(), sig.r_b8.x.clone(),
// msg, sig.r_b8.y.clone(),
// ]; pk.x.clone(),
// let poseidon = Poseidon::new(); pk.y.clone(),
// let hm = match poseidon.hash(hm_input) { msgFr,
// Result::Err(_) => return false, ];
// Result::Ok(hm) => hm, let poseidon = Poseidon::new();
// }; let hm = match poseidon.hash(hm_input) {
// let l = match B8.mul_scalar(&sig.s) { Result::Err(_) => return false,
// Result::Err(_) => return false, Result::Ok(hm) => hm,
// Result::Ok(l) => l, };
// }; let l = match B8.mul_scalar(&sig.s) {
// let r = match sig Result::Err(_) => return false,
// .r_b8 Result::Ok(l) => l,
// .add(&pk.mul_scalar(&(8.to_bigint().unwrap() * hm)).unwrap()) };
// { let hmB = BigInt::parse_bytes(to_hex(&hm).as_bytes(), 16).unwrap();
// Result::Err(_) => return false, let r = match sig.r_b8.projective().add(
// Result::Ok(r) => r, &pk.mul_scalar(&(8.to_bigint().unwrap() * hmB))
// }; .unwrap()
// l.equals(r) .projective(),
// } ) {
Result::Err(_) => return false,
Result::Ok(r) => r,
};
l.equals(r.affine())
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
@@ -458,7 +475,7 @@ mod tests {
#[test] #[test]
fn test_add_same_point() { fn test_add_same_point() {
let p: Point = Point { let p: PointProjective = PointProjective {
x: Fr::from_str( x: Fr::from_str(
"17777552123799933955779906779655732241715742912184938656739573121738514868268", "17777552123799933955779906779655732241715742912184938656739573121738514868268",
) )
@@ -469,7 +486,7 @@ mod tests {
.unwrap(), .unwrap(),
z: Fr::one(), z: Fr::one(),
}; };
let q: Point = Point { let q: PointProjective = PointProjective {
x: Fr::from_str( x: Fr::from_str(
"17777552123799933955779906779655732241715742912184938656739573121738514868268", "17777552123799933955779906779655732241715742912184938656739573121738514868268",
) )
@@ -498,7 +515,7 @@ mod tests {
} }
#[test] #[test]
fn test_add_different_points() { fn test_add_different_points() {
let p: Point = Point { let p: PointProjective = PointProjective {
x: Fr::from_str( x: Fr::from_str(
"17777552123799933955779906779655732241715742912184938656739573121738514868268", "17777552123799933955779906779655732241715742912184938656739573121738514868268",
) )
@@ -509,7 +526,7 @@ mod tests {
.unwrap(), .unwrap(),
z: Fr::one(), z: Fr::one(),
}; };
let q: Point = Point { let q: PointProjective = PointProjective {
x: Fr::from_str( x: Fr::from_str(
"16540640123574156134436876038791482806971768689494387082833631921987005038935", "16540640123574156134436876038791482806971768689494387082833631921987005038935",
) )
@@ -548,11 +565,10 @@ mod tests {
"2626589144620713026669568689430873010625803728049924121243784502389097019475", "2626589144620713026669568689430873010625803728049924121243784502389097019475",
) )
.unwrap(), .unwrap(),
z: Fr::one(),
}; };
let res_m = p.mul_scalar(&3.to_bigint().unwrap()).unwrap().affine(); let res_m = p.mul_scalar(&3.to_bigint().unwrap()).unwrap();
let res_a = p.add(&p).unwrap(); let res_a = p.projective().add(&p.projective()).unwrap();
let res_a = res_a.add(&p).unwrap().affine(); let res_a = res_a.add(&p.projective()).unwrap().affine();
assert_eq!(res_m.x, res_a.x); assert_eq!(res_m.x, res_a.x);
assert_eq!( assert_eq!(
res_m.x, res_m.x,
@@ -574,7 +590,7 @@ mod tests {
10, 10,
) )
.unwrap(); .unwrap();
let res2 = p.mul_scalar(&n).unwrap().affine(); let res2 = p.mul_scalar(&n).unwrap();
assert_eq!( assert_eq!(
res2.x, res2.x,
Fr::from_str( Fr::from_str(
@@ -611,26 +627,26 @@ mod tests {
// assert_eq!(v, true); // assert_eq!(v, true);
// } // }
// #[test] #[test]
// fn test_new_key_sign_verify_poseidon_0() { fn test_new_key_sign_verify_poseidon_0() {
// let sk = new_key(); let sk = new_key();
// let pk = sk.public().unwrap(); let pk = sk.public().unwrap();
// let msg = 5.to_bigint().unwrap(); let msg = 5.to_bigint().unwrap();
// let sig = sk.sign_poseidon(msg.clone()).unwrap(); let sig = sk.sign_poseidon(msg.clone()).unwrap();
// let v = verify_poseidon(pk, sig, msg); let v = verify_poseidon(pk, sig, msg);
// assert_eq!(v, true); assert_eq!(v, true);
// } }
//
// #[test] #[test]
// fn test_new_key_sign_verify_poseidon_1() { fn test_new_key_sign_verify_poseidon_1() {
// let sk = new_key(); let sk = new_key();
// let pk = sk.public().unwrap(); let pk = sk.public().unwrap();
// let msg = BigInt::parse_bytes(b"123456789012345678901234567890", 10).unwrap(); let msg = BigInt::parse_bytes(b"123456789012345678901234567890", 10).unwrap();
// let sig = sk.sign_poseidon(msg.clone()).unwrap(); let sig = sk.sign_poseidon(msg.clone()).unwrap();
// let v = verify_poseidon(pk, sig, msg); let v = verify_poseidon(pk, sig, msg);
// assert_eq!(v, true); assert_eq!(v, true);
// } }
//
#[test] #[test]
fn test_point_compress_decompress() { fn test_point_compress_decompress() {
let p: Point = Point { let p: Point = Point {
@@ -642,7 +658,6 @@ mod tests {
"2626589144620713026669568689430873010625803728049924121243784502389097019475", "2626589144620713026669568689430873010625803728049924121243784502389097019475",
) )
.unwrap(), .unwrap(),
z: Fr::one(),
}; };
let p_comp = p.compress(); let p_comp = p.compress();
assert_eq!( assert_eq!(
@@ -668,7 +683,6 @@ mod tests {
.unwrap(); .unwrap();
let mut e_px_bytes: [u8; 32] = [0; 32]; let mut e_px_bytes: [u8; 32] = [0; 32];
e_px_bytes.copy_from_slice(&expected_px_raw); e_px_bytes.copy_from_slice(&expected_px_raw);
// let expected_px: BigInt = BigInt::from_bytes_le(Sign::Plus, &e_px_bytes);
let expected_px: Fr = let expected_px: Fr =
Fr::from_str(&BigInt::from_bytes_le(Sign::Plus, &e_px_bytes).to_string()).unwrap(); Fr::from_str(&BigInt::from_bytes_le(Sign::Plus, &e_px_bytes).to_string()).unwrap();
assert_eq!(&p.x, &expected_px); assert_eq!(&p.x, &expected_px);
@@ -688,7 +702,6 @@ mod tests {
.unwrap(); .unwrap();
let mut e_px_bytes: [u8; 32] = [0; 32]; let mut e_px_bytes: [u8; 32] = [0; 32];
e_px_bytes.copy_from_slice(&expected_px_raw); e_px_bytes.copy_from_slice(&expected_px_raw);
// let expected_px: BigInt = BigInt::from_bytes_le(Sign::Plus, &e_px_bytes);
let expected_px: Fr = let expected_px: Fr =
Fr::from_str(&BigInt::from_bytes_le(Sign::Plus, &e_px_bytes).to_string()).unwrap(); Fr::from_str(&BigInt::from_bytes_le(Sign::Plus, &e_px_bytes).to_string()).unwrap();
assert_eq!(&p.x, &expected_px); assert_eq!(&p.x, &expected_px);
@@ -697,7 +710,7 @@ mod tests {
#[test] #[test]
fn test_point_decompress_loop() { fn test_point_decompress_loop() {
for _ in 0..5 { for _ in 0..5 {
let random_bytes = rand::thread_rng().gen::<[u8; 32]>(); let random_bytes = rand6::thread_rng().gen::<[u8; 32]>();
let sk_raw: BigInt = BigInt::from_bytes_le(Sign::Plus, &random_bytes[..]); let sk_raw: BigInt = BigInt::from_bytes_le(Sign::Plus, &random_bytes[..]);
let mut hasher = Blake2b::new(); let mut hasher = Blake2b::new();
let (_, sk_raw_bytes) = sk_raw.to_bytes_be(); let (_, sk_raw_bytes) = sk_raw.to_bytes_be();
@@ -711,34 +724,33 @@ mod tests {
let sk = BigInt::from_bytes_le(Sign::Plus, &h[..]); let sk = BigInt::from_bytes_le(Sign::Plus, &h[..]);
let point = B8.mul_scalar(&sk).unwrap(); let point = B8.mul_scalar(&sk).unwrap();
let cmp_point = point.compress(); let cmp_point = point.compress();
let dcmp_point = decompress_point(cmp_point).unwrap().affine(); let dcmp_point = decompress_point(cmp_point).unwrap();
let point_affine = point.affine();
assert_eq!(&point_affine.x, &dcmp_point.x); assert_eq!(&point.x, &dcmp_point.x);
assert_eq!(&point_affine.y, &dcmp_point.y); assert_eq!(&point.y, &dcmp_point.y);
} }
} }
// #[test] #[test]
// fn test_signature_compress_decompress() { fn test_signature_compress_decompress() {
// let sk = new_key(); let sk = new_key();
// let pk = sk.public().unwrap(); let pk = sk.public().unwrap();
//
// for i in 0..5 { for i in 0..5 {
// let msg_raw = "123456".to_owned() + &i.to_string(); let msg_raw = "123456".to_owned() + &i.to_string();
// let msg = BigInt::parse_bytes(msg_raw.as_bytes(), 10).unwrap(); let msg = BigInt::parse_bytes(msg_raw.as_bytes(), 10).unwrap();
// let sig = sk.sign_mimc(msg.clone()).unwrap(); let sig = sk.sign_poseidon(msg.clone()).unwrap();
//
// let compressed_sig = sig.compress(); let compressed_sig = sig.compress();
// let decompressed_sig = decompress_signature(&compressed_sig).unwrap(); let decompressed_sig = decompress_signature(&compressed_sig).unwrap();
// assert_eq!(&sig.r_b8.x, &decompressed_sig.r_b8.x); assert_eq!(&sig.r_b8.x, &decompressed_sig.r_b8.x);
// assert_eq!(&sig.r_b8.y, &decompressed_sig.r_b8.y); assert_eq!(&sig.r_b8.y, &decompressed_sig.r_b8.y);
// assert_eq!(&sig.s, &decompressed_sig.s); assert_eq!(&sig.s, &decompressed_sig.s);
//
// let v = verify_mimc(pk.clone(), decompressed_sig, msg); let v = verify_poseidon(pk.clone(), decompressed_sig, msg);
// assert_eq!(v, true); assert_eq!(v, true);
// } }
// } }
// #[test] // #[test]
// fn test_schnorr_signature() { // fn test_schnorr_signature() {