-
Notifications
You must be signed in to change notification settings - Fork 292
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
42 changed files
with
405 additions
and
354 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
mod getters; | ||
mod point_to_symmetric_key; | ||
mod public_keys; | ||
|
||
// Add once enabled in key registry: | ||
// get_ovpk_m, | ||
// get_tpk_m | ||
use crate::keys::getters::{get_npk_m, get_ivpk_m}; | ||
use crate::keys::{getters::{get_npk_m, get_ivpk_m}, public_keys::PublicKeys}; |
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,117 @@ | ||
use dep::protocol_types::{ | ||
address::PublicKeysHash, constants::GENERATOR_INDEX__PUBLIC_KEYS_HASH, hash::poseidon2_hash, | ||
grumpkin_point::GrumpkinPoint, traits::{Deserialize, Serialize} | ||
}; | ||
|
||
// Note: In fetch_key_from_registry we expect that the shared mutable slot is index * 2 + 1 for the x coordinate and | ||
// index * 2 + 2 for the y coordinate. For example, the npk_m x coordinates will be stored in a map at storage slot | ||
// 0 * 2 + 1 = 1, and the npk_m y coordinates at 2 * 2 + 2 = 6. If this changes the function will need to be | ||
// refactored. | ||
global NULLIFIER_INDEX = 0; | ||
global INCOMING_INDEX = 1; | ||
global OUTGOING_INDEX = 2; | ||
global TAGGING_INDEX = 3; | ||
|
||
global PUBLIC_KEYS_LENGTH = 8; | ||
|
||
struct PublicKeys { | ||
npk_m: GrumpkinPoint, | ||
ivpk_m: GrumpkinPoint, | ||
ovpk_m: GrumpkinPoint, | ||
tpk_m: GrumpkinPoint, | ||
} | ||
|
||
impl PublicKeys { | ||
pub fn hash(self) -> PublicKeysHash { | ||
PublicKeysHash::from_field( | ||
poseidon2_hash( | ||
[ | ||
self.npk_m.x, | ||
self.npk_m.y, | ||
self.ivpk_m.x, | ||
self.ivpk_m.y, | ||
self.ovpk_m.x, | ||
self.ovpk_m.y, | ||
self.tpk_m.x, | ||
self.tpk_m.y, | ||
GENERATOR_INDEX__PUBLIC_KEYS_HASH | ||
] | ||
) | ||
) | ||
} | ||
|
||
pub fn get_key_by_index(self, index: Field) -> GrumpkinPoint { | ||
assert(index as u8 < 4, "Invalid key index"); | ||
if index == NULLIFIER_INDEX { | ||
self.npk_m | ||
} else if index == INCOMING_INDEX { | ||
self.ivpk_m | ||
} else if index == OUTGOING_INDEX { | ||
self.ovpk_m | ||
} else { | ||
self.tpk_m | ||
} | ||
} | ||
} | ||
|
||
impl Serialize<PUBLIC_KEYS_LENGTH> for PublicKeys { | ||
fn serialize(self) -> [Field; PUBLIC_KEYS_LENGTH] { | ||
[ | ||
self.npk_m.x, | ||
self.npk_m.y, | ||
self.ivpk_m.x, | ||
self.ivpk_m.y, | ||
self.ovpk_m.x, | ||
self.ovpk_m.y, | ||
self.tpk_m.x, | ||
self.tpk_m.y, | ||
] | ||
} | ||
} | ||
|
||
impl Deserialize<PUBLIC_KEYS_LENGTH> for PublicKeys { | ||
fn deserialize(serialized: [Field; PUBLIC_KEYS_LENGTH]) -> PublicKeys { | ||
PublicKeys { | ||
npk_m: GrumpkinPoint { x: serialized[0], y: serialized[1] }, | ||
ivpk_m: GrumpkinPoint { x: serialized[2], y: serialized[3] }, | ||
ovpk_m: GrumpkinPoint { x: serialized[4], y: serialized[5] }, | ||
tpk_m: GrumpkinPoint { x: serialized[6], y: serialized[7] }, | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn compute_public_keys_hash() { | ||
let keys = PublicKeys { | ||
npk_m: GrumpkinPoint { x: 1, y: 2 }, | ||
ivpk_m: GrumpkinPoint { x: 3, y: 4 }, | ||
ovpk_m: GrumpkinPoint { x: 5, y: 6 }, | ||
tpk_m: GrumpkinPoint { x: 7, y: 8 } | ||
}; | ||
|
||
let actual = keys.hash(); | ||
let expected_public_keys_hash = 0x1936abe4f6a920d16a9f6917f10a679507687e2cd935dd1f1cdcb1e908c027f3; | ||
assert(actual.to_field() == expected_public_keys_hash); | ||
} | ||
|
||
#[test] | ||
fn test_public_keys_serialization() { | ||
let keys = PublicKeys { | ||
npk_m: GrumpkinPoint { x: 1, y: 2 }, | ||
ivpk_m: GrumpkinPoint { x: 3, y: 4 }, | ||
ovpk_m: GrumpkinPoint { x: 5, y: 6 }, | ||
tpk_m: GrumpkinPoint { x: 7, y: 8 } | ||
}; | ||
|
||
let serialized = keys.serialize(); | ||
let deserialized = PublicKeys::deserialize(serialized); | ||
|
||
assert_eq(keys.npk_m.x, deserialized.npk_m.x); | ||
assert_eq(keys.npk_m.y, deserialized.npk_m.y); | ||
assert_eq(keys.ivpk_m.x, deserialized.ivpk_m.x); | ||
assert_eq(keys.ivpk_m.y, deserialized.ivpk_m.y); | ||
assert_eq(keys.ovpk_m.x, deserialized.ovpk_m.x); | ||
assert_eq(keys.ovpk_m.y, deserialized.ovpk_m.y); | ||
assert_eq(keys.tpk_m.x, deserialized.tpk_m.x); | ||
assert_eq(keys.tpk_m.y, deserialized.tpk_m.y); | ||
} |
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
12 changes: 4 additions & 8 deletions
12
noir-projects/noir-contracts/contracts/schnorr_single_key_account_contract/src/util.nr
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 |
---|---|---|
@@ -1,21 +1,17 @@ | ||
use dep::aztec::prelude::AztecAddress; | ||
use dep::aztec::protocol_types::address::PublicKeysHash; | ||
use dep::std::{schnorr::verify_signature_slice}; | ||
use dep::aztec::prelude::AztecAddress; | ||
use crate::auth_oracle::AuthWitness; | ||
|
||
pub fn recover_address(message_hash: Field, witness: AuthWitness) -> AztecAddress { | ||
let message_bytes = message_hash.to_be_bytes(32); | ||
// In a single key account contract we re-used ivpk_m as signing key | ||
let verification = verify_signature_slice( | ||
witness.ivpk_m.x, | ||
witness.ivpk_m.y, | ||
witness.keys.ivpk_m.x, | ||
witness.keys.ivpk_m.y, | ||
witness.signature, | ||
message_bytes | ||
); | ||
assert(verification == true); | ||
|
||
AztecAddress::compute( | ||
PublicKeysHash::compute(witness.npk_m, witness.ivpk_m, witness.ovpk_m, witness.tpk_m), | ||
witness.partial_address | ||
) | ||
AztecAddress::compute(witness.keys.hash(), witness.partial_address) | ||
} |
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
2 changes: 1 addition & 1 deletion
2
noir-projects/noir-protocol-circuits/crates/types/src/partial_state_reference.nr
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.