-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat: add secp256k1 verify support #81
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5e6d80c
chore(devcontainer): add ca-certificates
mariocao 2edcd6b
chore(vm): fix test execution
mariocao c9ed860
feat(vm): add secp256k1 verify
mariocao 31d5fca
feat(as-sdk): add secp256k1 verify support
mariocao 336c5f7
test: add integration test for secp256k1 verify
mariocao 7922b4a
refactor(vm): use trySync instead of try/catch
mariocao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { | ||
Bytes, | ||
OracleProgram, | ||
Process, | ||
decodeHex, | ||
secp256k1Verify, | ||
} from "../../as-sdk/assembly"; | ||
|
||
export class TestSecp256k1VerifyValid extends OracleProgram { | ||
execution(): void { | ||
const message = Bytes.fromString("Hello, SEDA!"); | ||
const signature = Bytes.fromBytes( | ||
decodeHex( | ||
"58376cc76f4d4959b0adf8070ecf0079db889915a75370f6e39a8451ba5be0c35f091fa4d2fda3ced5b6e6acd1dbb4a45f2c6a1e643622ee4cf8b802b373d38f", | ||
), | ||
); | ||
const publicKey = Bytes.fromBytes( | ||
decodeHex( | ||
"02a2bebd272aa28e410cc74cef28e5ce74a9ffc94caf817ed9bd23b01ce2068c7b", | ||
), | ||
); | ||
const isValidSignature = secp256k1Verify(message, signature, publicKey); | ||
|
||
if (isValidSignature) { | ||
Process.success(Bytes.fromString("valid secp256k1 signature")); | ||
} else { | ||
Process.error(Bytes.fromString("invalid secp256k1 signature")); | ||
} | ||
} | ||
} | ||
|
||
export class TestSecp256k1VerifyInvalid extends OracleProgram { | ||
execution(): void { | ||
const message = Bytes.fromString("Hello, this is an invalid message!"); | ||
const signature = Bytes.fromBytes( | ||
decodeHex( | ||
"58376cc76f4d4959b0adf8070ecf0079db889915a75370f6e39a8451ba5be0c35f091fa4d2fda3ced5b6e6acd1dbb4a45f2c6a1e643622ee4cf8b802b373d38f", | ||
), | ||
); | ||
const publicKey = Bytes.fromBytes( | ||
decodeHex( | ||
"02a2bebd272aa28e410cc74cef28e5ce74a9ffc94caf817ed9bd23b01ce2068c7b", | ||
), | ||
); | ||
const isValidSignature = secp256k1Verify(message, signature, publicKey); | ||
|
||
if (isValidSignature) { | ||
Process.success(Bytes.fromString("valid secp256k1 signature")); | ||
} else { | ||
Process.error(Bytes.fromString("invalid secp256k1 signature")); | ||
} | ||
} | ||
} |
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,34 @@ | ||
import { beforeEach, describe, expect, it, mock } from "bun:test"; | ||
import { readFile } from "node:fs/promises"; | ||
import { executeDrWasm } from "@seda/dev-tools"; | ||
import { Response } from "node-fetch"; | ||
|
||
const mockHttpFetch = mock(); | ||
|
||
describe("Crypto", () => { | ||
it("Test valid Secp256k1 signature", async () => { | ||
const wasmBinary = await readFile( | ||
"dist/libs/as-sdk-integration-tests/debug.wasm", | ||
); | ||
|
||
const result = await executeDrWasm( | ||
wasmBinary, | ||
Buffer.from("testSecp256k1VerifyValid"), | ||
); | ||
|
||
expect(result.resultAsString).toEqual("valid secp256k1 signature"); | ||
}); | ||
|
||
it("Test invalid Secp256k1 signature", async () => { | ||
const wasmBinary = await readFile( | ||
"dist/libs/as-sdk-integration-tests/debug.wasm", | ||
); | ||
|
||
const result = await executeDrWasm( | ||
wasmBinary, | ||
Buffer.from("testSecp256k1VerifyInvalid"), | ||
); | ||
|
||
expect(result.resultAsString).toEqual("invalid secp256k1 signature"); | ||
}); | ||
}); |
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,40 @@ | ||
import { call_result_write, secp256k1_verify } from "./bindings/seda_v1"; | ||
import { Bytes } from "./bytes"; | ||
import { PromiseStatus } from "./promise"; | ||
|
||
export function secp256k1Verify( | ||
message: Bytes, | ||
signature: Bytes, | ||
publicKey: Bytes, | ||
): bool { | ||
const messageBuffer = message.buffer(); | ||
const messagePtr = changetype<usize>(messageBuffer); | ||
|
||
const signatureBuffer = signature.buffer(); | ||
const signaturePtr = changetype<usize>(signatureBuffer); | ||
|
||
const publicKeyBuffer = publicKey.buffer(); | ||
const publicKeyPtr = changetype<usize>(publicKeyBuffer); | ||
|
||
// Call secp256k1_verify and get the response length | ||
const responseLength = secp256k1_verify( | ||
messagePtr, | ||
messageBuffer.byteLength, | ||
signaturePtr, | ||
signatureBuffer.byteLength, | ||
publicKeyPtr, | ||
publicKeyBuffer.byteLength, | ||
); | ||
|
||
// Allocate an ArrayBuffer for the response | ||
const responseBuffer = new ArrayBuffer(responseLength); | ||
// Get the pointer to the response buffer | ||
const responseBufferPtr = changetype<usize>(responseBuffer); | ||
// Write the result to the allocated buffer | ||
call_result_write(responseBufferPtr, responseLength); | ||
// Convert the response buffer into a Uint8Array | ||
const responseArray = Uint8Array.wrap(responseBuffer); | ||
|
||
// Return true if the response is [1] (valid) | ||
return responseArray[0] === 1; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { describe, expect, it } from "bun:test"; | ||
import { secp256k1Verify } from "./crypto"; | ||
|
||
describe("Crypto", () => { | ||
it("should Secp256k1 verify", async () => { | ||
const message = Buffer.from("Hello, SEDA!"); | ||
const signature = Buffer.from( | ||
"58376cc76f4d4959b0adf8070ecf0079db889915a75370f6e39a8451ba5be0c35f091fa4d2fda3ced5b6e6acd1dbb4a45f2c6a1e643622ee4cf8b802b373d38f", | ||
"hex", | ||
); | ||
const publicKey = Buffer.from( | ||
"02a2bebd272aa28e410cc74cef28e5ce74a9ffc94caf817ed9bd23b01ce2068c7b", | ||
"hex", | ||
); | ||
const result = secp256k1Verify(message, signature, publicKey); | ||
|
||
// Check if the result is a Uint8Array and has the value [1] | ||
expect(result).toEqual(new Uint8Array([1])); | ||
}); | ||
}); |
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,25 @@ | ||
import { keccak_256 } from "@noble/hashes/sha3"; | ||
import * as Secp256k1 from "@noble/secp256k1"; | ||
|
||
export function keccak256(input: Buffer): Buffer { | ||
const hasher = keccak_256.create(); | ||
hasher.update(input); | ||
|
||
return Buffer.from(hasher.digest()); | ||
} | ||
|
||
export function secp256k1Verify( | ||
message: Buffer, | ||
signature: Buffer, | ||
publicKey: Buffer, | ||
): Uint8Array { | ||
const signedMessage = keccak256(message); | ||
const isValidSignature = Secp256k1.verify( | ||
signature, | ||
signedMessage, | ||
publicKey, | ||
); | ||
|
||
// Return 1 as Uint8Array if valid, 0 if not | ||
return new Uint8Array([isValidSignature ? 1 : 0]); | ||
} |
This file was deleted.
Oops, something went wrong.
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're not using this import right?