Skip to content

Commit

Permalink
Initial commit of examples
Browse files Browse the repository at this point in the history
  • Loading branch information
codingnirvana committed Aug 14, 2023
0 parents commit 2c95514
Show file tree
Hide file tree
Showing 17 changed files with 328 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "noir_basics"
entry = "hash.nr"
type = "bin"
authors = [""]
compiler_version = "0.9.0"

[dependencies]
u2b = { tag = "v0.3.0-nightly", git = "https://github.com/codingnirvana/noir-u2b" }
58 changes: 58 additions & 0 deletions conversions.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use dep::std;
use dep::std::unsafe::zeroed;
use dep::u2b;

fn main() {

// Field to bit, [u1]
let a: Field = 100;
let a_bits = a.to_be_bits(32);
std::println(slice_to_array(a_bits));

// Field to bytes, [u8]
let a_bytes = a.to_be_bytes(32);
std::println(slice_to_array(a_bytes));

// Field to u32, u4 (beware of underflow)
std::println(a as u32);
std::println(a as u4);


let num: u16 = 256;

let num_arr = u2b::u16_to_u8(num);
std::println(num_arr);

let num: u64 = 123451224112;
std::println(num);

let num_arr: [u8; 8] = u2b::u64_to_u8(num);
std::println(num_arr);

let num: u64 = u8_to_u64(num_arr);
std::println(num);
}

fn slice_to_array<T>(s: [T]) -> [T; 32] {
let mut arr: [T; 32] = [std::unsafe::zeroed(); 32];
for i in 0..32 {
arr[i] = s[i];
}
arr
}

fn u8_to_u64(num_arr: [u8; 8]) -> u64 {
let mut out: u64 = 0;
let mut v: u64 = 1;
for i in 0..8 {
out += num_arr[7 - i] as u64 * v;
v *= 256;
}
out
}


#[test]
fn test_conversions() {
main();
}
8 changes: 8 additions & 0 deletions crypto_primitives/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "crypto_primitives"
entry = "src/hash.nr"
type = "bin"
authors = [""]
compiler_version = "0.9.0"

[dependencies]
58 changes: 58 additions & 0 deletions crypto_primitives/src/address_generation.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

use dep::std;

fn address_from_public_key(pub_key: [u8; 64]) -> Field {
std::println(pub_key);
let hash_of_public_key = std::hash::keccak256(pub_key, 32);
std::println(hash_of_public_key);
let mut result: Field = 0;
let mut v: Field = 1;

for i in 0..20 {
result += hash_of_public_key[31 - i] as Field * v;
v *= 256;
}
result
}

struct key {
public_key_as_hex: str<128>,
public_key_in_u8: [u8; 64],
expected_address_as_hex: str<40>,
expected_address_as_field: Field
}

fn main() {

// Used Python to get the byte array
// >>> public_key = "6d7bac197da6e91f506db699a1844efc93b9bf5508bacb6870a98fc2137c4330741f94af61aa635a44f9059ff2082737d43cfb376124ae8772250b85f656aa1d"
// >>> c2=[int(public_key[i:i+2],16) for i in range(0,len(public_key),2)]
// To convert to int used int("",16)

let keys = [
key {
public_key_as_hex : "6d7bac197da6e91f506db699a1844efc93b9bf5508bacb6870a98fc2137c4330741f94af61aa635a44f9059ff2082737d43cfb376124ae8772250b85f656aa1d",
public_key_in_u8 : [109 as u8, 123, 172, 25, 125, 166, 233, 31, 80, 109, 182, 153, 161, 132, 78, 252, 147, 185, 191, 85, 8, 186, 203, 104, 112, 169, 143, 194, 19, 124, 67, 48, 116, 31, 148, 175, 97, 170, 99, 90, 68, 249, 5, 159, 242, 8, 39, 55, 212, 60, 251, 55, 97, 36, 174, 135, 114, 37, 11, 133, 246, 86, 170, 29],
expected_address_as_hex : "df92c554c0078eec90937d7a5944976ef31d602c",
expected_address_as_field : 1276378040652000083123543424805442496024278818860
},
key {
public_key_as_hex : "3b88b538dff7db813b6c8be6bfce81f6dd9d820213fe9211e9f5a631c360c7ddbb26690ae40eac62e0b5aaf2d8a5c4287e3c383fc1c00916ce12e354e1eb12eb",
public_key_in_u8 : [59 as u8, 136, 181, 56, 223, 247, 219, 129, 59, 108, 139, 230, 191, 206, 129, 246, 221, 157, 130, 2, 19, 254, 146, 17, 233, 245, 166, 49, 195, 96, 199, 221, 187, 38, 105, 10, 228, 14, 172, 98, 224, 181, 170, 242, 216, 165, 196, 40, 126, 60, 56, 63, 193, 192, 9, 22, 206, 18, 227, 84, 225, 235, 18, 235],
expected_address_as_hex : "622CF04ee8659bC45d76deF393077Ddcc5396761",
expected_address_as_field : 560483262129028339421417742434801506800232916833
},

];

for i in 0..2 {
let address = address_from_public_key(keys[i].public_key_in_u8);
std::println(address);
assert(address == keys[i].expected_address_as_field);
}
}

