Skip to content
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

SIMD-0075: Precompile for Secp256r1 #3152

Open
wants to merge 50 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
226eeef
feat: secp256r1 precompile
0xRigel Oct 13, 2024
4c753ff
add: num_signatures == 0 check from SIMD-0152
0xRigel Oct 13, 2024
b0ec063
rm: unnecessary comment
0xRigel Oct 13, 2024
6d9d46e
fix: legacy numeric constant
0xRigel Oct 13, 2024
943bd7c
Merge branch 'master' into secp256r1-precompile
0xRigel Oct 15, 2024
f74009a
CI/fix: compilation for wasm32 target
0xRigel Oct 15, 2024
46e15a1
Merge branch 'master' into secp256r1-precompile
0xRigel Oct 15, 2024
6011adf
Merge branch 'secp256r1-precompile' of https://github.com/iceomatic/a…
0xRigel Oct 15, 2024
bc83ba6
Merge branch 'master' into secp256r1-precompile
0xRigel Oct 16, 2024
86a25ea
Extract secp256r1 crate
0xRigel Oct 16, 2024
2598735
rm: unnecessary import
0xRigel Oct 16, 2024
eb3c492
update: sbf/Cargo.lock
0xRigel Oct 16, 2024
4d5b213
rm: unnecesary re-exports
0xRigel Oct 16, 2024
337433c
add: secp256r1 precompile to docs
0xRigel Oct 16, 2024
eeb5023
add: docs/description to sdk/program/src/lib.rs
0xRigel Oct 16, 2024
4b257a0
fix: alpha sort deps
0xRigel Oct 23, 2024
372be6c
fixes
0xRigel Oct 23, 2024
30dbde6
docs fixes
0xRigel Oct 23, 2024
2d64c1f
add: solana-instruction std feature to deps
0xRigel Oct 23, 2024
efe084b
Merge branch 'master' into secp256r1-precompile
0xRigel Oct 23, 2024
e266020
fix: lockfile from rebase
0xRigel Oct 23, 2024
4cd7bc0
fix: target architecture
0xRigel Oct 23, 2024
ce302a7
fix: workflow for client_target android
0xRigel Oct 23, 2024
897eace
add: sudo to workflow perl install
0xRigel Oct 24, 2024
c6c2249
fix: Cargo toml workspace member
0xRigel Oct 24, 2024
930db7c
Merge branch 'master' into secp256r1-precompile
0xRigel Oct 24, 2024
3df67bb
Merge branch 'secp256r1-precompile' of https://github.com/0xRigel/aga…
0xRigel Oct 24, 2024
3a73e79
modify: ranlib path in client-targets.yaml
0xRigel Oct 24, 2024
52fb88a
fix: secp256r1/Cargo.toml formatting
0xRigel Oct 24, 2024
632118c
add: openssl feature
0xRigel Oct 31, 2024
5437fc5
fixes
0xRigel Oct 31, 2024
fdf7673
add: precompile signature range error
0xRigel Nov 1, 2024
d3d24ea
more adjustments
0xRigel Nov 1, 2024
cbc7219
change: feature id
0xRigel Nov 2, 2024
3659bd0
Merge remote-tracking branch 'origin/master' into secp256r1-precompile
0xRigel Nov 6, 2024
0d2f171
fix: cargo format
0xRigel Nov 6, 2024
7e8a3ea
Revert "add: precompile signature range error"
0xRigel Nov 6, 2024
f27ce17
Merge branch 'anza-xyz:master' into secp256r1-precompile
0xRigel Nov 6, 2024
64291f6
Merge branch 'master' into secp256r1-precompile
0xRigel Nov 6, 2024
b44c02f
fix: cargo sanity
0xRigel Nov 6, 2024
8629123
Merge branch 'master' into secp256r1-precompile
0xRigel Nov 6, 2024
26649ff
fix: client target openssl dep
0xRigel Nov 6, 2024
e539f5c
fix: 31 byte r,s support in new_secp256r1_instruction
0xRigel Nov 6, 2024
fd7bbb6
update: Cargo.lock
0xRigel Nov 6, 2024
1522fcc
fix: unchecked math in new_secp256r1_instruction
0xRigel Nov 6, 2024
7f54c62
Merge branch 'master' into secp256r1-precompile
0xRigel Nov 6, 2024
a765f92
fixes & increased test coverage
0xRigel Nov 7, 2024
51ed60e
Merge branch 'master' into secp256r1-precompile
0xRigel Nov 7, 2024
f0cfecd
Merge branch 'master' into secp256r1-precompile
0xRigel Nov 7, 2024
6b92a71
Merge branch 'secp256r1-precompile' of https://github.com/0xRigel/aga…
0xRigel Nov 7, 2024
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
18 changes: 18 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ solana-rayon-threadlimit = { path = "rayon-threadlimit", version = "=2.1.0" }
solana-remote-wallet = { path = "remote-wallet", version = "=2.1.0", default-features = false }
solana-rent = { path = "sdk/rent", version = "=2.1.0", default-features = false }
solana-sanitize = { path = "sdk/sanitize", version = "=2.1.0" }
solana-secp256r1 = { path = "sdk/secp256r1", version = "=2.1.0" }
solana-serde-varint = { path = "sdk/serde-varint", version = "=2.1.0" }
solana-serialize-utils = { path = "sdk/serialize-utils", version = "=2.1.0" }
solana-sha256-hasher = { path = "sdk/sha256-hasher", version = "=2.1.0" }
Expand Down
65 changes: 65 additions & 0 deletions docs/src/runtime/programs.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,71 @@ also receive data from the transaction itself.
Cost of the transaction will count the number of signatures to verify multiplied
by the signature cost verify multiplier.

