Offer read_be
and write_be
utilities to simplify code, and for testing.
This commit is contained in:
parent
d67109d5d3
commit
2bfce59d8e
@ -617,8 +617,6 @@ pub mod g1 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn into_affine_unchecked(&self) -> Result<G1Affine, GroupDecodingError> {
|
fn into_affine_unchecked(&self) -> Result<G1Affine, GroupDecodingError> {
|
||||||
use byteorder::{ReadBytesExt, BigEndian};
|
|
||||||
|
|
||||||
// Create a copy of this representation.
|
// Create a copy of this representation.
|
||||||
let mut copy = self.0;
|
let mut copy = self.0;
|
||||||
|
|
||||||
@ -654,13 +652,8 @@ pub mod g1 {
|
|||||||
{
|
{
|
||||||
let mut reader = ©[..];
|
let mut reader = ©[..];
|
||||||
|
|
||||||
for b in x.0.iter_mut().rev() {
|
x.read_be(&mut reader).unwrap();
|
||||||
*b = reader.read_u64::<BigEndian>().unwrap();
|
y.read_be(&mut reader).unwrap();
|
||||||
}
|
|
||||||
|
|
||||||
for b in y.0.iter_mut().rev() {
|
|
||||||
*b = reader.read_u64::<BigEndian>().unwrap();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(G1Affine {
|
Ok(G1Affine {
|
||||||
@ -671,8 +664,6 @@ pub mod g1 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn from_affine(affine: G1Affine) -> Self {
|
fn from_affine(affine: G1Affine) -> Self {
|
||||||
use byteorder::{WriteBytesExt, BigEndian};
|
|
||||||
|
|
||||||
let mut res = Self::empty();
|
let mut res = Self::empty();
|
||||||
|
|
||||||
if affine.is_zero() {
|
if affine.is_zero() {
|
||||||
@ -682,13 +673,8 @@ pub mod g1 {
|
|||||||
} else {
|
} else {
|
||||||
let mut writer = &mut res.0[..];
|
let mut writer = &mut res.0[..];
|
||||||
|
|
||||||
for digit in affine.x.into_repr().as_ref().iter().rev() {
|
affine.x.into_repr().write_be(&mut writer).unwrap();
|
||||||
writer.write_u64::<BigEndian>(*digit).unwrap();
|
affine.y.into_repr().write_be(&mut writer).unwrap();
|
||||||
}
|
|
||||||
|
|
||||||
for digit in affine.y.into_repr().as_ref().iter().rev() {
|
|
||||||
writer.write_u64::<BigEndian>(*digit).unwrap();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
res
|
res
|
||||||
@ -733,8 +719,6 @@ pub mod g1 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn into_affine_unchecked(&self) -> Result<G1Affine, GroupDecodingError> {
|
fn into_affine_unchecked(&self) -> Result<G1Affine, GroupDecodingError> {
|
||||||
use byteorder::{ReadBytesExt, BigEndian};
|
|
||||||
|
|
||||||
// Create a copy of this representation.
|
// Create a copy of this representation.
|
||||||
let mut copy = self.0;
|
let mut copy = self.0;
|
||||||
|
|
||||||
@ -767,9 +751,7 @@ pub mod g1 {
|
|||||||
{
|
{
|
||||||
let mut reader = ©[..];
|
let mut reader = ©[..];
|
||||||
|
|
||||||
for b in x.0.iter_mut().rev() {
|
x.read_be(&mut reader).unwrap();
|
||||||
*b = reader.read_u64::<BigEndian>().unwrap();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Interpret as Fq element.
|
// Interpret as Fq element.
|
||||||
@ -804,8 +786,6 @@ pub mod g1 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn from_affine(affine: G1Affine) -> Self {
|
fn from_affine(affine: G1Affine) -> Self {
|
||||||
use byteorder::{WriteBytesExt, BigEndian};
|
|
||||||
|
|
||||||
let mut res = Self::empty();
|
let mut res = Self::empty();
|
||||||
|
|
||||||
if affine.is_zero() {
|
if affine.is_zero() {
|
||||||
@ -816,9 +796,7 @@ pub mod g1 {
|
|||||||
{
|
{
|
||||||
let mut writer = &mut res.0[..];
|
let mut writer = &mut res.0[..];
|
||||||
|
|
||||||
for digit in affine.x.into_repr().as_ref().iter().rev() {
|
affine.x.into_repr().write_be(&mut writer).unwrap();
|
||||||
writer.write_u64::<BigEndian>(*digit).unwrap();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut negy = affine.y;
|
let mut negy = affine.y;
|
||||||
@ -1187,8 +1165,6 @@ pub mod g2 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn into_affine_unchecked(&self) -> Result<G2Affine, GroupDecodingError> {
|
fn into_affine_unchecked(&self) -> Result<G2Affine, GroupDecodingError> {
|
||||||
use byteorder::{ReadBytesExt, BigEndian};
|
|
||||||
|
|
||||||
// Create a copy of this representation.
|
// Create a copy of this representation.
|
||||||
let mut copy = self.0;
|
let mut copy = self.0;
|
||||||
|
|
||||||
@ -1226,21 +1202,10 @@ pub mod g2 {
|
|||||||
{
|
{
|
||||||
let mut reader = ©[..];
|
let mut reader = ©[..];
|
||||||
|
|
||||||
for b in x_c1.0.iter_mut().rev() {
|
x_c1.read_be(&mut reader).unwrap();
|
||||||
*b = reader.read_u64::<BigEndian>().unwrap();
|
x_c0.read_be(&mut reader).unwrap();
|
||||||
}
|
y_c1.read_be(&mut reader).unwrap();
|
||||||
|
y_c0.read_be(&mut reader).unwrap();
|
||||||
for b in x_c0.0.iter_mut().rev() {
|
|
||||||
*b = reader.read_u64::<BigEndian>().unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
for b in y_c1.0.iter_mut().rev() {
|
|
||||||
*b = reader.read_u64::<BigEndian>().unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
for b in y_c0.0.iter_mut().rev() {
|
|
||||||
*b = reader.read_u64::<BigEndian>().unwrap();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(G2Affine {
|
Ok(G2Affine {
|
||||||
@ -1257,8 +1222,6 @@ pub mod g2 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn from_affine(affine: G2Affine) -> Self {
|
fn from_affine(affine: G2Affine) -> Self {
|
||||||
use byteorder::{WriteBytesExt, BigEndian};
|
|
||||||
|
|
||||||
let mut res = Self::empty();
|
let mut res = Self::empty();
|
||||||
|
|
||||||
if affine.is_zero() {
|
if affine.is_zero() {
|
||||||
@ -1268,21 +1231,10 @@ pub mod g2 {
|
|||||||
} else {
|
} else {
|
||||||
let mut writer = &mut res.0[..];
|
let mut writer = &mut res.0[..];
|
||||||
|
|
||||||
for digit in affine.x.c1.into_repr().as_ref().iter().rev() {
|
affine.x.c1.into_repr().write_be(&mut writer).unwrap();
|
||||||
writer.write_u64::<BigEndian>(*digit).unwrap();
|
affine.x.c0.into_repr().write_be(&mut writer).unwrap();
|
||||||
}
|
affine.y.c1.into_repr().write_be(&mut writer).unwrap();
|
||||||
|
affine.y.c0.into_repr().write_be(&mut writer).unwrap();
|
||||||
for digit in affine.x.c0.into_repr().as_ref().iter().rev() {
|
|
||||||
writer.write_u64::<BigEndian>(*digit).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
for digit in affine.y.c1.into_repr().as_ref().iter().rev() {
|
|
||||||
writer.write_u64::<BigEndian>(*digit).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
for digit in affine.y.c0.into_repr().as_ref().iter().rev() {
|
|
||||||
writer.write_u64::<BigEndian>(*digit).unwrap();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
res
|
res
|
||||||
@ -1327,8 +1279,6 @@ pub mod g2 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn into_affine_unchecked(&self) -> Result<G2Affine, GroupDecodingError> {
|
fn into_affine_unchecked(&self) -> Result<G2Affine, GroupDecodingError> {
|
||||||
use byteorder::{ReadBytesExt, BigEndian};
|
|
||||||
|
|
||||||
// Create a copy of this representation.
|
// Create a copy of this representation.
|
||||||
let mut copy = self.0;
|
let mut copy = self.0;
|
||||||
|
|
||||||
@ -1362,13 +1312,8 @@ pub mod g2 {
|
|||||||
{
|
{
|
||||||
let mut reader = ©[..];
|
let mut reader = ©[..];
|
||||||
|
|
||||||
for b in x_c1.0.iter_mut().rev() {
|
x_c1.read_be(&mut reader).unwrap();
|
||||||
*b = reader.read_u64::<BigEndian>().unwrap();
|
x_c0.read_be(&mut reader).unwrap();
|
||||||
}
|
|
||||||
|
|
||||||
for b in x_c0.0.iter_mut().rev() {
|
|
||||||
*b = reader.read_u64::<BigEndian>().unwrap();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Interpret as Fq element.
|
// Interpret as Fq element.
|
||||||
@ -1406,8 +1351,6 @@ pub mod g2 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn from_affine(affine: G2Affine) -> Self {
|
fn from_affine(affine: G2Affine) -> Self {
|
||||||
use byteorder::{WriteBytesExt, BigEndian};
|
|
||||||
|
|
||||||
let mut res = Self::empty();
|
let mut res = Self::empty();
|
||||||
|
|
||||||
if affine.is_zero() {
|
if affine.is_zero() {
|
||||||
@ -1418,13 +1361,8 @@ pub mod g2 {
|
|||||||
{
|
{
|
||||||
let mut writer = &mut res.0[..];
|
let mut writer = &mut res.0[..];
|
||||||
|
|
||||||
for digit in affine.x.c1.into_repr().as_ref().iter().rev() {
|
affine.x.c1.into_repr().write_be(&mut writer).unwrap();
|
||||||
writer.write_u64::<BigEndian>(*digit).unwrap();
|
affine.x.c0.into_repr().write_be(&mut writer).unwrap();
|
||||||
}
|
|
||||||
|
|
||||||
for digit in affine.x.c0.into_repr().as_ref().iter().rev() {
|
|
||||||
writer.write_u64::<BigEndian>(*digit).unwrap();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut negy = affine.y;
|
let mut negy = affine.y;
|
||||||
|
@ -221,6 +221,13 @@ impl AsRef<[u64]> for FqRepr {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl AsMut<[u64]> for FqRepr {
|
||||||
|
#[inline(always)]
|
||||||
|
fn as_mut(&mut self) -> &mut [u64] {
|
||||||
|
&mut self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<u64> for FqRepr {
|
impl From<u64> for FqRepr {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn from(val: u64) -> FqRepr {
|
fn from(val: u64) -> FqRepr {
|
||||||
|
@ -57,6 +57,13 @@ impl AsRef<[u64]> for FrRepr {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl AsMut<[u64]> for FrRepr {
|
||||||
|
#[inline(always)]
|
||||||
|
fn as_mut(&mut self) -> &mut [u64] {
|
||||||
|
&mut self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<u64> for FrRepr {
|
impl From<u64> for FrRepr {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn from(val: u64) -> FrRepr {
|
fn from(val: u64) -> FrRepr {
|
||||||
|
26
src/lib.rs
26
src/lib.rs
@ -29,6 +29,7 @@ pub mod wnaf;
|
|||||||
|
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
use std::io::{self, Read, Write};
|
||||||
|
|
||||||
/// An "engine" is a collection of types (fields, elliptic curve groups, etc.)
|
/// An "engine" is a collection of types (fields, elliptic curve groups, etc.)
|
||||||
/// with well-defined relationships. In particular, the G1/G2 curve groups are
|
/// with well-defined relationships. In particular, the G1/G2 curve groups are
|
||||||
@ -336,6 +337,7 @@ pub trait PrimeFieldRepr: Sized +
|
|||||||
'static +
|
'static +
|
||||||
rand::Rand +
|
rand::Rand +
|
||||||
AsRef<[u64]> +
|
AsRef<[u64]> +
|
||||||
|
AsMut<[u64]> +
|
||||||
From<u64>
|
From<u64>
|
||||||
{
|
{
|
||||||
/// Subtract another reprensetation from this one, returning the borrow bit.
|
/// Subtract another reprensetation from this one, returning the borrow bit.
|
||||||
@ -366,6 +368,30 @@ pub trait PrimeFieldRepr: Sized +
|
|||||||
/// Performs a leftwise bitshift of this number, effectively multiplying
|
/// Performs a leftwise bitshift of this number, effectively multiplying
|
||||||
/// it by 2. Overflow is ignored.
|
/// it by 2. Overflow is ignored.
|
||||||
fn mul2(&mut self);
|
fn mul2(&mut self);
|
||||||
|
|
||||||
|
/// Writes this `PrimeFieldRepr` as a big endian integer. Always writes
|
||||||
|
/// `(num_bits` / 8) bytes.
|
||||||
|
fn write_be<W: Write>(&self, mut writer: W) -> io::Result<()> {
|
||||||
|
use byteorder::{WriteBytesExt, BigEndian};
|
||||||
|
|
||||||
|
for digit in self.as_ref().iter().rev() {
|
||||||
|
writer.write_u64::<BigEndian>(*digit)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Reads a big endian integer occupying (`num_bits` / 8) bytes into this
|
||||||
|
/// representation.
|
||||||
|
fn read_be<R: Read>(&mut self, mut reader: R) -> io::Result<()> {
|
||||||
|
use byteorder::{ReadBytesExt, BigEndian};
|
||||||
|
|
||||||
|
for digit in self.as_mut().iter_mut().rev() {
|
||||||
|
*digit = reader.read_u64::<BigEndian>()?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An error that may occur when trying to interpret a `PrimeFieldRepr` as a
|
/// An error that may occur when trying to interpret a `PrimeFieldRepr` as a
|
||||||
|
Loading…
Reference in New Issue
Block a user