Skip to content

Commit

Permalink
Add ToBytes and FromBytes for u128 (#94)
Browse files Browse the repository at this point in the history
* add the to and from bytes for u128

* update the CHANGELOG
  • Loading branch information
weikengchen authored Nov 24, 2020
1 parent 2803cf1 commit b58cc70
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
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

0 comments on commit b58cc70

Please sign in to comment.