From 809d43625e717faa8cf5f09c9883aaa2d04b3b08 Mon Sep 17 00:00:00 2001 From: Nanak Nihal Singh Khalsa Date: Thu, 9 Feb 2023 13:01:45 -0500 Subject: [PATCH] added (no pun intended) sum feature --- src/lib.rs | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f0c27d8..b2159d5 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 std::iter::Sum; use num::Num; use serde::{Serialize, ser::SerializeSeq}; use bytes::{BytesMut, BufMut}; @@ -161,16 +162,44 @@ impl ToDecimalString for Fr { BigInt::from_str_radix(&hex_str, 16).unwrap().to_string() } } -pub trait FrToBigInt { +pub trait FrBigIntConversion { + fn from_bigint(bi: &BigInt) -> Fr; fn to_bigint(&self) -> BigInt; } -impl FrToBigInt for Fr { +impl FrBigIntConversion for Fr { + // Note: this could probably be more efficient by converting bigint to raw repr to Fr + fn from_bigint(bi: &BigInt) -> Fr { + Fr::from_str(&bi.to_string()).unwrap() + } fn to_bigint(&self) -> BigInt { BigInt::from_str(&self.to_dec_string()).unwrap() } } +pub struct FrWrapper { + pub fr: Fr +} +impl FrWrapper { + pub fn from_fr(fr: Fr) -> FrWrapper { + FrWrapper { fr: fr } + } +} + +impl Sum for FrWrapper { + fn sum>(iter: I) -> Self { + iter.reduce( + |frw1,frw2| { + let mut tmp = frw1.fr.clone(); + tmp.add_assign(&frw2.fr); + FrWrapper { fr: tmp } + } + + ).unwrap() + } +} + + impl Serialize for Point { fn serialize(&self, serializer: S) -> Result where