mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-13 22:49:25 +00:00
Big-endian secq256k1 scalars
Also restores the prior, safer, Encryption::register function.
This commit is contained in:
@@ -48,8 +48,8 @@ pub(crate) use sealed::*;
|
|||||||
/// Wraps a message with a key to use for encryption in the future.
|
/// Wraps a message with a key to use for encryption in the future.
|
||||||
#[derive(Clone, PartialEq, Eq, Debug, Zeroize)]
|
#[derive(Clone, PartialEq, Eq, Debug, Zeroize)]
|
||||||
pub struct EncryptionKeyMessage<C: Ciphersuite, M: Message> {
|
pub struct EncryptionKeyMessage<C: Ciphersuite, M: Message> {
|
||||||
pub(crate) msg: M,
|
msg: M,
|
||||||
pub(crate) enc_key: C::G,
|
enc_key: C::G,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Doesn't impl ReadWrite so that doesn't need to be imported
|
// Doesn't impl ReadWrite so that doesn't need to be imported
|
||||||
@@ -348,12 +348,17 @@ impl<C: Ciphersuite> Decryption<C> {
|
|||||||
pub(crate) fn new(context: [u8; 32]) -> Self {
|
pub(crate) fn new(context: [u8; 32]) -> Self {
|
||||||
Self { context, enc_keys: HashMap::new() }
|
Self { context, enc_keys: HashMap::new() }
|
||||||
}
|
}
|
||||||
pub(crate) fn register(&mut self, participant: Participant, key: C::G) {
|
pub(crate) fn register<M: Message>(
|
||||||
|
&mut self,
|
||||||
|
participant: Participant,
|
||||||
|
msg: EncryptionKeyMessage<C, M>,
|
||||||
|
) -> M {
|
||||||
assert!(
|
assert!(
|
||||||
!self.enc_keys.contains_key(&participant),
|
!self.enc_keys.contains_key(&participant),
|
||||||
"Re-registering encryption key for a participant"
|
"Re-registering encryption key for a participant"
|
||||||
);
|
);
|
||||||
self.enc_keys.insert(participant, key);
|
self.enc_keys.insert(participant, msg.enc_key);
|
||||||
|
msg.msg
|
||||||
}
|
}
|
||||||
|
|
||||||
// Given a message, and the intended decryptor, and a proof for its key, decrypt the message.
|
// Given a message, and the intended decryptor, and a proof for its key, decrypt the message.
|
||||||
@@ -425,7 +430,12 @@ impl<C: Ciphersuite> Zeroize for Encryption<C> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<C: Ciphersuite> Encryption<C> {
|
impl<C: Ciphersuite> Encryption<C> {
|
||||||
pub(crate) fn new(context: [u8; 32], i: Participant, enc_key: Zeroizing<C::F>) -> Self {
|
pub(crate) fn new<R: RngCore + CryptoRng>(
|
||||||
|
context: [u8; 32],
|
||||||
|
i: Participant,
|
||||||
|
rng: &mut R,
|
||||||
|
) -> Self {
|
||||||
|
let enc_key = Zeroizing::new(C::random_nonzero_F(rng));
|
||||||
Self {
|
Self {
|
||||||
context,
|
context,
|
||||||
i,
|
i,
|
||||||
@@ -439,8 +449,12 @@ impl<C: Ciphersuite> Encryption<C> {
|
|||||||
EncryptionKeyMessage { msg, enc_key: self.enc_pub_key }
|
EncryptionKeyMessage { msg, enc_key: self.enc_pub_key }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn register(&mut self, participant: Participant, key: C::G) {
|
pub(crate) fn register<M: Message>(
|
||||||
self.decryption.register(participant, key)
|
&mut self,
|
||||||
|
participant: Participant,
|
||||||
|
msg: EncryptionKeyMessage<C, M>,
|
||||||
|
) -> M {
|
||||||
|
self.decryption.register(participant, msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn encrypt<R: RngCore + CryptoRng, E: Encryptable>(
|
pub(crate) fn encrypt<R: RngCore + CryptoRng, E: Encryptable>(
|
||||||
|
|||||||
@@ -133,8 +133,7 @@ impl<C: Ciphersuite> KeyGenMachine<C> {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// Additionally create an encryption mechanism to protect the secret shares
|
// Additionally create an encryption mechanism to protect the secret shares
|
||||||
let encryption =
|
let encryption = Encryption::new(self.context, self.params.i, rng);
|
||||||
Encryption::new(self.context, self.params.i, Zeroizing::new(C::random_nonzero_F(rng)));
|
|
||||||
|
|
||||||
// Step 4: Broadcast
|
// Step 4: Broadcast
|
||||||
let msg =
|
let msg =
|
||||||
@@ -178,7 +177,7 @@ fn polynomial<F: PrimeField + Zeroize>(
|
|||||||
// The encryption system also explicitly uses Zeroizing<M> so it can ensure anything being
|
// The encryption system also explicitly uses Zeroizing<M> so it can ensure anything being
|
||||||
// encrypted is within Zeroizing. Accordingly, internally having Zeroizing would be redundant.
|
// encrypted is within Zeroizing. Accordingly, internally having Zeroizing would be redundant.
|
||||||
#[derive(Clone, PartialEq, Eq)]
|
#[derive(Clone, PartialEq, Eq)]
|
||||||
pub struct SecretShare<F: PrimeField>(pub(crate) F::Repr);
|
pub struct SecretShare<F: PrimeField>(F::Repr);
|
||||||
impl<F: PrimeField> AsRef<[u8]> for SecretShare<F> {
|
impl<F: PrimeField> AsRef<[u8]> for SecretShare<F> {
|
||||||
fn as_ref(&self) -> &[u8] {
|
fn as_ref(&self) -> &[u8] {
|
||||||
self.0.as_ref()
|
self.0.as_ref()
|
||||||
@@ -262,8 +261,7 @@ impl<C: Ciphersuite> SecretShareMachine<C> {
|
|||||||
let mut commitments = HashMap::new();
|
let mut commitments = HashMap::new();
|
||||||
for l in (1 ..= self.params.n()).map(Participant) {
|
for l in (1 ..= self.params.n()).map(Participant) {
|
||||||
let Some(msg) = commitment_msgs.remove(&l) else { continue };
|
let Some(msg) = commitment_msgs.remove(&l) else { continue };
|
||||||
self.encryption.register(l, msg.enc_key);
|
let mut msg = self.encryption.register(l, msg);
|
||||||
let mut msg = msg.msg;
|
|
||||||
|
|
||||||
if msg.commitments.len() != self.params.t().into() {
|
if msg.commitments.len() != self.params.t().into() {
|
||||||
Err(FrostError::InvalidCommitments(l))?;
|
Err(FrostError::InvalidCommitments(l))?;
|
||||||
@@ -610,8 +608,7 @@ impl<C: Ciphersuite> AdditionalBlameMachine<C> {
|
|||||||
for i in 1 ..= n {
|
for i in 1 ..= n {
|
||||||
let i = Participant::new(i).unwrap();
|
let i = Participant::new(i).unwrap();
|
||||||
let Some(msg) = commitment_msgs.remove(&i) else { Err(DkgError::MissingParticipant(i))? };
|
let Some(msg) = commitment_msgs.remove(&i) else { Err(DkgError::MissingParticipant(i))? };
|
||||||
encryption.register(i, msg.enc_key);
|
commitments.insert(i, encryption.register(i, msg).commitments);
|
||||||
commitments.insert(i, msg.msg.commitments);
|
|
||||||
}
|
}
|
||||||
Ok(AdditionalBlameMachine(BlameMachine { commitments, encryption, result: None }))
|
Ok(AdditionalBlameMachine(BlameMachine { commitments, encryption, result: None }))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,5 +2,4 @@
|
|||||||
|
|
||||||
An implementation of the curve secp256k1 cycles with.
|
An implementation of the curve secp256k1 cycles with.
|
||||||
|
|
||||||
Scalars are encoded as little-endian and field elements are encoded as
|
Scalars and field elements are encoded in their big-endian formats.
|
||||||
big-endian.
|
|
||||||
|
|||||||
@@ -183,7 +183,7 @@ macro_rules! field {
|
|||||||
fn random(mut rng: impl RngCore) -> Self {
|
fn random(mut rng: impl RngCore) -> Self {
|
||||||
let mut bytes = [0; 64];
|
let mut bytes = [0; 64];
|
||||||
rng.fill_bytes(&mut bytes);
|
rng.fill_bytes(&mut bytes);
|
||||||
$FieldName(Residue::new(&reduce(U512::from_le_slice(bytes.as_ref()))))
|
$FieldName(Residue::new(&reduce(U512::from_be_slice(bytes.as_ref()))))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn square(&self) -> Self {
|
fn square(&self) -> Self {
|
||||||
@@ -230,12 +230,12 @@ macro_rules! field {
|
|||||||
const DELTA: Self = $FieldName(Residue::new(&U256::from_be_hex($DELTA)));
|
const DELTA: Self = $FieldName(Residue::new(&U256::from_be_hex($DELTA)));
|
||||||
|
|
||||||
fn from_repr(bytes: Self::Repr) -> CtOption<Self> {
|
fn from_repr(bytes: Self::Repr) -> CtOption<Self> {
|
||||||
let res = U256::from_le_slice(&bytes);
|
let res = U256::from_be_slice(&bytes);
|
||||||
CtOption::new($FieldName(Residue::new(&res)), res.ct_lt(&$MODULUS))
|
CtOption::new($FieldName(Residue::new(&res)), res.ct_lt(&$MODULUS))
|
||||||
}
|
}
|
||||||
fn to_repr(&self) -> Self::Repr {
|
fn to_repr(&self) -> Self::Repr {
|
||||||
let mut repr = [0; 32];
|
let mut repr = [0; 32];
|
||||||
repr.copy_from_slice(&self.0.retrieve().to_le_bytes());
|
repr.copy_from_slice(&self.0.retrieve().to_be_bytes());
|
||||||
repr
|
repr
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -248,7 +248,9 @@ macro_rules! field {
|
|||||||
type ReprBits = [u8; 32];
|
type ReprBits = [u8; 32];
|
||||||
|
|
||||||
fn to_le_bits(&self) -> FieldBits<Self::ReprBits> {
|
fn to_le_bits(&self) -> FieldBits<Self::ReprBits> {
|
||||||
self.to_repr().into()
|
let mut repr = [0; 32];
|
||||||
|
repr.copy_from_slice(&self.0.retrieve().to_le_bytes());
|
||||||
|
repr.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn char_le_bits() -> FieldBits<Self::ReprBits> {
|
fn char_le_bits() -> FieldBits<Self::ReprBits> {
|
||||||
|
|||||||
Reference in New Issue
Block a user