Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 6 additions & 0 deletions docs/docs/migration_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ keywords: [sandbox, aztec, notes, migration, updating, upgrading]

Aztec is in full-speed development. Literally every version breaks compatibility with the previous ones. This page attempts to target errors and difficulties you might encounter when upgrading, and how to resolve them.

## TBD

### [aztec.js] Random addresses are now valid

The `AztecAddress.random()` function now returns valid addresses, i.e. addresses that can receive encrypted messages and therefore have notes be sent to them. `AztecAddress.isValid()` was also added to check for validity of an address.

## 0.63.0
### [PXE] Note tagging and discovery

Expand Down
28 changes: 28 additions & 0 deletions yarn-project/foundation/src/aztec-address/aztec-address.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Fr } from '../fields/fields.js';
import { AztecAddress } from './index.js';

describe('aztec-address', () => {
describe('isValid', () => {
it('returns true for a valid address', () => {
// The point (5, 21888242871839275195798879923479812031525119486506890092185616889232283231735) is on the
// Grumpkin curve.
const address = new AztecAddress(new Fr(5));
expect(address.isValid()).toEqual(true);
});

it('returns false for an invalid address', () => {
// No point on the Grumpkin curve has an x coordinate equal to 6.
const address = new AztecAddress(new Fr(6));
expect(address.isValid()).toEqual(false);
});
});

describe('random', () => {
it('alwways returns a valid address', () => {
for (let i = 0; i < 100; ++i) {
const address = AztecAddress.random();
expect(address.isValid()).toEqual(true);
}
});
});
});
27 changes: 26 additions & 1 deletion yarn-project/foundation/src/aztec-address/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,17 @@ export class AztecAddress {
return new AztecAddress(hexToBuffer(buf));
}

/**
Comment thread
nventuro marked this conversation as resolved.
* @returns a random valid address (i.e. one that can be encryted to).
*/
static random() {
return new AztecAddress(Fr.random());
// About a third of random field elements result in invalid addresses, so we loop until we get a valid one.
Comment thread
nventuro marked this conversation as resolved.
Outdated
while (true) {
const candidate = new AztecAddress(Fr.random());
if (candidate.isValid()) {
return candidate;
}
}
}

get size() {
Expand All @@ -85,6 +94,22 @@ export class AztecAddress {
return this.value.isZero();
}

/**
* @returns true if the address is valid. Invalid addresses cannot receive encrypted messages.
*/
isValid() {
Comment thread
nventuro marked this conversation as resolved.
// An address is a field value (Fr), which for some purposes is assumed to be the x coordinate of a point in the
// Grumpkin curve (notably in order to encrypt to it). An address that is not the x coordinate of such a point is
// called an 'invalid' address.
//
// For Grumpkin, y^2 = x^3 − 17 . There exist values x ∈ Fr for which no y satisfies this equation. This means that
// given such an x and t = x^3 − 17, then sqrt(t) does not exist in Fr.

const cube = this.value.mul(this.value).mul(this.value);
const t = cube.sub(new Fr(17));
return t.sqrt() !== null;
}

toBuffer() {
return this.value.toBuffer();
}
Expand Down