slashMember now accepts secret instead of idCommitment

This commit is contained in:
AtHeartEngineer
2023-04-13 15:21:08 +09:00
parent 4b1309bf1e
commit a89305f3ef
3 changed files with 126 additions and 113 deletions

View File

@@ -10,6 +10,7 @@ module.exports = {
semi: ["warn", "never"],
"import/extensions": ["warn", "never"],
"@typescript-eslint/semi": ["warn", "never"],
"indent": ["warn", 2]
},
settings: {
"import/parsers": {

View File

@@ -111,12 +111,21 @@ export default class Registry {
/**
* Removes a member from the registry and adds them to the slashed registry.
* @param identityCommitment IdentityCommitment of the member to be removed.
* @param secret Secret of the member to be removed.
*/
public slashMember(identityCommitment: bigint) {
const index = this._registry.indexOf(identityCommitment)
this._registry.delete(index)
this._slashed.insert(identityCommitment)
public slashMember(secret: bigint) {
const identityCommitment = poseidon([secret])
this._addSlashedMember(identityCommitment)
this._removeMember(identityCommitment)
}
/**
* Removes a member from the registry and adds them to the slashed registry.
* @param identityCommitment identityCommitment of the member to be removed.
*/
public slashMemberByIdentityCommitment(identityCommitment: bigint) {
this._addSlashedMember(identityCommitment)
this._removeMember(identityCommitment)
}
/**
@@ -124,7 +133,7 @@ export default class Registry {
* If a member exists in the registry, the member can't be added to the slashed.
* @param identityCommitment New member.
*/
public addSlashedMember(identityCommitment: bigint) {
public _addSlashedMember(identityCommitment: bigint) {
if (this._slashed.indexOf(identityCommitment) !== -1) {
throw new Error('Member already in slashed registry.')
}
@@ -138,9 +147,9 @@ export default class Registry {
* Adds new members to the slashed registry.
* @param identityCommitments New members.
*/
public addSlashedMembers(identityCommitments: bigint[]) {
public _addSlashedMembers(identityCommitments: bigint[]) {
for (const identityCommitment of identityCommitments) {
this.addSlashedMember(identityCommitment)
this._addSlashedMember(identityCommitment)
}
}
@@ -148,7 +157,10 @@ export default class Registry {
* Removes a member from the registry.
* @param identityCommitment IdentityCommitment of the member to be removed.
*/
public removeMember(identityCommitment: bigint) {
public _removeMember(identityCommitment: bigint) {
if (this._registry.indexOf(identityCommitment) == -1) {
throw new Error("Member doesn't exist in registry.")
}
const index = this._registry.indexOf(identityCommitment)
this._registry.delete(index)
}
@@ -216,7 +228,7 @@ export default class Registry {
console.debug(registryObject)
const registryInstance = new Registry(registryObject.treeDepth, BigInt(registryObject.zeroValue))
registryInstance.addMembers(registryObject.registry.map((x) => BigInt(x)))
registryInstance.addSlashedMembers(registryObject.slashed.map((x) => BigInt(x)))
registryInstance._addSlashedMembers(registryObject.slashed.map((x) => BigInt(x)))
return registryInstance
}
}

View File

@@ -1,130 +1,130 @@
import Registry, { DEFAULT_REGISTRY_TREE_DEPTH } from "../src/registry"
import Registry, { DEFAULT_REGISTRY_TREE_DEPTH } from '../src/registry'
describe("Registry", () => {
describe("Registry Creation", () => {
test("Should create a registry", () => {
const registry = new Registry()
describe('Registry', () => {
describe('Registry Creation', () => {
test('Should create a registry', () => {
const registry = new Registry()
expect(registry.root.toString()).toContain("150197")
expect(registry._treeDepth).toBe(DEFAULT_REGISTRY_TREE_DEPTH)
expect(registry._zeroValue).toBe(BigInt(0))
expect(registry.members).toHaveLength(0)
})
test("Should not create a registry with a wrong tree depth", () => {
const wrongRegistry = () => new Registry(33)
expect(wrongRegistry).toThrow("The tree depth must be between 16 and 32")
})
test("Should create a group with different parameters", () => {
const registry = new Registry(32, BigInt(1))
expect(registry.root.toString()).toContain("640470")
expect(registry._treeDepth).toBe(32)
expect(registry._zeroValue).toBe(BigInt(1))
expect(registry.members).toHaveLength(0)
})
expect(registry.root.toString()).toContain('150197')
expect(registry._treeDepth).toBe(DEFAULT_REGISTRY_TREE_DEPTH)
expect(registry._zeroValue).toBe(BigInt(0))
expect(registry.members).toHaveLength(0)
})
describe("Add Member", () => {
test("Should add a member to a group", () => {
const registry = new Registry()
test('Should not create a registry with a wrong tree depth', () => {
const wrongRegistry = () => new Registry(33)
registry.addMember(BigInt(3))
expect(registry.members).toHaveLength(1)
})
test("Shouldn't be able to add Zero Value as member", () => {
const registry = new Registry()
const result = () => registry.addMember(BigInt(0))
expect(result).toThrow("Can't add zero value as member.")
})
expect(wrongRegistry).toThrow('The tree depth must be between 16 and 32')
})
describe("Add Members", () => {
test("Should add many members to a group", () => {
const registry = new Registry()
// make test to do large batch insertions
registry.addMembers([BigInt(1), BigInt(3)])
test('Should create a group with different parameters', () => {
const registry = new Registry(32, BigInt(1))
expect(registry.members).toHaveLength(2)
})
expect(registry.root.toString()).toContain('640470')
expect(registry._treeDepth).toBe(32)
expect(registry._zeroValue).toBe(BigInt(1))
expect(registry.members).toHaveLength(0)
})
})
describe("Index Member", () => {
test("Should return the index of a member in a group", () => {
const registry = new Registry()
describe('Add Member', () => {
test('Should add a member to a group', () => {
const registry = new Registry()
registry.addMembers([BigInt(1), BigInt(3)])
registry.addMember(BigInt(3))
const index = registry.indexOf(BigInt(3))
expect(index).toBe(1)
})
expect(registry.members).toHaveLength(1)
})
test("Shouldn't be able to add Zero Value as member", () => {
const registry = new Registry()
describe("Remove Member", () => {
test("Should remove a member from a group", () => {
const registry = new Registry()
registry.addMembers([BigInt(1), BigInt(2)])
registry.removeMember(BigInt(1))
const result = () => registry.addMember(BigInt(0))
expect(registry.members).toHaveLength(2)
expect(registry.members[0]).toBe(registry._zeroValue)
})
expect(result).toThrow("Can't add zero value as member.")
})
})
describe("Slash Member", () => {
test("Should slash a member from a group", () => {
const registry = new Registry()
registry.addMembers([BigInt(1), BigInt(2)])
registry.slashMember(BigInt(1))
expect(registry.slashedMembers).toHaveLength(1)
expect(registry.slashedMembers[0]).toBe(BigInt(1))
expect(registry.slashedRoot.toString()).toContain("8796144249463725711720918130641160729715802427308818390609092244052653115670")
})
test("Should not be able to add slashed member", () => {
const registry = new Registry()
registry.addMembers([BigInt(1), BigInt(2)])
registry.slashMember(BigInt(1))
expect(() => registry.addMember(BigInt(1))).toThrow("Can't add slashed member.")
})
describe('Add Members', () => {
test('Should add many members to a group', () => {
const registry = new Registry()
// make test to do large batch insertions
registry.addMembers([BigInt(1), BigInt(3)])
expect(registry.members).toHaveLength(2)
})
})
describe("Merkle Proof", () => {
test("Should return a merkle proof", () => {
const registry = new Registry()
registry.addMember(BigInt(1))
const proof = registry.generateMerkleProof(BigInt(1))
expect(String(proof.root)).toContain("879614424946372571172091813064116")
})
test("Should throw error when given invalid leaf", () => {
const registry = new Registry()
registry.addMember(BigInt(1))
const treeDepth = registry._treeDepth;
describe('Index Member', () => {
test('Should return the index of a member in a group', () => {
const registry = new Registry()
const result = () => Registry.generateMerkleProof(treeDepth, BigInt(0), [BigInt(1), BigInt(2)], BigInt(3))
registry.addMembers([BigInt(1), BigInt(3)])
expect(result).toThrow("The leaf does not exist")
const index = registry.indexOf(BigInt(3))
const result2 = () => Registry.generateMerkleProof(treeDepth, BigInt(0), [BigInt(1), BigInt(2)], BigInt(0))
expect(result2).toThrow("Can't generate a proof for a zero leaf")
})
expect(index).toBe(1)
})
})
test("Should export/import to json", () => {
const registry_json_test = new Registry()
registry_json_test.addMembers([BigInt(1), BigInt(2)])
const json = registry_json_test.export()
console.debug(json)
const registry_from_json = Registry.import(json)
expect(registry_from_json.members).toHaveLength(2)
expect(registry_from_json.root).toEqual(registry_json_test.root)
expect(registry_from_json.slashedRoot).toEqual(registry_json_test.slashedRoot)
describe('Remove Member', () => {
test('Should remove a member from a group', () => {
const registry = new Registry()
registry.addMembers([BigInt(1), BigInt(2)])
registry._removeMember(BigInt(1))
expect(registry.members).toHaveLength(2)
expect(registry.members[0]).toBe(registry._zeroValue)
})
})
describe('Slash Member', () => {
test('Should slash a member from a group', () => {
const registry = new Registry()
registry.addMembers([BigInt(1), BigInt(2)])
registry.slashMember(BigInt(1))
expect(registry.slashedMembers).toHaveLength(1)
expect(registry.slashedMembers[0]).toBe(BigInt(1))
expect(registry.slashedRoot.toString()).toContain('8796144249463725711720918130641160729715802427308818390609092244052653115670')
})
test('Should not be able to add slashed member', () => {
const registry = new Registry()
registry.addMembers([BigInt(1), BigInt(2)])
registry.slashMember(BigInt(1))
expect(() => registry.addMember(BigInt(1))).toThrow("Can't add slashed member.")
})
})
describe('Merkle Proof', () => {
test('Should return a merkle proof', () => {
const registry = new Registry()
registry.addMember(BigInt(1))
const proof = registry.generateMerkleProof(BigInt(1))
expect(String(proof.root)).toContain('879614424946372571172091813064116')
})
test('Should throw error when given invalid leaf', () => {
const registry = new Registry()
registry.addMember(BigInt(1))
const treeDepth = registry._treeDepth
const result = () => Registry.generateMerkleProof(treeDepth, BigInt(0), [BigInt(1), BigInt(2)], BigInt(3))
expect(result).toThrow('The leaf does not exist')
const result2 = () => Registry.generateMerkleProof(treeDepth, BigInt(0), [BigInt(1), BigInt(2)], BigInt(0))
expect(result2).toThrow("Can't generate a proof for a zero leaf")
})
})
test('Should export/import to json', () => {
const registryJsonTest = new Registry()
registryJsonTest.addMembers([BigInt(1), BigInt(2)])
const json = registryJsonTest.export()
console.debug(json)
const registryFromJson = Registry.import(json)
expect(registryFromJson.members).toHaveLength(2)
expect(registryFromJson.root).toEqual(registryJsonTest.root)
expect(registryFromJson.slashedRoot).toEqual(registryJsonTest.slashedRoot)
})
})