-
Notifications
You must be signed in to change notification settings - Fork 25
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
Clean up Field
trait
#958
Merged
andyleiserson
merged 3 commits into
private-attribution:main
from
andyleiserson:field-cleanup
Feb 23, 2024
Merged
Clean up Field
trait
#958
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
|
@@ -2,14 +2,12 @@ use std::convert::Infallible; | |
|
||
use curve25519_dalek::scalar::Scalar; | ||
use generic_array::GenericArray; | ||
use hkdf::Hkdf; | ||
use sha2::Sha256; | ||
use typenum::U32; | ||
use typenum::{U2, U32}; | ||
|
||
use crate::{ | ||
ff::{boolean_array::BA256, Field, Serializable, U128Conversions}, | ||
ff::{boolean_array::BA256, Field, Serializable}, | ||
impl_shared_value_common, | ||
protocol::prss::FromRandomU128, | ||
protocol::prss::FromRandom, | ||
secret_sharing::{Block, FieldVectorizable, SharedValue, StdArray, Vectorizable}, | ||
}; | ||
|
||
|
@@ -193,43 +191,17 @@ impl Field for Fp25519 { | |
const ONE: Fp25519 = Fp25519::ONE; | ||
} | ||
|
||
// TODO(812): remove these impls | ||
impl U128Conversions for Fp25519 { | ||
///both following methods are based on hashing and do not allow to actually convert elements in Fp25519 | ||
/// from or into u128. However it is sufficient to generate random elements in Fp25519 | ||
fn as_u128(&self) -> u128 { | ||
unimplemented!() | ||
} | ||
impl FromRandom for Fp25519 { | ||
type SourceLength = U2; | ||
|
||
///PRSS uses `truncate_from function`, we need to expand the u128 using a PRG (Sha256) to a [u8;32] | ||
fn truncate_from<T: Into<u128>>(_v: T) -> Self { | ||
unimplemented!() | ||
} | ||
} | ||
|
||
impl FromRandomU128 for Fp25519 { | ||
fn from_random_u128(v: u128) -> Self { | ||
let hk = Hkdf::<Sha256>::new(None, &v.to_le_bytes()); | ||
let mut okm = [0u8; 32]; | ||
//error invalid length from expand only happens when okm is very large | ||
hk.expand(&[], &mut okm).unwrap(); | ||
Fp25519::deserialize_infallible(&okm.into()) | ||
} | ||
} | ||
|
||
///implement `TryFrom` since required by Field | ||
impl TryFrom<u128> for Fp25519 { | ||
type Error = crate::error::Error; | ||
|
||
fn try_from(v: u128) -> Result<Self, Self::Error> { | ||
let mut bits = [0u8; 32]; | ||
bits[..].copy_from_slice(&v.to_le_bytes()); | ||
let f: Fp25519 = Fp25519::ONE; | ||
f.serialize((&mut bits).into()); | ||
Ok(f) | ||
fn from_random(src: GenericArray<u128, Self::SourceLength>) -> Self { | ||
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. Would it make sense to use a u8 as a base and increase the SourceLength? I think even Aes uses a GenericArray over u8 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. yea we have #951 for that |
||
let mut src_bytes = [0u8; 32]; | ||
src_bytes[0..16].copy_from_slice(&src[0].to_le_bytes()); | ||
src_bytes[16..32].copy_from_slice(&src[1].to_le_bytes()); | ||
// Reduces mod order | ||
Fp25519::deserialize_infallible(<&GenericArray<u8, U32>>::from(&src_bytes)) | ||
} | ||
} | ||
// TODO(812): end remove impls | ||
|
||
#[cfg(all(test, unit_test))] | ||
mod test { | ||
|
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
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.
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.
so we don't need this awesome macro anymore? :)