Skip to content

Commit

Permalink
Implement FromStr for all the fields
Browse files Browse the repository at this point in the history
  • Loading branch information
cronokirby committed Jan 30, 2024
1 parent 6617e42 commit 2835d70
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 9 deletions.
1 change: 1 addition & 0 deletions proptest-regressions/fields/fr/arkworks.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
# It is recommended to check this file in to source control so that
# everyone who runs the test benefits from these saved cases.
cc 77fe8cd73668f517c0ddfd13999e303d1121ad67db8c896f6d7f33195288d791 # shrinks to a = Fr(0x0000000000000001000000000000000000000000000000000000000000000000)
cc 82d116306befe522f9442271ef7cf8675f016da4d94c21e2fb631fd544187bf6 # shrinks to a = Fr(0x0000000000000000000000000000000000000000000000000000000000000000)
29 changes: 26 additions & 3 deletions src/fields/fp/arkworks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,23 @@ impl ark_std::rand::distributions::Distribution<Fp> for ark_std::rand::distribut
impl FromStr for Fp {
type Err = ();

fn from_str(_s: &str) -> Result<Self, Self::Err> {
unimplemented!()
fn from_str(s: &str) -> Result<Self, Self::Err> {
// CANDO: a more efficient method accumulating into 64 bits first.
let mut acc = Self::zero();

let ten = Self::from(10u8);

for c in s.chars() {
match c.to_digit(10) {
Some(c) => {
acc = ten * acc + Self::from(u64::from(c));
}
None => {
return Err(());
}
}
}
Ok(acc)
}
}

Expand Down Expand Up @@ -354,7 +369,7 @@ impl Display for Fp {
mod tests {
use super::*;
extern crate alloc;
use alloc::vec::Vec;
use alloc::{format, vec::Vec};
use ark_bls12_377::Fq as ArkFp;
use proptest::prelude::*;

Expand Down Expand Up @@ -587,6 +602,14 @@ mod tests {
}
}

proptest! {
#[test]
fn test_from_str(a in arb_fp()) {
let x = <Fp as PrimeField>::BigInt::from(a);
assert_eq!(Ok(a), Fp::from_str(&format!("{}", x)));
}
}

#[test]
fn test_from_le_bytes_mod_order_examples() {
let p_plus_1_bytes: [u8; 48] = [
Expand Down
29 changes: 26 additions & 3 deletions src/fields/fq/arkworks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,23 @@ impl Display for Fq {
impl FromStr for Fq {
type Err = ();

fn from_str(_s: &str) -> Result<Self, Self::Err> {
unimplemented!()
fn from_str(s: &str) -> Result<Self, Self::Err> {
// CANDO: a more efficient method accumulating into 64 bits first.
let mut acc = Self::zero();

let ten = Self::from(10u8);

for c in s.chars() {
match c.to_digit(10) {
Some(c) => {
acc = ten * acc + Self::from(u64::from(c));
}
None => {
return Err(());
}
}
}
Ok(acc)
}
}

Expand Down Expand Up @@ -354,7 +369,7 @@ impl From<BigInt<4>> for Fq {
mod tests {
use super::*;
extern crate alloc;
use alloc::vec::Vec;
use alloc::{format, vec::Vec};
use ark_bls12_377::Fr as ArkFq;
use proptest::prelude::*;

Expand Down Expand Up @@ -585,6 +600,14 @@ mod tests {
}
}

proptest! {
#[test]
fn test_from_str(a in arb_fq()) {
let x = <Fq as PrimeField>::BigInt::from(a);
assert_eq!(Ok(a), Fq::from_str(&format!("{}", x)));
}
}

#[test]
fn test_from_le_bytes_mod_order_examples() {
let p_plus_1_bytes: [u8; 32] = [
Expand Down
30 changes: 27 additions & 3 deletions src/fields/fr/arkworks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,24 @@ impl Display for Fr {
impl FromStr for Fr {
type Err = ();

fn from_str(_s: &str) -> Result<Self, Self::Err> {
unimplemented!()
fn from_str(s: &str) -> Result<Self, Self::Err> {
ark_std::dbg!(&s);
// CANDO: a more efficient method accumulating into 64 bits first.
let mut acc = Self::zero();

let ten = Self::from(10u8);

for c in s.chars() {
match c.to_digit(10) {
Some(c) => {
acc = ten * acc + Self::from(u64::from(c));
}
None => {
return Err(());
}
}
}
Ok(acc)
}
}

Expand Down Expand Up @@ -356,7 +372,7 @@ impl From<BigInt<4>> for Fr {
mod tests {
use super::*;
extern crate alloc;
use alloc::vec::Vec;
use alloc::{format, vec::Vec};
use proptest::prelude::*;

prop_compose! {
Expand Down Expand Up @@ -570,6 +586,14 @@ mod tests {
}
}

proptest! {
#[test]
fn test_from_str(a in arb_fr()) {
let x = <Fr as PrimeField>::BigInt::from(a);
assert_eq!(Ok(a), Fr::from_str(&format!("{}", x)));
}
}

#[test]
fn test_from_le_bytes_mod_order_examples() {
let p_plus_1_bytes: [u8; 32] = [
Expand Down

0 comments on commit 2835d70

Please sign in to comment.