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

Always derive sk_prf #2189

Draft
wants to merge 5 commits into
base: staging
Choose a base branch
from
Draft
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
40 changes: 18 additions & 22 deletions circuit/account/src/compute_key/equal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ impl<A: Aleo> Equal<Self> for ComputeKey<A> {
// Determine if this operation is constant or variable.
match self.is_constant() && other.is_constant() {
true => Boolean::constant(self.eject_value() == other.eject_value()),
false => {
self.pk_sig.is_equal(other.pk_sig())
& self.pr_sig.is_equal(other.pr_sig())
& self.sk_prf.is_equal(other.sk_prf())
}
false => self.pk_sig.is_equal(other.pk_sig()) & self.pr_sig.is_equal(other.pr_sig()),
}
}

Expand All @@ -50,7 +46,7 @@ impl<A: Aleo> Metrics<dyn Equal<ComputeKey<A>, Output = Boolean<A>>> for Compute
fn count(case: &Self::Case) -> Count {
match case.0.is_constant() && case.1.is_constant() {
true => Count::is(0, 0, 0, 0),
false => Count::is(0, 0, 14, 19),
false => Count::is(0, 0, 11, 19),
}
}
}
Expand Down Expand Up @@ -148,28 +144,28 @@ mod tests {
let mut rng = TestRng::default();

check_is_equal(Mode::Constant, Mode::Constant, 0, 0, 0, 0, &mut rng);
check_is_equal(Mode::Constant, Mode::Public, 0, 0, 14, 14, &mut rng);
check_is_equal(Mode::Constant, Mode::Private, 0, 0, 14, 14, &mut rng);
check_is_equal(Mode::Public, Mode::Constant, 0, 0, 14, 14, &mut rng);
check_is_equal(Mode::Private, Mode::Constant, 0, 0, 14, 14, &mut rng);
check_is_equal(Mode::Public, Mode::Public, 0, 0, 14, 14, &mut rng);
check_is_equal(Mode::Public, Mode::Private, 0, 0, 14, 14, &mut rng);
check_is_equal(Mode::Private, Mode::Public, 0, 0, 14, 14, &mut rng);
check_is_equal(Mode::Private, Mode::Private, 0, 0, 14, 14, &mut rng);
check_is_equal(Mode::Constant, Mode::Public, 0, 0, 11, 11, &mut rng);
check_is_equal(Mode::Constant, Mode::Private, 0, 0, 11, 11, &mut rng);
check_is_equal(Mode::Public, Mode::Constant, 0, 0, 11, 11, &mut rng);
check_is_equal(Mode::Private, Mode::Constant, 0, 0, 11, 11, &mut rng);
check_is_equal(Mode::Public, Mode::Public, 0, 0, 11, 11, &mut rng);
check_is_equal(Mode::Public, Mode::Private, 0, 0, 11, 11, &mut rng);
check_is_equal(Mode::Private, Mode::Public, 0, 0, 11, 11, &mut rng);
check_is_equal(Mode::Private, Mode::Private, 0, 0, 11, 11, &mut rng);
}

