Merge pull request #50 from vacp2p/sc_core_crypto

Sc core cryptography and usage
This commit is contained in:
tyshko-rostyslav 2025-03-17 07:16:21 -04:00 committed by GitHub
commit 90c8b50689
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 295 additions and 86 deletions

262
Cargo.lock generated
View File

@ -351,6 +351,12 @@ dependencies = [
"memchr",
]
[[package]]
name = "allocator-api2"
version = "0.2.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923"
[[package]]
name = "anyhow"
version = "1.0.96"
@ -381,9 +387,20 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a22f4561524cd949590d78d7d4c5df8f592430d221f7f3c9497bbafd8972120f"
dependencies = [
"ark-ec",
"ark-ff",
"ark-std",
"ark-ec 0.4.2",
"ark-ff 0.4.2",
"ark-std 0.4.0",
]
[[package]]
name = "ark-bn254"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d69eab57e8d2663efa5c63135b2af4f396d66424f88954c21104125ab6b3e6bc"
dependencies = [
"ark-ec 0.5.0",
"ark-ff 0.5.0",
"ark-std 0.5.0",
]
[[package]]
@ -392,12 +409,12 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f3a13b34da09176a8baba701233fdffbaa7c1b1192ce031a3da4e55ce1f1a56"
dependencies = [
"ark-ec",
"ark-ff",
"ark-ec 0.4.2",
"ark-ff 0.4.2",
"ark-relations",
"ark-serialize",
"ark-serialize 0.4.2",
"ark-snark",
"ark-std",
"ark-std 0.4.0",
"blake2",
"derivative",
"digest 0.10.7",
@ -410,10 +427,10 @@ version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "defd9a439d56ac24968cca0571f598a61bc8c55f71d50a89cda591cb750670ba"
dependencies = [
"ark-ff",
"ark-poly",
"ark-serialize",
"ark-std",
"ark-ff 0.4.2",
"ark-poly 0.4.2",
"ark-serialize 0.4.2",
"ark-std 0.4.0",
"derivative",
"hashbrown 0.13.2",
"itertools 0.10.5",
@ -421,16 +438,37 @@ dependencies = [
"zeroize",
]
[[package]]
name = "ark-ec"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43d68f2d516162846c1238e755a7c4d131b892b70cc70c471a8e3ca3ed818fce"
dependencies = [
"ahash 0.8.11",
"ark-ff 0.5.0",
"ark-poly 0.5.0",
"ark-serialize 0.5.0",
"ark-std 0.5.0",
"educe",
"fnv",
"hashbrown 0.15.2",
"itertools 0.13.0",
"num-bigint 0.4.6",
"num-integer",
"num-traits",
"zeroize",
]
[[package]]
name = "ark-ff"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec847af850f44ad29048935519032c33da8aa03340876d351dfab5660d2966ba"
dependencies = [
"ark-ff-asm",
"ark-ff-macros",
"ark-serialize",
"ark-std",
"ark-ff-asm 0.4.2",
"ark-ff-macros 0.4.2",
"ark-serialize 0.4.2",
"ark-std 0.4.0",
"derivative",
"digest 0.10.7",
"itertools 0.10.5",
@ -441,6 +479,26 @@ dependencies = [
"zeroize",
]
[[package]]
name = "ark-ff"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a177aba0ed1e0fbb62aa9f6d0502e9b46dad8c2eab04c14258a1212d2557ea70"
dependencies = [
"ark-ff-asm 0.5.0",
"ark-ff-macros 0.5.0",
"ark-serialize 0.5.0",
"ark-std 0.5.0",
"arrayvec 0.7.6",
"digest 0.10.7",
"educe",
"itertools 0.13.0",
"num-bigint 0.4.6",
"num-traits",
"paste 1.0.15",
"zeroize",
]
[[package]]
name = "ark-ff-asm"
version = "0.4.2"
@ -451,6 +509,16 @@ dependencies = [
"syn 1.0.109",
]
[[package]]
name = "ark-ff-asm"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62945a2f7e6de02a31fe400aa489f0e0f5b2502e69f95f853adb82a96c7a6b60"
dependencies = [
"quote",
"syn 2.0.98",
]
[[package]]
name = "ark-ff-macros"
version = "0.4.2"
@ -464,6 +532,19 @@ dependencies = [
"syn 1.0.109",
]
[[package]]
name = "ark-ff-macros"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09be120733ee33f7693ceaa202ca41accd5653b779563608f1234f78ae07c4b3"
dependencies = [
"num-bigint 0.4.6",
"num-traits",
"proc-macro2",
"quote",
"syn 2.0.98",
]
[[package]]
name = "ark-groth16"
version = "0.4.0"
@ -471,12 +552,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "20ceafa83848c3e390f1cbf124bc3193b3e639b3f02009e0e290809a501b95fc"
dependencies = [
"ark-crypto-primitives",
"ark-ec",
"ark-ff",
"ark-poly",
"ark-ec 0.4.2",
"ark-ff 0.4.2",
"ark-poly 0.4.2",
"ark-relations",
"ark-serialize",
"ark-std",
"ark-serialize 0.4.2",
"ark-std 0.4.0",
]
[[package]]
@ -485,21 +566,36 @@ version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d320bfc44ee185d899ccbadfa8bc31aab923ce1558716e1997a1e74057fe86bf"
dependencies = [
"ark-ff",
"ark-serialize",
"ark-std",
"ark-ff 0.4.2",
"ark-serialize 0.4.2",
"ark-std 0.4.0",
"derivative",
"hashbrown 0.13.2",
]
[[package]]
name = "ark-poly"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "579305839da207f02b89cd1679e50e67b4331e2f9294a57693e5051b7703fe27"
dependencies = [
"ahash 0.8.11",
"ark-ff 0.5.0",
"ark-serialize 0.5.0",
"ark-std 0.5.0",
"educe",
"fnv",
"hashbrown 0.15.2",
]
[[package]]
name = "ark-relations"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "00796b6efc05a3f48225e59cb6a2cda78881e7c390872d5786aaf112f31fb4f0"
dependencies = [
"ark-ff",
"ark-std",
"ark-ff 0.4.2",
"ark-std 0.4.0",
"tracing",
"tracing-subscriber",
]
@ -510,8 +606,21 @@ version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "adb7b85a02b83d2f22f89bd5cac66c9c89474240cb6207cb1efc16d098e822a5"
dependencies = [
"ark-serialize-derive",
"ark-std",
"ark-serialize-derive 0.4.2",
"ark-std 0.4.0",
"digest 0.10.7",
"num-bigint 0.4.6",
]
[[package]]
name = "ark-serialize"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f4d068aaf107ebcd7dfb52bc748f8030e0fc930ac8e360146ca54c1203088f7"
dependencies = [
"ark-serialize-derive 0.5.0",
"ark-std 0.5.0",
"arrayvec 0.7.6",
"digest 0.10.7",
"num-bigint 0.4.6",
]
@ -527,16 +636,27 @@ dependencies = [
"syn 1.0.109",
]
[[package]]
name = "ark-serialize-derive"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "213888f660fddcca0d257e88e54ac05bca01885f258ccdf695bafd77031bb69d"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.98",
]
[[package]]
name = "ark-snark"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "84d3cc6833a335bb8a600241889ead68ee89a3cf8448081fb7694c0fe503da63"
dependencies = [
"ark-ff",
"ark-ff 0.4.2",
"ark-relations",
"ark-serialize",
"ark-std",
"ark-serialize 0.4.2",
"ark-std 0.4.0",
]
[[package]]
@ -549,6 +669,16 @@ dependencies = [
"rand 0.8.5",
]
[[package]]
name = "ark-std"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "246a225cc6131e9ee4f24619af0f19d67761fff15d7ccc22e42b80846e69449a"
dependencies = [
"num-traits",
"rand 0.8.5",
]
[[package]]
name = "arrayref"
version = "0.3.9"
@ -570,6 +700,12 @@ version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b"
[[package]]
name = "arrayvec"
version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50"
[[package]]
name = "atty"
version = "0.2.14"
@ -1424,6 +1560,18 @@ dependencies = [
"spki",
]
[[package]]
name = "educe"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d7bc049e1bd8cdeb31b68bbd586a9464ecf9f3944af3958a7a9d0f8b9799417"
dependencies = [
"enum-ordinalize",
"proc-macro2",
"quote",
"syn 2.0.98",
]
[[package]]
name = "either"
version = "1.14.0"
@ -1484,6 +1632,26 @@ dependencies = [
"syn 2.0.98",
]
[[package]]
name = "enum-ordinalize"
version = "4.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fea0dcfa4e54eeb516fe454635a95753ddd39acda650ce703031c6973e315dd5"
dependencies = [
"enum-ordinalize-derive",
]
[[package]]
name = "enum-ordinalize-derive"
version = "4.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d28318a75d4aead5c4db25382e8ef717932d0346600cacae6357eb5941bc5ff"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.98",
]
[[package]]
name = "env_logger"
version = "0.10.2"
@ -1901,6 +2069,9 @@ name = "hashbrown"
version = "0.15.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289"
dependencies = [
"allocator-api2",
]
[[package]]
name = "heck"
@ -2323,6 +2494,15 @@ dependencies = [
"either",
]
[[package]]
name = "itertools"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186"
dependencies = [
"either",
]
[[package]]
name = "itertools"
version = "0.14.0"
@ -2478,6 +2658,18 @@ dependencies = [
"vcpkg",
]
[[package]]
name = "light-poseidon"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39e3d87542063daaccbfecd78b60f988079b6ec4e089249658b9455075c78d42"
dependencies = [
"ark-bn254 0.5.0",
"ark-ff 0.5.0",
"num-bigint 0.4.6",
"thiserror 1.0.69",
]
[[package]]
name = "linux-raw-sys"
version = "0.4.15"
@ -2741,6 +2933,7 @@ dependencies = [
"reqwest 0.11.27",
"risc0-zkvm",
"rpc_primitives",
"sc_core",
"secp256k1-zkp",
"serde",
"serde_json",
@ -3792,10 +3985,10 @@ version = "1.2.5"
source = "git+https://github.com/risc0/risc0.git?branch=release-1.2#2225069ebc465320695ef3d5e028f6072e103718"
dependencies = [
"anyhow",
"ark-bn254",
"ark-ec",
"ark-bn254 0.4.0",
"ark-ec 0.4.2",
"ark-groth16",
"ark-serialize",
"ark-serialize 0.4.2",
"bytemuck",
"hex",
"num-bigint 0.4.6",
@ -4069,12 +4262,15 @@ version = "0.1.0"
dependencies = [
"accounts",
"anyhow",
"ark-bn254 0.5.0",
"ark-ff 0.5.0",
"bincode",
"common",
"elliptic-curve",
"env_logger",
"hex",
"k256",
"light-poseidon",
"log",
"monotree",
"rand 0.8.5",

View File

@ -44,6 +44,9 @@ toml = "0.7.4"
secp256k1-zkp = "0.11.0"
bincode = "1.3.3"
tempfile = "3.14.0"
light-poseidon = "0.3.0"
ark-bn254 = "0.5.0"
ark-ff = "0.5.0"
rocksdb = { version = "0.21.0", default-features = false, features = [
"snappy",

View File

@ -23,6 +23,9 @@ risc0-zkvm = { git = "https://github.com/risc0/risc0.git", branch = "release-1.2
hex.workspace = true
actix-rt.workspace = true
[dependencies.sc_core]
path = "../sc_core"
[dependencies.accounts]
path = "../accounts"

View File

@ -8,15 +8,10 @@ use k256::elliptic_curve::group::GroupEncoding;
use ::storage::transaction::{Transaction, TransactionPayload, TxKind};
use accounts::account_core::{Account, AccountAddress};
use anyhow::{anyhow, Result};
use anyhow::Result;
use config::NodeConfig;
use executions::{
private_exec::{generate_commitments, generate_nullifiers},
se::{commit, tag_random},
};
use executions::private_exec::{generate_commitments, generate_nullifiers};
use log::info;
use rand::thread_rng;
use secp256k1_zkp::{CommitmentSecrets, Tweak};
use sequencer_client::{json::SendTxResponse, SequencerClient};
use serde::{Deserialize, Serialize};
use storage::NodeChainStore;
@ -207,7 +202,10 @@ impl NodeCore {
.map(|hash_data| hash_data.try_into().unwrap())
.collect(),
nullifier_created_hashes: vec![],
execution_proof_private: hex::encode(serde_json::to_vec(&receipt).unwrap()),
execution_proof_private: sc_core::transaction_payloads_tools::encode_receipt(
receipt,
)
.unwrap(),
encoded_data: vec![(encoded_data.0, encoded_data.1.to_vec())],
ephemeral_pub_key: eph_pub_key.to_vec(),
}
@ -259,7 +257,10 @@ impl NodeCore {
.map(|hash_data| hash_data.try_into().unwrap())
.collect(),
nullifier_created_hashes: vec![],
execution_proof_private: hex::encode(serde_json::to_vec(&receipt).unwrap()),
execution_proof_private: sc_core::transaction_payloads_tools::encode_receipt(
receipt,
)
.unwrap(),
encoded_data,
ephemeral_pub_key: eph_pub_key.to_vec(),
}
@ -268,24 +269,6 @@ impl NodeCore {
))
}
pub fn deposit_money_public(&self, acc: AccountAddress, amount: u128) -> Transaction {
TransactionPayload {
tx_kind: TxKind::Public,
execution_input: serde_json::to_vec(&ActionData::MintMoneyPublicTx(
MintMoneyPublicTx { acc, amount },
))
.unwrap(),
execution_output: vec![],
utxo_commitments_spent_hashes: vec![],
utxo_commitments_created_hashes: vec![],
nullifier_created_hashes: vec![],
execution_proof_private: "".to_string(),
encoded_data: vec![],
ephemeral_pub_key: vec![],
}
.into()
}
pub async fn transfer_utxo_private(
&self,
utxo: UTXO,
@ -350,7 +333,10 @@ impl NodeCore {
.map(|hash_data| hash_data.try_into().unwrap())
.collect(),
nullifier_created_hashes: vec![nullifier.try_into().unwrap()],
execution_proof_private: hex::encode(serde_json::to_vec(&receipt).unwrap()),
execution_proof_private: sc_core::transaction_payloads_tools::encode_receipt(
receipt,
)
.unwrap(),
encoded_data,
ephemeral_pub_key: eph_pub_key.to_vec(),
}
@ -449,7 +435,10 @@ impl NodeCore {
.map(|hash_data| hash_data.try_into().unwrap())
.collect(),
nullifier_created_hashes: nullifiers,
execution_proof_private: hex::encode(serde_json::to_vec(&receipt).unwrap()),
execution_proof_private: sc_core::transaction_payloads_tools::encode_receipt(
receipt,
)
.unwrap(),
encoded_data,
ephemeral_pub_key: eph_pub_key.to_vec(),
}
@ -467,29 +456,16 @@ impl NodeCore {
) -> Result<(Transaction, Vec<(AccountAddress, [u8; 32])>), ExecutionFailureKind> {
let acc_map_read_guard = self.storage.read().await;
let accout = acc_map_read_guard.acc_map.get(&acc).unwrap();
let account = acc_map_read_guard.acc_map.get(&acc).unwrap();
let commitment_secrets = CommitmentSecrets {
value: balance,
value_blinding_factor: Tweak::from_slice(
&accout
.key_holder
.utxo_secret_key_holder
.viewing_secret_key
.to_bytes()
.to_vec(),
)
.map_err(|err| anyhow!("{:?}", err))
.map_err(ExecutionFailureKind::write_error)?,
generator_blinding_factor: Tweak::new(&mut thread_rng()),
};
let tag = tag_random();
let commitment = commit(&commitment_secrets, tag);
let commitment = sc_core::transaction_payloads_tools::generate_secret_random_commitment(
balance, account,
)
.unwrap();
let nullifier = executions::se::generate_nullifiers(
&commitment,
&accout
&account
.key_holder
.utxo_secret_key_holder
.nullifier_secret_key
@ -508,7 +484,7 @@ impl NodeCore {
.map(|(utxo, _)| utxo.clone())
.collect();
let ephm_key_holder = &accout.produce_ephemeral_key_holder();
let ephm_key_holder = &account.produce_ephemeral_key_holder();
ephm_key_holder.log();
let eph_pub_key = ephm_key_holder.generate_ephemeral_public_key().to_bytes();
@ -547,7 +523,10 @@ impl NodeCore {
.map(|hash_data| hash_data.try_into().unwrap())
.collect(),
nullifier_created_hashes: vec![nullifier.try_into().unwrap()],
execution_proof_private: hex::encode(serde_json::to_vec(&receipt).unwrap()),
execution_proof_private: sc_core::transaction_payloads_tools::encode_receipt(
receipt,
)
.unwrap(),
encoded_data,
ephemeral_pub_key: eph_pub_key.to_vec(),
}
@ -596,7 +575,8 @@ impl NodeCore {
utxo_commitments_spent_hashes: vec![commitment_in],
utxo_commitments_created_hashes: vec![],
nullifier_created_hashes: vec![nullifier.try_into().unwrap()],
execution_proof_private: hex::encode(serde_json::to_vec(&receipt).unwrap()),
execution_proof_private: sc_core::transaction_payloads_tools::encode_receipt(receipt)
.unwrap(),
encoded_data: vec![],
ephemeral_pub_key: vec![],
}
@ -664,7 +644,15 @@ impl NodeCore {
//Considering proof time, needs to be done before proof
let tx_roots = self.get_roots().await;
let tx = self.deposit_money_public(acc, amount);
let tx: Transaction =
sc_core::transaction_payloads_tools::create_public_transaction_payload(
serde_json::to_vec(&ActionData::MintMoneyPublicTx(MintMoneyPublicTx {
acc,
amount,
}))
.unwrap(),
)
.into();
tx.log();
Ok(self.sequencer_client.send_tx(tx, tx_roots).await?)
@ -1168,7 +1156,10 @@ impl NodeCore {
.map(|hash_data| hash_data.try_into().unwrap())
.collect(),
nullifier_created_hashes: vec![nullifier.try_into().unwrap()],
execution_proof_private: hex::encode(serde_json::to_vec(&receipt).unwrap()),
execution_proof_private: sc_core::transaction_payloads_tools::encode_receipt(
receipt,
)
.unwrap(),
encoded_data,
ephemeral_pub_key: eph_pub_key.to_vec(),
}

View File

@ -16,6 +16,9 @@ monotree.workspace = true
bincode.workspace = true
elliptic-curve.workspace = true
hex.workspace = true
light-poseidon.workspace = true
ark-bn254.workspace = true
ark-ff.workspace = true
risc0-zkvm = { git = "https://github.com/risc0/risc0.git", branch = "release-1.2" }

View File

@ -0,0 +1,12 @@
use ark_bn254::Fr;
// use ark_ff::{BigInteger, PrimeField};
use light_poseidon::{Poseidon, PoseidonBytesHasher};
#[allow(unused)]
fn poseidon_hash(inputs: &[&[u8]]) -> anyhow::Result<[u8; 32]> {
let mut poseidon = Poseidon::<Fr>::new_circom(2).unwrap();
let hash = poseidon.hash_bytes_be(inputs)?;
Ok(hash)
}

View File

@ -1,3 +1,4 @@
pub mod cryptography;
pub mod proofs_circuits;
pub mod transaction_payloads_tools;
pub mod utxo_manipulator;