Skip to content

Commit

Permalink
Merge pull request #24 from Cypher-Laboratory/add_sha256_in_hash_func…
Browse files Browse the repository at this point in the history
…tion

Add sha256 in hash function
  • Loading branch information
Elli610 authored Oct 28, 2024
2 parents f804827 + 50bfd2f commit 9fe72b5
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 12 deletions.
4 changes: 0 additions & 4 deletions packages/lsag-ts/test/ringSignature/constructor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ it("Should throw if at least 1 response is 0 - secp256k1", () => {
data.linkabilityFlag ? [data.linkabilityFlag] : [],
),
secp256k1,
// config,
);

const keyImage = customMapped.mult(data.signerPrivKey);
Expand All @@ -155,7 +154,6 @@ it("Should throw if curve is invalid", () => {
data.linkabilityFlag ? [data.linkabilityFlag] : [],
),
secp256k1,
// config,
);

const keyImage = customMapped.mult(data.signerPrivKey);
Expand All @@ -178,7 +176,6 @@ it("Should pass if all parameters are valid - secp256k1", () => {
data.linkabilityFlag ? [data.linkabilityFlag] : [],
),
secp256k1,
// config,
);

const keyImage = customMapped.mult(data.signerPrivKey);
Expand All @@ -203,7 +200,6 @@ it("Should throw if at least 1 response is 0 - secp256k1", () => {
data.linkabilityFlag ? [data.linkabilityFlag] : [],
),
secp256k1,
// config,
);

const keyImage = customMapped.mult(data.signerPrivKey);
Expand Down
13 changes: 13 additions & 0 deletions packages/lsag-ts/test/ringSignature/sign.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,17 @@ describe("Test sign()", () => {
expect(ringSignature).toBeInstanceOf(RingSignature);
expect(ringSignature.verify()).toBe(true);
});
/* ------------CONFIG.HASH = SHA256------------ */
it("Should return a valid ring signature if config.hash is SHA256 - secp256k1", () => {
const ringSignature = RingSignature.sign(
data.publicKeys_secp256k1,
data.signerPrivKey,
data.message,
secp256k1,
data.linkabilityFlag,
{ hash: HashFunction.SHA256 },
);
expect(ringSignature).toBeInstanceOf(RingSignature);
expect(ringSignature.verify()).toBe(true);
});
});
2 changes: 0 additions & 2 deletions packages/lsag-ts/test/ringSignature/toBase64.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ describe("Test toBase64()", () => {
data.linkabilityFlag ? [data.linkabilityFlag] : [],
),
secp256k1,
// config,
);

const keyImage = customMapped.mult(data.signerPrivKey);
Expand All @@ -40,7 +39,6 @@ describe("Test toBase64()", () => {
data.linkabilityFlag ? [data.linkabilityFlag] : [],
),
secp256k1,
// config,
);

const keyImage = customMapped.mult(data.signerPrivKey);
Expand Down
2 changes: 0 additions & 2 deletions packages/lsag-ts/test/ringSignature/toJsonString.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ describe("Test toJsonString()", () => {
data.linkabilityFlag ? [data.linkabilityFlag] : [],
),
secp256k1,
// config,
);

const keyImage = customMapped.mult(data.signerPrivKey);
Expand All @@ -39,7 +38,6 @@ describe("Test toJsonString()", () => {
data.linkabilityFlag ? [data.linkabilityFlag] : [],
),
secp256k1,
// config,
);

const keyImage = customMapped.mult(data.signerPrivKey);
Expand Down
21 changes: 17 additions & 4 deletions packages/ring-sig-utils/src/utils/hashFunction.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { keccak_256 as keccak256 } from "@noble/hashes/sha3";
import { utf8ToBytes } from "@noble/hashes/utils";
import { sha512 } from "@noble/hashes/sha512";
import { sha256 } from "@noble/hashes/sha256";
import { SignatureConfig } from "../interfaces";
import { uint8ArrayToHex } from ".";
import { Curve, CurveName } from "../curves";
Expand All @@ -10,6 +11,7 @@ import { hashToCurve } from "@noble/curves/secp256k1";
export enum HashFunction {
KECCAK256 = "keccak256",
SHA512 = "sha512",
SHA256 = "sha256",
}

/* ------------------ Hash functions ------------------ */
Expand All @@ -35,6 +37,9 @@ export function hash(
case HashFunction.SHA512: {
return sha_512(data);
}
case HashFunction.SHA256: {
return sha_256(data);
}
default: {
return keccak_256(data, config?.evmCompatibility);
}
Expand Down Expand Up @@ -91,10 +96,18 @@ export function keccak_256(
* @returns - The hash of the data as an hex string
*/
export function sha_512(input: (string | bigint)[]): string {
const serialized = input
.map((x) => (typeof x === "bigint" ? x.toString() : x))
.join("");
return Buffer.from(sha512(serialized)).toString("hex");
return Buffer.from(sha512(serializeInput(input))).toString("hex");
}

/**
* Hash data using sha256
*
* @param input - The data to hash
*
* @returns - The hash of the data as an hex string
*/
export function sha_256(input: (string | bigint)[]): string {
return Buffer.from(sha256(serializeInput(input))).toString("hex");
}

/**
Expand Down
14 changes: 14 additions & 0 deletions packages/sag-ts/test/ringSignature/constructor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,20 @@ describe("Test Constructor", () => {
),
);
});
it("Should pass if config.hash is sha256", () => {
expect(
() =>
new RingSignature(
data.message,
data.publicKeys_ed25519,
data.randomC,
data.randomResponses,
ed25519,
{ hash: HashFunction.SHA256 },
),
);
});

it("Should pass if config.hash is not undefined", () => {
expect(
() =>
Expand Down
22 changes: 22 additions & 0 deletions packages/sag-ts/test/ringSignature/sign.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,17 @@ describe("Test sign()", () => {
expect(ringSignature).toBeInstanceOf(RingSignature);
expect(ringSignature.verify()).toBe(true);
});
it("Should return a valid ring signature if config.hash is SHA256 - secp256k1", () => {
const ringSignature = RingSignature.sign(
data.publicKeys_secp256k1,
data.signerPrivKey,
data.message,
secp256k1,
{ hash: HashFunction.SHA256 },
);
expect(ringSignature).toBeInstanceOf(RingSignature);
expect(ringSignature.verify()).toBe(true);
});

it("Should return a valid ring signature if config.hash is SHA512 - ed25519", () => {
const ringSignature = RingSignature.sign(
Expand All @@ -142,4 +153,15 @@ describe("Test sign()", () => {
expect(ringSignature).toBeInstanceOf(RingSignature);
expect(ringSignature.verify()).toBe(true);
});
it("Should return a valid ring signature if config.hash is SHA256 - ed25519", () => {
const ringSignature = RingSignature.sign(
data.publicKeys_ed25519,
data.signerPrivKey,
data.message,
ed25519,
{ hash: HashFunction.SHA256 },
);
expect(ringSignature).toBeInstanceOf(RingSignature);
expect(ringSignature.verify()).toBe(true);
});
});

0 comments on commit 9fe72b5

Please sign in to comment.