Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ToBytes and FromBytes for u128 #94

Merged
merged 2 commits into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
- #31 (ark-ec) Speedup point doubling on twisted edwards curves
- #35 (ark-ff) Implement `ToConstraintField` for `bool`
- #48 (ark-ff) Speedup `sqrt` on `QuadExtField`

- #94 (ark-ff) Implement `ToBytes` and `FromBytes` for `u128`

### Bug fixes
- #36 (ark-ec) In Short-Weierstrass curves, include an infinity bit in `ToConstraintField`.
Expand Down
39 changes: 39 additions & 0 deletions ff/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,29 @@ macro_rules! array_bytes {
Ok(res)
}
}

impl ToBytes for [u128; $N] {
#[inline]
fn write<W: Write>(&self, mut writer: W) -> IoResult<()> {
for num in self {
writer.write_all(&num.to_le_bytes())?;
}
Ok(())
}
}

impl FromBytes for [u128; $N] {
#[inline]
fn read<R: Read>(mut reader: R) -> IoResult<Self> {
let mut res = [0u128; $N];
for num in res.iter_mut() {
let mut bytes = [0u8; 16];
reader.read_exact(&mut bytes)?;
*num = u128::from_le_bytes(bytes);
}
Ok(res)
}
}
};
}

Expand Down Expand Up @@ -225,6 +248,22 @@ impl FromBytes for u64 {
}
}

impl ToBytes for u128 {
#[inline]
fn write<W: Write>(&self, mut writer: W) -> IoResult<()> {
writer.write_all(&self.to_le_bytes())
}
}

impl FromBytes for u128 {
#[inline]
fn read<R: Read>(mut reader: R) -> IoResult<Self> {
let mut bytes = [0u8; 16];
reader.read_exact(&mut bytes)?;
Ok(u128::from_le_bytes(bytes))
}
}

impl ToBytes for () {
#[inline]
fn write<W: Write>(&self, _writer: W) -> IoResult<()> {
Expand Down