#[test]
fn tst_address_generation() {
main();
}
4 changes: 4 additions & 0 deletions crypto_primitives/src/hash.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {


}
16 changes: 16 additions & 0 deletions enums.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
mod OrderType {
global FULL_OPEN: Field = 0;
global PARTIAL_OPEN: Field = 1;
global FULL_RESTRICTED: Field = 2;
global PARTIAL_RESTRICTED: Field = 3;
global CONTRACT: Field = 4;
}

fn main(order_type: Field) {
assert(order_type != OrderType::FULL_OPEN);
}

#[test]
fn test_enum() {
main(OrderType::PARTIAL_OPEN);
}
7 changes: 7 additions & 0 deletions example/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "example"
type = "bin"
authors = [""]
compiler_version = "0.9.0"

[dependencies]
9 changes: 9 additions & 0 deletions example/Prover.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[[items]]
cost = 1
price = 1
quantity = 1

[[items]]
cost = 8
price = 4
quantity = 2
1 change: 1 addition & 0 deletions example/Verifier.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
return = "0x0000000000000000000000000000000000000000000000000000000000000009"
1 change: 1 addition & 0 deletions example/proofs/example.proof
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1589594ca5395798b473e3813bc41699d2d427bbbf1e04c4a6d104ca6fe1718f22bc271434e273600077d3c91667914b7e4309aca9d2a517b6f201c3d2b6f899260795efa2985777b817f4ca17e9d4f32ae061d514c7dafe52b59649f42cb3b41dc5fdb58b162ae29718a33ef63b20e326a7fc051162ec760bc42f1a07a881320b769904d3b5882157751ae1fc26654ea6bef932707374a200e024caedd006140c40dad60f0f7e68b97c66b1e3a6809a59858605513530cd3bfb565ea513d6e50ba237f5f981c4283ad6ff63d1c84e58dac0d699e4f6a9a7bb0597c8dab237f9044e9fdee2bd4fed740700e51e598915e163a1277f32429adf0c858adc290cdb22f395240385306f40328f375be5a8c0afc4e8556f894aee82c77b4da43a60c00d0fad71846219ca8075742dcddff7cc419899ee28ba993776125c07da1527251f361fa6085b05fdcda67a6e4396fb12a27c081ac656146c6ced88647298fa4f21f07857603bc85c4bbf4e1b013ac10c7334118ea5cf08ceb4bbc0e82041491b0608d6474b285e624d755855bb6ad4b234faccce1f055e91a65db466947e26e90ecbb4e6a1c4d19d650904083ba107ecf52a1e623c368a72c0361e6cff6c35ca2f420c6af1966ff764df5c1585d82f6f6c591f049ce6693b8a86ef6250d94a0e19ec4281677ce187fb3b3a97e665d89e868d99353266fcd4861601c41b126fc524e4e06c687e2ba59ced7ea04a294c41e539e998a07cda70b0eaa405704d24972ef288e7640946dde00deea608d875d6bf91f2a078add3eb85dd99df25e70d6e1b59217b47818057c70da5eb1b50ec62787f4361280ce1c07c657cc54b3e15591004a6f662c4742ac7e3cdebb7580051d4be7d8d3cbdc8eba2c8de9a4919491b05f533392112445cd53293233fe44e7666ae4ea8a465e0e0e4b14a55111a44db0e61c09e474fd72a4b27a7482892dc9e3dd35fa334f2e82ab526d93a0f0c350629c4adef90df035be83b7b793d664d0f977e7713def73d7fe821e38d21cc2f211a00d8637437a672ec0c2a020a5bddff7cf5769eff1a485da0ebc0ccb5e549b51c89dc29fed373f24d47ddb1e61e45fe3313582d089990dc4d25a9029461b4371f8f29f813f8c47a0b2390002143a3f6bcc87b5885b8e2ed72ee2a800febbfe9139c829f4615b7c02140f988565ef1f1d6ca4f95f1cf45601813855ed8c975f03057d1f97f4df38cbdd9f3a764c73ec5d60633e8faa889b1930ae109781bf4930d968c6137ea37a372d531587718ffe6788caa23538c797a624d30ce4d7de2ba0057559fc4670936ebace0dc4f038366eb247c9a66a2dd1cede70bb3a61aff5f2413c5ccbcb215263e880392d0617ad5eb73c942e7b5bc27b38bc27fbdab1bbc281fd6083d6a19d443895f69b9110e091216eb433e7623523c31c4d02fbe5300230d9ee9b1463aad8fa6210d46e0d3090b409ae288e42e427ff9bddc456a053e10e3a2ec3682ea91efbdbd8f0c2ca56f096e6cd766d0bd3fb3ae59e1d89343b12bf728633c0a06b15069be86d470e62a2846f6b7463e0dd52cea367d81b209d91a14d141d8e75c1c46f07c20786051aa1431b83bc3a779387d9742447616a4cf2ebcd3e241b2f8e76a32d6bc5e811961645623d8b6859303554cfd25b1e2b1a804301feba8b7b7da9f28ebdd68cfe43068ad000b539cd4d27c14a736781f07dc0a07ba67f0ee16f78c6f46b4f4a0075c9537c4866a6d8732e6be46db2e5b5e110c1ae91cd072b4e11c91ec2d20202c670d6e965050170954f51ccf3e4ad4c55206a2158f370d7f3034ab2c0fc176ccc70d23b64286717d5fb7503b8ff778b09d063a2bc7e7080855f0ec91c3bb802c0da9d11b916a1fdee22c6473d2c93301e826623471177993814d51880c6da206734982fbd438f639fb31be1cc5d166e64015b6ef60815ad53166fbfc640c404db4ee4d4d7c980eebf3bc1186249ad40a7b1b8e89dcc991344e5442573b981070e11ad811f7aedf9e5426bb25c9511060b02166245911c7936b4188b21323e0940d4762d672c5b050b49164c56e074cb6e5273dbed559fdf2882ecf0ceaafb0b73973ed9aeddc810314fc0e6512bd890d1a0fdf54e43924761479b5a18c80702a88c1c28901813e39935167e67fe497b44615ee62ee407d87c8a0abd70b8bf32f06546dbcc8cdf55a65b8de5d643aadc880245e1c8cfdf91380f2c42e949c49d89f31aec3fd8993e21d0da808666c5bd7d70304deed3b9d51f5af89bddfcc31ae65184296014a48a3d72944313a2b0df97426c6e335f92811f71d0128a9c5cf3af132f9d212bc253e24be239b983bb08a46230c2feda80ef994003a7eb00af5b6e1130b9086b5380730d8027bcfe9d90e0a1ec38568a3dcd111d9eaf3c1db09473d196d3bffc6560b3911b6eba392eb739b13411711a59a3eeb057e69af4c56a24733dc0ec3e5453a2ec05c9584baaa800b125ee26f954d442d44b05a2c5664fc5211994bdf9bb1034fe39ba16ce42f5086226a6e57fe2c3a49393ef9fb5b4174e75ada6d8e4266d2e58ce9f23cb2bbbca60b273d1ffb927a009c6d63a1e1cb398eec0ffaa1105695e5d35716d52e6932a00e4d4a4aa4611bbfc9df6a8d5a4af2a200e1cd2aaaad60c20817d265ee76f48f027e9ec48af69318679ba027266938504fb08d9c9595f93682cf560285aa335402b3d6aa4135ca051ae8982a699ca7ac56549969a20d2350630b47d810cdb79b02e90e8ff77500f1ce35902dacd017085cf8a536ae844d6a434739ad9bf13be2031e4675adb437de81828830f0038664639cb103bafb778423832b832714c0290e46b01fddf8b793518c3245eef0caf1a2a96de63a93a6f0953724c85e6fa24a2bb32fc1d45c3adefe8ef55f7b628a81250da414ba0a5a1af034cb93e4d2d0521b5d21f0cc1ce4a4d775ec4fe0e5f10391fb8eb2e26a96babbb2fb8244b00e34295803e757dab6eda0a86f42f32252720bfb396042c7a031521afeb296b39609
43 changes: 43 additions & 0 deletions example/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Looping - over a range, over a list by index, using for-each style

