Verified source
0xae27…671f
0xae2774355390cef9b4e4a6e56c3e94d9fc98671f
Verification
- Code hash
- 0xd85a99160aa0eb76d0792e7b1101dbb1bc9d1e48
- Status
- full match
- Provider
- demo-vendored
- Compiler
- nargo 1.0.0-beta.26
- Language
- noir
- Bundle
- sha1:8349c47ad8b04d52b6f58db9d63fad86872580a0
Sources
Nargo.toml13 lines
1[package]
2name = "zk_shields"
3type = "bin"
4authors = [""]
5compiler_version = ">=0.29.0"
6version = "1.0.0"
7
8[dependencies]
9
10[tool.NodeGuardians]
11part_1 = "Shield Calculations"
12part_2 = "Survival Proof"
13
Prover.toml17 lines
1initial_shield = "10000"
2
3shield_regen_percentage = "10"
4
5asteroid_masses_positive = [
6 "100",
7 "2000",
8 "200",
9 "100",
10 "100",
11 "50",
12 "50",
13 "14",
14]
15
16asteroid_masses_negative = ["2000", "300", "200", "20", "15", "20", "1", "1"]
17
src/main.nr39 lines
1mod shield;
2
3// We are on a space ship, moving near light speed towards a distant planet
4// We are about to pass trough an asteroid field, changing the course is very expensive at this speed
5// We have to proove to everyone that we can survive the collisions
6
7// We need to have at least 1 unit of shields left in order to survive the cosmic radiation.
8
9// We can not reveal how much shields we have will have in the end so that the space pirates won't know if it is safe to atack us
10// The space pirates can track our course but our shield technology remains a well guarded secret
11
12fn main(initial_shield: Field, shield_regen_percentage: Field, asteroid_masses_positive: pub [Field; 8], asteroid_masses_negative: pub [Field; 8]) -> pub bool {
13 println("Positive Test Case");
14
15 let did_survive_positive = shield::iterate_asteroids(initial_shield, shield_regen_percentage , asteroid_masses_positive);
16 if(did_survive_positive){
17 println("shields will hold as expected")
18 }
19 else{
20 println("shields will not hold but where expected to hold")
21 }
22
23 println("------------------");
24 println("Negative Test Case");
25 println("------------------");
26
27 let did_survive_negative = shield::iterate_asteroids(initial_shield, shield_regen_percentage, asteroid_masses_negative);
28 if(did_survive_negative){
29 println("shields will hold, but where expected to fail")
30 }
31 else{
32 println("shields will not hold as expected")
33 }
34
35 assert(did_survive_positive == true);
36 assert(did_survive_negative == false);
37 did_survive_positive & !did_survive_negative
38}
39
src/shield.nr68 lines
1pub fn iterate_asteroids(initial_shield: Field, shield_regen_percentage: Field, masses: [Field; 8]) -> bool {
2 let mut remaining_shield = initial_shield;
3
4 for i in 0..8 {
5 let mass = masses[i];
6 let damage = calculate_damage(initial_shield, remaining_shield, mass);
7 remaining_shield -= damage;
8
9 let mut regeneration = 0;
10 if (remaining_shield as u32 > 0){
11 regeneration = calculate_shield_regeneration(initial_shield ,remaining_shield, shield_regen_percentage);
12 remaining_shield += regeneration;
13 }
14 status_report(i,initial_shield, remaining_shield, damage, regeneration);
15 }
16
17 // We need to have at least 1 unit of shields left in order to survive the cosmic radiation.
18 let result = remaining_shield as u32 > 0;
19 result
20}
21
22fn calculate_damage(initial_shield: Field, remaining_shield: Field, mass: Field) -> Field {
23 // Shields get exponentially less efficient
24 // At 100% shields 1 unit of mass will drain 1 point of energy
25 // At 50% shields 1 unit of mass will drain 50 points of energy
26 let shield_pct = calculate_remaining_shield_pct(initial_shield, remaining_shield);
27 let mut damage = 0;
28 if(shield_pct == 100){
29 damage = mass * 1;
30 }
31 else{
32 damage = mass * (100 - shield_pct);
33 }
34 if(damage as u32 > remaining_shield as u32){
35 damage = remaining_shield;
36 }
37 damage as Field
38}
39
40fn calculate_shield_regeneration(initial_shield:Field, remaining_shield:Field, shield_regen_percentage: Field) -> Field{
41 // shields regain a percentage of the maxium capacity after each hit
42 let mut regen = (initial_shield * shield_regen_percentage) / 100;
43 if((remaining_shield + regen) as u32 > initial_shield as u32){
44 regen = initial_shield - remaining_shield;
45 }
46 regen as Field
47}
48fn calculate_remaining_shield_pct(initial_shield: Field, remaining_shield: Field) -> Field {
49 let result = (remaining_shield * 100) as u32 / initial_shield as u32;
50 result as Field
51}
52
53fn status_report(iteration: u32 ,initial_shield: Field, remaining_shield: Field, damage: Field, regenerated_shield: Field){
54 println(f"----- iteration {iteration} -----");
55
56 // in noir, fields can't be printed directly so we convert them to an integer type first
57 let damage_as_u32 = damage as u32;
58 println(f"Damage: {damage_as_u32}");
59
60 let regenerated_shield_as_u32 = regenerated_shield as u32;
61 println(f"Regenerated {regenerated_shield_as_u32} energy");
62
63 let remaining_shield_as_u32 = remaining_shield as u32;
64 let remaining_shield_pct = calculate_remaining_shield_pct(initial_shield, remaining_shield);
65 let remaining_shield_pct_as_u32 = remaining_shield_pct as u32;
66 println(f"Shield status {remaining_shield_pct_as_u32}% {remaining_shield_as_u32}");
67}
68
This bundle publishes no ABI and no storage layout. §10's interface view and slot mapping are rendered from the bundle's debug object, and this producer writes an empty one — a circuit has no contract-shaped storage layout to declare. Where a bundle carries them, the interface view is also what links each function to the transactions that called it; with no ABI there is nothing to link from, so the link is absent rather than broken.
Deployments
This code hash is bound to one address in this generation. Source is keyed by code hash rather than by address, so a second deployment of the same bytecode would appear here already verified — that is what keying by code hash buys.