#[test]
fn test_is_not_equal() {
let mut rng = TestRng::default();

check_is_not_equal(Mode::Constant, Mode::Constant, 0, 0, 0, 0, &mut rng);
check_is_not_equal(Mode::Constant, Mode::Public, 0, 0, 14, 14, &mut rng);
check_is_not_equal(Mode::Constant, Mode::Private, 0, 0, 14, 14, &mut rng);
check_is_not_equal(Mode::Public, Mode::Constant, 0, 0, 14, 14, &mut rng);
check_is_not_equal(Mode::Private, Mode::Constant, 0, 0, 14, 14, &mut rng);
check_is_not_equal(Mode::Public, Mode::Public, 0, 0, 14, 14, &mut rng);
check_is_not_equal(Mode::Public, Mode::Private, 0, 0, 14, 14, &mut rng);
check_is_not_equal(Mode::Private, Mode::Public, 0, 0, 14, 14, &mut rng);
check_is_not_equal(Mode::Private, Mode::Private, 0, 0, 14, 14, &mut rng);
check_is_not_equal(Mode::Constant, Mode::Public, 0, 0, 11, 11, &mut rng);
check_is_not_equal(Mode::Constant, Mode::Private, 0, 0, 11, 11, &mut rng);
check_is_not_equal(Mode::Public, Mode::Constant, 0, 0, 11, 11, &mut rng);
check_is_not_equal(Mode::Private, Mode::Constant, 0, 0, 11, 11, &mut rng);
check_is_not_equal(Mode::Public, Mode::Public, 0, 0, 11, 11, &mut rng);
check_is_not_equal(Mode::Public, Mode::Private, 0, 0, 11, 11, &mut rng);
check_is_not_equal(Mode::Private, Mode::Public, 0, 0, 11, 11, &mut rng);
check_is_not_equal(Mode::Private, Mode::Private, 0, 0, 11, 11, &mut rng);
}
}
10 changes: 4 additions & 6 deletions circuit/account/src/compute_key/from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ use super::*;
impl<A: Aleo> From<(Group<A>, Group<A>)> for ComputeKey<A> {
/// Derives the account compute key from a tuple `(pk_sig, pr_sig)`.
fn from((pk_sig, pr_sig): (Group<A>, Group<A>)) -> Self {
// Compute sk_prf := HashToScalar(pk_sig || pr_sig).
let sk_prf = A::hash_to_scalar_psd4(&[pk_sig.to_x_coordinate(), pr_sig.to_x_coordinate()]);
// Output the compute key.
Self { pk_sig, pr_sig, sk_prf }
Self { pk_sig, pr_sig }
}
}

Expand Down Expand Up @@ -63,16 +61,16 @@ mod tests {

#[test]
fn test_from_constant() -> Result<()> {
check_from(Mode::Constant, 254, 0, 0, 0)
check_from(Mode::Constant, 0, 0, 0, 0)
}

#[test]
fn test_from_public() -> Result<()> {
check_from(Mode::Public, 1, 0, 845, 847)
check_from(Mode::Public, 0, 0, 0, 0)
}

#[test]
fn test_from_private() -> Result<()> {
check_from(Mode::Private, 1, 0, 845, 847)
check_from(Mode::Private, 0, 0, 0, 0)
}
}
8 changes: 3 additions & 5 deletions circuit/account/src/compute_key/from_private_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ impl<A: Aleo> ComputeKey<A> {
let pk_sig = A::g_scalar_multiply(sk_sig);
// Compute `pr_sig` := G^r_sig.
let pr_sig = A::g_scalar_multiply(r_sig);
// Compute `sk_prf` := RO(G^sk_sig || G^r_sig).
let sk_prf = A::hash_to_scalar_psd4(&[pk_sig.to_x_coordinate(), pr_sig.to_x_coordinate()]);

// Return the compute key.
Self { pk_sig, pr_sig, sk_prf }
Self { pk_sig, pr_sig }
}
}

Expand Down Expand Up @@ -75,11 +73,11 @@ mod tests {

#[test]
fn test_from_private_key_public() -> Result<()> {
check_from_private_key(Mode::Public, 1001, 0, 4347, 4353)
check_from_private_key(Mode::Public, 1001, 0, 3502, 3506)
}

#[test]
fn test_from_private_key_private() -> Result<()> {
check_from_private_key(Mode::Private, 1001, 0, 4347, 4353)
check_from_private_key(Mode::Private, 1001, 0, 3502, 3506)
}
}
12 changes: 6 additions & 6 deletions circuit/account/src/compute_key/helpers/from_bits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,31 +100,31 @@ mod tests {

#[test]
fn test_from_bits_le_constant() {
check_from_bits_le(Mode::Constant, 272, 0, 0, 0);
check_from_bits_le(Mode::Constant, 18, 0, 0, 0);
}

#[test]
fn test_from_bits_le_public() {
check_from_bits_le(Mode::Public, 9, 0, 1375, 1379);
check_from_bits_le(Mode::Public, 8, 0, 530, 532);
}

#[test]
fn test_from_bits_le_private() {
check_from_bits_le(Mode::Private, 9, 0, 1375, 1379);
check_from_bits_le(Mode::Private, 8, 0, 530, 532);
}

#[test]
fn test_from_bits_be_constant() {
check_from_bits_be(Mode::Constant, 272, 0, 0, 0);
check_from_bits_be(Mode::Constant, 18, 0, 0, 0);
}

#[test]
fn test_from_bits_be_public() {
check_from_bits_be(Mode::Public, 9, 0, 1375, 1379);
check_from_bits_be(Mode::Public, 8, 0, 530, 532);
}

#[test]
fn test_from_bits_be_private() {
check_from_bits_be(Mode::Private, 9, 0, 1375, 1379);
check_from_bits_be(Mode::Private, 8, 0, 530, 532);
}
}
20 changes: 8 additions & 12 deletions circuit/account/src/compute_key/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ pub struct ComputeKey<A: Aleo> {
pk_sig: Group<A>,
/// The signature public randomizer `pr_sig` := G^r_sig.
pr_sig: Group<A>,
/// The PRF secret key `sk_prf` := RO(G^sk_sig || G^r_sig).
sk_prf: Scalar<A>,
}

