mirror of
https://github.com/Qortal/pirate-librustzcash.git
synced 2025-02-11 17:55:46 +00:00
no_std support for ff crate
This commit is contained in:
parent
1c9f5742fa
commit
ec2c304efd
@ -314,7 +314,7 @@ impl PrimeField for Fr {
|
|||||||
|
|
||||||
fn from_repr(repr: FrRepr) -> Result<Self, PrimeFieldDecodingError> {
|
fn from_repr(repr: FrRepr) -> Result<Self, PrimeFieldDecodingError> {
|
||||||
if repr.0[0] >= (MODULUS_R.0 as u64) {
|
if repr.0[0] >= (MODULUS_R.0 as u64) {
|
||||||
Err(PrimeFieldDecodingError::NotInField(format!("{}", repr)))
|
Err(PrimeFieldDecodingError::NotInField)
|
||||||
} else {
|
} else {
|
||||||
Ok(Fr(Wrapping(repr.0[0] as u32)))
|
Ok(Fr(Wrapping(repr.0[0] as u32)))
|
||||||
}
|
}
|
||||||
|
@ -11,14 +11,15 @@ repository = "https://github.com/ebfull/ff"
|
|||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
byteorder = "1"
|
byteorder = { version = "1", default-features = false }
|
||||||
ff_derive = { version = "0.4.0", path = "ff_derive", optional = true }
|
ff_derive = { version = "0.4.0", path = "ff_derive", optional = true }
|
||||||
rand_core = "0.5"
|
rand_core = { version = "0.5", default-features = false }
|
||||||
subtle = "2.2.1"
|
subtle = { version = "2.2.1", default-features = false, features = ["i128"] }
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = []
|
default = ["std"]
|
||||||
derive = ["ff_derive"]
|
derive = ["ff_derive"]
|
||||||
|
std = []
|
||||||
|
|
||||||
[badges]
|
[badges]
|
||||||
maintenance = { status = "actively-developed" }
|
maintenance = { status = "actively-developed" }
|
||||||
|
@ -113,9 +113,9 @@ fn prime_field_repr_impl(repr: &syn::Ident, limbs: usize) -> proc_macro2::TokenS
|
|||||||
#[derive(Copy, Clone, PartialEq, Eq, Default)]
|
#[derive(Copy, Clone, PartialEq, Eq, Default)]
|
||||||
pub struct #repr(pub [u64; #limbs]);
|
pub struct #repr(pub [u64; #limbs]);
|
||||||
|
|
||||||
impl ::std::fmt::Debug for #repr
|
impl ::core::fmt::Debug for #repr
|
||||||
{
|
{
|
||||||
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
|
||||||
write!(f, "0x")?;
|
write!(f, "0x")?;
|
||||||
for i in self.0.iter().rev() {
|
for i in self.0.iter().rev() {
|
||||||
write!(f, "{:016x}", *i)?;
|
write!(f, "{:016x}", *i)?;
|
||||||
@ -125,8 +125,8 @@ fn prime_field_repr_impl(repr: &syn::Ident, limbs: usize) -> proc_macro2::TokenS
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ::std::fmt::Display for #repr {
|
impl ::core::fmt::Display for #repr {
|
||||||
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
|
||||||
write!(f, "0x")?;
|
write!(f, "0x")?;
|
||||||
for i in self.0.iter().rev() {
|
for i in self.0.iter().rev() {
|
||||||
write!(f, "{:016x}", *i)?;
|
write!(f, "{:016x}", *i)?;
|
||||||
@ -153,7 +153,7 @@ fn prime_field_repr_impl(repr: &syn::Ident, limbs: usize) -> proc_macro2::TokenS
|
|||||||
impl From<u64> for #repr {
|
impl From<u64> for #repr {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn from(val: u64) -> #repr {
|
fn from(val: u64) -> #repr {
|
||||||
use std::default::Default;
|
use core::default::Default;
|
||||||
|
|
||||||
let mut repr = Self::default();
|
let mut repr = Self::default();
|
||||||
repr.0[0] = val;
|
repr.0[0] = val;
|
||||||
@ -163,22 +163,22 @@ fn prime_field_repr_impl(repr: &syn::Ident, limbs: usize) -> proc_macro2::TokenS
|
|||||||
|
|
||||||
impl Ord for #repr {
|
impl Ord for #repr {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn cmp(&self, other: &#repr) -> ::std::cmp::Ordering {
|
fn cmp(&self, other: &#repr) -> ::core::cmp::Ordering {
|
||||||
for (a, b) in self.0.iter().rev().zip(other.0.iter().rev()) {
|
for (a, b) in self.0.iter().rev().zip(other.0.iter().rev()) {
|
||||||
if a < b {
|
if a < b {
|
||||||
return ::std::cmp::Ordering::Less
|
return ::core::cmp::Ordering::Less
|
||||||
} else if a > b {
|
} else if a > b {
|
||||||
return ::std::cmp::Ordering::Greater
|
return ::core::cmp::Ordering::Greater
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
::std::cmp::Ordering::Equal
|
::core::cmp::Ordering::Equal
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialOrd for #repr {
|
impl PartialOrd for #repr {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn partial_cmp(&self, other: &#repr) -> Option<::std::cmp::Ordering> {
|
fn partial_cmp(&self, other: &#repr) -> Option<::core::cmp::Ordering> {
|
||||||
Some(self.cmp(other))
|
Some(self.cmp(other))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -209,7 +209,7 @@ fn prime_field_repr_impl(repr: &syn::Ident, limbs: usize) -> proc_macro2::TokenS
|
|||||||
while n >= 64 {
|
while n >= 64 {
|
||||||
let mut t = 0;
|
let mut t = 0;
|
||||||
for i in self.0.iter_mut().rev() {
|
for i in self.0.iter_mut().rev() {
|
||||||
::std::mem::swap(&mut t, i);
|
::core::mem::swap(&mut t, i);
|
||||||
}
|
}
|
||||||
n -= 64;
|
n -= 64;
|
||||||
}
|
}
|
||||||
@ -257,7 +257,7 @@ fn prime_field_repr_impl(repr: &syn::Ident, limbs: usize) -> proc_macro2::TokenS
|
|||||||
while n >= 64 {
|
while n >= 64 {
|
||||||
let mut t = 0;
|
let mut t = 0;
|
||||||
for i in &mut self.0 {
|
for i in &mut self.0 {
|
||||||
::std::mem::swap(&mut t, i);
|
::core::mem::swap(&mut t, i);
|
||||||
}
|
}
|
||||||
n -= 64;
|
n -= 64;
|
||||||
}
|
}
|
||||||
@ -767,15 +767,15 @@ fn prime_field_impl(
|
|||||||
let top_limb_index = limbs - 1;
|
let top_limb_index = limbs - 1;
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
impl ::std::marker::Copy for #name { }
|
impl ::core::marker::Copy for #name { }
|
||||||
|
|
||||||
impl ::std::clone::Clone for #name {
|
impl ::core::clone::Clone for #name {
|
||||||
fn clone(&self) -> #name {
|
fn clone(&self) -> #name {
|
||||||
*self
|
*self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ::std::default::Default for #name {
|
impl ::core::default::Default for #name {
|
||||||
fn default() -> #name {
|
fn default() -> #name {
|
||||||
#name::zero()
|
#name::zero()
|
||||||
}
|
}
|
||||||
@ -787,17 +787,17 @@ fn prime_field_impl(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ::std::cmp::PartialEq for #name {
|
impl ::core::cmp::PartialEq for #name {
|
||||||
fn eq(&self, other: &#name) -> bool {
|
fn eq(&self, other: &#name) -> bool {
|
||||||
self.0 == other.0
|
self.0 == other.0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ::std::cmp::Eq for #name { }
|
impl ::core::cmp::Eq for #name { }
|
||||||
|
|
||||||
impl ::std::fmt::Debug for #name
|
impl ::core::fmt::Debug for #name
|
||||||
{
|
{
|
||||||
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
|
||||||
write!(f, "{}({:?})", stringify!(#name), self.into_repr())
|
write!(f, "{}({:?})", stringify!(#name), self.into_repr())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -805,20 +805,20 @@ fn prime_field_impl(
|
|||||||
/// Elements are ordered lexicographically.
|
/// Elements are ordered lexicographically.
|
||||||
impl Ord for #name {
|
impl Ord for #name {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn cmp(&self, other: &#name) -> ::std::cmp::Ordering {
|
fn cmp(&self, other: &#name) -> ::core::cmp::Ordering {
|
||||||
self.into_repr().cmp(&other.into_repr())
|
self.into_repr().cmp(&other.into_repr())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialOrd for #name {
|
impl PartialOrd for #name {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn partial_cmp(&self, other: &#name) -> Option<::std::cmp::Ordering> {
|
fn partial_cmp(&self, other: &#name) -> Option<::core::cmp::Ordering> {
|
||||||
Some(self.cmp(other))
|
Some(self.cmp(other))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ::std::fmt::Display for #name {
|
impl ::core::fmt::Display for #name {
|
||||||
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
|
||||||
write!(f, "{}({})", stringify!(#name), self.into_repr())
|
write!(f, "{}({})", stringify!(#name), self.into_repr())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -839,7 +839,7 @@ fn prime_field_impl(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ::std::ops::Neg for #name {
|
impl ::core::ops::Neg for #name {
|
||||||
type Output = #name;
|
type Output = #name;
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
@ -854,7 +854,7 @@ fn prime_field_impl(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'r> ::std::ops::Add<&'r #name> for #name {
|
impl<'r> ::core::ops::Add<&'r #name> for #name {
|
||||||
type Output = #name;
|
type Output = #name;
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
@ -865,7 +865,7 @@ fn prime_field_impl(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ::std::ops::Add for #name {
|
impl ::core::ops::Add for #name {
|
||||||
type Output = #name;
|
type Output = #name;
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
@ -874,7 +874,7 @@ fn prime_field_impl(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'r> ::std::ops::AddAssign<&'r #name> for #name {
|
impl<'r> ::core::ops::AddAssign<&'r #name> for #name {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn add_assign(&mut self, other: &#name) {
|
fn add_assign(&mut self, other: &#name) {
|
||||||
// This cannot exceed the backing capacity.
|
// This cannot exceed the backing capacity.
|
||||||
@ -885,14 +885,14 @@ fn prime_field_impl(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ::std::ops::AddAssign for #name {
|
impl ::core::ops::AddAssign for #name {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn add_assign(&mut self, other: #name) {
|
fn add_assign(&mut self, other: #name) {
|
||||||
self.add_assign(&other);
|
self.add_assign(&other);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'r> ::std::ops::Sub<&'r #name> for #name {
|
impl<'r> ::core::ops::Sub<&'r #name> for #name {
|
||||||
type Output = #name;
|
type Output = #name;
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
@ -903,7 +903,7 @@ fn prime_field_impl(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ::std::ops::Sub for #name {
|
impl ::core::ops::Sub for #name {
|
||||||
type Output = #name;
|
type Output = #name;
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
@ -912,7 +912,7 @@ fn prime_field_impl(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'r> ::std::ops::SubAssign<&'r #name> for #name {
|
impl<'r> ::core::ops::SubAssign<&'r #name> for #name {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn sub_assign(&mut self, other: &#name) {
|
fn sub_assign(&mut self, other: &#name) {
|
||||||
// If `other` is larger than `self`, we'll need to add the modulus to self first.
|
// If `other` is larger than `self`, we'll need to add the modulus to self first.
|
||||||
@ -924,14 +924,14 @@ fn prime_field_impl(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ::std::ops::SubAssign for #name {
|
impl ::core::ops::SubAssign for #name {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn sub_assign(&mut self, other: #name) {
|
fn sub_assign(&mut self, other: #name) {
|
||||||
self.sub_assign(&other);
|
self.sub_assign(&other);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'r> ::std::ops::Mul<&'r #name> for #name {
|
impl<'r> ::core::ops::Mul<&'r #name> for #name {
|
||||||
type Output = #name;
|
type Output = #name;
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
@ -942,7 +942,7 @@ fn prime_field_impl(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ::std::ops::Mul for #name {
|
impl ::core::ops::Mul for #name {
|
||||||
type Output = #name;
|
type Output = #name;
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
@ -951,7 +951,7 @@ fn prime_field_impl(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'r> ::std::ops::MulAssign<&'r #name> for #name {
|
impl<'r> ::core::ops::MulAssign<&'r #name> for #name {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn mul_assign(&mut self, other: &#name)
|
fn mul_assign(&mut self, other: &#name)
|
||||||
{
|
{
|
||||||
@ -959,7 +959,7 @@ fn prime_field_impl(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ::std::ops::MulAssign for #name {
|
impl ::core::ops::MulAssign for #name {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn mul_assign(&mut self, other: #name)
|
fn mul_assign(&mut self, other: #name)
|
||||||
{
|
{
|
||||||
@ -977,7 +977,7 @@ fn prime_field_impl(
|
|||||||
|
|
||||||
Ok(r)
|
Ok(r)
|
||||||
} else {
|
} else {
|
||||||
Err(PrimeFieldDecodingError::NotInField(format!("{}", r.0)))
|
Err(PrimeFieldDecodingError::NotInField)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,17 +1,22 @@
|
|||||||
//! This crate provides traits for working with finite fields.
|
//! This crate provides traits for working with finite fields.
|
||||||
|
|
||||||
// Catch documentation errors caused by code changes.
|
// Catch documentation errors caused by code changes.
|
||||||
|
#![no_std]
|
||||||
#![deny(intra_doc_link_resolution_failure)]
|
#![deny(intra_doc_link_resolution_failure)]
|
||||||
#![allow(unused_imports)]
|
#![allow(unused_imports)]
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
#[macro_use]
|
||||||
|
extern crate std;
|
||||||
|
|
||||||
#[cfg(feature = "derive")]
|
#[cfg(feature = "derive")]
|
||||||
pub use ff_derive::*;
|
pub use ff_derive::*;
|
||||||
|
|
||||||
|
use core::fmt;
|
||||||
|
use core::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};
|
||||||
use rand_core::RngCore;
|
use rand_core::RngCore;
|
||||||
use std::error::Error;
|
#[cfg(feature = "std")]
|
||||||
use std::fmt;
|
|
||||||
use std::io::{self, Read, Write};
|
use std::io::{self, Read, Write};
|
||||||
use std::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};
|
|
||||||
use subtle::{ConditionallySelectable, CtOption};
|
use subtle::{ConditionallySelectable, CtOption};
|
||||||
|
|
||||||
/// This trait represents an element of a field.
|
/// This trait represents an element of a field.
|
||||||
@ -150,6 +155,7 @@ pub trait PrimeFieldRepr:
|
|||||||
fn shl(&mut self, amt: u32);
|
fn shl(&mut self, amt: u32);
|
||||||
|
|
||||||
/// Writes this `PrimeFieldRepr` as a big endian integer.
|
/// Writes this `PrimeFieldRepr` as a big endian integer.
|
||||||
|
#[cfg(feature = "std")]
|
||||||
fn write_be<W: Write>(&self, mut writer: W) -> io::Result<()> {
|
fn write_be<W: Write>(&self, mut writer: W) -> io::Result<()> {
|
||||||
use byteorder::{BigEndian, WriteBytesExt};
|
use byteorder::{BigEndian, WriteBytesExt};
|
||||||
|
|
||||||
@ -161,6 +167,7 @@ pub trait PrimeFieldRepr:
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Reads a big endian integer into this representation.
|
/// Reads a big endian integer into this representation.
|
||||||
|
#[cfg(feature = "std")]
|
||||||
fn read_be<R: Read>(&mut self, mut reader: R) -> io::Result<()> {
|
fn read_be<R: Read>(&mut self, mut reader: R) -> io::Result<()> {
|
||||||
use byteorder::{BigEndian, ReadBytesExt};
|
use byteorder::{BigEndian, ReadBytesExt};
|
||||||
|
|
||||||
@ -172,6 +179,7 @@ pub trait PrimeFieldRepr:
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Writes this `PrimeFieldRepr` as a little endian integer.
|
/// Writes this `PrimeFieldRepr` as a little endian integer.
|
||||||
|
#[cfg(feature = "std")]
|
||||||
fn write_le<W: Write>(&self, mut writer: W) -> io::Result<()> {
|
fn write_le<W: Write>(&self, mut writer: W) -> io::Result<()> {
|
||||||
use byteorder::{LittleEndian, WriteBytesExt};
|
use byteorder::{LittleEndian, WriteBytesExt};
|
||||||
|
|
||||||
@ -183,6 +191,7 @@ pub trait PrimeFieldRepr:
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Reads a little endian integer into this representation.
|
/// Reads a little endian integer into this representation.
|
||||||
|
#[cfg(feature = "std")]
|
||||||
fn read_le<R: Read>(&mut self, mut reader: R) -> io::Result<()> {
|
fn read_le<R: Read>(&mut self, mut reader: R) -> io::Result<()> {
|
||||||
use byteorder::{LittleEndian, ReadBytesExt};
|
use byteorder::{LittleEndian, ReadBytesExt};
|
||||||
|
|
||||||
@ -199,13 +208,14 @@ pub trait PrimeFieldRepr:
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum PrimeFieldDecodingError {
|
pub enum PrimeFieldDecodingError {
|
||||||
/// The encoded value is not in the field
|
/// The encoded value is not in the field
|
||||||
NotInField(String),
|
NotInField,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Error for PrimeFieldDecodingError {
|
#[cfg(feature = "std")]
|
||||||
|
impl std::error::Error for PrimeFieldDecodingError {
|
||||||
fn description(&self) -> &str {
|
fn description(&self) -> &str {
|
||||||
match *self {
|
match *self {
|
||||||
PrimeFieldDecodingError::NotInField(..) => "not an element of the field",
|
PrimeFieldDecodingError::NotInField => "not an element of the field",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -213,9 +223,7 @@ impl Error for PrimeFieldDecodingError {
|
|||||||
impl fmt::Display for PrimeFieldDecodingError {
|
impl fmt::Display for PrimeFieldDecodingError {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
||||||
match *self {
|
match *self {
|
||||||
PrimeFieldDecodingError::NotInField(ref repr) => {
|
PrimeFieldDecodingError::NotInField => write!(f, "not an element of the field"),
|
||||||
write!(f, "{} is not an element of the field", repr)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -454,7 +454,7 @@ impl PrimeField for Fs {
|
|||||||
|
|
||||||
Ok(r)
|
Ok(r)
|
||||||
} else {
|
} else {
|
||||||
Err(PrimeFieldDecodingError::NotInField(format!("{}", r.0)))
|
Err(PrimeFieldDecodingError::NotInField)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user