You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

693 lines
21 KiB

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