#[cfg(console)]
Expand Down Expand Up @@ -63,8 +61,9 @@ impl<A: Aleo> ComputeKey<A> {
}

/// Returns the PRF secret key.
pub const fn sk_prf(&self) -> &Scalar<A> {
&self.sk_prf
pub fn sk_prf(&self) -> Scalar<A> {
// Compute sk_prf := HashToScalar(pk_sig || pr_sig).
A::hash_to_scalar_psd4(&[self.pk_sig.to_x_coordinate(), self.pr_sig.to_x_coordinate()])
}
}

Expand All @@ -74,7 +73,7 @@ impl<A: Aleo> Eject for ComputeKey<A> {

/// Ejects the mode of the compute key.
fn eject_mode(&self) -> Mode {
(&self.pk_sig, &self.pr_sig, &self.sk_prf).eject_mode()
(&self.pk_sig, &self.pr_sig).eject_mode()
}

/// Ejects the compute key.
Expand Down Expand Up @@ -108,10 +107,7 @@ pub(crate) mod tests {

Circuit::scope(format!("New {mode}"), || {
let candidate = ComputeKey::<Circuit>::new(mode, compute_key);
match mode.is_constant() {
true => assert_eq!(Mode::Constant, candidate.eject_mode()),
false => assert_eq!(Mode::Private, candidate.eject_mode()),
};
assert_eq!(mode, candidate.eject_mode());
assert_eq!(compute_key, candidate.eject_value());
// TODO (howardwu): Resolve skipping the cost count checks for the burn-in round.
if i > 0 {
Expand All @@ -125,16 +121,16 @@ pub(crate) mod tests {

#[test]
fn test_compute_key_new_constant() -> Result<()> {
check_new(Mode::Constant, 274, 0, 0, 0)
check_new(Mode::Constant, 20, 0, 0, 0)
}

#[test]
fn test_compute_key_new_public() -> Result<()> {
check_new(Mode::Public, 9, 4, 869, 873)
check_new(Mode::Public, 8, 4, 24, 26)
}

#[test]
fn test_compute_key_new_private() -> Result<()> {
check_new(Mode::Private, 9, 0, 873, 873)
check_new(Mode::Private, 8, 0, 28, 26)
}
}
3 changes: 1 addition & 2 deletions circuit/account/src/compute_key/ternary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ impl<A: Aleo> Ternary for ComputeKey<A> {
Self {
pk_sig: Group::ternary(condition, &first.pk_sig, &second.pk_sig),
pr_sig: Group::ternary(condition, &first.pr_sig, &second.pr_sig),
sk_prf: Scalar::ternary(condition, &first.sk_prf, &second.sk_prf),
}
}
}
Expand All @@ -36,7 +35,7 @@ impl<A: Aleo> Metrics<dyn Ternary<Boolean = Boolean<A>, Output = ComputeKey<A>>>
(Mode::Constant, _, _)
| (Mode::Public, Mode::Constant, Mode::Constant)
| (Mode::Private, Mode::Constant, Mode::Constant) => Count::is(0, 0, 0, 0),
_ => Count::is(0, 0, 5, 5),
_ => Count::is(0, 0, 4, 4),
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions circuit/account/src/compute_key/to_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl<A: Aleo> ComputeKey<A> {
/// Returns the account address for this account compute key.
pub fn to_address(&self) -> Address<A> {
// Compute pk_prf := G^sk_prf.
let pk_prf = A::g_scalar_multiply(&self.sk_prf);
let pk_prf = A::g_scalar_multiply(&self.sk_prf());
// Compute the address := pk_sig + pr_sig + pk_prf.
Address::from_group(&self.pk_sig + &self.pr_sig + pk_prf)
}
Expand Down Expand Up @@ -62,16 +62,16 @@ mod tests {

#[test]
fn test_to_address_constant() -> Result<()> {
check_to_address(Mode::Constant, 1008, 0, 0, 0)
check_to_address(Mode::Constant, 1262, 0, 0, 0)
}

#[test]
fn test_to_address_public() -> Result<()> {
check_to_address(Mode::Public, 504, 0, 1260, 1260)
check_to_address(Mode::Public, 505, 0, 2105, 2107)
}

#[test]
fn test_to_address_private() -> Result<()> {
check_to_address(Mode::Private, 504, 0, 1260, 1260)
check_to_address(Mode::Private, 505, 0, 2105, 2107)
}
}
4 changes: 2 additions & 2 deletions circuit/account/src/private_key/to_compute_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ mod tests {

#[test]
fn test_to_compute_key_public() -> Result<()> {
check_to_compute_key(Mode::Public, 1001, 0, 4347, 4353)
check_to_compute_key(Mode::Public, 1000, 0, 3502, 3506)
}

#[test]
fn test_to_compute_key_private() -> Result<()> {
check_to_compute_key(Mode::Private, 1001, 0, 4347, 4353)
check_to_compute_key(Mode::Private, 1000, 0, 3502, 3506)
}
}
34 changes: 17 additions & 17 deletions circuit/account/src/signature/equal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl<A: Aleo> Metrics<dyn Equal<Signature<A>, Output = Boolean<A>>> for Signatur
fn count(case: &Self::Case) -> Count {
match case.0.is_constant() && case.1.is_constant() {
true => Count::is(0, 0, 0, 0),
false => Count::is(0, 0, 20, 20),
false => Count::is(0, 0, 17, 17),
}
}
}
Expand Down Expand Up @@ -136,28 +136,28 @@ mod tests {
let mut rng = TestRng::default();

check_is_equal(Mode::Constant, Mode::Constant, 0, 0, 0, 0, &mut rng);
check_is_equal(Mode::Constant, Mode::Public, 0, 0, 20, 20, &mut rng);
check_is_equal(Mode::Constant, Mode::Private, 0, 0, 20, 20, &mut rng);
check_is_equal(Mode::Public, Mode::Constant, 0, 0, 20, 20, &mut rng);
check_is_equal(Mode::Private, Mode::Constant, 0, 0, 20, 20, &mut rng);
check_is_equal(Mode::Public, Mode::Public, 0, 0, 20, 20, &mut rng);
check_is_equal(Mode::Public, Mode::Private, 0, 0, 20, 20, &mut rng);
check_is_equal(Mode::Private, Mode::Public, 0, 0, 20, 20, &mut rng);
check_is_equal(Mode::Private, Mode::Private, 0, 0, 20, 20, &mut rng);
check_is_equal(Mode::Constant, Mode::Public, 0, 0, 17, 17, &mut rng);
check_is_equal(Mode::Constant, Mode::Private, 0, 0, 17, 17, &mut rng);
check_is_equal(Mode::Public, Mode::Constant, 0, 0, 17, 17, &mut rng);
check_is_equal(Mode::Private, Mode::Constant, 0, 0, 17, 17, &mut rng);
check_is_equal(Mode::Public, Mode::Public, 0, 0, 17, 17, &mut rng);
check_is_equal(Mode::Public, Mode::Private, 0, 0, 17, 17, &mut rng);
check_is_equal(Mode::Private, Mode::Public, 0, 0, 17, 17, &mut rng);
check_is_equal(Mode::Private, Mode::Private, 0, 0, 17, 17, &mut rng);
}

#[test]
fn test_is_not_equal() {
let mut rng = TestRng::default();

check_is_not_equal(Mode::Constant, Mode::Constant, 0, 0, 0, 0, &mut rng);
check_is_not_equal(Mode::Constant, Mode::Public, 0, 0, 20, 20, &mut rng);
check_is_not_equal(Mode::Constant, Mode::Private, 0, 0, 20, 20, &mut rng);
check_is_not_equal(Mode::Public, Mode::Constant, 0, 0, 20, 20, &mut rng);
check_is_not_equal(Mode::Private, Mode::Constant, 0, 0, 20, 20, &mut rng);
check_is_not_equal(Mode::Public, Mode::Public, 0, 0, 20, 20, &mut rng);
check_is_not_equal(Mode::Public, Mode::Private, 0, 0, 20, 20, &mut rng);
check_is_not_equal(Mode::Private, Mode::Public, 0, 0, 20, 20, &mut rng);
check_is_not_equal(Mode::Private, Mode::Private, 0, 0, 20, 20, &mut rng);
check_is_not_equal(Mode::Constant, Mode::Public, 0, 0, 17, 17, &mut rng);
check_is_not_equal(Mode::Constant, Mode::Private, 0, 0, 17, 17, &mut rng);
check_is_not_equal(Mode::Public, Mode::Constant, 0, 0, 17, 17, &mut rng);
check_is_not_equal(Mode::Private, Mode::Constant, 0, 0, 17, 17, &mut rng);
check_is_not_equal(Mode::Public, Mode::Public, 0, 0, 17, 17, &mut rng);
check_is_not_equal(Mode::Public, Mode::Private, 0, 0, 17, 17, &mut rng);
check_is_not_equal(Mode::Private, Mode::Public, 0, 0, 17, 17, &mut rng);
check_is_not_equal(Mode::Private, Mode::Private, 0, 0, 17, 17, &mut rng);
}
}
12 changes: 6 additions & 6 deletions circuit/account/src/signature/helpers/from_bits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,31 +118,31 @@ mod tests {

#[test]
fn test_from_bits_le_constant() {
check_from_bits_le(Mode::Constant, 272, 0, 0, 0);
check_from_bits_le(Mode::Constant, 18, 0, 0, 0);
}

#[test]
fn test_from_bits_le_public() {
check_from_bits_le(Mode::Public, 9, 0, 1875, 1881);
check_from_bits_le(Mode::Public, 8, 0, 1030, 1034);
}

#[test]
fn test_from_bits_le_private() {
check_from_bits_le(Mode::Private, 9, 0, 1875, 1881);
check_from_bits_le(Mode::Private, 8, 0, 1030, 1034);
}

#[test]
fn test_from_bits_be_constant() {
check_from_bits_be(Mode::Constant, 272, 0, 0, 0);
check_from_bits_be(Mode::Constant, 18, 0, 0, 0);
}

#[test]
fn test_from_bits_be_public() {
check_from_bits_be(Mode::Public, 9, 0, 1875, 1881);
check_from_bits_be(Mode::Public, 8, 0, 1030, 1034);
}

#[test]
fn test_from_bits_be_private() {
check_from_bits_be(Mode::Private, 9, 0, 1875, 1881);
check_from_bits_be(Mode::Private, 8, 0, 1030, 1034);
}
}
6 changes: 3 additions & 3 deletions circuit/account/src/signature/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,16 +181,16 @@ mod tests {

#[test]
fn test_signature_new_constant() -> Result<()> {
check_new(Mode::Constant, 276, 0, 0, 0)
check_new(Mode::Constant, 22, 0, 0, 0)
}

#[test]
fn test_signature_new_public() -> Result<()> {
check_new(Mode::Public, 9, 6, 869, 873)
check_new(Mode::Public, 8, 6, 24, 26)
}

#[test]
fn test_signature_new_private() -> Result<()> {
check_new(Mode::Private, 9, 0, 875, 873)
check_new(Mode::Private, 8, 0, 30, 26)
}
}
Loading