From c10293fbc4673e2a161866a626ac919df2ceda99 Mon Sep 17 00:00:00 2001 From: Nanak Nihal Singh Khalsa Date: Sun, 5 Feb 2023 13:45:55 -0500 Subject: [PATCH] to_dec_string() --- Cargo.toml | 1 - src/lib.rs | 18 +++++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 63f2cae..bfada65 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,7 +23,6 @@ arrayref = "0.3.5" lazy_static = "1.4.0" serde = { version = "1.0.152", features = ["derive"] } bytes = "1.4.0" -rust-gmp = "0.5.0" [dev-dependencies] criterion = "0.3" diff --git a/src/lib.rs b/src/lib.rs index 65d89ea..dbdf97e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,7 @@ // For LICENSE check https://github.com/arnaucube/babyjubjub-rs use ff::*; +use num::Num; use serde::{Serialize, ser::SerializeSeq}; use bytes::{BytesMut, BufMut}; use poseidon_rs::Poseidon; @@ -150,13 +151,24 @@ pub struct Point { pub y: Fr, } +pub trait ToDecimalString { + fn to_dec_string(&self) -> String; +} +impl ToDecimalString for Fr { + fn to_dec_string(&self) -> String { + let mut s = self.to_string(); + let hex_str = s[5..s.len()-1].to_string(); + BigInt::from_str_radix(&hex_str, 16).unwrap().to_string() + } +} + impl Serialize for Point { fn serialize(&self, serializer: S) -> Result where S: serde::Serializer { let mut seq = serializer.serialize_seq(Some(2))?; - seq.serialize_element(&self.x.to_string())?; - seq.serialize_element(&self.y.to_string())?; + seq.serialize_element(&self.x.to_dec_string())?; + seq.serialize_element(&self.y.to_dec_string())?; seq.end() } } @@ -390,7 +402,7 @@ pub fn decompress_signature(b: &[u8; 64]) -> Result { Result::Ok(res) => Ok(Signature { r_b8: res, s: s.to_string() }), } } -#[derive(Debug)] +#[derive(Debug, Serialize)] pub struct ElGamalEncryption { pub c1: Point, pub c2: Point