Skip to content

Commit

Permalink
Back to stable version (#65)
Browse files Browse the repository at this point in the history
* back to stable version

* ci test

* ci test 02

* ci test 03
  • Loading branch information
maciejka authored May 21, 2024
1 parent ec96e88 commit b149384
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 10 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/starknet-contracts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ jobs:
steps:
- uses: actions/checkout@v3
- uses: software-mansion/setup-scarb@v1
with:
scarb-version: 'nightly'
- name: Scarb version
run: scarb --version
working-directory: onchain
- name: Check cairo format
run: scarb fmt --check
working-directory: onchain
Expand All @@ -23,8 +24,6 @@ jobs:
steps:
- uses: actions/checkout@v3
- uses: software-mansion/setup-scarb@v1
with:
scarb-version: 'nightly'
- name: Run cairo tests
run: scarb test
working-directory: onchain
53 changes: 53 additions & 0 deletions onchain/Scarb.lock
Original file line number Diff line number Diff line change
@@ -1,10 +1,63 @@
# Code generated by scarb DO NOT EDIT.
version = 1

[[package]]
name = "alexandria_bytes"
version = "0.1.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git#78b0d5114e7dbd71e79c4504e8ecb56d9b5c6995"
dependencies = [
"alexandria_data_structures",
"alexandria_math",
]

[[package]]
name = "alexandria_data_structures"
version = "0.2.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git#78b0d5114e7dbd71e79c4504e8ecb56d9b5c6995"
dependencies = [
"alexandria_encoding",
]

[[package]]
name = "alexandria_encoding"
version = "0.1.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git#78b0d5114e7dbd71e79c4504e8ecb56d9b5c6995"
dependencies = [
"alexandria_bytes",
"alexandria_math",
"alexandria_numeric",
]

[[package]]
name = "alexandria_math"
version = "0.2.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git#78b0d5114e7dbd71e79c4504e8ecb56d9b5c6995"
dependencies = [
"alexandria_data_structures",
]

[[package]]
name = "alexandria_numeric"
version = "0.1.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git#78b0d5114e7dbd71e79c4504e8ecb56d9b5c6995"
dependencies = [
"alexandria_math",
"alexandria_searching",
]

[[package]]
name = "alexandria_searching"
version = "0.1.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git#78b0d5114e7dbd71e79c4504e8ecb56d9b5c6995"
dependencies = [
"alexandria_data_structures",
]

[[package]]
name = "joyboy"
version = "0.1.0"
dependencies = [
"alexandria_math",
"snforge_std",
]

Expand Down
1 change: 1 addition & 0 deletions onchain/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2023_11"

[dependencies]
starknet = "2.6.3"
alexandria_math = { git = "https://github.com/keep-starknet-strange/alexandria.git" }

[dev-dependencies]
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry", tag = "v0.21.0" }
Expand Down
60 changes: 58 additions & 2 deletions onchain/src/bip340.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,74 @@
use core::byte_array::ByteArrayTrait;
use core::option::OptionTrait;
use core::result::ResultTrait;
use core::sha256::compute_sha256_byte_array;
// TODO: uncomment once Cairo 2.7 is available
// use core::sha256::compute_sha256_byte_array;
use core::starknet::SyscallResultTrait;
use core::to_byte_array::AppendFormattedToByteArray;
use core::to_byte_array::{AppendFormattedToByteArray, FormatAsByteArray};
use core::traits::Into;
use starknet::{secp256k1::{Secp256k1Point}, secp256_trait::{Secp256Trait, Secp256PointTrait}};

use alexandria_math::sha256::sha256;
use joyboy::utils::{shl, shr};

const TWO_POW_32: u128 = 0x100000000;
const TWO_POW_64: u128 = 0x10000000000000000;
const TWO_POW_96: u128 = 0x1000000000000000000000000;

const p: u256 = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F;

fn compute_sha256_byte_array(m: @ByteArray) -> [u32; 8] {
let mut ba = ArrayTrait::new();
let len = m.len();
let mut i = 0;
loop {
if i == len {
break ();
}
ba.append(m.at(i).unwrap());
i += 1;
};

let sha = sha256(ba);

let r = [
shl((*sha.at(0)).into(), 24_u32)
+ shl((*sha.at(1)).into(), 16_u32)
+ shl((*sha.at(2)).into(), 8_u32)
+ (*sha.at(3)).into(),
shl((*sha.at(4)).into(), 24_u32)
+ shl((*sha.at(5)).into(), 16_u32)
+ shl((*sha.at(6)).into(), 8_u32)
+ (*sha.at(7)).into(),
shl((*sha.at(8)).into(), 24_u32)
+ shl((*sha.at(9)).into(), 16_u32)
+ shl((*sha.at(10)).into(), 8_u32)
+ (*sha.at(11)).into(),
shl((*sha.at(12)).into(), 24_u32)
+ shl((*sha.at(13)).into(), 16_u32)
+ shl((*sha.at(14)).into(), 8_u32)
+ (*sha.at(15)).into(),
shl((*sha.at(16)).into(), 24_u32)
+ shl((*sha.at(17)).into(), 16_u32)
+ shl((*sha.at(18)).into(), 8_u32)
+ (*sha.at(19)).into(),
shl((*sha.at(20)).into(), 24_u32)
+ shl((*sha.at(21)).into(), 16_u32)
+ shl((*sha.at(22)).into(), 8_u32)
+ (*sha.at(23)).into(),
shl((*sha.at(24)).into(), 24_u32)
+ shl((*sha.at(25)).into(), 16_u32)
+ shl((*sha.at(26)).into(), 8_u32)
+ (*sha.at(27)).into(),
shl((*sha.at(28)).into(), 24_u32)
+ shl((*sha.at(29)).into(), 16_u32)
+ shl((*sha.at(30)).into(), 8_u32)
+ (*sha.at(31)).into(),
];

r
}

/// Computes BIP0340/challenge tagged hash.
///
/// References:
Expand Down
3 changes: 2 additions & 1 deletion onchain/src/social/bech32.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! bech32 encoding implementation

use core::traits::{Into, TryInto};
use core::array::ToSpanTrait;
// TODO: uncomment once Cairo 2.7 is available
// use core::array::ToSpanTrait;
use core::option::OptionTrait;
use core::array::ArrayTrait;
use core::byte_array::ByteArrayTrait;
Expand Down
3 changes: 2 additions & 1 deletion onchain/src/social/profile.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use core::option::OptionTrait;
use core::traits::TryInto;
use core::byte_array::ByteArrayTrait;
use core::array::SpanTrait;
use core::array::ToSpanTrait;
// TODO: uncomment once Cairo 2.7 is available
// use core::array::ToSpanTrait;

//! Representation of Nostr profiles

Expand Down
4 changes: 2 additions & 2 deletions onchain/src/utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl Pow2u8u8 of Pow2<u8, u8> {
5 => 0b100000,
6 => 0b1000000,
7 => 0b10000000,
_ => core::panic_with_felt252('n = {n} ouf of range'),
_ => core::panic_with_felt252('n ouf of range'),
}
}
}
Expand Down Expand Up @@ -56,7 +56,7 @@ impl Pow2u32u32 of Pow2<u32, u32> {
29 => 0b100000000000000000000000000000,
30 => 0b1000000000000000000000000000000,
31 => 0b10000000000000000000000000000000,
_ => core::panic_with_felt252('n = {n} ouf of range'),
_ => core::panic_with_felt252('n ouf of range'),
}
}
}
Expand Down

0 comments on commit b149384

Please sign in to comment.