// Tuples - accessing (.0, .1) and unpacking into several vars (abi does not support tuple types)

// Arrays

use dep::std;

global RATE_IN_PERCENT: Field = 5;

struct Item {
price: Field,
quantity: Field,
cost: Field
}

impl Item {
fn new(price: Field, quantity: Field, cost:Field) -> Item {
Item {price, quantity, cost}
}

fn check_cost(self) -> bool {
self.price * self.quantity == self.cost
}
}

fn main(items: [Item; 2]) -> pub Field {
assert(items.all(|i:Item| i.check_cost()));

let mut total = items.fold(0, |x, i: Item| x + i.cost );
total + (total * 5)/100
}

#[test]
fn test_main() {
let item1 = Item { price: 1, quantity: 1, cost: 1 };
let item2 = Item::new(2, 4, 8);
let items = [item1, item2];
let total = main(items);
std::println(total);
assert (total == 3283236430775891283336960861788591263282254660062405151554730627986371274352);
}

13 changes: 13 additions & 0 deletions fields.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use dep::std;

fn main(x: Field, y: Field) -> pub Field {
x * y
//assert (x * y == z);
}

#[test]
fn main_test() {
let first = main(1, 2);
let second = main(1, 2);
std::println(f"b: {first} , a: {second}");
}
27 changes: 27 additions & 0 deletions generics.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use dep::std;