## Secp256r1 Program

The program for verifying secp256r1 signatures. It takes a secp256r1 signature,
a public key, and a message. Up to 8 signatures can be verified. If any of the
signatures fail to verify, an error is returned.

- Program id: `Secp256r1SigVerify1111111111111111111111111`
- Instructions: [secp256r1_instruction](https://docs.rs/solana-secp256r1)

The secp256r1 program processes an instruction. The first `u8` is a count of the number of signatures to check, followed by a single byte padding. After that, the following struct is serialized, one for each signature to check:

```rust
struct Secp256r1SignatureOffsets {
signature_offset: u16, // offset to compact secp256r1 signature of 64 bytes
signature_instruction_index: u16, // instruction index to find signature
public_key_offset: u16, // offset to compressed public key of 33 bytes
public_key_instruction_index: u16, // instruction index to find public key
message_data_offset: u16, // offset to start of message data
message_data_size: u16, // size of message data
message_instruction_index: u16, // index of instruction data to get message data
}

```

The pseudo code of the signature verification:

process_instruction() {
if data.len() < SIGNATURE_OFFSETS_START {
return Error
}

num_signatures = data[0] as usize
if num_signatures == 0 || num_signatures > 8 {
return Error
}

expected_data_size = num_signatures * SIGNATURE_OFFSETS_SERIALIZED_SIZE + SIGNATURE_OFFSETS_START
if data.len() < expected_data_size {
return Error
}

for i in 0..num_signatures {
offsets = parse_signature_offsets(data, i)

signature = get_data_slice(data, instruction_datas, offsets.signature_instruction_index, offsets.signature_offset, SIGNATURE_SERIALIZED_SIZE)

if s > half_curve_order {
return Error
}

pubkey = get_data_slice(data, instruction_datas, offsets.public_key_instruction_index, offsets.public_key_offset, COMPRESSED_PUBKEY_SERIALIZED_SIZE)

message = get_data_slice(data, instruction_datas, offsets.message_instruction_index, offsets.message_data_offset, offsets.message_data_size)

if !verify_signature(signature, pubkey, message) {
return Error
}
}

return Success
}

Note: Low S values are enforced for all signatures to avoid accidental signature
malleability.

### Optimization notes

The operation will have to take place after (at least partial) deserialization,
Expand Down
Loading
Loading