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

Allow variable length message in ed_verify #6419

Merged
merged 61 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
54ca7dd
LDC multi-chunk contract
vaivaswatha Jul 6, 2024
40f371f
Some updates to build with dento/blob-tx branches of fuel-core and fu…
vaivaswatha Jul 9, 2024
686d275
Support for the BSIZ and BLDD instructions
vaivaswatha Jul 12, 2024
eedd49a
feat: support using 4 registers with ED19
kayagokalp Aug 12, 2024
4bd4ac7
chore: bump to use fuel-core v0.31.0 and sdk v0.65.1 (#6252)
kayagokalp Jul 17, 2024
ebb030b
feat: create and deploy a reference proxy contract for contracts with…
kayagokalp Aug 7, 2024
0aa3ee5
Updates ABI with program_type and hash based ids.
esdrubal Jul 10, 2024
6ee44ec
Uses fuels-core updated branch.
esdrubal Jul 11, 2024
7c9133f
Changed fuel_abi to return Result.
esdrubal Jul 11, 2024
6e22225
Updates fuel-vm and fuel-core to latest.
esdrubal Jul 12, 2024
97fb129
Passes resolved_type to get_abi_type_id.
esdrubal Jul 12, 2024
93368c0
Fixes cargo clippy.
esdrubal Jul 12, 2024
d6f4f5b
Use resolved types.
esdrubal Jul 15, 2024
cac38d7
Fixes generic enums and structs type ids.
esdrubal Jul 15, 2024
79299fb
Updates cargo crates.
esdrubal Jul 18, 2024
b4b6de9
Fixes.
esdrubal Jul 18, 2024
fd1d9ea
Fixes.
esdrubal Jul 19, 2024
8b574bf
Updates Cargo.lock
esdrubal Jul 19, 2024
c5cf48b
Updates json_abi_oracle_new_encoding.json files.
esdrubal Jul 19, 2024
49b8196
Updates hardcoded JSON ABI.
esdrubal Jul 22, 2024
2c90f01
Updates usage of module name.
esdrubal Jul 22, 2024
9c41ee5
Fixes cargo clippy.
esdrubal Jul 22, 2024
9d7d1de
Fixes hardcoded JSON ABI.
esdrubal Jul 22, 2024
82d79b3
Renames typesMetadata to metadataTypes.
esdrubal Jul 23, 2024
40d3b11
Fixes fuel-abi-types version.
esdrubal Jul 23, 2024
3de40c9
Adds missing fields to TEST_JSON_ABI.
esdrubal Jul 23, 2024
ab5bc18
Updates json_abi files.
esdrubal Jul 23, 2024
d22361c
Makes json-abi-with-callpaths true by default and removed flag.
esdrubal Jul 24, 2024
9f64110
Updates fuel-abi-types version.
esdrubal Jul 24, 2024
f7553bf
Reverts wrong renaming.
esdrubal Jul 24, 2024
b711f40
Removes remaining json-abi-with-callpaths flag.
esdrubal Jul 25, 2024
5bad022
Fixes abi generation.
esdrubal Aug 13, 2024
0a7757f
Adds missing fuels entry in Cargo.toml.
esdrubal Aug 13, 2024
4891138
Updates proxy_contract-abi.json
esdrubal Aug 13, 2024
db0ace8
Updates vm opcodes in std and parser.
esdrubal Aug 13, 2024
92e17ad
Updates Cargo.lock.
esdrubal Aug 13, 2024
67b8ac0
Revert "feat: create and deploy a reference proxy contract for contra…
esdrubal Aug 13, 2024
10cd8ec
Fixes build.
esdrubal Aug 13, 2024
93866ff
Moved comments up because of sway formatter.
esdrubal Aug 13, 2024
6217d38
Fixes build.
esdrubal Aug 13, 2024
dbe53c3
Updates changed json abis.
esdrubal Aug 13, 2024
6275234
Replaces static_gas_price with starting_gas_price.
esdrubal Aug 13, 2024
7c3dae2
Replaces Contract::new with Contract::regular.
esdrubal Aug 13, 2024
1d55638
Partially reverts LDC changes.
esdrubal Aug 14, 2024
0d3f777
Updates proxy-abi contract .bin .json.
esdrubal Aug 14, 2024
fa2fe1b
add updated .abi .json files
kayagokalp Aug 14, 2024
49604c1
fix tests by pinning storage slot
kayagokalp Aug 14, 2024
74333f0
Merge branch 'master' into esdrubal/abi_changes
IGI-111 Aug 14, 2024
3ceb59e
Merge branch 'master' into esdrubal/abi_changes
sdankel Aug 14, 2024
d340feb
add len parameter and tests
SwayStar123 Aug 15, 2024
3b3b92a
Merge branch 'master' into swaystar123/variablelengthed_verify
Voxelot Aug 15, 2024
a9bcbf8
Merge branch 'master' into swaystar123/variablelengthed_verify
SwayStar123 Aug 16, 2024
3ed7e1b
change ed_verify to take in msg instead of hash
SwayStar123 Aug 16, 2024
35e980e
remove len parameter, add 0 len fail check
SwayStar123 Aug 16, 2024
238da66
remove unnecessary msg hashes
SwayStar123 Aug 16, 2024
2fe1a25
rewrite test
SwayStar123 Aug 16, 2024
a5b2bea
fmt
SwayStar123 Aug 16, 2024
4cd64fb
use fixed private key in rust code to generate sigs
SwayStar123 Aug 16, 2024
97dfaaa
Merge branch 'master' into swaystar123/variablelengthed_verify
SwayStar123 Aug 16, 2024
c4480fa
manual fmt
SwayStar123 Aug 16, 2024
a4c80a4
remove tab
SwayStar123 Aug 16, 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
29 changes: 17 additions & 12 deletions sway-lib-std/src/ecr.sw
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ library;

use ::address::Address;
use ::b512::B512;
use ::bytes::Bytes;
use ::registers::error;
use ::hash::*;
use ::result::Result::{self, *};
Expand All @@ -11,6 +12,8 @@ use ::result::Result::{self, *};
pub enum EcRecoverError {
/// The error variant used when the recover fails.
UnrecoverablePublicKey: (),
/// The length of the message is zero.
ZeroLengthMessage: (),
}

/// Recover the public key derived from the private key used to sign a message.
Expand Down Expand Up @@ -129,8 +132,8 @@ pub fn ec_recover_r1(signature: B512, msg_hash: b256) -> Result<B512, EcRecoverE
/// # Arguments
///
/// * `public_key`: [b256] - The public key that signed the message.
/// * `signature`: [B512] - The signature generated by signing a message hash.
/// * `msg_hash`: [b256] - The hashed signed data.
/// * `signature`: [B512] - The signature generated by signing a message.
/// * `msg`: [Bytes] - The signed data.
///
/// # Returns
///
Expand All @@ -139,7 +142,7 @@ pub fn ec_recover_r1(signature: B512, msg_hash: b256) -> Result<B512, EcRecoverE
/// # Examples
///
/// ```sway
/// use std::{ecr::ed_verify, b512::B512};
/// use std::{ecr::ed_verify, b512::B512, bytes::Bytes};
///
/// fn foo() {
/// let pub_key = 0x314fa58689bbe1da2430517de2d772b384a1c1d2e9cb87e73c6afcf246045b10;
Expand All @@ -150,22 +153,24 @@ pub fn ec_recover_r1(signature: B512, msg_hash: b256) -> Result<B512, EcRecoverE
/// let lo = 0xf5084560039486d3462dd65a40c80a74709b2f06d450ffc5dc00345c6b2cdd00;
/// let signature: B512 = B512::from((hi, lo));
/// // A verified public key with signature
/// let verified = ed_verify(pub_key, signature, msg_hash).unwrap();
/// let verified = ed_verify(pub_key, signature, Bytes::from(msg_hash)).unwrap();
/// assert(verified);
/// }
/// ```
pub fn ed_verify(
public_key: b256,
signature: B512,
msg_hash: b256,
) -> Result<bool, EcRecoverError> {
pub fn ed_verify(public_key: b256, signature: B512, msg: Bytes) -> Result<bool, EcRecoverError> {
let len = msg.len();

if len == 0 {
return Err(EcRecoverError::ZeroLengthMessage);
};

let was_error = asm(
buffer: public_key,
sig: __addr_of(signature),
hash: msg_hash,
len: 32,
msg: msg.ptr(),
len: len,
) {
ed19 buffer sig hash len;
ed19 buffer sig msg len;
err
};
// check the $err register to see if the `ed19` opcode succeeded
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ library;

use std::{
b512::B512,
bytes::Bytes,
ecr::{
ec_recover,
ec_recover_address,
Expand Down Expand Up @@ -106,28 +107,103 @@ fn ecr_ec_recover_address_r1() {
assert(result_2.is_err());
}

#[test]
fn ecr_ed_verify() {
let pub_key_1 = 0x314fa58689bbe1da2430517de2d772b384a1c1d2e9cb87e73c6afcf246045b10;
let msg_1 = b256::zero();
let msg_hash_1 = sha256(msg_1);
// Calculated with ed25519-dalek in a rust program

let hi_1 = 0xf38cef9361894be6c6e0eddec28a663d099d7ddff17c8077a1447d7ecb4e6545;
let lo_1 = 0xf5084560039486d3462dd65a40c80a74709b2f06d450ffc5dc00345c6b2cdd00;
let signature_1: B512 = B512::from((hi_1, lo_1));
// A verified public key with signature
let verified_1 = ed_verify(pub_key_1, signature_1, msg_hash_1);
assert(verified_1.is_ok());
assert(verified_1.unwrap());
// use ed25519_dalek::ed25519::signature::Signer;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just checking: did you mean to leave this old test in a comment?

// use ed25519_dalek::SECRET_KEY_LENGTH;
// use ed25519_dalek::SigningKey;

let pub_key_2 = 0x314fa58689bbe1da2430517de2d772b384a1c1d2e9cb87e73c6afcf246045b10;
let msg_2 = b256::zero();
let msg_hash_2 = sha256(msg_2);
// fn main() {
// let bytes = hex::decode("638aa7abd1acd372c1ab3bc4951d9df3b33eabb2c019bf60a8c1ff2e424adeb67127a92630327cfa3fac37b0dcc969968da0efb18bbbbf498c16966373973b21").unwrap();
// let bytes: [u8; 64] = bytes.try_into().unwrap();
// let signing_key: SigningKey = SigningKey::from_keypair_bytes(&bytes).unwrap();

let hi_2 = b256::zero();
let lo_2 = 0xf5084560039486d3462dd65a40c80a74709b2f06d450ffc5dc00345c6b2cdd00;
let signature_2: B512 = B512::from((hi_2, lo_2));
let verified_2 = ed_verify(pub_key_2, signature_2, msg_hash_2);
// let keypair_bytes = signing_key.to_keypair_bytes();
// let secret_key = &keypair_bytes[..SECRET_KEY_LENGTH];
// let public_key = &keypair_bytes[SECRET_KEY_LENGTH..];

// println!("Secret Key: {}", hex::encode(secret_key));
// println!("Public Key: {}", hex::encode(public_key));


// for x in [1, 16, 32, 64] {
// let mut bytes = Vec::new();

// for i in 0..x {
// bytes.push(i as u8);
// }

assert(verified_2.is_err());
// let bytes: &[u8] = &bytes;

// let signature = signing_key.sign(bytes);
// let signature = signature.to_bytes();
// let lo: [u8; 32] = signature[0..32].try_into().unwrap();
// let hi: [u8; 32] = signature[32..64].try_into().unwrap();

// println!("x = {}, ({}, {})", x, hex::encode(lo), hex::encode(hi));
// }
// }

// Secret Key: 638aa7abd1acd372c1ab3bc4951d9df3b33eabb2c019bf60a8c1ff2e424adeb6
// Public Key: 7127a92630327cfa3fac37b0dcc969968da0efb18bbbbf498c16966373973b21
// x = 1, (f5a5aafe874a12bf3460b0a31428306a3c0bf148b23c0726add73f149fb4238f, 11fd17bd7e9e64878f1cf680c316df925ff29784798cca9c8b70209f58fc6004)
// x = 16, (5573fe0bf140c8f1ca1b6b41fd4dc0bfcf92aefc67ab7dfd8aac1c264a66e67f, b47ed5cd8285cc2e8bf4a24a5e923a543278c43630f6e3d3da5a884de4982406)
// x = 32, (00d8a17c74a926854155f0092fe8c2db55220cff891a38f0ee00e549fec8ba07, f2dda3573b2f03d19eefebf93aa93d4ebca81e2c42de5b0f52d8c957f6390a0b)
// x = 64, (9a9e7077c905c855c86fb6aea6052f50a2cf29f70205f465d809cb0b81c6503f, fea5d320a5f9d4164b7eca627d3e81293083e7f6682b3b1ebc257459fcf89b08)
#[test]
fn ecr_ed_verify() {
let pub_key = 0x7127a92630327cfa3fac37b0dcc969968da0efb18bbbbf498c16966373973b21;

let lens = [1, 16, 32, 64];
let sigs = [
(
0xf5a5aafe874a12bf3460b0a31428306a3c0bf148b23c0726add73f149fb4238f,
0x11fd17bd7e9e64878f1cf680c316df925ff29784798cca9c8b70209f58fc6004,
),
(
0x5573fe0bf140c8f1ca1b6b41fd4dc0bfcf92aefc67ab7dfd8aac1c264a66e67f,
0xb47ed5cd8285cc2e8bf4a24a5e923a543278c43630f6e3d3da5a884de4982406,
),
(
0x00d8a17c74a926854155f0092fe8c2db55220cff891a38f0ee00e549fec8ba07,
0xf2dda3573b2f03d19eefebf93aa93d4ebca81e2c42de5b0f52d8c957f6390a0b,
),
(
0x9a9e7077c905c855c86fb6aea6052f50a2cf29f70205f465d809cb0b81c6503f,
0xfea5d320a5f9d4164b7eca627d3e81293083e7f6682b3b1ebc257459fcf89b08,
),
];

let mut i = 0;
while i < 4 {
let len = lens[i];
let sig = B512::from((sigs[i].0, sigs[i].1));

let mut msg = Bytes::new();
let mut j = 0_u8;
while j < len {
msg.push(j);
j += 1;
}

let verified = ed_verify(pub_key, sig, msg);
assert(verified.is_ok());
assert(verified.unwrap());

i += 1;
}
}

#[test]
fn ecr_ed_verify_fail() {
let pub_key = 0x7127a92630327cfa3fac37b0dcc969968da0efb18bbbbf498c16966373973b21;
let msg = Bytes::new();
let sig = B512::from((
0x19d821bfe7da223e53428b72a59e316c6981fcbba63dff89a11f01ce3d33af44,
0xb49089aa12883bfffda92f3aadfd9153f654fb235baef6ab7958c6029fa35f0a,
));

let verified = ed_verify(pub_key, sig, msg);
// Should return error for msg len 0
assert(verified.is_err());
}
Loading