struct Bar<T> {
one: Field,
two: Field,
other: T
}

impl<T> Bar<T> {
fn get_other(self) -> T {
self.other
}
}

fn main() {
let bar1: Bar<Field> = Bar { one: 1, two: 2, other: 3};
let bar2: Bar<[Field; 1]> = Bar { one: 1, two: 2, other: [0]};
let nested_generics: Bar<Bar<Field>> = Bar { one : 1 , two : 2 , other : Bar { one : 1, two : 2, other: 3}};
assert(bar1.get_other() == bar1.other);
assert(nested_generics.other.other == bar1.get_other());

}

#[test]
fn test_generics() {
main();
}
7 changes: 7 additions & 0 deletions modules/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "modules"
type = "bin"
authors = [""]
compiler_version = "0.9.0"

[dependencies]
7 changes: 7 additions & 0 deletions modules/src/foo.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn hello(x : Field) -> Field {
x
}

fn hi(x : Field) -> Field {
x + 1
}
13 changes: 13 additions & 0 deletions modules/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use dep::std;
mod foo; // <-- declare the foo module

fn main(x: Field, y: pub Field) {
assert(x != foo::hello(y));
assert(x != foo::hi(y));
}

#[test]
fn test_main() {
std::println("In modules");
main(1, 3);
}
47 changes: 47 additions & 0 deletions slices.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use dep::std;

fn main() {
// initialize
let mut slice = [50; 2];

// assign
slice[0] = 10;

// push_back
slice = slice.push_back(20);
assert(slice.len() == 3);

// push_front
slice = slice.push_front(10);
assert(slice.len() == 4);
assert(slice[0] == 10);

// pop_back
let (slice, _) = slice.pop_back();
assert(slice.len() == 3);

// pop_front
let mut (slice, elem) = slice.pop_back();
assert(slice.len() == 2);
assert(elem == 50);

// insert at index
slice = slice.insert(2, 100);
assert(slice[2] == 100);

// remove at index
let (_, removed) = slice.remove(2);
assert(removed == 100);

// append two slices
let append = [1,2].append([3,4,5]);
assert(append.len() == 5);
assert(append[0] == 1);
assert(append[4] == 5);
}


#[test]
fn test_slices() {
main();
}

0 comments on commit 2c95514

Please sign in to comment.