arnaucube
arnaucube/babyjubjub-arkPublic Self-hosted repository
babyjubjub-ark/src/lib.rs
src/lib.rsRaw
  1// WARNING still updating the code, it works, but is still in process the refactor.
  2
  3extern crate rand;
  4#[macro_use]
  5extern crate ff;
  6use ff::*;
  7
  8use poseidon_rs::Poseidon;
  9pub type Fr = poseidon_rs::Fr; // alias
 10
 11#[macro_use]
 12extern crate arrayref;
 13extern crate generic_array;
 14extern crate mimc_rs;
 15extern crate num;
 16extern crate num_bigint;
 17extern crate num_traits;
 18
 19extern crate rand6;
 20use rand6::Rng;
 21
 22use blake2::{Blake2b, Digest};
 23use mimc_rs::Mimc7;
 24use std::cmp::min;
 25
 26use num_bigint::{BigInt, RandBigInt, RandomBits, Sign, ToBigInt};
 27use num_traits::{One, Zero};
 28
 29use generic_array::GenericArray;
 30
 31pub mod utils;
 32
 33#[macro_use]
 34extern crate lazy_static;
 35
 36lazy_static! {
 37    static ref D: Fr = Fr::from_str("168696").unwrap();
 38    static ref D_big: BigInt = BigInt::parse_bytes(b"168696", 10).unwrap();
 39    static ref A: Fr = Fr::from_str("168700").unwrap();
 40    static ref A_big: BigInt = BigInt::parse_bytes(b"168700", 10).unwrap();
 41    pub static ref Q: BigInt = BigInt::parse_bytes(
 42        b"21888242871839275222246405745257275088548364400416034343698204186575808495617",10
 43    )
 44    .unwrap();
 45    // pub static ref Q: Fr = Fr::from_str(
 46    //     "21888242871839275222246405745257275088548364400416034343698204186575808495617"
 47    // )
 48    // .unwrap();
 49    static ref B8: Point = Point {
 50        x: Fr::from_str(
 51            "5299619240641551281634865583518297030282874472190772894086521144482721001553",
 52        )
 53        .unwrap(),
 54        y: Fr::from_str(
 55            "16950150798460657717958625567821834550301663161624707787222815936182638968203",
 56        )
 57        .unwrap(),
 58            // z: Fr::one(),
 59    };
 60    static ref ORDER: Fr = Fr::from_str(
 61        "21888242871839275222246405745257275088614511777268538073601725287587578984328",
 62    )
 63    .unwrap();
 64
 65    // SUBORDER = ORDER >> 3
 66    // static ref SUBORDER: Fr = Fr::from_str(
 67    //     "i2736030358979909402780800718157159386076813972158567259200215660948447373041",
 68    // )
 69    // .unwrap();
 70    static ref SUBORDER: BigInt = &BigInt::parse_bytes(
 71                b"21888242871839275222246405745257275088614511777268538073601725287587578984328",
 72                        10,
 73                            )
 74            .unwrap()
 75                    >> 3;
 76}
 77
 78#[derive(Clone, Debug)]
 79pub struct PointProjective {
 80    pub x: Fr,
 81    pub y: Fr,
 82    pub z: Fr,
 83}
 84
 85impl PointProjective {
 86    pub fn affine(&self) -> Point {
 87        if self.z.is_zero() {
 88            return Point {
 89                x: Fr::zero(),
 90                y: Fr::zero(),
 91            };
 92        }
 93
 94        let mut zinv = self.z.inverse().unwrap();
 95        let mut x = self.x;
 96        x.mul_assign(&zinv);
 97        let mut y = self.y;
 98        y.mul_assign(&zinv);
 99
100        Point {
101            x: x.clone(),
102            y: y.clone(),
103        }
104    }
105    pub fn add(&self, q: &PointProjective) -> Result<PointProjective, String> {
106        // add-2008-bbjlp https://hyperelliptic.org/EFD/g1p/auto-twisted-projective.html#doubling-dbl-2008-bbjlp
107        let mut a = self.z;
108        a.mul_assign(&q.z);
109        let mut b = a;
110        b.square();
111        let mut c = self.x;
112        c.mul_assign(&q.x);
113        let mut d = self.y;
114        d.mul_assign(&q.y);
115        let mut e = D.clone();
116        e.mul_assign(&c);
117        e.mul_assign(&d);
118        let mut f = b;
119        f.sub_assign(&e);
120        let mut g = b;
121        g.add_assign(&e);
122        let mut x1y1 = self.x;
123        x1y1.add_assign(&self.y);
124        let mut x2y2 = q.x;
125        x2y2.add_assign(&q.y);
126        let mut aux = x1y1;
127        aux.mul_assign(&x2y2);
128        aux.sub_assign(&c);
129        aux.sub_assign(&d);
130        let mut x3 = a;
131        x3.mul_assign(&f);
132        x3.mul_assign(&aux);
133        let mut ac = A.clone();
134        ac.mul_assign(&c);
135        let mut dac = d;
136        dac.sub_assign(&ac);
137        let mut y3 = a;
138        y3.mul_assign(&g);
139        y3.mul_assign(&dac);
140        let mut z3 = f;
141        z3.mul_assign(&g);
142
143        Ok(PointProjective {
144            x: x3.clone(),
145            y: y3.clone(),
146            z: z3.clone(),
147        })
148    }
149}
150
151#[derive(Clone, Debug)]
152pub struct Point {
153    pub x: Fr,
154    pub y: Fr,
155}
156
157impl Point {
158    pub fn projective(&self) -> PointProjective {
159        PointProjective {
160            x: self.x.clone(),
161            y: self.y.clone(),
162            z: Fr::one(),
163        }
164    }
165
166    pub fn mul_scalar(&self, n: &BigInt) -> Result<Point, String> {
167        let mut r: PointProjective = PointProjective {
168            x: Fr::zero(),
169            y: Fr::one(),
170            z: Fr::one(),
171        };
172        let mut exp: PointProjective = self.projective();
173        let (_, b) = n.to_bytes_le();
174        for i in 0..n.bits() {
175            if test_bit(&b, i) {
176                r = r.add(&exp)?;
177            }
178            exp = exp.add(&exp)?;
179        }
180        Ok(r.affine())
181    }
182
183    pub fn compress(&self) -> [u8; 32] {
184        let p = &self;
185        let mut r: [u8; 32] = [0; 32];
186        let x_big = BigInt::parse_bytes(to_hex(&p.x).as_bytes(), 16).unwrap();
187        let y_big = BigInt::parse_bytes(to_hex(&p.y).as_bytes(), 16).unwrap();
188        let (_, y_bytes) = y_big.to_bytes_le();
189        let len = min(y_bytes.len(), r.len());
190        r[..len].copy_from_slice(&y_bytes[..len]);
191        if &x_big > &(&Q.clone() >> 1) {
192            r[31] = r[31] | 0x80;
193        }
194        r
195    }
196
197    pub fn equals(&self, p: Point) -> bool {
198        if self.x == p.x && self.y == p.y {
199            return true;
200        }
201        false
202    }
203}
204
205pub fn test_bit(b: &Vec<u8>, i: usize) -> bool {
206    return b[i / 8] & (1 << (i % 8)) != 0;
207}
208
209pub fn decompress_point(bb: [u8; 32]) -> Result<Point, String> {
210    // https://tools.ietf.org/html/rfc8032#section-5.2.3
211    let mut sign: bool = false;
212    let mut b = bb.clone();
213    if b[31] & 0x80 != 0x00 {
214        sign = true;
215        b[31] = b[31] & 0x7F;
216    }
217    let y: BigInt = BigInt::from_bytes_le(Sign::Plus, &b[..]);
218    if y >= Q.clone() {
219        return Err("y outside the Finite Field over R".to_string());
220    }
221    let one: BigInt = One::one();
222
223    // x^2 = (1 - y^2) / (a - d * y^2) (mod p)
224    let den = utils::modinv(
225        &utils::modulus(
226            &(&A_big.clone() - utils::modulus(&(&D_big.clone() * (&y * &y)), &Q)),
227            &Q,
228        ),
229        &Q,
230    )?;
231    let mut x: BigInt = utils::modulus(&((one - utils::modulus(&(&y * &y), &Q)) * den), &Q);
232    x = utils::modsqrt(&x, &Q)?;
233
234    if sign && !(&x > &(&Q.clone() >> 1)) || (!sign && (&x > &(&Q.clone() >> 1))) {
235        x = x * -1.to_bigint().unwrap();
236    }
237    x = utils::modulus(&x, &Q);
238    let x_fr: Fr = Fr::from_str(&x.to_string()).unwrap();
239    let y_fr: Fr = Fr::from_str(&y.to_string()).unwrap();
240    Ok(Point { x: x_fr, y: y_fr })
241}
242
243#[derive(Debug, Clone)]
244pub struct Signature {
245    r_b8: Point,
246    s: BigInt,
247}
248
249impl Signature {
250    pub fn compress(&self) -> [u8; 64] {
251        let mut b: Vec<u8> = Vec::new();
252        b.append(&mut self.r_b8.compress().to_vec());
253        let (_, s_bytes) = self.s.to_bytes_le();
254        let mut s_32bytes: [u8; 32] = [0; 32];
255        let len = min(s_bytes.len(), s_32bytes.len());
256        s_32bytes[..len].copy_from_slice(&s_bytes[..len]);
257        b.append(&mut s_32bytes.to_vec());
258        let mut r: [u8; 64] = [0; 64];
259        r[..].copy_from_slice(&b[..]);
260        r
261    }
262}
263
264pub fn decompress_signature(b: &[u8; 64]) -> Result<Signature, String> {
265    let r_b8_bytes: [u8; 32] = *array_ref!(b[..32], 0, 32);
266    let s: BigInt = BigInt::from_bytes_le(Sign::Plus, &b[32..]);
267    let r_b8 = decompress_point(r_b8_bytes);
268    match r_b8 {
269        Result::Err(err) => return Err(err.to_string()),
270        Result::Ok(res) => Ok(Signature {
271            r_b8: res.clone(),
272            s: s,
273        }),
274    }
275}
276
277pub struct PrivateKey {
278    key: BigInt,
279}
280
281impl PrivateKey {
282    pub fn public(&self) -> Result<Point, String> {
283        // https://tools.ietf.org/html/rfc8032#section-5.1.5
284        let pk = B8.mul_scalar(&self.key)?;
285        Ok(pk.clone())
286    }
287
288    pub fn sign(&self, msg: BigInt) -> Result<Signature, String> {
289        if msg > Q.clone() {
290            return Err("msg outside the Finite Field".to_string());
291        }
292        // https://tools.ietf.org/html/rfc8032#section-5.1.6
293        let mut hasher = Blake2b::new();
294        let (_, sk_bytes) = self.key.to_bytes_be();
295        hasher.input(sk_bytes);
296        let mut h = hasher.result(); // h: hash(sk)
297                                     // s: h[32:64]
298        let (_, msg_bytes) = msg.to_bytes_be();
299        let msgFr: Fr = Fr::from_str(&msg.to_string()).unwrap();
300
301        let s = GenericArray::<u8, generic_array::typenum::U32>::from_mut_slice(&mut h[32..64]);
302        let r_bytes = utils::concatenate_arrays(s, &msg_bytes);
303        let mut r = BigInt::from_bytes_be(Sign::Plus, &r_bytes[..]);
304        r = utils::modulus(&r, &SUBORDER);
305        let r8: Point = B8.mul_scalar(&r)?;
306        let a = &self.public()?;
307
308        let hm_input = vec![r8.x.clone(), r8.y.clone(), a.x.clone(), a.y.clone(), msgFr];
309        let poseidon = Poseidon::new();
310        let hm = poseidon.hash(hm_input)?;
311
312        let mut s = &self.key << 3;
313        let hmB = BigInt::parse_bytes(to_hex(&hm).as_bytes(), 16).unwrap();
314        s = hmB * s;
315        s = r + s;
316        s = s % &SUBORDER.clone();
317
318        Ok(Signature {
319            r_b8: r8.clone(),
320            s: s,
321        })
322    }
323
324    pub fn sign_schnorr(&self, m: BigInt) -> Result<(Point, BigInt), String> {
325        // random r
326        let mut rng = rand6::thread_rng();
327        let k = rng.gen_biguint(1024).to_bigint().unwrap();
328
329        // r = k·G
330        let r = B8.mul_scalar(&k)?;
331
332        // h = H(x, r, m)
333        let pk = &self.public()?;
334        let h = schnorr_hash(&pk, m, &r)?;
335
336        // s= k+x·h
337        let s = k + &self.key * &h;
338        Ok((r, s))
339    }
340}
341
342pub fn schnorr_hash(pk: &Point, msg: BigInt, c: &Point) -> Result<BigInt, String> {
343    if msg > Q.clone() {
344        return Err("msg outside the Finite Field".to_string());
345    }
346    let msgFr: Fr = Fr::from_str(&msg.to_string()).unwrap();
347    let hm_input = vec![pk.x.clone(), pk.y.clone(), c.x.clone(), c.y.clone(), msgFr];
348    let poseidon = Poseidon::new();
349    let h = poseidon.hash(hm_input)?;
350    println!("h {:?}", h.to_string());
351    let hB = BigInt::parse_bytes(to_hex(&h).as_bytes(), 16).unwrap();
352    Ok(hB)
353}
354
355pub fn verify_schnorr(pk: Point, m: BigInt, r: Point, s: BigInt) -> Result<bool, String> {
356    // sG = s·G
357    let sg = B8.mul_scalar(&s)?;
358
359    // r + h · x
360    let h = schnorr_hash(&pk, m, &r)?;
361    let pk_h = pk.mul_scalar(&h)?;
362    let right = r.projective().add(&pk_h.projective())?;
363
364    Ok(sg.equals(right.affine()))
365}
366
367pub fn new_key() -> PrivateKey {
368    // https://tools.ietf.org/html/rfc8032#section-5.1.5
369    let mut rng = rand6::thread_rng();
370    let sk_raw = rng.gen_biguint(1024).to_bigint().unwrap();
371
372    let mut hasher = Blake2b::new();
373    let (_, sk_raw_bytes) = sk_raw.to_bytes_be();
374    hasher.input(sk_raw_bytes);
375    let mut h = hasher.result();
376
377    h[0] = h[0] & 0xF8;
378    h[31] = h[31] & 0x7F;
379    h[31] = h[31] | 0x40;
380
381    let sk = BigInt::from_bytes_le(Sign::Plus, &h[..]);
382
383    PrivateKey { key: sk }
384}
385
386pub fn verify(pk: Point, sig: Signature, msg: BigInt) -> bool {
387    if msg > Q.clone() {
388        return false;
389    }
390    let (_, msg_bytes) = msg.to_bytes_be();
391    let msgFr: Fr = Fr::from_str(&msg.to_string()).unwrap();
392    let hm_input = vec![
393        sig.r_b8.x.clone(),
394        sig.r_b8.y.clone(),
395        pk.x.clone(),
396        pk.y.clone(),
397        msgFr,
398    ];
399    let poseidon = Poseidon::new();
400    let hm = match poseidon.hash(hm_input) {
401        Result::Err(_) => return false,
402        Result::Ok(hm) => hm,
403    };
404    let l = match B8.mul_scalar(&sig.s) {
405        Result::Err(_) => return false,
406        Result::Ok(l) => l,
407    };
408    let hmB = BigInt::parse_bytes(to_hex(&hm).as_bytes(), 16).unwrap();
409    let r = match sig.r_b8.projective().add(
410        &pk.mul_scalar(&(8.to_bigint().unwrap() * hmB))
411            .unwrap()
412            .projective(),
413    ) {
414        Result::Err(_) => return false,
415        Result::Ok(r) => r,
416    };
417    l.equals(r.affine())
418}
419
420#[cfg(test)]
421mod tests {
422    use super::*;
423    extern crate rustc_hex;
424    use rustc_hex::{FromHex, ToHex};
425
426    #[test]
427    fn test_add_same_point() {
428        let p: PointProjective = PointProjective {
429            x: Fr::from_str(
430                "17777552123799933955779906779655732241715742912184938656739573121738514868268",
431            )
432            .unwrap(),
433            y: Fr::from_str(
434                "2626589144620713026669568689430873010625803728049924121243784502389097019475",
435            )
436            .unwrap(),
437            z: Fr::one(),
438        };
439        let q: PointProjective = PointProjective {
440            x: Fr::from_str(
441                "17777552123799933955779906779655732241715742912184938656739573121738514868268",
442            )
443            .unwrap(),
444            y: Fr::from_str(
445                "2626589144620713026669568689430873010625803728049924121243784502389097019475",
446            )
447            .unwrap(),
448            z: Fr::one(),
449        };
450        let res = p.add(&q).unwrap().affine();
451        assert_eq!(
452            res.x,
453            Fr::from_str(
454                "6890855772600357754907169075114257697580319025794532037257385534741338397365"
455            )
456            .unwrap()
457        );
458        assert_eq!(
459            res.y,
460            Fr::from_str(
461                "4338620300185947561074059802482547481416142213883829469920100239455078257889"
462            )
463            .unwrap()
464        );
465    }
466    #[test]
467    fn test_add_different_points() {
468        let p: PointProjective = PointProjective {
469            x: Fr::from_str(
470                "17777552123799933955779906779655732241715742912184938656739573121738514868268",
471            )
472            .unwrap(),
473            y: Fr::from_str(
474                "2626589144620713026669568689430873010625803728049924121243784502389097019475",
475            )
476            .unwrap(),
477            z: Fr::one(),
478        };
479        let q: PointProjective = PointProjective {
480            x: Fr::from_str(
481                "16540640123574156134436876038791482806971768689494387082833631921987005038935",
482            )
483            .unwrap(),
484            y: Fr::from_str(
485                "20819045374670962167435360035096875258406992893633759881276124905556507972311",
486            )
487            .unwrap(),
488            z: Fr::one(),
489        };
490        let res = p.add(&q).unwrap().affine();
491        assert_eq!(
492            res.x,
493            Fr::from_str(
494                "7916061937171219682591368294088513039687205273691143098332585753343424131937"
495            )
496            .unwrap()
497        );
498        assert_eq!(
499            res.y,
500            Fr::from_str(
501                "14035240266687799601661095864649209771790948434046947201833777492504781204499"
502            )
503            .unwrap()
504        );
505    }
506
507    #[test]
508    fn test_mul_scalar() {
509        let p: Point = Point {
510            x: Fr::from_str(
511                "17777552123799933955779906779655732241715742912184938656739573121738514868268",
512            )
513            .unwrap(),
514            y: Fr::from_str(
515                "2626589144620713026669568689430873010625803728049924121243784502389097019475",
516            )
517            .unwrap(),
518        };
519        let res_m = p.mul_scalar(&3.to_bigint().unwrap()).unwrap();
520        let res_a = p.projective().add(&p.projective()).unwrap();
521        let res_a = res_a.add(&p.projective()).unwrap().affine();
522        assert_eq!(res_m.x, res_a.x);
523        assert_eq!(
524            res_m.x,
525            Fr::from_str(
526                "19372461775513343691590086534037741906533799473648040012278229434133483800898"
527            )
528            .unwrap()
529        );
530        assert_eq!(
531            res_m.y,
532            Fr::from_str(
533                "9458658722007214007257525444427903161243386465067105737478306991484593958249"
534            )
535            .unwrap()
536        );
537
538        let n = BigInt::parse_bytes(
539            b"14035240266687799601661095864649209771790948434046947201833777492504781204499",
540            10,
541        )
542        .unwrap();
543        let res2 = p.mul_scalar(&n).unwrap();
544        assert_eq!(
545            res2.x,
546            Fr::from_str(
547                "17070357974431721403481313912716834497662307308519659060910483826664480189605"
548            )
549            .unwrap()
550        );
551        assert_eq!(
552            res2.y,
553            Fr::from_str(
554                "4014745322800118607127020275658861516666525056516280575712425373174125159339"
555            )
556            .unwrap()
557        );
558    }
559
560    // #[test]
561    // fn test_new_key_sign_verify_mimc_0() {
562    //     let sk = new_key();
563    //     let pk = sk.public().unwrap();
564    //     let msg = 5.to_bigint().unwrap();
565    //     let sig = sk.sign_mimc(msg.clone()).unwrap();
566    //     let v = verify_mimc(pk, sig, msg);
567    //     assert_eq!(v, true);
568    // }
569    //
570    // #[test]
571    // fn test_new_key_sign_verify_mimc_1() {
572    //     let sk = new_key();
573    //     let pk = sk.public().unwrap();
574    //     let msg = BigInt::parse_bytes(b"123456789012345678901234567890", 10).unwrap();
575    //     let sig = sk.sign_mimc(msg.clone()).unwrap();
576    //     let v = verify_mimc(pk, sig, msg);
577    //     assert_eq!(v, true);
578    // }
579
580    #[test]
581    fn test_new_key_sign_verify_0() {
582        let sk = new_key();
583        let pk = sk.public().unwrap();
584        let msg = 5.to_bigint().unwrap();
585        let sig = sk.sign(msg.clone()).unwrap();
586        let v = verify(pk, sig, msg);
587        assert_eq!(v, true);
588    }
589
590    #[test]
591    fn test_new_key_sign_verify_1() {
592        let sk = new_key();
593        let pk = sk.public().unwrap();
594        let msg = BigInt::parse_bytes(b"123456789012345678901234567890", 10).unwrap();
595        let sig = sk.sign(msg.clone()).unwrap();
596        let v = verify(pk, sig, msg);
597        assert_eq!(v, true);
598    }
599
600    #[test]
601    fn test_point_compress_decompress() {
602        let p: Point = Point {
603            x: Fr::from_str(
604                "17777552123799933955779906779655732241715742912184938656739573121738514868268",
605            )
606            .unwrap(),
607            y: Fr::from_str(
608                "2626589144620713026669568689430873010625803728049924121243784502389097019475",
609            )
610            .unwrap(),
611        };
612        let p_comp = p.compress();
613        assert_eq!(
614            p_comp[..].to_hex(),
615            "53b81ed5bffe9545b54016234682e7b2f699bd42a5e9eae27ff4051bc698ce85"
616        );
617        let p2 = decompress_point(p_comp).unwrap();
618        assert_eq!(p.x, p2.x);
619        assert_eq!(p.y, p2.y);
620    }
621
622    #[test]
623    fn test_point_decompress0() {
624        let y_bytes_raw = "b5328f8791d48f20bec6e481d91c7ada235f1facf22547901c18656b6c3e042f"
625            .from_hex()
626            .unwrap();
627        let mut y_bytes: [u8; 32] = [0; 32];
628        y_bytes.copy_from_slice(&y_bytes_raw);
629        let p = decompress_point(y_bytes).unwrap();
630
631        let expected_px_raw = "b86cc8d9c97daef0afe1a4753c54fb2d8a530dc74c7eee4e72b3fdf2496d2113"
632            .from_hex()
633            .unwrap();
634        let mut e_px_bytes: [u8; 32] = [0; 32];
635        e_px_bytes.copy_from_slice(&expected_px_raw);
636        let expected_px: Fr =
637            Fr::from_str(&BigInt::from_bytes_le(Sign::Plus, &e_px_bytes).to_string()).unwrap();
638        assert_eq!(&p.x, &expected_px);
639    }
640
641    #[test]
642    fn test_point_decompress1() {
643        let y_bytes_raw = "70552d3ff548e09266ded29b33ce75139672b062b02aa66bb0d9247ffecf1d0b"
644            .from_hex()
645            .unwrap();
646        let mut y_bytes: [u8; 32] = [0; 32];
647        y_bytes.copy_from_slice(&y_bytes_raw);
648        let p = decompress_point(y_bytes).unwrap();
649
650        let expected_px_raw = "30f1635ba7d56f9cb32c3ffbe6dca508a68c7f43936af11a23c785ce98cb3404"
651            .from_hex()
652            .unwrap();
653        let mut e_px_bytes: [u8; 32] = [0; 32];
654        e_px_bytes.copy_from_slice(&expected_px_raw);
655        let expected_px: Fr =
656            Fr::from_str(&BigInt::from_bytes_le(Sign::Plus, &e_px_bytes).to_string()).unwrap();
657        assert_eq!(&p.x, &expected_px);
658    }
659
660    #[test]
661    fn test_point_decompress_loop() {
662        for _ in 0..5 {
663            let random_bytes = rand6::thread_rng().gen::<[u8; 32]>();
664            let sk_raw: BigInt = BigInt::from_bytes_le(Sign::Plus, &random_bytes[..]);
665            let mut hasher = Blake2b::new();
666            let (_, sk_raw_bytes) = sk_raw.to_bytes_be();
667            hasher.input(sk_raw_bytes);
668            let mut h = hasher.result();
669
670            h[0] = h[0] & 0xF8;
671            h[31] = h[31] & 0x7F;
672            h[31] = h[31] | 0x40;
673
674            let sk = BigInt::from_bytes_le(Sign::Plus, &h[..]);
675            let point = B8.mul_scalar(&sk).unwrap();
676            let cmp_point = point.compress();
677            let dcmp_point = decompress_point(cmp_point).unwrap();
678
679            assert_eq!(&point.x, &dcmp_point.x);
680            assert_eq!(&point.y, &dcmp_point.y);
681        }
682    }
683
684    #[test]
685    fn test_signature_compress_decompress() {
686        let sk = new_key();
687        let pk = sk.public().unwrap();
688
689        for i in 0..5 {
690            let msg_raw = "123456".to_owned() + &i.to_string();
691            let msg = BigInt::parse_bytes(msg_raw.as_bytes(), 10).unwrap();
692            let sig = sk.sign(msg.clone()).unwrap();
693
694            let compressed_sig = sig.compress();
695            let decompressed_sig = decompress_signature(&compressed_sig).unwrap();
696            assert_eq!(&sig.r_b8.x, &decompressed_sig.r_b8.x);
697            assert_eq!(&sig.r_b8.y, &decompressed_sig.r_b8.y);
698            assert_eq!(&sig.s, &decompressed_sig.s);
699
700            let v = verify(pk.clone(), decompressed_sig, msg);
701            assert_eq!(v, true);
702        }
703    }
704
705    #[test]
706    fn test_schnorr_signature() {
707        let sk = new_key();
708        let pk = sk.public().unwrap();
709
710        let msg = BigInt::parse_bytes(b"123456789012345678901234567890", 10).unwrap();
711        let (s, e) = sk.sign_schnorr(msg.clone()).unwrap();
712        println!("s {:?}", s.x.to_string());
713        println!("s {:?}", s.y.to_string());
714        println!("e {:?}", e.to_string());
715        let verification = verify_schnorr(pk, msg, s, e).unwrap();
716        assert_eq!(true, verification);
717    }
718}