-
Notifications
You must be signed in to change notification settings - Fork 48
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
Move Fp
and R1CS
to manta-crypto
#248
Closed
Closed
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
316b996
wip: remove all scale dep
GhostOfGauss 3bb5ec5
migrate Fp
GhostOfGauss bb0a7e6
move r1cs to manta-crypto
GhostOfGauss 7fc06f1
remove unused imports
GhostOfGauss 382b461
fix dep.s for tests
GhostOfGauss 8e4b4e5
fix: changelog
GhostOfGauss 3b4e37c
merge 'main' into 'feat/move_fp'
GhostOfGauss d1a1a40
wip: move R1CS
GhostOfGauss 4ef56d6
fmt
GhostOfGauss f6fe9ca
feat: finish moving FpVar out of manta-pay
GhostOfGauss a982d32
remove comments
GhostOfGauss f22aaec
Merge branch 'main' into feat/move_fp
bhgomes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
// Copyright 2019-2022 Manta Network. | ||
// This file is part of manta-rs. | ||
// | ||
// manta-rs is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// manta-rs is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with manta-rs. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
//! Codec Utilities | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The stuff in this file can go in |
||
|
||
use ark_std::io::{self, Error, ErrorKind}; | ||
use manta_util::codec::{Read, ReadExactError, Write}; | ||
|
||
pub use crate::arkworks::serialize::{ | ||
CanonicalDeserialize, CanonicalSerialize, SerializationError, | ||
}; | ||
|
||
/// Serialization Hook | ||
pub trait HasSerialization<'s>: 's { | ||
/// Serialize Type | ||
type Serialize: CanonicalSerialize + From<&'s Self>; | ||
} | ||
|
||
/// Deserialization Hook | ||
pub trait HasDeserialization: Sized { | ||
/// Deserialize Type | ||
type Deserialize: CanonicalDeserialize + Into<Self>; | ||
} | ||
|
||
/// Arkworks Reader | ||
pub struct ArkReader<R> | ||
where | ||
R: Read, | ||
{ | ||
/// Reader State | ||
state: Result<R, R::Error>, | ||
} | ||
|
||
impl<R> ArkReader<R> | ||
where | ||
R: Read, | ||
{ | ||
/// Builds a new [`ArkReader`] from `reader`. | ||
#[inline] | ||
pub fn new(reader: R) -> Self { | ||
Self { state: Ok(reader) } | ||
} | ||
|
||
/// Updates the internal reader state by performing the `f` computation. | ||
#[inline] | ||
fn update<T, F>(&mut self, f: F) -> Option<T> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we want this fn to be private? |
||
where | ||
F: FnOnce(&mut R) -> Result<T, R::Error>, | ||
{ | ||
if let Ok(reader) = self.state.as_mut() { | ||
match f(reader) { | ||
Ok(value) => return Some(value), | ||
Err(err) => self.state = Err(err), | ||
} | ||
} | ||
None | ||
} | ||
|
||
/// Returns the reader state back or an error if it occured during any [`Read`](io::Read) | ||
/// methods. | ||
#[inline] | ||
pub fn finish(self) -> Result<R, R::Error> { | ||
self.state | ||
} | ||
} | ||
|
||
impl<R> io::Read for ArkReader<R> | ||
where | ||
R: Read, | ||
{ | ||
#[inline] | ||
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error> { | ||
self.update(|reader| reader.read(buf)) | ||
.ok_or_else(|| Error::new(ErrorKind::Other, "Reading Error")) | ||
} | ||
|
||
#[inline] | ||
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error> { | ||
match self.update(|reader| match reader.read_exact(buf) { | ||
Ok(value) => Ok(Ok(value)), | ||
Err(ReadExactError::Read(err)) => Err(err), | ||
Err(ReadExactError::UnexpectedEnd(err)) => Ok(Err(err)), | ||
}) { | ||
Some(Ok(_)) => Ok(()), | ||
Some(Err(_)) => Err(Error::new( | ||
ErrorKind::UnexpectedEof, | ||
"Unexpected end of buffer.", | ||
)), | ||
_ => Err(Error::new(ErrorKind::Other, "Reading Error")), | ||
} | ||
} | ||
} | ||
|
||
/// Arkworks Writer | ||
pub struct ArkWriter<W> | ||
where | ||
W: Write, | ||
{ | ||
/// Writer State | ||
state: Result<W, W::Error>, | ||
} | ||
|
||
impl<W> ArkWriter<W> | ||
where | ||
W: Write, | ||
{ | ||
/// Builds a new [`ArkWriter`] from `writer`. | ||
#[inline] | ||
pub fn new(writer: W) -> Self { | ||
Self { state: Ok(writer) } | ||
} | ||
|
||
/// Updates the internal writer state by performing the `f` computation. | ||
#[inline] | ||
fn update<T, F>(&mut self, f: F) -> Option<T> | ||
where | ||
F: FnOnce(&mut W) -> Result<T, W::Error>, | ||
{ | ||
if let Ok(writer) = self.state.as_mut() { | ||
match f(writer) { | ||
Ok(value) => return Some(value), | ||
Err(err) => self.state = Err(err), | ||
} | ||
} | ||
None | ||
} | ||
|
||
/// Returns the writer state back or an error if it occured during any [`Write`](io::Write) | ||
/// methods. | ||
#[inline] | ||
pub fn finish(self) -> Result<W, W::Error> { | ||
self.state | ||
} | ||
} | ||
|
||
impl<W> io::Write for ArkWriter<W> | ||
where | ||
W: Write, | ||
{ | ||
#[inline] | ||
fn write(&mut self, mut buf: &[u8]) -> Result<usize, Error> { | ||
self.update(|writer| writer.write(&mut buf)) | ||
.ok_or_else(|| Error::new(ErrorKind::Other, "Writing Error")) | ||
} | ||
|
||
#[inline] | ||
fn flush(&mut self) -> Result<(), Error> { | ||
// NOTE: We can't necessarily do better than this for now, unfortunately. | ||
Ok(()) | ||
} | ||
|
||
#[inline] | ||
fn write_all(&mut self, mut buf: &[u8]) -> Result<(), Error> { | ||
self.update(|writer| writer.write(&mut buf)) | ||
.map(|_| ()) | ||
.ok_or_else(|| Error::new(ErrorKind::Other, "Writing Error")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in this case, cause we need it, lets re-export it too